Author: teemperor Date: Wed Sep 25 05:40:01 2019 New Revision: 372858 URL: http://llvm.org/viewvc/llvm-project?rev=372858&view=rev Log: [lldb][NFC] Add CompletionRequest::AppendEmptyArgument
This is the only legitimate use we currently have for modifying a CompletionRequest. Add a utility function for this purpose and remove the remaining setters which go against the idea of having an immutable CompletionRequest. Modified: lldb/trunk/include/lldb/Utility/CompletionRequest.h lldb/trunk/source/Commands/CommandObjectMultiword.cpp lldb/trunk/source/Interpreter/CommandInterpreter.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=372858&r1=372857&r2=372858&view=diff ============================================================================== --- lldb/trunk/include/lldb/Utility/CompletionRequest.h (original) +++ lldb/trunk/include/lldb/Utility/CompletionRequest.h Wed Sep 25 05:40:01 2019 @@ -123,11 +123,15 @@ public: m_parsed_line.Shift(); } - void SetCursorIndex(size_t i) { m_cursor_index = i; } - size_t GetCursorIndex() const { return m_cursor_index; } + /// Adds an empty argument at the end of the argument list and moves + /// the cursor to this new argument. + void AppendEmptyArgument() { + m_parsed_line.AppendArgument(llvm::StringRef()); + m_cursor_index++; + m_cursor_char_position = 0; + } - void SetCursorCharPosition(size_t pos) { m_cursor_char_position = pos; } - size_t GetCursorCharPosition() const { return m_cursor_char_position; } + size_t GetCursorIndex() const { return m_cursor_index; } /// Adds a possible completion string. If the completion was already /// suggested before, it will not be added to the list of results. A copy of @@ -193,7 +197,7 @@ public: } llvm::StringRef GetCursorArgumentPrefix() const { - return GetCursorArgument().substr(0, GetCursorCharPosition()); + return GetCursorArgument().substr(0, m_cursor_char_position); } private: Modified: lldb/trunk/source/Commands/CommandObjectMultiword.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectMultiword.cpp?rev=372858&r1=372857&r2=372858&view=diff ============================================================================== --- lldb/trunk/source/Commands/CommandObjectMultiword.cpp (original) +++ lldb/trunk/source/Commands/CommandObjectMultiword.cpp Wed Sep 25 05:40:01 2019 @@ -195,8 +195,7 @@ void CommandObjectMultiword::HandleCompl if (cmd_obj != nullptr) { if (request.GetParsedLine().GetArgumentCount() != 1) { request.GetParsedLine().Shift(); - request.SetCursorCharPosition(0); - request.GetParsedLine().AppendArgument(llvm::StringRef()); + request.AppendEmptyArgument(); cmd_obj->HandleCompletion(request); } } Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=372858&r1=372857&r2=372858&view=diff ============================================================================== --- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original) +++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Wed Sep 25 05:40:01 2019 @@ -1779,9 +1779,7 @@ void CommandInterpreter::HandleCompletio look_for_subcommand = true; new_matches.DeleteStringAtIndex(0); new_descriptions.DeleteStringAtIndex(0); - request.GetParsedLine().AppendArgument(llvm::StringRef()); - request.SetCursorIndex(request.GetCursorIndex() + 1U); - request.SetCursorCharPosition(0); + request.AppendEmptyArgument(); } } request.AddCompletions(new_matches, new_descriptions); Modified: lldb/trunk/unittests/Utility/CompletionRequestTest.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CompletionRequestTest.cpp?rev=372858&r1=372857&r2=372858&view=diff ============================================================================== --- lldb/trunk/unittests/Utility/CompletionRequestTest.cpp (original) +++ lldb/trunk/unittests/Utility/CompletionRequestTest.cpp Wed Sep 25 05:40:01 2019 @@ -15,7 +15,6 @@ TEST(CompletionRequest, Constructor) { std::string command = "a bad c"; const unsigned cursor_pos = 3; const size_t arg_index = 1; - const size_t arg_cursor_pos = 1; StringList matches; CompletionResult result; @@ -25,10 +24,9 @@ TEST(CompletionRequest, Constructor) { EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str()); EXPECT_EQ(request.GetRawCursorPos(), cursor_pos); EXPECT_EQ(request.GetCursorIndex(), arg_index); - EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos); EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 2u); - EXPECT_STREQ(request.GetParsedLine().GetArgumentAtIndex(1), "b"); + EXPECT_EQ(request.GetCursorArgumentPrefix().str(), "b"); } TEST(CompletionRequest, FakeLastArg) { @@ -43,10 +41,9 @@ TEST(CompletionRequest, FakeLastArg) { EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str()); EXPECT_EQ(request.GetRawCursorPos(), cursor_pos); EXPECT_EQ(request.GetCursorIndex(), 3U); - EXPECT_EQ(request.GetCursorCharPosition(), 0U); EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 4U); - EXPECT_STREQ(request.GetParsedLine().GetArgumentAtIndex(3), ""); + EXPECT_EQ(request.GetCursorArgumentPrefix().str(), ""); } TEST(CompletionRequest, TryCompleteCurrentArgGood) { @@ -90,7 +87,6 @@ TEST(CompletionRequest, ShiftArguments) std::string command = "a bad c"; const unsigned cursor_pos = 3; const size_t arg_index = 1; - const size_t arg_cursor_pos = 1; StringList matches; CompletionResult result; @@ -100,7 +96,6 @@ TEST(CompletionRequest, ShiftArguments) EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str()); EXPECT_EQ(request.GetRawCursorPos(), cursor_pos); EXPECT_EQ(request.GetCursorIndex(), arg_index); - EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos); EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 2u); EXPECT_STREQ(request.GetParsedLine().GetArgumentAtIndex(1), "b"); @@ -112,13 +107,10 @@ TEST(CompletionRequest, ShiftArguments) EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str()); EXPECT_EQ(request.GetRawCursorPos(), cursor_pos); - // Relative cursor position in arg is identical. - EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos); - // Partially parsed line and cursor should be updated. EXPECT_EQ(request.GetCursorIndex(), arg_index - 1U); EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 1u); - EXPECT_STREQ(request.GetParsedLine().GetArgumentAtIndex(0), "b"); + EXPECT_EQ(request.GetCursorArgumentPrefix().str(), "b"); } TEST(CompletionRequest, DuplicateFiltering) { _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits