diff options
author | Raphael Isemann <teemperor@gmail.com> | 2019-09-23 08:59:21 +0000 |
---|---|---|
committer | Raphael Isemann <teemperor@gmail.com> | 2019-09-23 08:59:21 +0000 |
commit | 93ca36d756d84310bd01c74f35dbdddf4d3035f9 (patch) | |
tree | 54693950e2913ff7350f3134d38eb90570b11ef4 | |
parent | 4ba6d0ded2303881bb09ed606970c6c910d183c1 (diff) | |
download | bcm5719-llvm-93ca36d756d84310bd01c74f35dbdddf4d3035f9.tar.gz bcm5719-llvm-93ca36d756d84310bd01c74f35dbdddf4d3035f9.zip |
[lldb][NFC] Remove argument prefix checking boilerplate when adding completions
llvm-svn: 372561
-rw-r--r-- | lldb/include/lldb/Utility/CompletionRequest.h | 18 | ||||
-rw-r--r-- | lldb/source/Commands/CommandCompletions.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Interpreter/OptionValueBoolean.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Interpreter/OptionValueEnumeration.cpp | 3 | ||||
-rw-r--r-- | lldb/source/Interpreter/OptionValueUUID.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Interpreter/Options.cpp | 5 | ||||
-rw-r--r-- | lldb/source/Utility/ArchSpec.cpp | 13 | ||||
-rw-r--r-- | lldb/unittests/Utility/CompletionRequestTest.cpp | 37 |
8 files changed, 64 insertions, 30 deletions
diff --git a/lldb/include/lldb/Utility/CompletionRequest.h b/lldb/include/lldb/Utility/CompletionRequest.h index e10c1f706f5..a43051954d3 100644 --- a/lldb/include/lldb/Utility/CompletionRequest.h +++ b/lldb/include/lldb/Utility/CompletionRequest.h @@ -146,6 +146,24 @@ public: m_result.AddResult(completion, description, mode); } + /// Adds a possible completion string if the completion would complete the + /// current argument. + /// + /// \param match The suggested completion. + /// \param description An optional description of the completion string. The + /// description will be displayed to the user alongside the completion. + template <CompletionMode M = CompletionMode::Normal> + void TryCompleteCurrentArg(llvm::StringRef completion, + llvm::StringRef description = "") { + // Trying to rewrite the whole line while checking for the current + // argument never makes sense. Completion modes are always hardcoded, so + // this can be a static_assert. + static_assert(M != CompletionMode::RewriteLine, + "Shouldn't rewrite line with this function"); + if (completion.startswith(GetCursorArgumentPrefix())) + AddCompletion(completion, description, M); + } + /// Adds multiple possible completion strings. /// /// \param completions The list of completions. diff --git a/lldb/source/Commands/CommandCompletions.cpp b/lldb/source/Commands/CommandCompletions.cpp index f00a35d3d81..00ba108f388 100644 --- a/lldb/source/Commands/CommandCompletions.cpp +++ b/lldb/source/Commands/CommandCompletions.cpp @@ -308,10 +308,8 @@ void CommandCompletions::SettingsNames(CommandInterpreter &interpreter, } } - for (const std::string &s : g_property_names) { - if (llvm::StringRef(s).startswith(request.GetCursorArgumentPrefix())) - request.AddCompletion(s); - } + for (const std::string &s : g_property_names) + request.TryCompleteCurrentArg(s); } void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter, diff --git a/lldb/source/Interpreter/OptionValueBoolean.cpp b/lldb/source/Interpreter/OptionValueBoolean.cpp index bfc7760e207..4a084156a0a 100644 --- a/lldb/source/Interpreter/OptionValueBoolean.cpp +++ b/lldb/source/Interpreter/OptionValueBoolean.cpp @@ -82,8 +82,6 @@ void OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter, if (request.GetCursorArgumentPrefix().empty()) entries = entries.take_front(2); - for (auto entry : entries) { - if (entry.startswith_lower(request.GetCursorArgumentPrefix())) - request.AddCompletion(entry); - } + for (auto entry : entries) + request.TryCompleteCurrentArg(entry); } diff --git a/lldb/source/Interpreter/OptionValueEnumeration.cpp b/lldb/source/Interpreter/OptionValueEnumeration.cpp index 126a4f53e10..26933aa7824 100644 --- a/lldb/source/Interpreter/OptionValueEnumeration.cpp +++ b/lldb/source/Interpreter/OptionValueEnumeration.cpp @@ -108,8 +108,7 @@ void OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter, if (!request.GetCursorArgumentPrefix().empty()) { for (size_t i = 0; i < num_enumerators; ++i) { llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef(); - if (name.startswith(request.GetCursorArgumentPrefix())) - request.AddCompletion(name); + request.TryCompleteCurrentArg(name); } return; } diff --git a/lldb/source/Interpreter/OptionValueUUID.cpp b/lldb/source/Interpreter/OptionValueUUID.cpp index 483c47807d2..7a6bc65b25a 100644 --- a/lldb/source/Interpreter/OptionValueUUID.cpp +++ b/lldb/source/Interpreter/OptionValueUUID.cpp @@ -80,10 +80,6 @@ void OptionValueUUID::AutoComplete(CommandInterpreter &interpreter, const UUID &module_uuid = module_sp->GetUUID(); if (!module_uuid.IsValid()) continue; - llvm::ArrayRef<uint8_t> module_bytes = module_uuid.GetBytes(); - if (module_bytes.size() >= uuid_bytes.size() && - module_bytes.take_front(uuid_bytes.size()).equals(uuid_bytes)) { - request.AddCompletion(module_uuid.GetAsString()); - } + request.TryCompleteCurrentArg(module_uuid.GetAsString()); } } diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index 09ecfed206c..1f633824a80 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -759,10 +759,7 @@ void Options::HandleOptionArgumentCompletion( request.GetCursorCharPosition()); for (const auto &enum_value : enum_values) { - if (strstr(enum_value.string_value, match_string.c_str()) == - enum_value.string_value) { - request.AddCompletion(enum_value.string_value); - } + request.TryCompleteCurrentArg(enum_value.string_value); } } diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp index fc2d43925a3..1f22265d3e7 100644 --- a/lldb/source/Utility/ArchSpec.cpp +++ b/lldb/source/Utility/ArchSpec.cpp @@ -244,17 +244,8 @@ void ArchSpec::ListSupportedArchNames(StringList &list) { } void ArchSpec::AutoComplete(CompletionRequest &request) { - if (!request.GetCursorArgumentPrefix().empty()) { - for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) { - if (NameMatches(g_core_definitions[i].name, NameMatch::StartsWith, - request.GetCursorArgumentPrefix())) - request.AddCompletion(g_core_definitions[i].name); - } - } else { - StringList matches; - ListSupportedArchNames(matches); - request.AddCompletions(matches); - } + for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) + request.TryCompleteCurrentArg(g_core_definitions[i].name); } #define CPU_ANY (UINT32_MAX) diff --git a/lldb/unittests/Utility/CompletionRequestTest.cpp b/lldb/unittests/Utility/CompletionRequestTest.cpp index 469aad22a7e..ad3c560a916 100644 --- a/lldb/unittests/Utility/CompletionRequestTest.cpp +++ b/lldb/unittests/Utility/CompletionRequestTest.cpp @@ -31,6 +31,43 @@ TEST(CompletionRequest, Constructor) { EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b"); } +TEST(CompletionRequest, TryCompleteCurrentArgGood) { + std::string command = "a bad c"; + StringList matches, descriptions; + CompletionResult result; + + CompletionRequest request(command, 3, result); + request.TryCompleteCurrentArg("boo", "car"); + result.GetMatches(matches); + result.GetDescriptions(descriptions); + + EXPECT_EQ(1U, result.GetResults().size()); + EXPECT_STREQ("boo", matches.GetStringAtIndex(0U)); + EXPECT_EQ(1U, descriptions.GetSize()); + EXPECT_STREQ("car", descriptions.GetStringAtIndex(0U)); +} + +TEST(CompletionRequest, TryCompleteCurrentArgBad) { + std::string command = "a bad c"; + CompletionResult result; + + CompletionRequest request(command, 3, result); + request.TryCompleteCurrentArg("car", "card"); + + EXPECT_EQ(0U, result.GetResults().size()); +} + +TEST(CompletionRequest, TryCompleteCurrentArgMode) { + std::string command = "a bad c"; + CompletionResult result; + + CompletionRequest request(command, 3, result); + request.TryCompleteCurrentArg<CompletionMode::Partial>("bar", "bard"); + + EXPECT_EQ(1U, result.GetResults().size()); + EXPECT_EQ(CompletionMode::Partial, result.GetResults()[0].GetMode()); +} + TEST(CompletionRequest, ShiftArguments) { std::string command = "a bad c"; const unsigned cursor_pos = 3; |