diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-04-15 08:10:27 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-04-18 19:53:10 +0430 |
commit | 1699ddc18610ffca902007f852e1ce961afd807e (patch) | |
tree | 78538e8731e057a827c0e4ede7c28a0009d2ff13 | |
parent | d5b3998d23775cc9ff12bf7c766b1f1411450f4b (diff) | |
download | serenity-1699ddc18610ffca902007f852e1ce961afd807e.zip |
LibLine: Make it possible to avoid autocompletion if requested
Setting 'allow_commit_without_listing' to false will now make LibLine
show the suggestion before actually committing to it; this is useful for
completions that will replace all the user input, where mistakes can go
unnoticed without some visual cue.
-rw-r--r-- | Userland/Libraries/LibLine/Editor.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibLine/SuggestionManager.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibLine/SuggestionManager.h | 3 | ||||
-rw-r--r-- | Userland/Shell/Shell.cpp | 1 |
4 files changed, 15 insertions, 2 deletions
diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index 798319bb32..631f0fd3ad 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -1167,7 +1167,7 @@ void Editor::handle_read_event() m_suggestion_manager.previous(); } - if (m_suggestion_manager.count() < 2) { + if (m_suggestion_manager.count() < 2 && !completion_result.avoid_committing_to_single_suggestion) { // We have none, or just one suggestion, // we should just commit that and continue // after it, as if it were auto-completed. diff --git a/Userland/Libraries/LibLine/SuggestionManager.cpp b/Userland/Libraries/LibLine/SuggestionManager.cpp index 574d3b4de2..749e37e234 100644 --- a/Userland/Libraries/LibLine/SuggestionManager.cpp +++ b/Userland/Libraries/LibLine/SuggestionManager.cpp @@ -112,6 +112,15 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion if (m_next_suggestion_index < m_suggestions.size()) { auto& next_suggestion = m_suggestions[m_next_suggestion_index]; + if (mode == CompletePrefix && !next_suggestion.allow_commit_without_listing) { + result.new_completion_mode = CompletionMode::ShowSuggestions; + result.avoid_committing_to_single_suggestion = true; + m_last_shown_suggestion_display_length = 0; + m_last_shown_suggestion_was_complete = false; + m_last_shown_suggestion = String::empty(); + return result; + } + 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; @@ -121,7 +130,7 @@ SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion break; case ShowSuggestions: actual_offset = 0 - m_largest_common_suggestion_prefix_length + next_suggestion.invariant_offset; - if (can_complete) + if (can_complete && next_suggestion.allow_commit_without_listing) shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view.length(); break; default: diff --git a/Userland/Libraries/LibLine/SuggestionManager.h b/Userland/Libraries/LibLine/SuggestionManager.h index acb2c96404..f59d81516c 100644 --- a/Userland/Libraries/LibLine/SuggestionManager.h +++ b/Userland/Libraries/LibLine/SuggestionManager.h @@ -56,6 +56,7 @@ public: size_t input_offset { 0 }; size_t static_offset { 0 }; size_t invariant_offset { 0 }; + bool allow_commit_without_listing { true }; Utf32View text_view; Utf32View trivia_view; @@ -104,6 +105,8 @@ public: Vector<Utf32View> insert {}; Optional<Style> style_to_apply {}; + + bool avoid_committing_to_single_suggestion { false }; }; CompletionAttemptResult attempt_completion(CompletionMode, size_t initiation_start_index); diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index 95a22f9935..d6405cc7dc 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -1890,6 +1890,7 @@ ErrorOr<Vector<Line::CompletionSuggestion>> Shell::complete_via_program_itself(s }; suggestion.static_offset = object.get("static_offset").to_u64(0); suggestion.invariant_offset = object.get("invariant_offset").to_u64(0); + suggestion.allow_commit_without_listing = object.get("allow_commit_without_listing").to_bool(true); suggestions.append(move(suggestion)); } else { dbgln("LibLine: Unhandled completion kind: {}", kind); |