diff options
author | Med Ismail Bennani <ismail@bennani.ma> | 2023-06-06 10:24:48 -0700 |
---|---|---|
committer | Med Ismail Bennani <ismail@bennani.ma> | 2023-06-06 10:58:34 -0700 |
commit | 6a9c3e611505b7637b46fbaacaf50362c97a263d (patch) | |
tree | ad6cd9ec81eb504e18480d49fd1b616815eaa874 /lldb/source/Commands/CommandObjectCommands.cpp | |
parent | 1e82b20118e31bd6c3844a84e03f701997a9b7ed (diff) | |
download | llvm-6a9c3e611505b7637b46fbaacaf50362c97a263d.zip llvm-6a9c3e611505b7637b46fbaacaf50362c97a263d.tar.gz llvm-6a9c3e611505b7637b46fbaacaf50362c97a263d.tar.bz2 |
[lldb/Commands] Add support to auto-completion for user commands
This patch should allow the user to set specific auto-completion type
for their custom commands.
To do so, we had to hoist the `CompletionType` enum so the user can
access it and add a new completion type flag to the CommandScriptAdd
Command Object.
So now, the user can specify which completion type will be used with
their custom command, when they register it.
This also makes the `crashlog` custom commands use disk-file completion
type, to browse through the user file system and load the report.
Differential Revision: https://reviews.llvm.org/D152011
Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
Diffstat (limited to 'lldb/source/Commands/CommandObjectCommands.cpp')
-rw-r--r-- | lldb/source/Commands/CommandObjectCommands.cpp | 75 |
1 files changed, 56 insertions, 19 deletions
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 254e226..656ace2 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -66,9 +66,8 @@ public: void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override { - CommandCompletions::InvokeCommonCompletionCallbacks( - GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, - request, nullptr); + lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr); } Options *GetOptions() override { return &m_options; } @@ -1080,9 +1079,10 @@ class CommandObjectPythonFunction : public CommandObjectRaw { public: CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name, std::string funct, std::string help, - ScriptedCommandSynchronicity synch) + ScriptedCommandSynchronicity synch, + CompletionType completion_type) : CommandObjectRaw(interpreter, name), m_function_name(funct), - m_synchro(synch) { + m_synchro(synch), m_completion_type(completion_type) { if (!help.empty()) SetHelp(help); else { @@ -1116,6 +1116,15 @@ public: return CommandObjectRaw::GetHelpLong(); } + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), m_completion_type, request, nullptr); + } + + bool WantsCompletion() override { return true; } + protected: bool DoExecute(llvm::StringRef raw_command_line, CommandReturnObject &result) override { @@ -1146,6 +1155,7 @@ private: std::string m_function_name; ScriptedCommandSynchronicity m_synchro; bool m_fetched_help_long = false; + CompletionType m_completion_type = eNoCompletion; }; class CommandObjectScriptingObject : public CommandObjectRaw { @@ -1153,10 +1163,11 @@ public: CommandObjectScriptingObject(CommandInterpreter &interpreter, std::string name, StructuredData::GenericSP cmd_obj_sp, - ScriptedCommandSynchronicity synch) + ScriptedCommandSynchronicity synch, + CompletionType completion_type) : CommandObjectRaw(interpreter, name), m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false), - m_fetched_help_long(false) { + m_fetched_help_long(false), m_completion_type(completion_type) { StreamString stream; stream.Printf("For more information run 'help %s'", name.c_str()); SetHelp(stream.GetString()); @@ -1166,6 +1177,15 @@ public: ~CommandObjectScriptingObject() override = default; + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), m_completion_type, request, nullptr); + } + + bool WantsCompletion() override { return true; } + bool IsRemovable() const override { return true; } ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } @@ -1232,6 +1252,7 @@ private: ScriptedCommandSynchronicity m_synchro; bool m_fetched_help_short : 1; bool m_fetched_help_long : 1; + CompletionType m_completion_type = eNoCompletion; }; // CommandObjectCommandsScriptImport @@ -1263,9 +1284,8 @@ public: void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override { - CommandCompletions::InvokeCommonCompletionCallbacks( - GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, - request, nullptr); + lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr); } Options *GetOptions() override { return &m_options; } @@ -1439,6 +1459,18 @@ protected: "unrecognized value for synchronicity '%s'", option_arg.str().c_str()); break; + case 'C': { + Status error; + OptionDefinition definition = GetDefinitions()[option_idx]; + lldb::CompletionType completion_type = + static_cast<lldb::CompletionType>(OptionArgParser::ToOptionEnum( + option_arg, definition.enum_values, eNoCompletion, error)); + if (!error.Success()) + error.SetErrorStringWithFormat( + "unrecognized value for command completion type '%s'", + option_arg.str().c_str()); + m_completion_type = completion_type; + } break; default: llvm_unreachable("Unimplemented option"); } @@ -1450,6 +1482,7 @@ protected: m_class_name.clear(); m_funct_name.clear(); m_short_help.clear(); + m_completion_type = eNoCompletion; m_overwrite_lazy = eLazyBoolCalculate; m_synchronicity = eScriptedCommandSynchronicitySynchronous; } @@ -1466,6 +1499,7 @@ protected: LazyBool m_overwrite_lazy = eLazyBoolCalculate; ScriptedCommandSynchronicity m_synchronicity = eScriptedCommandSynchronicitySynchronous; + CompletionType m_completion_type = eNoCompletion; }; void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { @@ -1496,7 +1530,7 @@ protected: CommandObjectSP command_obj_sp(new CommandObjectPythonFunction( m_interpreter, m_cmd_name, funct_name_str, m_short_help, - m_synchronicity)); + m_synchronicity, m_completion_type)); if (!m_container) { Status error = m_interpreter.AddUserCommand( m_cmd_name, command_obj_sp, m_overwrite); @@ -1577,6 +1611,7 @@ protected: m_short_help.assign(m_options.m_short_help); m_synchronicity = m_options.m_synchronicity; + m_completion_type = m_options.m_completion_type; // Handle the case where we prompt for the script code first: if (m_options.m_class_name.empty() && m_options.m_funct_name.empty()) { @@ -1589,7 +1624,7 @@ protected: if (m_options.m_class_name.empty()) { new_cmd_sp.reset(new CommandObjectPythonFunction( m_interpreter, m_cmd_name, m_options.m_funct_name, - m_options.m_short_help, m_synchronicity)); + m_options.m_short_help, m_synchronicity, m_completion_type)); } else { ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter(); if (!interpreter) { @@ -1606,7 +1641,8 @@ protected: } new_cmd_sp.reset(new CommandObjectScriptingObject( - m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity)); + m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity, + m_completion_type)); } // Assume we're going to succeed... @@ -1634,6 +1670,7 @@ protected: bool m_overwrite = false; ScriptedCommandSynchronicity m_synchronicity = eScriptedCommandSynchronicitySynchronous; + CompletionType m_completion_type = eNoCompletion; }; // CommandObjectCommandsScriptList @@ -1706,8 +1743,8 @@ public: void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override { - CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request, - opt_element_vector); + lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs( + m_interpreter, request, opt_element_vector); } protected: @@ -1857,8 +1894,8 @@ public: void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override { - CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request, - opt_element_vector); + lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs( + m_interpreter, request, opt_element_vector); } protected: @@ -1997,8 +2034,8 @@ public: void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override { - CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request, - opt_element_vector); + lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs( + m_interpreter, request, opt_element_vector); } protected: |