Преглед изворни кода

Shell+LibLine: Record the input offset of completions

This makes the completion entry retain information about how much of the
suggestion was part of the string that caused the match.
AnotherTest пре 4 година
родитељ
комит
a9cee8ee02
2 измењених фајлова са 16 додато и 5 уклоњено
  1. 1 0
      Libraries/LibLine/SuggestionManager.h
  2. 15 5
      Shell/Shell.cpp

+ 1 - 0
Libraries/LibLine/SuggestionManager.h

@@ -72,6 +72,7 @@ public:
     Vector<u32> trailing_trivia;
     Style style;
     size_t start_index { 0 };
+    size_t input_offset { 0 };
 
     Utf32View text_view;
     Utf32View trivia_view;

+ 15 - 5
Shell/Shell.cpp

@@ -1185,6 +1185,7 @@ Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base, cons
                 } else {
                     suggestions.append({ escape_token(file), " " });
                 }
+                suggestions.last().input_offset = token_length;
             }
         }
     }
@@ -1202,8 +1203,9 @@ Vector<Line::CompletionSuggestion> Shell::complete_program_name(const String& na
         return complete_path("", name, offset);
 
     String completion = *match;
+    auto token_length = escape_token(name).length();
     if (m_editor)
-        m_editor->suggest(escape_token(name).length(), 0);
+        m_editor->suggest(token_length, 0);
 
     // Now that we have a program name starting with our token, we look at
     // other program names starting with our token and cut off any mismatching
@@ -1214,11 +1216,14 @@ Vector<Line::CompletionSuggestion> Shell::complete_program_name(const String& na
     int index = match - cached_path.data();
     for (int i = index - 1; i >= 0 && cached_path[i].starts_with(name); --i) {
         suggestions.append({ cached_path[i], " " });
+        suggestions.last().input_offset = token_length;
     }
     for (size_t i = index + 1; i < cached_path.size() && cached_path[i].starts_with(name); ++i) {
         suggestions.append({ cached_path[i], " " });
+        suggestions.last().input_offset = token_length;
     }
     suggestions.append({ cached_path[index], " " });
+    suggestions.last().input_offset = token_length;
 
     return suggestions;
 }
@@ -1250,6 +1255,7 @@ Vector<Line::CompletionSuggestion> Shell::complete_variable(const String& name,
             if (suggestions.contains_slow(name))
                 continue;
             suggestions.append(move(name));
+            suggestions.last().input_offset = offset;
         }
     }
 
@@ -1271,8 +1277,10 @@ Vector<Line::CompletionSuggestion> Shell::complete_user(const String& name, size
 
     while (di.has_next()) {
         String name = di.next_path();
-        if (name.starts_with(pattern))
+        if (name.starts_with(pattern)) {
             suggestions.append(name);
+            suggestions.last().input_offset = offset;
+        }
     }
 
     return suggestions;
@@ -1309,9 +1317,11 @@ Vector<Line::CompletionSuggestion> Shell::complete_option(const String& program_
                 builder.append(view);
                 return builder.to_string();
             };
-#define __ENUMERATE_SHELL_OPTION(name, d_, descr_)        \
-    if (StringView { #name }.starts_with(option_pattern)) \
-        suggestions.append(maybe_negate(#name));
+#define __ENUMERATE_SHELL_OPTION(name, d_, descr_)          \
+    if (StringView { #name }.starts_with(option_pattern)) { \
+        suggestions.append(maybe_negate(#name));            \
+        suggestions.last().input_offset = offset;           \
+    }
 
             ENUMERATE_SHELL_OPTIONS();
 #undef __ENUMERATE_SHELL_OPTION