Commit 243f52b5 authored by Raphael Isemann's avatar Raphael Isemann
Browse files

[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).
parent af071f03
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -115,7 +115,19 @@ public:
  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; }

+6 −1
Original line number Diff line number Diff line
@@ -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();

+8 −4
Original line number Diff line number Diff line
@@ -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.