diff options
Diffstat (limited to 'lldb/source')
34 files changed, 344 insertions, 478 deletions
diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp index ccc70b3ccdb..98354c414a7 100644 --- a/lldb/source/API/SBCommandInterpreter.cpp +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -353,8 +353,6 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions( current_line, cursor, last_char, match_start_point, max_return_elements, matches, descriptions); - int num_completions = 0; - // Sanity check the arguments that are passed in: cursor & last_char have to // be within the current_line. if (current_line == nullptr || cursor == nullptr || last_char == nullptr) @@ -368,22 +366,50 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions( last_char - current_line > static_cast<ptrdiff_t>(current_line_size)) return 0; + if (!IsValid()) + return 0; - if (IsValid()) { - lldb_private::StringList 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); - SBStringList temp_descriptions_list(&lldb_descriptions); - descriptions.AppendList(temp_descriptions_list); + lldb_private::StringList lldb_matches, lldb_descriptions; + CompletionResult result; + CompletionRequest request(current_line, cursor - current_line, result); + m_opaque_ptr->HandleCompletion(request); + result.GetMatches(lldb_matches); + result.GetDescriptions(lldb_descriptions); + + // Make the result array indexed from 1 again by adding the 'common prefix' + // of all completions as element 0. This is done to emulate the old API. + if (request.GetParsedLine().GetArgumentCount() == 0) { + // If we got an empty string, insert nothing. + lldb_matches.InsertStringAtIndex(0, ""); + lldb_descriptions.InsertStringAtIndex(0, ""); + } else { + // Now figure out if there is a common substring, and if so put that in + // element 0, otherwise put an empty string in element 0. + std::string command_partial_str = request.GetCursorArgumentPrefix().str(); + + std::string common_prefix = lldb_matches.LongestCommonPrefix(); + const size_t partial_name_len = command_partial_str.size(); + common_prefix.erase(0, partial_name_len); + + // 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 (lldb_matches.GetSize() == 1) { + char quote_char = request.GetParsedArg().quote; + common_prefix = + Args::EscapeLLDBCommandArgument(common_prefix, quote_char); + if (request.GetParsedArg().IsQuoted()) + common_prefix.push_back(quote_char); + common_prefix.push_back(' '); + } + lldb_matches.InsertStringAtIndex(0, common_prefix.c_str()); + lldb_descriptions.InsertStringAtIndex(0, ""); } - return num_completions; + SBStringList temp_matches_list(&lldb_matches); + matches.AppendList(temp_matches_list); + SBStringList temp_descriptions_list(&lldb_descriptions); + descriptions.AppendList(temp_descriptions_list); + return result.GetNumberOfResults(); } int SBCommandInterpreter::HandleCompletionWithDescriptions( diff --git a/lldb/source/Commands/CommandCompletions.cpp b/lldb/source/Commands/CommandCompletions.cpp index 1cff39c3734..a06f9a4db53 100644 --- a/lldb/source/Commands/CommandCompletions.cpp +++ b/lldb/source/Commands/CommandCompletions.cpp @@ -71,10 +71,9 @@ bool CommandCompletions::InvokeCommonCompletionCallbacks( return handled; } -int CommandCompletions::SourceFiles(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - request.SetWordComplete(true); +void CommandCompletions::SourceFiles(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { // Find some way to switch "include support files..." SourceFileCompleter completer(interpreter, false, request); @@ -85,12 +84,11 @@ int CommandCompletions::SourceFiles(CommandInterpreter &interpreter, } else { completer.DoCompletion(searcher); } - return request.GetNumberOfMatches(); } -static int DiskFilesOrDirectories(const llvm::Twine &partial_name, - bool only_directories, StringList &matches, - TildeExpressionResolver &Resolver) { +static void DiskFilesOrDirectories(const llvm::Twine &partial_name, + bool only_directories, StringList &matches, + TildeExpressionResolver &Resolver) { matches.Clear(); llvm::SmallString<256> CompletionBuffer; @@ -98,7 +96,7 @@ static int DiskFilesOrDirectories(const llvm::Twine &partial_name, partial_name.toVector(CompletionBuffer); if (CompletionBuffer.size() >= PATH_MAX) - return matches.GetSize(); + return; namespace path = llvm::sys::path; @@ -129,7 +127,7 @@ static int DiskFilesOrDirectories(const llvm::Twine &partial_name, matches.AppendString(Resolved); } } - return matches.GetSize(); + return; } // If there was no trailing slash, then we're done as soon as we resolve @@ -139,7 +137,7 @@ static int DiskFilesOrDirectories(const llvm::Twine &partial_name, // Make sure it ends with a separator. path::append(CompletionBuffer, path::get_separator()); matches.AppendString(CompletionBuffer); - return matches.GetSize(); + return; } // We want to keep the form the user typed, so we special case this to @@ -221,49 +219,44 @@ static int DiskFilesOrDirectories(const llvm::Twine &partial_name, matches.AppendString(CompletionBuffer); } - - return matches.GetSize(); } -static int DiskFilesOrDirectories(CompletionRequest &request, - bool only_directories) { - request.SetWordComplete(false); +static void DiskFilesOrDirectories(CompletionRequest &request, + bool only_directories) { StandardTildeExpressionResolver resolver; StringList matches; DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories, matches, resolver); request.AddCompletions(matches); - return request.GetNumberOfMatches(); } -int CommandCompletions::DiskFiles(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - return DiskFilesOrDirectories(request, /*only_dirs*/ false); +void CommandCompletions::DiskFiles(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + DiskFilesOrDirectories(request, /*only_dirs*/ false); } -int CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name, - StringList &matches, - TildeExpressionResolver &Resolver) { - return DiskFilesOrDirectories(partial_file_name, false, matches, Resolver); +void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name, + StringList &matches, + TildeExpressionResolver &Resolver) { + DiskFilesOrDirectories(partial_file_name, false, matches, Resolver); } -int CommandCompletions::DiskDirectories(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - return DiskFilesOrDirectories(request, /*only_dirs*/ true); +void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + DiskFilesOrDirectories(request, /*only_dirs*/ true); } -int CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name, - StringList &matches, - TildeExpressionResolver &Resolver) { - return DiskFilesOrDirectories(partial_file_name, true, matches, Resolver); +void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name, + StringList &matches, + TildeExpressionResolver &Resolver) { + DiskFilesOrDirectories(partial_file_name, true, matches, Resolver); } -int CommandCompletions::Modules(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - request.SetWordComplete(true); +void CommandCompletions::Modules(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { ModuleCompleter completer(interpreter, request); if (searcher == nullptr) { @@ -273,13 +266,11 @@ int CommandCompletions::Modules(CommandInterpreter &interpreter, } else { completer.DoCompletion(searcher); } - return request.GetNumberOfMatches(); } -int CommandCompletions::Symbols(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - request.SetWordComplete(true); +void CommandCompletions::Symbols(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { SymbolCompleter completer(interpreter, request); if (searcher == nullptr) { @@ -289,12 +280,11 @@ int CommandCompletions::Symbols(CommandInterpreter &interpreter, } else { completer.DoCompletion(searcher); } - return request.GetNumberOfMatches(); } -int CommandCompletions::SettingsNames(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { +void CommandCompletions::SettingsNames(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { // Cache the full setting name list static StringList g_property_names; if (g_property_names.GetSize() == 0) { @@ -309,44 +299,29 @@ int CommandCompletions::SettingsNames(CommandInterpreter &interpreter, } } - bool exact_match = false; - for (const std::string &s : g_property_names) { - if (llvm::StringRef(s).startswith(request.GetCursorArgumentPrefix())) { - if (request.GetCursorArgumentPrefix() == s) - exact_match = true; + if (llvm::StringRef(s).startswith(request.GetCursorArgumentPrefix())) request.AddCompletion(s); - } } - - request.SetWordComplete(exact_match); - - return request.GetNumberOfMatches(); } -int CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - StringList new_matches; - std::size_t num_matches = PluginManager::AutoCompletePlatformName( - request.GetCursorArgumentPrefix(), new_matches); - request.SetWordComplete(num_matches == 1); - request.AddCompletions(new_matches); - return request.GetNumberOfMatches(); +void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(), + request); } -int CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - const uint32_t num_matches = ArchSpec::AutoComplete(request); - request.SetWordComplete(num_matches == 1); - return num_matches; +void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + ArchSpec::AutoComplete(request); } -int CommandCompletions::VariablePath(CommandInterpreter &interpreter, - CompletionRequest &request, - SearchFilter *searcher) { - return Variable::AutoComplete(interpreter.GetExecutionContext(), request); +void CommandCompletions::VariablePath(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + Variable::AutoComplete(interpreter.GetExecutionContext(), request); } CommandCompletions::Completer::Completer(CommandInterpreter &interpreter, @@ -417,15 +392,14 @@ CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter, return Searcher::eCallbackReturnContinue; } -size_t -CommandCompletions::SourceFileCompleter::DoCompletion(SearchFilter *filter) { +void CommandCompletions::SourceFileCompleter::DoCompletion( + SearchFilter *filter) { filter->Search(*this); // Now convert the filelist to completions: for (size_t i = 0; i < m_matching_files.GetSize(); i++) { m_request.AddCompletion( m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString()); } - return m_request.GetNumberOfMatches(); } // SymbolCompleter @@ -489,13 +463,11 @@ Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback( return Searcher::eCallbackReturnContinue; } -size_t CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) { +void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) { filter->Search(*this); collection::iterator pos = m_match_set.begin(), end = m_match_set.end(); for (pos = m_match_set.begin(); pos != end; pos++) m_request.AddCompletion((*pos).GetCString()); - - return m_request.GetNumberOfMatches(); } // ModuleCompleter @@ -536,7 +508,6 @@ Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback( return Searcher::eCallbackReturnContinue; } -size_t CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) { +void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) { filter->Search(*this); - return m_request.GetNumberOfMatches(); } diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 65fc8869edf..4eb438f2b96 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -215,13 +215,12 @@ public: return ""; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } Options *GetOptions() override { return &m_options; } @@ -1388,13 +1387,12 @@ public: ~CommandObjectCommandsScriptImport() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } Options *GetOptions() override { return &m_options; } diff --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp index 46f12d8685b..79003d324c6 100644 --- a/lldb/source/Commands/CommandObjectExpression.cpp +++ b/lldb/source/Commands/CommandObjectExpression.cpp @@ -294,7 +294,7 @@ CommandObjectExpression::~CommandObjectExpression() = default; Options *CommandObjectExpression::GetOptions() { return &m_option_group; } -int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { +void CommandObjectExpression::HandleCompletion(CompletionRequest &request) { EvaluateExpressionOptions options; options.SetCoerceToId(m_varobj_options.use_objc); options.SetLanguage(m_command_options.language); @@ -311,7 +311,7 @@ int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { // This didn't work, so let's get out before we start doing things that // expect a valid frame pointer. if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) - return 0; + return; ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); @@ -321,7 +321,7 @@ int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { target = GetDummyTarget(); if (!target) - return 0; + return; unsigned cursor_pos = request.GetRawCursorPos(); llvm::StringRef code = request.GetRawLine(); @@ -341,7 +341,7 @@ int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { // exit. // FIXME: We should complete the options here. if (cursor_pos < raw_start) - return 0; + return; // Make the cursor_pos again relative to the start of the code string. assert(cursor_pos >= raw_start); @@ -354,10 +354,9 @@ int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { code, llvm::StringRef(), language, UserExpression::eResultTypeAny, options, nullptr, error)); if (error.Fail()) - return 0; + return; expr->Complete(exe_ctx, request, cursor_pos); - return request.GetNumberOfMatches(); } static lldb_private::Status diff --git a/lldb/source/Commands/CommandObjectExpression.h b/lldb/source/Commands/CommandObjectExpression.h index 89c8e1dbece..8ef76423906 100644 --- a/lldb/source/Commands/CommandObjectExpression.h +++ b/lldb/source/Commands/CommandObjectExpression.h @@ -54,7 +54,7 @@ public: Options *GetOptions() override; - int HandleCompletion(CompletionRequest &request) override; + void HandleCompletion(CompletionRequest &request) override; protected: // IOHandler::Delegate functions diff --git a/lldb/source/Commands/CommandObjectFrame.cpp b/lldb/source/Commands/CommandObjectFrame.cpp index 1087c611169..6cbf341d674 100644 --- a/lldb/source/Commands/CommandObjectFrame.cpp +++ b/lldb/source/Commands/CommandObjectFrame.cpp @@ -451,14 +451,13 @@ public: Options *GetOptions() override { return &m_option_group; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Arguments are the standard source file completer. CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: diff --git a/lldb/source/Commands/CommandObjectHelp.cpp b/lldb/source/Commands/CommandObjectHelp.cpp index 1100ddfe18e..811841fa47a 100644 --- a/lldb/source/Commands/CommandObjectHelp.cpp +++ b/lldb/source/Commands/CommandObjectHelp.cpp @@ -201,10 +201,10 @@ bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) { return result.Succeeded(); } -int CommandObjectHelp::HandleCompletion(CompletionRequest &request) { +void CommandObjectHelp::HandleCompletion(CompletionRequest &request) { // Return the completions of the commands in the help system: if (request.GetCursorIndex() == 0) { - return m_interpreter.HandleCompletionMatches(request); + m_interpreter.HandleCompletionMatches(request); } else { CommandObject *cmd_obj = m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref); @@ -216,9 +216,9 @@ int CommandObjectHelp::HandleCompletion(CompletionRequest &request) { if (cmd_obj) { request.GetParsedLine().Shift(); request.SetCursorIndex(request.GetCursorIndex() - 1); - return cmd_obj->HandleCompletion(request); + cmd_obj->HandleCompletion(request); } else { - return m_interpreter.HandleCompletionMatches(request); + m_interpreter.HandleCompletionMatches(request); } } } diff --git a/lldb/source/Commands/CommandObjectHelp.h b/lldb/source/Commands/CommandObjectHelp.h index a641b19a46d..826cd1e18a8 100644 --- a/lldb/source/Commands/CommandObjectHelp.h +++ b/lldb/source/Commands/CommandObjectHelp.h @@ -23,7 +23,7 @@ public: ~CommandObjectHelp() override; - int HandleCompletion(CompletionRequest &request) override; + void HandleCompletion(CompletionRequest &request) override; static void GenerateAdditionalHelpAvenuesMessage( Stream *s, llvm::StringRef command, llvm::StringRef prefix, diff --git a/lldb/source/Commands/CommandObjectMultiword.cpp b/lldb/source/Commands/CommandObjectMultiword.cpp index eeda1fed25c..ecc73a8cbe9 100644 --- a/lldb/source/Commands/CommandObjectMultiword.cpp +++ b/lldb/source/Commands/CommandObjectMultiword.cpp @@ -179,11 +179,7 @@ void CommandObjectMultiword::GenerateHelpText(Stream &output_stream) { "'help <command> <subcommand>'.\n"); } -int CommandObjectMultiword::HandleCompletion(CompletionRequest &request) { - // Any of the command matches will provide a complete word, otherwise the - // individual completers will override this. - request.SetWordComplete(true); - +void CommandObjectMultiword::HandleCompletion(CompletionRequest &request) { auto arg0 = request.GetParsedLine()[0].ref; if (request.GetCursorIndex() == 0) { StringList new_matches, descriptions; @@ -197,23 +193,19 @@ int CommandObjectMultiword::HandleCompletion(CompletionRequest &request) { StringList temp_matches; CommandObject *cmd_obj = GetSubcommandObject(arg0, &temp_matches); if (cmd_obj != nullptr) { - if (request.GetParsedLine().GetArgumentCount() == 1) { - request.SetWordComplete(true); - } else { + if (request.GetParsedLine().GetArgumentCount() != 1) { request.GetParsedLine().Shift(); request.SetCursorCharPosition(0); request.GetParsedLine().AppendArgument(llvm::StringRef()); - return cmd_obj->HandleCompletion(request); + cmd_obj->HandleCompletion(request); } } } - return new_matches.GetSize(); } else { StringList new_matches; CommandObject *sub_command_object = GetSubcommandObject(arg0, &new_matches); if (sub_command_object == nullptr) { request.AddCompletions(new_matches); - return request.GetNumberOfMatches(); } else { // Remove the one match that we got from calling GetSubcommandObject. new_matches.DeleteStringAtIndex(0); @@ -360,19 +352,17 @@ Options *CommandObjectProxy::GetOptions() { return nullptr; } -int CommandObjectProxy::HandleCompletion(CompletionRequest &request) { +void CommandObjectProxy::HandleCompletion(CompletionRequest &request) { CommandObject *proxy_command = GetProxyCommandObject(); if (proxy_command) - return proxy_command->HandleCompletion(request); - return 0; + proxy_command->HandleCompletion(request); } -int CommandObjectProxy::HandleArgumentCompletion( +void CommandObjectProxy::HandleArgumentCompletion( CompletionRequest &request, OptionElementVector &opt_element_vector) { CommandObject *proxy_command = GetProxyCommandObject(); if (proxy_command) - return proxy_command->HandleArgumentCompletion(request, opt_element_vector); - return 0; + proxy_command->HandleArgumentCompletion(request, opt_element_vector); } const char *CommandObjectProxy::GetRepeatCommand(Args ¤t_command_args, diff --git a/lldb/source/Commands/CommandObjectPlatform.cpp b/lldb/source/Commands/CommandObjectPlatform.cpp index 9080ced54ff..4d0b02ad39b 100644 --- a/lldb/source/Commands/CommandObjectPlatform.cpp +++ b/lldb/source/Commands/CommandObjectPlatform.cpp @@ -158,10 +158,9 @@ public: ~CommandObjectPlatformSelect() override = default; - int HandleCompletion(CompletionRequest &request) override { + void HandleCompletion(CompletionRequest &request) override { CommandCompletions::PlatformPluginNames(GetCommandInterpreter(), request, nullptr); - return request.GetNumberOfMatches(); } Options *GetOptions() override { return &m_option_group; } diff --git a/lldb/source/Commands/CommandObjectPlugin.cpp b/lldb/source/Commands/CommandObjectPlugin.cpp index 89e01ba5202..670835c3049 100644 --- a/lldb/source/Commands/CommandObjectPlugin.cpp +++ b/lldb/source/Commands/CommandObjectPlugin.cpp @@ -37,13 +37,12 @@ public: ~CommandObjectPluginLoad() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp index 3d7a59951b5..620be4e5586 100644 --- a/lldb/source/Commands/CommandObjectProcess.cpp +++ b/lldb/source/Commands/CommandObjectProcess.cpp @@ -127,14 +127,13 @@ public: ~CommandObjectProcessLaunch() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } Options *GetOptions() override { return &m_options; } diff --git a/lldb/source/Commands/CommandObjectSettings.cpp b/lldb/source/Commands/CommandObjectSettings.cpp index 2bdbcd52bc1..b9a7b64f91c 100644 --- a/lldb/source/Commands/CommandObjectSettings.cpp +++ b/lldb/source/Commands/CommandObjectSettings.cpp @@ -126,9 +126,9 @@ insert-before or insert-after."); bool m_force; }; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { const size_t argc = request.GetParsedLine().GetArgumentCount(); const char *arg = nullptr; @@ -164,7 +164,6 @@ insert-before or insert-after."); } } } - return request.GetNumberOfMatches(); } protected: @@ -271,13 +270,12 @@ public: ~CommandObjectSettingsShow() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: @@ -527,13 +525,12 @@ public: ~CommandObjectSettingsList() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: @@ -615,14 +612,13 @@ public: bool WantsCompletion() override { return true; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: @@ -728,16 +724,14 @@ public: // !WantsRawCommandString. bool WantsCompletion() override { return true; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Attempting to complete variable name if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - - return request.GetNumberOfMatches(); } protected: @@ -826,16 +820,14 @@ public: // !WantsRawCommandString. bool WantsCompletion() override { return true; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Attempting to complete variable name if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - - return request.GetNumberOfMatches(); } protected: @@ -929,16 +921,14 @@ public: // !WantsRawCommandString. bool WantsCompletion() override { return true; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Attempting to complete variable name if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - - return request.GetNumberOfMatches(); } protected: @@ -1021,16 +1011,14 @@ public: // !WantsRawCommandString. bool WantsCompletion() override { return true; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Attempting to complete variable name if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - - return request.GetNumberOfMatches(); } protected: @@ -1100,16 +1088,14 @@ public: ~CommandObjectSettingsClear() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { // Attempting to complete variable name if (request.GetCursorIndex() < 2) CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion, request, nullptr); - - return request.GetNumberOfMatches(); } protected: diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp index b1879c12904..325926373a4 100644 --- a/lldb/source/Commands/CommandObjectTarget.cpp +++ b/lldb/source/Commands/CommandObjectTarget.cpp @@ -258,13 +258,12 @@ public: Options *GetOptions() override { return &m_option_group; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: @@ -1844,13 +1843,12 @@ public: ~CommandObjectTargetModulesModuleAutoComplete() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eModuleCompletion, request, nullptr); - return request.GetNumberOfMatches(); } }; @@ -1883,13 +1881,12 @@ public: ~CommandObjectTargetModulesSourceFileAutoComplete() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eSourceFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } }; @@ -2534,13 +2531,12 @@ public: Options *GetOptions() override { return &m_option_group; } - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } protected: @@ -4090,13 +4086,12 @@ public: ~CommandObjectTargetSymbolsAdd() override = default; - int HandleArgumentCompletion( - CompletionRequest &request, - OptionElementVector &opt_element_vector) override { + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, request, nullptr); - return request.GetNumberOfMatches(); } Options *GetOptions() override { return &m_option_group; } diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp index de61c641a56..c90828f4098 100644 --- a/lldb/source/Core/FormatEntity.cpp +++ b/lldb/source/Core/FormatEntity.cpp @@ -2346,33 +2346,31 @@ static void AddMatches(const FormatEntity::Entry::Definition *def, } } -size_t FormatEntity::AutoComplete(CompletionRequest &request) { +void FormatEntity::AutoComplete(CompletionRequest &request) { llvm::StringRef str = request.GetCursorArgumentPrefix(); - request.SetWordComplete(false); - const size_t dollar_pos = str.rfind('$'); if (dollar_pos == llvm::StringRef::npos) - return 0; + return; // Hitting TAB after $ at the end of the string add a "{" if (dollar_pos == str.size() - 1) { std::string match = str.str(); match.append("{"); request.AddCompletion(match); - return 1; + return; } if (str[dollar_pos + 1] != '{') - return 0; + return; const size_t close_pos = str.find('}', dollar_pos + 2); if (close_pos != llvm::StringRef::npos) - return 0; + return; const size_t format_pos = str.find('%', dollar_pos + 2); if (format_pos != llvm::StringRef::npos) - return 0; + return; llvm::StringRef partial_variable(str.substr(dollar_pos + 2)); if (partial_variable.empty()) { @@ -2380,7 +2378,7 @@ size_t FormatEntity::AutoComplete(CompletionRequest &request) { StringList new_matches; AddMatches(&g_root, str, llvm::StringRef(), new_matches); request.AddCompletions(new_matches); - return request.GetNumberOfMatches(); + return; } // We have a partially specified variable, find it @@ -2388,7 +2386,7 @@ size_t FormatEntity::AutoComplete(CompletionRequest &request) { const FormatEntity::Entry::Definition *entry_def = FindEntry(partial_variable, &g_root, remainder); if (!entry_def) - return 0; + return; const size_t n = entry_def->num_children; @@ -2400,7 +2398,6 @@ size_t FormatEntity::AutoComplete(CompletionRequest &request) { } else { // "${thread.id" <TAB> request.AddCompletion(MakeMatch(str, "}")); - request.SetWordComplete(true); } } else if (remainder.equals(".")) { // "${thread." <TAB> @@ -2414,5 +2411,4 @@ size_t FormatEntity::AutoComplete(CompletionRequest &request) { AddMatches(entry_def, str, remainder, new_matches); request.AddCompletions(new_matches); } - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp index 1a35f300e52..a95e4c17f08 100644 --- a/lldb/source/Core/IOHandler.cpp +++ b/lldb/source/Core/IOHandler.cpp @@ -170,15 +170,14 @@ IOHandlerConfirm::IOHandlerConfirm(Debugger &debugger, llvm::StringRef prompt, IOHandlerConfirm::~IOHandlerConfirm() = default; -int IOHandlerConfirm::IOHandlerComplete(IOHandler &io_handler, - CompletionRequest &request) { +void IOHandlerConfirm::IOHandlerComplete(IOHandler &io_handler, + CompletionRequest &request) { if (request.GetRawCursorPos() == 0) { if (m_default_response) request.AddCompletion("y"); else request.AddCompletion("n"); } - return request.GetNumberOfMatches(); } void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler, @@ -216,47 +215,20 @@ void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler, } } -int IOHandlerDelegate::IOHandlerComplete(IOHandler &io_handler, - CompletionRequest &request) { +void IOHandlerDelegate::IOHandlerComplete(IOHandler &io_handler, + CompletionRequest &request) { switch (m_completion) { case Completion::None: break; - case Completion::LLDBCommand: - return io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion( - request); - case Completion::Expression: { - CompletionResult result; - CompletionRequest subrequest(request.GetRawLine(), - request.GetRawCursorPos(), result); + io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion(request); + break; + case Completion::Expression: CommandCompletions::InvokeCommonCompletionCallbacks( io_handler.GetDebugger().GetCommandInterpreter(), - CommandCompletions::eVariablePathCompletion, subrequest, nullptr); - StringList matches; - StringList descriptions; - result.GetMatches(matches); - result.GetDescriptions(descriptions); - - size_t num_matches = subrequest.GetNumberOfMatches(); - if (num_matches > 0) { - std::string common_prefix = matches.LongestCommonPrefix(); - 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 && subrequest.GetWordComplete()) { - common_prefix.push_back(' '); - } - common_prefix.erase(0, partial_name_len); - request.AddCompletion(common_prefix); - } - request.AddCompletions(matches, descriptions); - return request.GetNumberOfMatches(); - } break; + CommandCompletions::eVariablePathCompletion, request, nullptr); + break; } - - return 0; } IOHandlerEditline::IOHandlerEditline( @@ -445,13 +417,11 @@ int IOHandlerEditline::FixIndentationCallback(Editline *editline, *editline_reader, lines, cursor_position); } -int IOHandlerEditline::AutoCompleteCallback(CompletionRequest &request, - void *baton) { +void IOHandlerEditline::AutoCompleteCallback(CompletionRequest &request, + void *baton) { IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton; if (editline_reader) - return editline_reader->m_delegate.IOHandlerComplete(*editline_reader, - request); - return 0; + editline_reader->m_delegate.IOHandlerComplete(*editline_reader, request); } #endif diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp index 24cadcd85bf..e0026e3d001 100644 --- a/lldb/source/Core/PluginManager.cpp +++ b/lldb/source/Core/PluginManager.cpp @@ -1341,10 +1341,10 @@ PluginManager::GetPlatformCreateCallbackForPluginName(ConstString name) { return nullptr; } -size_t PluginManager::AutoCompletePlatformName(llvm::StringRef name, - StringList &matches) { +void PluginManager::AutoCompletePlatformName(llvm::StringRef name, + CompletionRequest &request) { if (name.empty()) - return matches.GetSize(); + return; std::lock_guard<std::recursive_mutex> guard(GetPlatformInstancesMutex()); PlatformInstances &instances = GetPlatformInstances(); @@ -1354,9 +1354,8 @@ size_t PluginManager::AutoCompletePlatformName(llvm::StringRef name, for (pos = instances.begin(); pos != end; ++pos) { llvm::StringRef plugin_name(pos->name.GetCString()); if (plugin_name.startswith(name_sref)) - matches.AppendString(plugin_name.data()); + request.AddCompletion(plugin_name.data()); } - return matches.GetSize(); } #pragma mark Process diff --git a/lldb/source/Expression/REPL.cpp b/lldb/source/Expression/REPL.cpp index d1e7b71e606..01350f3dd74 100644 --- a/lldb/source/Expression/REPL.cpp +++ b/lldb/source/Expression/REPL.cpp @@ -433,7 +433,8 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) { } } -int REPL::IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) { +void REPL::IOHandlerComplete(IOHandler &io_handler, + CompletionRequest &request) { // Complete an LLDB command if the first character is a colon... if (request.GetRawLine().startswith(":")) { Debugger &debugger = m_target.GetDebugger(); @@ -443,19 +444,19 @@ int REPL::IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) { CompletionResult sub_result; CompletionRequest sub_request(new_line, request.GetRawCursorPos() - 1, sub_result); - int result = debugger.GetCommandInterpreter().HandleCompletion(sub_request); + debugger.GetCommandInterpreter().HandleCompletion(sub_request); StringList matches, descriptions; sub_result.GetMatches(matches); sub_result.GetDescriptions(descriptions); request.AddCompletions(matches, descriptions); - return result; + return; } // Strip spaces from the line and see if we had only spaces if (request.GetRawLineUntilCursor().trim().empty()) { // Only spaces on this line, so just indent request.AddCompletion(m_indent_str); - return 1; + return; } std::string current_code; @@ -482,8 +483,12 @@ int REPL::IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) { StringList matches; int result = CompleteCode(current_code, matches); - request.AddCompletions(matches); - return result; + if (result == -2) { + assert(matches.GetSize() == 1); + request.AddCompletion(matches.GetStringAtIndex(0), "", + CompletionMode::RewriteLine); + } else + request.AddCompletions(matches); } bool QuitCommandOverrideCallback(void *baton, const char **argv) { diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index de6560c7af4..a2e8773f8f1 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -864,26 +864,59 @@ unsigned char Editline::BufferEndCommand(int ch) { /// Prints completions and their descriptions to the given file. Only the /// completions in the interval [start, end) are printed. -static void PrintCompletion(FILE *output_file, size_t start, size_t end, - StringList &completions, StringList &descriptions) { - // This is an 'int' because of printf. - int max_len = 0; - - for (size_t i = start; i < end; i++) { - const char *completion_str = completions.GetStringAtIndex(i); - max_len = std::max((int)strlen(completion_str), max_len); +static void +PrintCompletion(FILE *output_file, + llvm::ArrayRef<CompletionResult::Completion> results, + size_t max_len) { + for (const CompletionResult::Completion &c : results) { + fprintf(output_file, "\t%-*s", (int)max_len, c.GetCompletion().c_str()); + if (!c.GetDescription().empty()) + fprintf(output_file, " -- %s", c.GetDescription().c_str()); + fprintf(output_file, "\n"); } +} + +static void +DisplayCompletions(::EditLine *editline, FILE *output_file, + llvm::ArrayRef<CompletionResult::Completion> results) { + assert(!results.empty()); - for (size_t i = start; i < end; i++) { - const char *completion_str = completions.GetStringAtIndex(i); - const char *description_str = descriptions.GetStringAtIndex(i); + fprintf(output_file, "\n" ANSI_CLEAR_BELOW "Available completions:\n"); + const size_t page_size = 40; + bool all = false; + + auto longest = + std::max_element(results.begin(), results.end(), [](auto &c1, auto &c2) { + return c1.GetCompletion().size() < c2.GetCompletion().size(); + }); - if (completion_str) - fprintf(output_file, "\n\t%-*s", max_len, completion_str); + const size_t max_len = longest->GetCompletion().size(); - // Print the description if we got one. - if (description_str && strlen(description_str)) - fprintf(output_file, " -- %s", description_str); + if (results.size() < page_size) { + PrintCompletion(output_file, results, max_len); + return; + } + + size_t cur_pos = 0; + while (cur_pos < results.size()) { + size_t remaining = results.size() - cur_pos; + size_t next_size = all ? remaining : std::min(page_size, remaining); + + PrintCompletion(output_file, results.slice(cur_pos, next_size), max_len); + + cur_pos += next_size; + + if (cur_pos >= results.size()) + break; + + fprintf(output_file, "More (Y/n/a): "); + char reply = 'n'; + int got_char = el_getc(editline, &reply); + fprintf(output_file, "\n"); + if (got_char == -1 || reply == 'n') + break; + if (reply == 'a') + all = true; } } @@ -892,8 +925,6 @@ unsigned char Editline::TabCommand(int ch) { return CC_ERROR; const LineInfo *line_info = el_line(m_editline); - StringList completions, descriptions; - int page_size = 40; llvm::StringRef line(line_info->buffer, line_info->lastchar - line_info->buffer); @@ -901,71 +932,51 @@ unsigned char Editline::TabCommand(int ch) { CompletionResult result; CompletionRequest request(line, cursor_index, result); - const int num_completions = - m_completion_callback(request, m_completion_callback_baton); + m_completion_callback(request, m_completion_callback_baton); + + llvm::ArrayRef<CompletionResult::Completion> results = result.GetResults(); + StringList completions; result.GetMatches(completions); - result.GetDescriptions(descriptions); - if (num_completions == 0) + if (results.size() == 0) return CC_ERROR; - // if (num_completions == -1) - // { - // el_insertstr (m_editline, m_completion_key); - // return CC_REDISPLAY; - // } - // else - if (num_completions == -2) { - // Replace the entire line with the first string... - el_deletestr(m_editline, line_info->cursor - line_info->buffer); - el_insertstr(m_editline, completions.GetStringAtIndex(0)); + + if (results.size() == 1) { + CompletionResult::Completion completion = results.front(); + switch (completion.GetMode()) { + case CompletionMode::Normal: { + std::string to_add = completion.GetCompletion(); + to_add = to_add.substr(request.GetCursorArgumentPrefix().size()); + if (request.GetParsedArg().IsQuoted()) + to_add.push_back(request.GetParsedArg().quote); + to_add.push_back(' '); + el_insertstr(m_editline, to_add.c_str()); + break; + } + case CompletionMode::RewriteLine: { + el_deletestr(m_editline, line_info->cursor - line_info->buffer); + el_insertstr(m_editline, completion.GetCompletion().c_str()); + break; + } + } return CC_REDISPLAY; } // If we get a longer match display that first. - const char *completion_str = completions.GetStringAtIndex(0); - if (completion_str != nullptr && *completion_str != '\0') { - el_insertstr(m_editline, completion_str); + std::string longest_prefix = completions.LongestCommonPrefix(); + if (!longest_prefix.empty()) + longest_prefix = + longest_prefix.substr(request.GetCursorArgumentPrefix().size()); + if (!longest_prefix.empty()) { + el_insertstr(m_editline, longest_prefix.c_str()); return CC_REDISPLAY; } - if (num_completions > 1) { - int num_elements = num_completions + 1; - fprintf(m_output_file, "\n" ANSI_CLEAR_BELOW "Available completions:"); - if (num_completions < page_size) { - PrintCompletion(m_output_file, 1, num_elements, completions, - descriptions); - fprintf(m_output_file, "\n"); - } else { - int cur_pos = 1; - char reply; - int got_char; - while (cur_pos < num_elements) { - int endpoint = cur_pos + page_size; - if (endpoint > num_elements) - endpoint = num_elements; - - PrintCompletion(m_output_file, cur_pos, endpoint, completions, - descriptions); - cur_pos = endpoint; - - if (cur_pos >= num_elements) { - fprintf(m_output_file, "\n"); - break; - } - - fprintf(m_output_file, "\nMore (Y/n/a): "); - reply = 'n'; - got_char = el_getc(m_editline, &reply); - if (got_char == -1 || reply == 'n') - break; - if (reply == 'a') - page_size = num_elements - cur_pos; - } - } - DisplayInput(); - MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor); - } + DisplayCompletions(m_editline, m_output_file, results); + + DisplayInput(); + MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor); return CC_REDISPLAY; } diff --git a/lldb/source/Interpreter/CommandAlias.cpp b/lldb/source/Interpreter/CommandAlias.cpp index 8c40574ee50..4281ba5338a 100644 --- a/lldb/source/Interpreter/CommandAlias.cpp +++ b/lldb/source/Interpreter/CommandAlias.cpp @@ -115,18 +115,16 @@ bool CommandAlias::WantsCompletion() { return false; } -int CommandAlias::HandleCompletion(CompletionRequest &request) { +void CommandAlias::HandleCompletion(CompletionRequest &request) { if (IsValid()) - return m_underlying_command_sp->HandleCompletion(request); - return -1; + m_underlying_command_sp->HandleCompletion(request); } -int CommandAlias::HandleArgumentCompletion( +void CommandAlias::HandleArgumentCompletion( CompletionRequest &request, OptionElementVector &opt_element_vector) { if (IsValid()) - return m_underlying_command_sp->HandleArgumentCompletion( - request, opt_element_vector); - return -1; + m_underlying_command_sp->HandleArgumentCompletion(request, + opt_element_vector); } Options *CommandAlias::GetOptions() { diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 86cca78241e..dc030d743b2 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -1754,19 +1754,17 @@ bool CommandInterpreter::HandleCommand(const char *command_line, return result.Succeeded(); } -int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { - int num_command_matches = 0; +void CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { bool look_for_subcommand = false; // For any of the command completions a unique match will be a complete word. - request.SetWordComplete(true); if (request.GetCursorIndex() == -1) { // We got nothing on the command line, so return the list of commands bool include_aliases = true; StringList new_matches, descriptions; - num_command_matches = GetCommandNamesMatchingPartialString( - "", include_aliases, new_matches, descriptions); + GetCommandNamesMatchingPartialString("", include_aliases, new_matches, + descriptions); request.AddCompletions(new_matches, descriptions); } else if (request.GetCursorIndex() == 0) { // The cursor is in the first argument, so just do a lookup in the @@ -1776,15 +1774,12 @@ int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0), &new_matches, &new_descriptions); - if (num_command_matches == 1 && cmd_obj && cmd_obj->IsMultiwordObject() && + if (new_matches.GetSize() && cmd_obj && cmd_obj->IsMultiwordObject() && new_matches.GetStringAtIndex(0) != nullptr && strcmp(request.GetParsedLine().GetArgumentAtIndex(0), new_matches.GetStringAtIndex(0)) == 0) { - if (request.GetParsedLine().GetArgumentCount() == 1) { - request.SetWordComplete(true); - } else { + if (request.GetParsedLine().GetArgumentCount() != 1) { look_for_subcommand = true; - num_command_matches = 0; new_matches.DeleteStringAtIndex(0); new_descriptions.DeleteStringAtIndex(0); request.GetParsedLine().AppendArgument(llvm::StringRef()); @@ -1793,7 +1788,6 @@ int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { } } request.AddCompletions(new_matches, new_descriptions); - num_command_matches = request.GetNumberOfMatches(); } if (request.GetCursorIndex() > 0 || look_for_subcommand) { @@ -1802,77 +1796,32 @@ int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { // matching initial command: CommandObject *command_object = GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0)); - if (command_object == nullptr) { - return 0; - } else { + if (command_object) { request.GetParsedLine().Shift(); request.SetCursorIndex(request.GetCursorIndex() - 1); - num_command_matches = command_object->HandleCompletion(request); + command_object->HandleCompletion(request); } } - - return num_command_matches; } -int CommandInterpreter::HandleCompletion(CompletionRequest &orig_request) { - // Start a new subrequest we can modify. - CompletionResult result; - CompletionRequest request(orig_request.GetRawLine(), - orig_request.GetRawCursorPos(), result); +void CommandInterpreter::HandleCompletion(CompletionRequest &request) { + // 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; - else if (first_arg[0] == CommandHistory::g_repeat_char) { - if (auto hist_str = m_command_history.FindString(first_arg)) { - matches.InsertStringAtIndex(0, *hist_str); - descriptions.InsertStringAtIndex(0, "Previous command history event"); - return -2; - } else - return 0; + llvm::StringRef first_arg = request.GetParsedLine().GetArgumentAtIndex(0); + + if (!first_arg.empty()) { + if (first_arg.front() == m_comment_char) + return; + if (first_arg.front() == CommandHistory::g_repeat_char) { + if (auto hist_str = m_command_history.FindString(first_arg)) + request.AddCompletion(*hist_str, "Previous command history event", + CompletionMode::RewriteLine); + return; } } - int num_command_matches = HandleCompletionMatches(request); - result.GetMatches(matches); - result.GetDescriptions(descriptions); - - if (num_command_matches <= 0) - return num_command_matches; - - if (request.GetParsedLine().GetArgumentCount() == 0) { - // If we got an empty string, insert nothing. - matches.InsertStringAtIndex(0, ""); - descriptions.InsertStringAtIndex(0, ""); - } else { - // Now figure out if there is a common substring, and if so put that in - // element 0, otherwise put an empty string in element 0. - std::string command_partial_str = request.GetCursorArgumentPrefix().str(); - - std::string common_prefix = matches.LongestCommonPrefix(); - const size_t partial_name_len = command_partial_str.size(); - common_prefix.erase(0, partial_name_len); - - // 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_command_matches == 1 && request.GetWordComplete()) { - char quote_char = request.GetParsedLine()[request.GetCursorIndex()].quote; - common_prefix = - Args::EscapeLLDBCommandArgument(common_prefix, quote_char); - if (quote_char != '\0') - common_prefix.push_back(quote_char); - common_prefix.push_back(' '); - } - matches.InsertStringAtIndex(0, common_prefix.c_str()); - descriptions.InsertStringAtIndex(0, ""); - } - // Add completion to original request. - orig_request.AddCompletions(matches, descriptions); - return num_command_matches; + HandleCompletionMatches(request); } CommandInterpreter::~CommandInterpreter() {} diff --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp index fe46b6faf84..eb38971e726 100644 --- a/lldb/source/Interpreter/CommandObject.cpp +++ b/lldb/source/Interpreter/CommandObject.cpp @@ -257,14 +257,14 @@ void CommandObject::Cleanup() { m_api_locker.unlock(); } -int CommandObject::HandleCompletion(CompletionRequest &request) { +void CommandObject::HandleCompletion(CompletionRequest &request) { // Default implementation of WantsCompletion() is !WantsRawCommandString(). // Subclasses who want raw command string but desire, for example, argument // completion should override WantsCompletion() to return true, instead. if (WantsRawCommandString() && !WantsCompletion()) { // FIXME: Abstract telling the completion to insert the completion // character. - return -1; + return; } else { // Can we do anything generic with the options? Options *cur_options = GetOptions(); @@ -278,11 +278,11 @@ int CommandObject::HandleCompletion(CompletionRequest &request) { bool handled_by_options = cur_options->HandleOptionCompletion( request, opt_element_vector, GetCommandInterpreter()); if (handled_by_options) - return request.GetNumberOfMatches(); + return; } // If we got here, the last word is not an option or an option argument. - return HandleArgumentCompletion(request, opt_element_vector); + HandleArgumentCompletion(request, opt_element_vector); } } diff --git a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp index 94e52a36fb3..693d18ce7bd 100644 --- a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp +++ b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp @@ -84,13 +84,9 @@ bool CommandObjectRegexCommand::AddRegexCommand(const char *re_cstr, return false; } -int CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) { +void CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) { if (m_completion_type_mask) { CommandCompletions::InvokeCommonCompletionCallbacks( GetCommandInterpreter(), m_completion_type_mask, request, nullptr); - return request.GetNumberOfMatches(); - } else { - request.SetWordComplete(false); } - return 0; } diff --git a/lldb/source/Interpreter/OptionValue.cpp b/lldb/source/Interpreter/OptionValue.cpp index 00c8642595b..bc929aa9dab 100644 --- a/lldb/source/Interpreter/OptionValue.cpp +++ b/lldb/source/Interpreter/OptionValue.cpp @@ -565,11 +565,8 @@ bool OptionValue::DumpQualifiedName(Stream &strm) const { return dumped_something; } -size_t OptionValue::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); - return request.GetNumberOfMatches(); -} +void OptionValue::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) {} Status OptionValue::SetValueFromString(llvm::StringRef value, VarSetOperationType op) { diff --git a/lldb/source/Interpreter/OptionValueArch.cpp b/lldb/source/Interpreter/OptionValueArch.cpp index 92dc45d092b..7271c1471f9 100644 --- a/lldb/source/Interpreter/OptionValueArch.cpp +++ b/lldb/source/Interpreter/OptionValueArch.cpp @@ -68,11 +68,9 @@ lldb::OptionValueSP OptionValueArch::DeepCopy() const { return OptionValueSP(new OptionValueArch(*this)); } -size_t OptionValueArch::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); +void OptionValueArch::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { CommandCompletions::InvokeCommonCompletionCallbacks( interpreter, CommandCompletions::eArchitectureCompletion, request, nullptr); - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Interpreter/OptionValueBoolean.cpp b/lldb/source/Interpreter/OptionValueBoolean.cpp index 8be8220fb30..bfc7760e207 100644 --- a/lldb/source/Interpreter/OptionValueBoolean.cpp +++ b/lldb/source/Interpreter/OptionValueBoolean.cpp @@ -71,9 +71,8 @@ lldb::OptionValueSP OptionValueBoolean::DeepCopy() const { return OptionValueSP(new OptionValueBoolean(*this)); } -size_t OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); +void OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { static const llvm::StringRef g_autocomplete_entries[] = { "true", "false", "on", "off", "yes", "no", "1", "0"}; @@ -87,5 +86,4 @@ size_t OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter, if (entry.startswith_lower(request.GetCursorArgumentPrefix())) request.AddCompletion(entry); } - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Interpreter/OptionValueEnumeration.cpp b/lldb/source/Interpreter/OptionValueEnumeration.cpp index 0b76bd0601a..3df0e27bab4 100644 --- a/lldb/source/Interpreter/OptionValueEnumeration.cpp +++ b/lldb/source/Interpreter/OptionValueEnumeration.cpp @@ -102,10 +102,8 @@ lldb::OptionValueSP OptionValueEnumeration::DeepCopy() const { return OptionValueSP(new OptionValueEnumeration(*this)); } -size_t OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); - +void OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { const uint32_t num_enumerators = m_enumerations.GetSize(); if (!request.GetCursorArgumentPrefix().empty()) { for (size_t i = 0; i < num_enumerators; ++i) { @@ -118,5 +116,4 @@ size_t OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter, for (size_t i = 0; i < num_enumerators; ++i) request.AddCompletion(m_enumerations.GetCStringAtIndex(i).GetStringRef()); } - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Interpreter/OptionValueFileSpec.cpp b/lldb/source/Interpreter/OptionValueFileSpec.cpp index 062d7ccdf2a..20d3d4e68e3 100644 --- a/lldb/source/Interpreter/OptionValueFileSpec.cpp +++ b/lldb/source/Interpreter/OptionValueFileSpec.cpp @@ -99,12 +99,10 @@ lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { return OptionValueSP(new OptionValueFileSpec(*this)); } -size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); +void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { CommandCompletions::InvokeCommonCompletionCallbacks( interpreter, m_completion_mask, request, nullptr); - return request.GetNumberOfMatches(); } const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { diff --git a/lldb/source/Interpreter/OptionValueFormatEntity.cpp b/lldb/source/Interpreter/OptionValueFormatEntity.cpp index 1bb8c9f6955..8dc52650a33 100644 --- a/lldb/source/Interpreter/OptionValueFormatEntity.cpp +++ b/lldb/source/Interpreter/OptionValueFormatEntity.cpp @@ -116,7 +116,7 @@ lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const { return OptionValueSP(new OptionValueFormatEntity(*this)); } -size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - return FormatEntity::AutoComplete(request); +void OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { + FormatEntity::AutoComplete(request); } diff --git a/lldb/source/Interpreter/OptionValueUUID.cpp b/lldb/source/Interpreter/OptionValueUUID.cpp index f39b66b77bb..d2226ff3743 100644 --- a/lldb/source/Interpreter/OptionValueUUID.cpp +++ b/lldb/source/Interpreter/OptionValueUUID.cpp @@ -62,9 +62,8 @@ lldb::OptionValueSP OptionValueUUID::DeepCopy() const { return OptionValueSP(new OptionValueUUID(*this)); } -size_t OptionValueUUID::AutoComplete(CommandInterpreter &interpreter, - CompletionRequest &request) { - request.SetWordComplete(false); +void OptionValueUUID::AutoComplete(CommandInterpreter &interpreter, + CompletionRequest &request) { ExecutionContext exe_ctx(interpreter.GetExecutionContext()); Target *target = exe_ctx.GetTargetPtr(); if (target) { @@ -87,5 +86,4 @@ size_t OptionValueUUID::AutoComplete(CommandInterpreter &interpreter, } } } - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index ba15c020f2d..536cbaae5f2 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -645,8 +645,6 @@ bool Options::VerifyPartialOptions(CommandReturnObject &result) { bool Options::HandleOptionCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector, CommandInterpreter &interpreter) { - request.SetWordComplete(true); - // For now we just scan the completions to see if the cursor position is in // an option or its argument. Otherwise we'll call HandleArgumentCompletion. // In the future we can use completion to validate options as well if we @@ -739,7 +737,6 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, if (opt_defs_index != -1) { HandleOptionArgumentCompletion(subrequest, opt_element_vector, i, interpreter); - request.SetWordComplete(subrequest.GetWordComplete()); return true; } else { // No completion callback means no completions... diff --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp index 72025fc0e3b..3ec97b1fce7 100644 --- a/lldb/source/Symbol/Variable.cpp +++ b/lldb/source/Symbol/Variable.cpp @@ -583,7 +583,6 @@ static void PrivateAutoComplete( case eTypeClassTypedef: case eTypeClassVector: { request.AddCompletion(prefix_path.str()); - request.SetWordComplete(true); } break; case eTypeClassClass: @@ -603,7 +602,6 @@ static void PrivateAutoComplete( request.AddCompletion((prefix_path + "->").str()); else { request.AddCompletion(prefix_path.str()); - request.SetWordComplete(true); } } break; } @@ -749,12 +747,10 @@ static void PrivateAutoComplete( } } -size_t Variable::AutoComplete(const ExecutionContext &exe_ctx, - CompletionRequest &request) { +void Variable::AutoComplete(const ExecutionContext &exe_ctx, + CompletionRequest &request) { CompilerType compiler_type; PrivateAutoComplete(exe_ctx.GetFramePtr(), request.GetCursorArgumentPrefix(), "", compiler_type, request); - - return request.GetNumberOfMatches(); } diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp index 18e70e7ca07..fc2d43925a3 100644 --- a/lldb/source/Utility/ArchSpec.cpp +++ b/lldb/source/Utility/ArchSpec.cpp @@ -243,7 +243,7 @@ void ArchSpec::ListSupportedArchNames(StringList &list) { list.AppendString(g_core_definitions[i].name); } -size_t ArchSpec::AutoComplete(CompletionRequest &request) { +void ArchSpec::AutoComplete(CompletionRequest &request) { if (!request.GetCursorArgumentPrefix().empty()) { for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) { if (NameMatches(g_core_definitions[i].name, NameMatch::StartsWith, @@ -255,7 +255,6 @@ size_t ArchSpec::AutoComplete(CompletionRequest &request) { ListSupportedArchNames(matches); request.AddCompletions(matches); } - return request.GetNumberOfMatches(); } #define CPU_ANY (UINT32_MAX) diff --git a/lldb/source/Utility/CompletionRequest.cpp b/lldb/source/Utility/CompletionRequest.cpp index 6a8ab160c41..7fd5260f7d5 100644 --- a/lldb/source/Utility/CompletionRequest.cpp +++ b/lldb/source/Utility/CompletionRequest.cpp @@ -64,13 +64,16 @@ std::string CompletionResult::Completion::GetUniqueKey() const { std::string result; result.append(std::to_string(m_completion.size())); result.append(m_completion); + result.append(std::to_string(static_cast<int>(m_mode))); + result.append(":"); result.append(m_descripton); return result; } void CompletionResult::AddResult(llvm::StringRef completion, - llvm::StringRef description) { - Completion r(completion, description); + llvm::StringRef description, + CompletionMode mode) { + Completion r(completion, description, mode); // Add the completion if we haven't seen the same value before. if (m_added_values.insert(r.GetUniqueKey()).second) @@ -80,11 +83,11 @@ void CompletionResult::AddResult(llvm::StringRef completion, void CompletionResult::GetMatches(StringList &matches) const { matches.Clear(); for (const Completion &completion : m_results) - matches.AppendString(completion.m_completion); + matches.AppendString(completion.GetCompletion()); } void CompletionResult::GetDescriptions(StringList &descriptions) const { descriptions.Clear(); for (const Completion &completion : m_results) - descriptions.AppendString(completion.m_descripton); + descriptions.AppendString(completion.GetDescription()); } |