summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2022-03-28 21:53:12 +0430
committerAndreas Kling <kling@serenityos.org>2022-03-28 20:02:49 +0200
commitbcbe25861980ac2f5fc7022306685d0e89e5ecd1 (patch)
tree78bb15c46dd720b1c19f2742184f4921b5f96bec
parentd665492e356ab20ed5b6072a90dbea4e3138c125 (diff)
downloadserenity-bcbe25861980ac2f5fc7022306685d0e89e5ecd1.zip
LibCore: Set the correct invariant length for consecutive short options
When completing `ls -l` to add another short option, the invariant length should be zero as we are not replacing anything with our suggestion. Also skip the initial dash if there already is one. Fixes #13301.
-rw-r--r--Userland/Libraries/LibCore/ArgsParser.cpp9
1 files changed, 5 insertions, 4 deletions
diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp
index e351cf46b4..a339b8a0bd 100644
--- a/Userland/Libraries/LibCore/ArgsParser.cpp
+++ b/Userland/Libraries/LibCore/ArgsParser.cpp
@@ -753,11 +753,11 @@ void ArgsParser::autocomplete(FILE* file, StringView program_name, Span<char con
if (!completing_option)
return;
- auto write_completion = [&](auto format, auto& option, auto... args) {
+ auto write_completion = [&](auto format, auto& option, auto has_invariant, auto... args) {
JsonObject object;
object.set("completion", String::formatted(format, args...));
object.set("static_offset", 0);
- object.set("invariant_offset", option_to_complete.length());
+ object.set("invariant_offset", has_invariant ? option_to_complete.length() : 0u);
object.set("display_trivia", option.help_string);
object.set("trailing_trivia", option.requires_argument ? " " : "");
outln(file, "{}", object.to_string());
@@ -771,7 +771,7 @@ void ArgsParser::autocomplete(FILE* file, StringView program_name, Span<char con
continue;
StringView option_string = option.long_name;
if (option_string.starts_with(option_pattern)) {
- write_completion("--{}", option, option_string);
+ write_completion("--{}", option, true, option_string);
}
}
} else {
@@ -782,7 +782,8 @@ void ArgsParser::autocomplete(FILE* file, StringView program_name, Span<char con
if (option.short_name == 0)
continue;
- write_completion("-{}", option, option.short_name);
+ auto has_invariant = option_to_complete == "-";
+ write_completion("{}{}", option, has_invariant, has_invariant ? "-" : "", option.short_name);
}
}
}