From: Raphael Isemann Date: Mon, 23 Sep 2019 08:59:21 +0000 (+0000) Subject: [lldb][NFC] Remove argument prefix checking boilerplate when adding completions X-Git-Tag: llvmorg-11-init~8610 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=93ca36d756d84310bd01c74f35dbdddf4d3035f9;p=platform%2Fupstream%2Fllvm.git [lldb][NFC] Remove argument prefix checking boilerplate when adding completions llvm-svn: 372561 --- diff --git a/lldb/include/lldb/Utility/CompletionRequest.h b/lldb/include/lldb/Utility/CompletionRequest.h index e10c1f7..a4305195 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 + 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 f00a35d..00ba108 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 bfc7760..4a08415 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 126a4f5..26933aa 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 483c478..7a6bc65 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 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 09ecfed..1f63382 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 fc2d439..1f22265 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 469aad2..ad3c560 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("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;