diff options
author | Raphael Isemann <teemperor@gmail.com> | 2019-08-15 13:14:10 +0000 |
---|---|---|
committer | Raphael Isemann <teemperor@gmail.com> | 2019-08-15 13:14:10 +0000 |
commit | 2fc20f652cd848abdbfc40b6ae7fc0b0b403ea7d (patch) | |
tree | 44d036fe01586ba334cff331edceca798c93799c /lldb/source | |
parent | dc23c832f4f7e5ae8204acfe800bdb611525c8bd (diff) | |
download | bcm5719-llvm-2fc20f652cd848abdbfc40b6ae7fc0b0b403ea7d.tar.gz bcm5719-llvm-2fc20f652cd848abdbfc40b6ae7fc0b0b403ea7d.zip |
[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
llvm-svn: 369000
Diffstat (limited to 'lldb/source')
-rw-r--r-- | lldb/source/API/SBCommandInterpreter.cpp | 7 | ||||
-rw-r--r-- | lldb/source/Core/IOHandler.cpp | 54 | ||||
-rw-r--r-- | lldb/source/Expression/REPL.cpp | 39 | ||||
-rw-r--r-- | lldb/source/Host/common/Editline.cpp | 15 | ||||
-rw-r--r-- | lldb/source/Interpreter/CommandInterpreter.cpp | 16 |
5 files changed, 72 insertions, 59 deletions
diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp index 1c07e856395..ccc70b3ccdb 100644 --- a/lldb/source/API/SBCommandInterpreter.cpp +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -371,8 +371,11 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions( 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); diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp index 6dcd8e32fd4..1a35f300e52 100644 --- a/lldb/source/Core/IOHandler.cpp +++ b/lldb/source/Core/IOHandler.cpp @@ -170,17 +170,15 @@ IOHandlerConfirm::IOHandlerConfirm(Debugger &debugger, llvm::StringRef prompt, 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::IOHandlerInputComplete(IOHandler &io_handler, } } -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::FixIndentationCallback(Editline *editline, *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 diff --git a/lldb/source/Expression/REPL.cpp b/lldb/source/Expression/REPL.cpp index ffcfc1adf5b..d1e7b71e606 100644 --- a/lldb/source/Expression/REPL.cpp +++ b/lldb/source/Expression/REPL.cpp @@ -433,28 +433,28 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) { } } -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 &io_handler, const char *current_line, } } - 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) { diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index 4a34f53421e..de6560c7af4 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -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 ch) { 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; diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 114316ab264..86cca78241e 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -1814,18 +1814,16 @@ int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { 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(const char *current_line, matches.InsertStringAtIndex(0, common_prefix.c_str()); descriptions.InsertStringAtIndex(0, ""); } + // Add completion to original request. + orig_request.AddCompletions(matches, descriptions); return num_command_matches; } |