[Lldb-commits] [lldb] r372561 - [lldb][NFC] Remove argument prefix checking boilerplate when adding completions

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 23 01:59:21 PDT 2019


Author: teemperor
Date: Mon Sep 23 01:59:21 2019
New Revision: 372561

URL: http://llvm.org/viewvc/llvm-project?rev=372561&view=rev
Log:
[lldb][NFC] Remove argument prefix checking boilerplate when adding completions

Modified:
    lldb/trunk/include/lldb/Utility/CompletionRequest.h
    lldb/trunk/source/Commands/CommandCompletions.cpp
    lldb/trunk/source/Interpreter/OptionValueBoolean.cpp
    lldb/trunk/source/Interpreter/OptionValueEnumeration.cpp
    lldb/trunk/source/Interpreter/OptionValueUUID.cpp
    lldb/trunk/source/Interpreter/Options.cpp
    lldb/trunk/source/Utility/ArchSpec.cpp
    lldb/trunk/unittests/Utility/CompletionRequestTest.cpp

Modified: lldb/trunk/include/lldb/Utility/CompletionRequest.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/CompletionRequest.h?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/CompletionRequest.h (original)
+++ lldb/trunk/include/lldb/Utility/CompletionRequest.h Mon Sep 23 01:59:21 2019
@@ -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.

Modified: lldb/trunk/source/Commands/CommandCompletions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandCompletions.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandCompletions.cpp (original)
+++ lldb/trunk/source/Commands/CommandCompletions.cpp Mon Sep 23 01:59:21 2019
@@ -308,10 +308,8 @@ void CommandCompletions::SettingsNames(C
     }
   }
 
-  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,

Modified: lldb/trunk/source/Interpreter/OptionValueBoolean.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueBoolean.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueBoolean.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueBoolean.cpp Mon Sep 23 01:59:21 2019
@@ -82,8 +82,6 @@ void OptionValueBoolean::AutoComplete(Co
   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);
 }

Modified: lldb/trunk/source/Interpreter/OptionValueEnumeration.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueEnumeration.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueEnumeration.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueEnumeration.cpp Mon Sep 23 01:59:21 2019
@@ -108,8 +108,7 @@ void OptionValueEnumeration::AutoComplet
   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;
   }

Modified: lldb/trunk/source/Interpreter/OptionValueUUID.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueUUID.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueUUID.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueUUID.cpp Mon Sep 23 01:59:21 2019
@@ -80,10 +80,6 @@ void OptionValueUUID::AutoComplete(Comma
     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());
   }
 }

Modified: lldb/trunk/source/Interpreter/Options.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/Options.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/Options.cpp (original)
+++ lldb/trunk/source/Interpreter/Options.cpp Mon Sep 23 01:59:21 2019
@@ -759,10 +759,7 @@ void Options::HandleOptionArgumentComple
             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);
     }
   }
 

Modified: lldb/trunk/source/Utility/ArchSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/ArchSpec.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/source/Utility/ArchSpec.cpp (original)
+++ lldb/trunk/source/Utility/ArchSpec.cpp Mon Sep 23 01:59:21 2019
@@ -244,17 +244,8 @@ void ArchSpec::ListSupportedArchNames(St
 }
 
 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)

Modified: lldb/trunk/unittests/Utility/CompletionRequestTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CompletionRequestTest.cpp?rev=372561&r1=372560&r2=372561&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/CompletionRequestTest.cpp (original)
+++ lldb/trunk/unittests/Utility/CompletionRequestTest.cpp Mon Sep 23 01:59:21 2019
@@ -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;




More information about the lldb-commits mailing list