Skip to content

Commit 243f52b

Browse files
committed
[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).
1 parent af071f0 commit 243f52b

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

lldb/include/lldb/Utility/CompletionRequest.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,19 @@ class CompletionRequest {
115115
CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
116116
CompletionResult &result);
117117

118-
llvm::StringRef GetRawLine() const { return m_command; }
118+
/// Returns the raw user input used to create this CompletionRequest cut off
119+
/// at the cursor position. The cursor will be at the end of the raw line.
120+
llvm::StringRef GetRawLine() const {
121+
return m_command.substr(0, GetRawCursorPos());
122+
}
123+
124+
/// Returns the full raw user input used to create this CompletionRequest.
125+
/// This string is not cut off at the cursor position and will include
126+
/// characters behind the cursor position.
127+
///
128+
/// You should most likely *not* use this function unless the characters
129+
/// behind the cursor position influence the completion.
130+
llvm::StringRef GetRawLineWithUnusedSuffix() const { return m_command; }
119131

120132
unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
121133

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,12 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
311311
target = &GetDummyTarget();
312312

313313
unsigned cursor_pos = request.GetRawCursorPos();
314-
llvm::StringRef code = request.GetRawLine();
314+
// Get the full user input including the suffix. The suffix is necessary
315+
// as OptionsWithRaw will use it to detect if the cursor is cursor is in the
316+
// argument part of in the raw input part of the arguments. If we cut of
317+
// of the suffix then "expr -arg[cursor] --" would interpret the "-arg" as
318+
// the raw input (as the "--" is hidden in the suffix).
319+
llvm::StringRef code = request.GetRawLineWithUnusedSuffix();
315320

316321
const std::size_t original_code_size = code.size();
317322

lldb/unittests/Utility/CompletionRequestTest.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ TEST(CompletionRequest, Constructor) {
2121
CompletionRequest request(command, cursor_pos, result);
2222
result.GetMatches(matches);
2323

24-
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
24+
EXPECT_EQ(request.GetRawLine(), "a b");
25+
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
2526
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
2627
EXPECT_EQ(request.GetCursorIndex(), arg_index);
2728

@@ -38,7 +39,8 @@ TEST(CompletionRequest, FakeLastArg) {
3839

3940
CompletionRequest request(command, cursor_pos, result);
4041

41-
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
42+
EXPECT_EQ(request.GetRawLine(), command);
43+
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
4244
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
4345
EXPECT_EQ(request.GetCursorIndex(), 3U);
4446

@@ -93,7 +95,8 @@ TEST(CompletionRequest, ShiftArguments) {
9395
CompletionRequest request(command, cursor_pos, result);
9496
result.GetMatches(matches);
9597

96-
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
98+
EXPECT_EQ(request.GetRawLine(), "a b");
99+
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
97100
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
98101
EXPECT_EQ(request.GetCursorIndex(), arg_index);
99102

@@ -104,7 +107,8 @@ TEST(CompletionRequest, ShiftArguments) {
104107
request.ShiftArguments();
105108

106109
// The raw line/cursor stays identical.
107-
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
110+
EXPECT_EQ(request.GetRawLine(), "a b");
111+
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
108112
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
109113

110114
// Partially parsed line and cursor should be updated.

0 commit comments

Comments
 (0)