From 0751592a183394e333dd3e5bd60a461be00b07e0 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Thu, 21 May 2020 05:14:34 +0430 Subject: LibLine: Correctly track the completion start and end To achieve this, the API was tweaked a bit to allow for easier tracking of completions. This API change is non-disruptive to any application that does not use anchored styles. --- Libraries/LibLine/Editor.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'Libraries/LibLine/Editor.cpp') diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp index bd30a751d0..71eeb1efbc 100644 --- a/Libraries/LibLine/Editor.cpp +++ b/Libraries/LibLine/Editor.cpp @@ -445,7 +445,7 @@ String Editor::get_line(const String& prompt) // reverse tab can count as regular tab here m_times_tab_pressed++; - int token_start = m_cursor - 1 - m_last_shown_suggestion_display_length; + int token_start = m_cursor; // ask for completions only on the first tab // and scan for the largest common prefix to display @@ -523,7 +523,8 @@ String Editor::get_line(const String& prompt) m_refresh_needed = true; } m_last_shown_suggestion = m_suggestions[m_next_suggestion_index]; - m_last_shown_suggestion.token_start_index = token_start - m_next_suggestion_invariant_offset - m_last_shown_suggestion.trailing_trivia.length(); + m_last_shown_suggestion.token_start_index = token_start - m_next_suggestion_invariant_offset - m_next_suggestion_static_offset; + dbg() << "Last shown suggestion token start index: " << m_last_shown_suggestion.token_start_index << " Token Start " << token_start << " invariant offset " << m_next_suggestion_invariant_offset; m_last_shown_suggestion_display_length = m_last_shown_suggestion.text.length(); m_last_shown_suggestion_was_complete = true; if (m_times_tab_pressed == 1) { @@ -537,7 +538,8 @@ String Editor::get_line(const String& prompt) m_times_tab_pressed = 0; // add in the trivia of the last selected suggestion insert(m_last_shown_suggestion.trailing_trivia); - m_last_shown_suggestion_display_length += m_last_shown_suggestion.trailing_trivia.length(); + m_last_shown_suggestion_display_length = 0; + readjust_anchored_styles(m_last_shown_suggestion.token_start_index, ModificationKind::ForcedOverlapRemoval); stylize({ m_last_shown_suggestion.token_start_index, m_cursor, Span::Mode::CodepointOriented }, m_last_shown_suggestion.style); } } else { @@ -659,6 +661,8 @@ String Editor::get_line(const String& prompt) if (m_times_tab_pressed) { // Apply the style of the last suggestion + dbg() << "Last shown suggestion token start index: " << m_last_shown_suggestion.token_start_index << " invariant offset " << m_next_suggestion_invariant_offset << " static offset " << m_next_suggestion_static_offset; + readjust_anchored_styles(m_last_shown_suggestion.token_start_index, ModificationKind::ForcedOverlapRemoval); stylize({ m_last_shown_suggestion.token_start_index, m_cursor, Span::Mode::CodepointOriented }, m_last_shown_suggestion.style); // we probably have some suggestions drawn // let's clean them up @@ -1415,9 +1419,16 @@ void Editor::readjust_anchored_styles(size_t hint_index, ModificationKind modifi }; Vector anchors_to_relocate; auto index_shift = modification == ModificationKind::Insertion ? 1 : -1; + auto forced_removal = modification == ModificationKind::ForcedOverlapRemoval; for (auto& start_entry : m_anchored_spans_starting) { for (auto& end_entry : start_entry.value) { + if (forced_removal) { + if (start_entry.key <= hint_index && end_entry.key >= hint_index) { + // remove any overlapping regions + continue; + } + } if (start_entry.key >= hint_index) { if (start_entry.key == hint_index && end_entry.key == hint_index + 1 && modification == ModificationKind::Removal) { // remove the anchor, as all its text was wiped @@ -1443,5 +1454,4 @@ void Editor::readjust_anchored_styles(size_t hint_index, ModificationKind modifi stylize(relocation.new_span, relocation.style); } } - } -- cgit v1.2.3