Forráskód Böngészése

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.
Ali Mohammad Pur 3 éve
szülő
commit
1699ddc186

+ 1 - 1
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.

+ 10 - 1
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:

+ 3 - 0
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);

+ 1 - 0
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);