[Lldb-commits] [lldb] r369000 - [lldb][NFC] Refactor remaining completion logic to use CompletionRequests

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 15 06:14:11 PDT 2019


Author: teemperor
Date: Thu Aug 15 06:14:10 2019
New Revision: 369000

URL: http://llvm.org/viewvc/llvm-project?rev=369000&view=rev
Log:
[lldb][NFC] Refactor remaining completion logic to use CompletionRequests

This patch moves the remaining completion functions from the
old completion API (that used several variables) to just
passing a single CompletionRequest.

This is for the most part a simple change as we just replace
the old arguments with a single CompletionRequest argument.

There are a few places where I had to create new CompletionRequests
in the called functions as CompletionRequests itself are immutable
and don't expose their internal match list anymore. This means that
if a function wanted to change the CompletionRequest or directly
access the result list, we need to work around this by creating
a new CompletionRequest and a temporary match/description list.

Preparation work for rdar://53769355

Modified:
    lldb/trunk/include/lldb/Core/IOHandler.h
    lldb/trunk/include/lldb/Expression/REPL.h
    lldb/trunk/include/lldb/Host/Editline.h
    lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
    lldb/trunk/include/lldb/Utility/CompletionRequest.h
    lldb/trunk/source/API/SBCommandInterpreter.cpp
    lldb/trunk/source/Core/IOHandler.cpp
    lldb/trunk/source/Expression/REPL.cpp
    lldb/trunk/source/Host/common/Editline.cpp
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp

Modified: lldb/trunk/include/lldb/Core/IOHandler.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/IOHandler.h?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/IOHandler.h (original)
+++ lldb/trunk/include/lldb/Core/IOHandler.h Thu Aug 15 06:14:10 2019
@@ -10,6 +10,7 @@
 #define liblldb_IOHandler_h_
 
 #include "lldb/Core/ValueObjectList.h"
+#include "lldb/Utility/CompletionRequest.h"
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/Flags.h"
 #include "lldb/Utility/Predicate.h"
@@ -198,9 +199,8 @@ public:
 
   virtual void IOHandlerDeactivated(IOHandler &io_handler) {}
 
-  virtual int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
-                                const char *cursor, const char *last_char,
-                                StringList &matches, StringList &descriptions);
+  virtual int IOHandlerComplete(IOHandler &io_handler,
+                                CompletionRequest &request);
 
   virtual const char *IOHandlerGetFixIndentationCharacters() { return nullptr; }
 
@@ -414,10 +414,7 @@ private:
   static int FixIndentationCallback(Editline *editline, const StringList &lines,
                                     int cursor_position, void *baton);
 
-  static int AutoCompleteCallback(const char *current_line, const char *cursor,
-                                  const char *last_char,
-                                  StringList &matches, StringList &descriptions,
-                                  void *baton);
+  static int AutoCompleteCallback(CompletionRequest &request, void *baton);
 #endif
 
 protected:
@@ -448,9 +445,8 @@ public:
 
   bool GetResponse() const { return m_user_response; }
 
-  int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
-                        const char *cursor, const char *last_char,
-                        StringList &matches, StringList &descriptions) override;
+  int IOHandlerComplete(IOHandler &io_handler,
+                        CompletionRequest &request) override;
 
   void IOHandlerInputComplete(IOHandler &io_handler,
                               std::string &data) override;

Modified: lldb/trunk/include/lldb/Expression/REPL.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/REPL.h?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/REPL.h (original)
+++ lldb/trunk/include/lldb/Expression/REPL.h Thu Aug 15 06:14:10 2019
@@ -103,9 +103,8 @@ public:
   void IOHandlerInputComplete(IOHandler &io_handler,
                               std::string &line) override;
 
-  int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
-                        const char *cursor, const char *last_char,
-                        StringList &matches, StringList &descriptions) override;
+  int IOHandlerComplete(IOHandler &io_handler,
+                        CompletionRequest &request) override;
 
 protected:
   static int CalculateActualIndentation(const StringList &lines);

Modified: lldb/trunk/include/lldb/Host/Editline.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Editline.h?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Editline.h (original)
+++ lldb/trunk/include/lldb/Host/Editline.h Thu Aug 15 06:14:10 2019
@@ -53,6 +53,7 @@
 #include <vector>
 
 #include "lldb/Host/ConnectionFileDescriptor.h"
+#include "lldb/Utility/CompletionRequest.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Predicate.h"
 
@@ -97,10 +98,7 @@ typedef int (*FixIndentationCallbackType
                                           const StringList &lines,
                                           int cursor_position, void *baton);
 
-typedef int (*CompleteCallbackType)(const char *current_line,
-                                    const char *cursor, const char *last_char,
-                                    StringList &matches,
-                                    StringList &descriptions, void *baton);
+typedef int (*CompleteCallbackType)(CompletionRequest &request, void *baton);
 
 /// Status used to decide when and how to start editing another line in
 /// multi-line sessions

Modified: lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h Thu Aug 15 06:14:10 2019
@@ -308,29 +308,17 @@ public:
 
   CommandObject *GetCommandObjectForCommand(llvm::StringRef &command_line);
 
-  // This handles command line completion.  You are given a pointer to the
-  // command string buffer, to the current cursor, and to the end of the string
-  // (in case it is not NULL terminated). You also passed in an StringList
-  // object to fill with the returns. The first element of the array will be
-  // filled with the string that you would need to insert at the cursor point
-  // to complete the cursor point to the longest common matching prefix. If you
-  // want to limit the number of elements returned, set max_return_elements to
-  // the number of elements you want returned.  Otherwise set
-  // max_return_elements to -1. If you want to start some way into the match
-  // list, then set match_start_point to the desired start point. Returns: -1
+  // This handles command line completion. Returns: -1
   // if the completion character should be inserted -2 if the entire command
   // line should be deleted and replaced with matches.GetStringAtIndex(0)
   // INT_MAX if the number of matches is > max_return_elements, but it is
   // expensive to compute. Otherwise, returns the number of matches.
   //
   // FIXME: Only max_return_elements == -1 is supported at present.
-  int HandleCompletion(const char *current_line, const char *cursor,
-                       const char *last_char, StringList &matches,
-                       StringList &descriptions);
+  int HandleCompletion(CompletionRequest &request);
 
-  // This version just returns matches, and doesn't compute the substring.  It
-  // is here so the Help command can call it for the first argument. It uses
-  // a CompletionRequest for simplicity reasons.
+  // This version just returns matches, and doesn't compute the substring. It
+  // is here so the Help command can call it for the first argument.
   int HandleCompletionMatches(CompletionRequest &request);
 
   int GetCommandNamesMatchingPartialString(const char *cmd_cstr,

Modified: lldb/trunk/include/lldb/Utility/CompletionRequest.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/CompletionRequest.h?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/CompletionRequest.h (original)
+++ lldb/trunk/include/lldb/Utility/CompletionRequest.h Thu Aug 15 06:14:10 2019
@@ -75,6 +75,9 @@ public:
                     CompletionResult &result);
 
   llvm::StringRef GetRawLine() const { return m_command; }
+  llvm::StringRef GetRawLineUntilCursor() const {
+    return m_command.substr(0, m_cursor_index);
+  }
 
   unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
 

Modified: lldb/trunk/source/API/SBCommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBCommandInterpreter.cpp?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/source/API/SBCommandInterpreter.cpp (original)
+++ lldb/trunk/source/API/SBCommandInterpreter.cpp Thu Aug 15 06:14:10 2019
@@ -371,8 +371,11 @@ int SBCommandInterpreter::HandleCompleti
 
   if (IsValid()) {
     lldb_private::StringList lldb_matches, lldb_descriptions;
-    num_completions = m_opaque_ptr->HandleCompletion(
-        current_line, cursor, last_char, lldb_matches, lldb_descriptions);
+    CompletionResult result;
+    CompletionRequest request(current_line, cursor - current_line, result);
+    num_completions = m_opaque_ptr->HandleCompletion(request);
+    result.GetMatches(lldb_matches);
+    result.GetDescriptions(lldb_descriptions);
 
     SBStringList temp_matches_list(&lldb_matches);
     matches.AppendList(temp_matches_list);

Modified: lldb/trunk/source/Core/IOHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/IOHandler.cpp?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/source/Core/IOHandler.cpp (original)
+++ lldb/trunk/source/Core/IOHandler.cpp Thu Aug 15 06:14:10 2019
@@ -170,17 +170,15 @@ IOHandlerConfirm::IOHandlerConfirm(Debug
 
 IOHandlerConfirm::~IOHandlerConfirm() = default;
 
-int IOHandlerConfirm::IOHandlerComplete(
-    IOHandler &io_handler, const char *current_line, const char *cursor,
-    const char *last_char, StringList &matches, StringList &descriptions) {
-  if (current_line == cursor) {
-    if (m_default_response) {
-      matches.AppendString("y");
-    } else {
-      matches.AppendString("n");
-    }
+int IOHandlerConfirm::IOHandlerComplete(IOHandler &io_handler,
+                                        CompletionRequest &request) {
+  if (request.GetRawCursorPos() == 0) {
+    if (m_default_response)
+      request.AddCompletion("y");
+    else
+      request.AddCompletion("n");
   }
-  return matches.GetSize();
+  return request.GetNumberOfMatches();
 }
 
 void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler,
@@ -218,39 +216,43 @@ void IOHandlerConfirm::IOHandlerInputCom
   }
 }
 
-int IOHandlerDelegate::IOHandlerComplete(
-    IOHandler &io_handler, const char *current_line, const char *cursor,
-    const char *last_char, StringList &matches, StringList &descriptions) {
+int IOHandlerDelegate::IOHandlerComplete(IOHandler &io_handler,
+                                         CompletionRequest &request) {
   switch (m_completion) {
   case Completion::None:
     break;
 
   case Completion::LLDBCommand:
     return io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion(
-        current_line, cursor, last_char, matches, descriptions);
+        request);
   case Completion::Expression: {
     CompletionResult result;
-    CompletionRequest request(current_line, cursor - current_line, result);
+    CompletionRequest subrequest(request.GetRawLine(),
+                                 request.GetRawCursorPos(), result);
     CommandCompletions::InvokeCommonCompletionCallbacks(
         io_handler.GetDebugger().GetCommandInterpreter(),
-        CommandCompletions::eVariablePathCompletion, request, nullptr);
+        CommandCompletions::eVariablePathCompletion, subrequest, nullptr);
+    StringList matches;
+    StringList descriptions;
     result.GetMatches(matches);
     result.GetDescriptions(descriptions);
 
-    size_t num_matches = request.GetNumberOfMatches();
+    size_t num_matches = subrequest.GetNumberOfMatches();
     if (num_matches > 0) {
       std::string common_prefix = matches.LongestCommonPrefix();
-      const size_t partial_name_len = request.GetCursorArgumentPrefix().size();
+      const size_t partial_name_len =
+          subrequest.GetCursorArgumentPrefix().size();
 
       // If we matched a unique single command, add a space... Only do this if
       // the completer told us this was a complete word, however...
-      if (num_matches == 1 && request.GetWordComplete()) {
+      if (num_matches == 1 && subrequest.GetWordComplete()) {
         common_prefix.push_back(' ');
       }
       common_prefix.erase(0, partial_name_len);
-      matches.InsertStringAtIndex(0, std::move(common_prefix));
+      request.AddCompletion(common_prefix);
     }
-    return num_matches;
+    request.AddCompletions(matches, descriptions);
+    return request.GetNumberOfMatches();
   } break;
   }
 
@@ -443,14 +445,12 @@ int IOHandlerEditline::FixIndentationCal
       *editline_reader, lines, cursor_position);
 }
 
-int IOHandlerEditline::AutoCompleteCallback(
-    const char *current_line, const char *cursor, const char *last_char,
-    StringList &matches, StringList &descriptions, void *baton) {
+int IOHandlerEditline::AutoCompleteCallback(CompletionRequest &request,
+                                            void *baton) {
   IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton;
   if (editline_reader)
-    return editline_reader->m_delegate.IOHandlerComplete(
-        *editline_reader, current_line, cursor, last_char, matches,
-        descriptions);
+    return editline_reader->m_delegate.IOHandlerComplete(*editline_reader,
+                                                         request);
   return 0;
 }
 #endif

Modified: lldb/trunk/source/Expression/REPL.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/REPL.cpp?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/source/Expression/REPL.cpp (original)
+++ lldb/trunk/source/Expression/REPL.cpp Thu Aug 15 06:14:10 2019
@@ -433,28 +433,28 @@ void REPL::IOHandlerInputComplete(IOHand
   }
 }
 
-int REPL::IOHandlerComplete(IOHandler &io_handler, const char *current_line,
-                            const char *cursor, const char *last_char,
-                            StringList &matches, StringList &descriptions) {
-  matches.Clear();
-
-  llvm::StringRef line(current_line, cursor - current_line);
-
+int REPL::IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) {
   // Complete an LLDB command if the first character is a colon...
-  if (!line.empty() && line[0] == ':') {
+  if (request.GetRawLine().startswith(":")) {
     Debugger &debugger = m_target.GetDebugger();
 
     // auto complete LLDB commands
-    const char *lldb_current_line = line.substr(1).data();
-    return debugger.GetCommandInterpreter().HandleCompletion(
-        lldb_current_line, cursor, last_char, matches, descriptions);
+    llvm::StringRef new_line = request.GetRawLine().drop_front();
+    CompletionResult sub_result;
+    CompletionRequest sub_request(new_line, request.GetRawCursorPos() - 1,
+                                  sub_result);
+    int result = debugger.GetCommandInterpreter().HandleCompletion(sub_request);
+    StringList matches, descriptions;
+    sub_result.GetMatches(matches);
+    sub_result.GetDescriptions(descriptions);
+    request.AddCompletions(matches, descriptions);
+    return result;
   }
 
   // Strip spaces from the line and see if we had only spaces
-  line = line.ltrim();
-  if (line.empty()) {
+  if (request.GetRawLineUntilCursor().trim().empty()) {
     // Only spaces on this line, so just indent
-    matches.AppendString(m_indent_str);
+    request.AddCompletion(m_indent_str);
     return 1;
   }
 
@@ -477,12 +477,13 @@ int REPL::IOHandlerComplete(IOHandler &i
     }
   }
 
-  if (cursor > current_line) {
-    current_code.append("\n");
-    current_code.append(current_line, cursor - current_line);
-  }
+  current_code.append("\n");
+  current_code += request.GetRawLineUntilCursor();
 
-  return CompleteCode(current_code, matches);
+  StringList matches;
+  int result = CompleteCode(current_code, matches);
+  request.AddCompletions(matches);
+  return result;
 }
 
 bool QuitCommandOverrideCallback(void *baton, const char **argv) {

Modified: lldb/trunk/source/Host/common/Editline.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Editline.cpp?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Editline.cpp (original)
+++ lldb/trunk/source/Host/common/Editline.cpp Thu Aug 15 06:14:10 2019
@@ -14,6 +14,7 @@
 #include "lldb/Host/Editline.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Host.h"
+#include "lldb/Utility/CompletionRequest.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/LLDBAssert.h"
 #include "lldb/Utility/SelectHelper.h"
@@ -894,9 +895,17 @@ unsigned char Editline::TabCommand(int c
   StringList completions, descriptions;
   int page_size = 40;
 
-  const int num_completions = m_completion_callback(
-      line_info->buffer, line_info->cursor, line_info->lastchar,
-      completions, descriptions, m_completion_callback_baton);
+  llvm::StringRef line(line_info->buffer,
+                       line_info->lastchar - line_info->buffer);
+  unsigned cursor_index = line_info->cursor - line_info->buffer;
+  CompletionResult result;
+  CompletionRequest request(line, cursor_index, result);
+
+  const int num_completions =
+      m_completion_callback(request, m_completion_callback_baton);
+
+  result.GetMatches(completions);
+  result.GetDescriptions(descriptions);
 
   if (num_completions == 0)
     return CC_ERROR;

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=369000&r1=368999&r2=369000&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Thu Aug 15 06:14:10 2019
@@ -1814,18 +1814,16 @@ int CommandInterpreter::HandleCompletion
   return num_command_matches;
 }
 
-int CommandInterpreter::HandleCompletion(const char *current_line,
-                                         const char *cursor,
-                                         const char *last_char,
-                                         StringList &matches,
-                                         StringList &descriptions) {
-
-  llvm::StringRef command_line(current_line, last_char - current_line);
+int CommandInterpreter::HandleCompletion(CompletionRequest &orig_request) {
+  // Start a new subrequest we can modify.
   CompletionResult result;
-  CompletionRequest request(command_line, cursor - current_line, result);
+  CompletionRequest request(orig_request.GetRawLine(),
+                            orig_request.GetRawCursorPos(), result);
   // Don't complete comments, and if the line we are completing is just the
   // history repeat character, substitute the appropriate history line.
   const char *first_arg = request.GetParsedLine().GetArgumentAtIndex(0);
+  StringList matches, descriptions;
+
   if (first_arg) {
     if (first_arg[0] == m_comment_char)
       return 0;
@@ -1872,6 +1870,8 @@ int CommandInterpreter::HandleCompletion
     matches.InsertStringAtIndex(0, common_prefix.c_str());
     descriptions.InsertStringAtIndex(0, "");
   }
+  // Add completion to original request.
+  orig_request.AddCompletions(matches, descriptions);
   return num_command_matches;
 }
 




More information about the lldb-commits mailing list