1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:47:35 +00:00

LibLine+Userland: Make suggestion offsets per-suggestion

This allows the user to modify different parts of the input with
different suggestions.
This commit is contained in:
Ali Mohammad Pur 2022-02-28 17:28:47 +03:30 committed by Andreas Kling
parent 1fcef99ff7
commit 118590325a
6 changed files with 79 additions and 51 deletions

View file

@ -489,7 +489,7 @@ void Editor::stylize(Span const& span, Style const& style)
ending_map.set(start, style);
}
void Editor::suggest(size_t invariant_offset, size_t static_offset, Span::Mode offset_mode) const
void Editor::transform_suggestion_offsets(size_t& invariant_offset, size_t& static_offset, Span::Mode offset_mode) const
{
auto internal_static_offset = static_offset;
auto internal_invariant_offset = invariant_offset;
@ -501,7 +501,8 @@ void Editor::suggest(size_t invariant_offset, size_t static_offset, Span::Mode o
internal_static_offset = offsets.start;
internal_invariant_offset = offsets.end - offsets.start;
}
m_suggestion_manager.set_suggestion_variants(internal_static_offset, internal_invariant_offset, 0);
invariant_offset = internal_invariant_offset;
static_offset = internal_static_offset;
}
void Editor::initialize()
@ -1141,7 +1142,6 @@ void Editor::handle_read_event()
// We have none, or just one suggestion,
// we should just commit that and continue
// after it, as if it were auto-completed.
suggest(0, 0, Span::CodepointOriented);
m_times_tab_pressed = 0;
m_suggestion_manager.reset();
m_suggestion_display->finish();
@ -1180,7 +1180,6 @@ void Editor::cleanup_suggestions()
m_refresh_needed = true;
}
m_suggestion_manager.reset();
suggest(0, 0, Span::CodepointOriented);
m_suggestion_display->finish();
}
m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB

View file

@ -221,7 +221,7 @@ public:
// +-|- static offset: the suggestions start here
// +- invariant offset: the suggestions do not change up to here
//
void suggest(size_t invariant_offset = 0, size_t static_offset = 0, Span::Mode offset_mode = Span::ByteOriented) const;
void transform_suggestion_offsets(size_t& invariant_offset, size_t& static_offset, Span::Mode offset_mode = Span::ByteOriented) const;
const struct termios& termios() const { return m_termios; }
const struct termios& default_termios() const { return m_default_termios; }

View file

@ -84,10 +84,12 @@ CompletionSuggestion const& SuggestionManager::suggest()
void SuggestionManager::set_current_suggestion_initiation_index(size_t index)
{
auto& suggestion = m_suggestions[m_next_suggestion_index];
if (m_last_shown_suggestion_display_length)
m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_last_shown_suggestion_display_length;
m_last_shown_suggestion.start_index = index - suggestion.static_offset - m_last_shown_suggestion_display_length;
else
m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_next_suggestion_invariant_offset;
m_last_shown_suggestion.start_index = index - suggestion.static_offset - suggestion.invariant_offset;
m_last_shown_suggestion_display_length = m_last_shown_suggestion.text_view.length();
m_last_shown_suggestion_was_complete = true;
@ -98,7 +100,9 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion
CompletionAttemptResult result { mode };
if (m_next_suggestion_index < m_suggestions.size()) {
auto can_complete = m_next_suggestion_invariant_offset <= m_largest_common_suggestion_prefix_length;
auto& next_suggestion = m_suggestions[m_next_suggestion_index];
auto can_complete = next_suggestion.invariant_offset <= m_largest_common_suggestion_prefix_length;
ssize_t actual_offset;
size_t shown_length = m_last_shown_suggestion_display_length;
switch (mode) {
@ -106,7 +110,7 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion
actual_offset = 0;
break;
case ShowSuggestions:
actual_offset = 0 - m_largest_common_suggestion_prefix_length + m_next_suggestion_invariant_offset;
actual_offset = 0 - m_largest_common_suggestion_prefix_length + next_suggestion.invariant_offset;
if (can_complete)
shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view.length();
break;
@ -114,11 +118,11 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion
if (m_last_shown_suggestion_display_length == 0)
actual_offset = 0;
else
actual_offset = 0 - m_last_shown_suggestion_display_length + m_next_suggestion_invariant_offset;
actual_offset = 0 - m_last_shown_suggestion_display_length + next_suggestion.invariant_offset;
break;
}
result.offset_region_to_remove = { m_next_suggestion_invariant_offset, shown_length };
result.offset_region_to_remove = { next_suggestion.invariant_offset, shown_length };
result.new_cursor_offset = actual_offset;
auto& suggestion = suggest();
@ -127,7 +131,7 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion
if (mode == CompletePrefix) {
// Only auto-complete *if possible*.
if (can_complete) {
result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, m_largest_common_suggestion_prefix_length - m_next_suggestion_invariant_offset));
result.insert.append(suggestion.text_view.substring_view(suggestion.invariant_offset, m_largest_common_suggestion_prefix_length - suggestion.invariant_offset));
m_last_shown_suggestion_display_length = m_largest_common_suggestion_prefix_length;
// Do not increment the suggestion index, as the first tab should only be a *peek*.
if (m_suggestions.size() == 1) {
@ -147,7 +151,7 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion
m_last_shown_suggestion_was_complete = false;
m_last_shown_suggestion = String::empty();
} else {
result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, suggestion.text_view.length() - m_next_suggestion_invariant_offset));
result.insert.append(suggestion.text_view.substring_view(suggestion.invariant_offset, suggestion.text_view.length() - suggestion.invariant_offset));
// Add in the trivia of the last selected suggestion.
result.insert.append(suggestion.trivia_view);
m_last_shown_suggestion_display_length += suggestion.trivia_view.length();

View file

@ -53,6 +53,8 @@ public:
Style style;
size_t start_index { 0 };
size_t input_offset { 0 };
size_t static_offset { 0 };
size_t invariant_offset { 0 };
Utf32View text_view;
Utf32View trivia_view;
@ -102,12 +104,6 @@ public:
void next();
void previous();
void set_suggestion_variants(size_t static_offset, size_t invariant_offset, size_t suggestion_index) const
{
m_next_suggestion_index = suggestion_index;
m_next_suggestion_static_offset = static_offset;
m_next_suggestion_invariant_offset = invariant_offset;
}
CompletionSuggestion const& suggest();
CompletionSuggestion const& current_suggestion() const { return m_last_shown_suggestion; }
@ -131,8 +127,6 @@ private:
size_t m_last_shown_suggestion_display_length { 0 };
bool m_last_shown_suggestion_was_complete { false };
mutable size_t m_next_suggestion_index { 0 };
mutable size_t m_next_suggestion_invariant_offset { 0 };
mutable size_t m_next_suggestion_static_offset { 0 };
size_t m_largest_common_suggestion_prefix_length { 0 };
mutable size_t m_last_displayed_suggestion_index { 0 };
size_t m_selected_suggestion_index { 0 };