[Lldb-commits] [lldb] 243f52b - [lldb] Cut off unused suffix in CompletionRequest::GetRawLine

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Tue Jan 28 02:12:48 PST 2020


Author: Raphael Isemann
Date: 2020-01-28T11:12:22+01:00
New Revision: 243f52b58bcefab68fdebefc6d64f7f0c182c0fe

URL: https://github.com/llvm/llvm-project/commit/243f52b58bcefab68fdebefc6d64f7f0c182c0fe
DIFF: https://github.com/llvm/llvm-project/commit/243f52b58bcefab68fdebefc6d64f7f0c182c0fe.diff

LOG: [lldb] Cut off unused suffix in CompletionRequest::GetRawLine

The GetRawLine currently returns the full command line used
to create the CompletionRequest. So for example for "foo b[tab] --arg"
it would return the whole string instead of "foo b". Usually
completion code makes the wrong assumption that the cursor is at
the end of the line and handing out the complete line will cause
that people implement completions that also make this assumption.

This patch makes GetRawLine() return only the string until the
cursor and hides the suffix (so that the cursor is always at the
end of this string) and adds another function GetRawLineWithUnusedSuffix
that is specifically the line with the suffix that isn't used by
the CompletionRequest for argument parsing etc.

There is only one user of this new function that actually needs the
suffix and that is the expression command which needs the suffix to
detect if it is in the raw or argument part of the command (by looking
at the "--" separator).

Added: 
    

Modified: 
    lldb/include/lldb/Utility/CompletionRequest.h
    lldb/source/Commands/CommandObjectExpression.cpp
    lldb/unittests/Utility/CompletionRequestTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/CompletionRequest.h b/lldb/include/lldb/Utility/CompletionRequest.h
index 5418efe180bf..358d88703c71 100644
--- a/lldb/include/lldb/Utility/CompletionRequest.h
+++ b/lldb/include/lldb/Utility/CompletionRequest.h
@@ -115,7 +115,19 @@ class CompletionRequest {
   CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
                     CompletionResult &result);
 
-  llvm::StringRef GetRawLine() const { return m_command; }
+  /// Returns the raw user input used to create this CompletionRequest cut off
+  /// at the cursor position. The cursor will be at the end of the raw line.
+  llvm::StringRef GetRawLine() const {
+    return m_command.substr(0, GetRawCursorPos());
+  }
+
+  /// Returns the full raw user input used to create this CompletionRequest.
+  /// This string is not cut off at the cursor position and will include
+  /// characters behind the cursor position.
+  ///
+  /// You should most likely *not* use this function unless the characters
+  /// behind the cursor position influence the completion.
+  llvm::StringRef GetRawLineWithUnusedSuffix() const { return m_command; }
 
   unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
 

diff  --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index 6fb6b1df2923..fabc49683b8f 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -311,7 +311,12 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
     target = &GetDummyTarget();
 
   unsigned cursor_pos = request.GetRawCursorPos();
-  llvm::StringRef code = request.GetRawLine();
+  // Get the full user input including the suffix. The suffix is necessary
+  // as OptionsWithRaw will use it to detect if the cursor is cursor is in the
+  // argument part of in the raw input part of the arguments. If we cut of
+  // of the suffix then "expr -arg[cursor] --" would interpret the "-arg" as
+  // the raw input (as the "--" is hidden in the suffix).
+  llvm::StringRef code = request.GetRawLineWithUnusedSuffix();
 
   const std::size_t original_code_size = code.size();
 

diff  --git a/lldb/unittests/Utility/CompletionRequestTest.cpp b/lldb/unittests/Utility/CompletionRequestTest.cpp
index 24d9febcec2d..02a5f5366a42 100644
--- a/lldb/unittests/Utility/CompletionRequestTest.cpp
+++ b/lldb/unittests/Utility/CompletionRequestTest.cpp
@@ -21,7 +21,8 @@ TEST(CompletionRequest, Constructor) {
   CompletionRequest request(command, cursor_pos, result);
   result.GetMatches(matches);
 
-  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawLine(), "a b");
+  EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
   EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
   EXPECT_EQ(request.GetCursorIndex(), arg_index);
 
@@ -38,7 +39,8 @@ TEST(CompletionRequest, FakeLastArg) {
 
   CompletionRequest request(command, cursor_pos, result);
 
-  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawLine(), command);
+  EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
   EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
   EXPECT_EQ(request.GetCursorIndex(), 3U);
 
@@ -93,7 +95,8 @@ TEST(CompletionRequest, ShiftArguments) {
   CompletionRequest request(command, cursor_pos, result);
   result.GetMatches(matches);
 
-  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawLine(), "a b");
+  EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
   EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
   EXPECT_EQ(request.GetCursorIndex(), arg_index);
 
@@ -104,7 +107,8 @@ TEST(CompletionRequest, ShiftArguments) {
   request.ShiftArguments();
 
   // The raw line/cursor stays identical.
-  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawLine(), "a b");
+  EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
   EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
 
   // Partially parsed line and cursor should be updated.


        


More information about the lldb-commits mailing list