summaryrefslogtreecommitdiff
path: root/Shell/Shell.cpp
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-10-03 17:24:49 +0330
committerAndreas Kling <kling@serenityos.org>2020-10-04 23:12:28 +0200
commita9cee8ee02123e12f239866809f088016f9d23c1 (patch)
treebd76d9bb94ecd8602f2d608102fa27457a220bc7 /Shell/Shell.cpp
parentf164b808b5a6ed69f6fd0aea8ee0c88f4d377a24 (diff)
downloadserenity-a9cee8ee02123e12f239866809f088016f9d23c1.zip
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.
Diffstat (limited to 'Shell/Shell.cpp')
-rw-r--r--Shell/Shell.cpp20
1 files changed, 15 insertions, 5 deletions
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp
index e965645a93..ef2a0b2bf9 100644
--- a/Shell/Shell.cpp
+++ b/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