From cb3cf589ed8a5df38a801bbc34a21951924e9195 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sun, 19 Apr 2020 18:32:28 +0430 Subject: LibLine: Allow suggestions to have trailing trivia strings These strings would be applied when inserted into the buffer, but are not shown as part of the suggestion. This commit also patches up Userland/js and Shell to use this functionality --- Libraries/LibLine/Editor.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'Libraries/LibLine/Editor.h') diff --git a/Libraries/LibLine/Editor.h b/Libraries/LibLine/Editor.h index f5085cfcbd..ce7813d31a 100644 --- a/Libraries/LibLine/Editor.h +++ b/Libraries/LibLine/Editor.h @@ -53,6 +53,28 @@ struct KeyCallback { Function callback; }; +struct CompletionSuggestion { + // intentionally not explicit (allows suggesting bare strings) + CompletionSuggestion(const String& completion) + : text(completion) + , trailing_trivia("") + { + } + CompletionSuggestion(const StringView& completion, const StringView& trailing_trivia) + : text(completion) + , trailing_trivia(trailing_trivia) + { + } + + bool operator==(const CompletionSuggestion& suggestion) const + { + return suggestion.text == text; + } + + String text; + String trailing_trivia; +}; + class Editor { public: Editor(); @@ -79,8 +101,8 @@ public: void register_character_input_callback(char ch, Function callback); - Function(const String&)> on_tab_complete_first_token; - Function(const String&)> on_tab_complete_other_token; + Function(const String&)> on_tab_complete_first_token; + Function(const String&)> on_tab_complete_other_token; Function on_display_refresh; // FIXME: we will have to kindly ask our instantiators to set our signal handlers @@ -195,8 +217,8 @@ private: size_t m_origin_y { 0 }; String m_new_prompt; - Vector m_suggestions; - String m_last_shown_suggestion { String::empty() }; + Vector m_suggestions; + CompletionSuggestion m_last_shown_suggestion { String::empty() }; size_t m_last_shown_suggestion_display_length { 0 }; bool m_last_shown_suggestion_was_complete { false }; size_t m_next_suggestion_index { 0 }; -- cgit v1.2.3