https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/155705
>From f56a1a836e2fbe865d8e143d021389c99ec341e6 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere <[email protected]> Date: Wed, 27 Aug 2025 14:25:29 -0700 Subject: [PATCH] [lldb] Add more command option mnemonics Add a bunch of mnemonics to the command options now that they're highlighted in the help output. This uncovered two issues: - We had an instance where we weren't applying the ANSI formatting. - We had a place where we were now incorrectly computing the column width. Both are fixed by this PR. --- lldb/include/lldb/Utility/AnsiTerminal.h | 5 ++++ lldb/source/Commands/Options.td | 22 +++++++++++------- lldb/source/Host/common/Editline.cpp | 15 +++++------- lldb/source/Interpreter/Options.cpp | 23 +++++++++++++------ .../completion/TestCompletion.py | 12 +++++----- 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/lldb/include/lldb/Utility/AnsiTerminal.h b/lldb/include/lldb/Utility/AnsiTerminal.h index 5c99341ad888a..7db184ad67225 100644 --- a/lldb/include/lldb/Utility/AnsiTerminal.h +++ b/lldb/include/lldb/Utility/AnsiTerminal.h @@ -260,6 +260,11 @@ inline std::string TrimAndPad(llvm::StringRef str, size_t visible_length, return result; } +inline size_t ColumnWidth(llvm::StringRef str) { + std::string stripped = ansi::StripAnsiTerminalCodes(str); + return llvm::sys::locale::columnWidth(stripped); +} + } // namespace ansi } // namespace lldb_private diff --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td index 4a70e55d6ad89..123745cbf7f50 100644 --- a/lldb/source/Commands/Options.td +++ b/lldb/source/Commands/Options.td @@ -253,9 +253,9 @@ let Command = "breakpoint delete" in { def breakpoint_delete_disabled : Option<"disabled", "d">, Group<1>, - Desc<"Delete all breakpoints which are currently disabled. When using " - "the disabled option any breakpoints listed on the command line " - "are EXCLUDED from deletion.">; + Desc<"${D}elete all breakpoints which are currently disabled. When " + "using the disabled option any breakpoints listed on the command " + "line are EXCLUDED from deletion.">; } let Command = "breakpoint name" in { @@ -349,11 +349,17 @@ let Command = "disassemble" in { : Option<"arch", "A">, Arg<"Architecture">, Desc<"Specify the architecture to use for cross disassembly.">; - def disassemble_options_start_address : Option<"start-address", "s">, - Groups<[1,2]>, Arg<"AddressOrExpression">, Required, - Desc<"Address at which to start disassembling.">; - def disassemble_options_end_address : Option<"end-address", "e">, Group<1>, - Arg<"AddressOrExpression">, Desc<"Address at which to end disassembling.">; + def disassemble_options_start_address + : Option<"start-address", "s">, + Groups<[1, 2]>, + Arg<"AddressOrExpression">, + Required, + Desc<"Address at which to ${s}tart disassembling.">; + def disassemble_options_end_address + : Option<"end-address", "e">, + Group<1>, + Arg<"AddressOrExpression">, + Desc<"Address at which to ${e}nd disassembling.">; def disassemble_options_count : Option<"count", "c">, Groups<[2,3,4,5,7]>, Arg<"NumLines">, Desc<"Number of instructions to display.">; def disassemble_options_name : Option<"name", "n">, Group<3>, diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index 1fc86c8a3e1be..1b1922e710764 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -98,11 +98,6 @@ bool IsOnlySpaces(const EditLineStringType &content) { return true; } -static size_t ColumnWidth(llvm::StringRef str) { - std::string stripped = ansi::StripAnsiTerminalCodes(str); - return llvm::sys::locale::columnWidth(stripped); -} - static int GetOperation(HistoryOperation op) { // The naming used by editline for the history operations is counter // intuitive to how it's used in LLDB's editline implementation. @@ -329,8 +324,8 @@ std::string Editline::PromptForIndex(int line_index) { if (m_set_continuation_prompt.length() > 0) { continuation_prompt = m_set_continuation_prompt; // Ensure that both prompts are the same length through space padding - const size_t prompt_width = ColumnWidth(prompt); - const size_t cont_prompt_width = ColumnWidth(continuation_prompt); + const size_t prompt_width = ansi::ColumnWidth(prompt); + const size_t cont_prompt_width = ansi::ColumnWidth(continuation_prompt); const size_t padded_prompt_width = std::max(prompt_width, cont_prompt_width); if (prompt_width < padded_prompt_width) @@ -355,7 +350,9 @@ void Editline::SetCurrentLine(int line_index) { m_current_prompt = PromptForIndex(line_index); } -size_t Editline::GetPromptWidth() { return ColumnWidth(PromptForIndex(0)); } +size_t Editline::GetPromptWidth() { + return ansi::ColumnWidth(PromptForIndex(0)); +} bool Editline::IsEmacs() { const char *editor; @@ -445,7 +442,7 @@ void Editline::DisplayInput(int firstIndex) { int Editline::CountRowsForLine(const EditLineStringType &content) { std::string prompt = PromptForIndex(0); // Prompt width is constant during an edit session - int line_length = (int)(content.length() + ColumnWidth(prompt)); + int line_length = (int)(content.length() + ansi::ColumnWidth(prompt)); return (line_length / m_terminal_width) + 1; } diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index ec725428483ff..a51fa2ee20bc7 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -273,11 +273,13 @@ void Options::OutputFormattedUsageText(Stream &strm, actual_text.append("] "); } } - actual_text.append(option_def.usage_text); + actual_text.append( + ansi::FormatAnsiTerminalCodes(option_def.usage_text, use_color)); + const size_t visible_length = ansi::ColumnWidth(actual_text); // Will it all fit on one line? - if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < + if (static_cast<uint32_t>(visible_length + strm.GetIndentLevel()) < output_max_columns) { // Output it as a single line. strm.Indent(ansi::FormatAnsiTerminalCodes(actual_text, use_color)); @@ -288,7 +290,7 @@ void Options::OutputFormattedUsageText(Stream &strm, int text_width = output_max_columns - strm.GetIndentLevel() - 1; int start = 0; int end = start; - int final_end = actual_text.length(); + int final_end = visible_length; int sub_len; while (end < final_end) { @@ -631,6 +633,7 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, auto opt_defs = GetDefinitions(); llvm::StringRef cur_opt_str = request.GetCursorArgumentPrefix(); + const bool use_color = interpreter.GetDebugger().GetUseColor(); for (size_t i = 0; i < opt_element_vector.size(); i++) { size_t opt_pos = static_cast<size_t>(opt_element_vector[i].opt_pos); @@ -650,7 +653,8 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, if (!def.short_option) continue; opt_str[1] = def.short_option; - request.AddCompletion(opt_str, def.usage_text); + request.AddCompletion(opt_str, ansi::FormatAnsiTerminalCodes( + def.usage_text, use_color)); } return true; @@ -662,7 +666,8 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, full_name.erase(full_name.begin() + 2, full_name.end()); full_name.append(def.long_option); - request.AddCompletion(full_name, def.usage_text); + request.AddCompletion(full_name, ansi::FormatAnsiTerminalCodes( + def.usage_text, use_color)); } return true; } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) { @@ -673,7 +678,9 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, const OptionDefinition &opt = opt_defs[opt_defs_index]; llvm::StringRef long_option = opt.long_option; if (cur_opt_str.starts_with("--") && cur_opt_str != long_option) { - request.AddCompletion("--" + long_option.str(), opt.usage_text); + request.AddCompletion( + "--" + long_option.str(), + ansi::FormatAnsiTerminalCodes(opt.usage_text, use_color)); return true; } else request.AddCompletion(request.GetCursorArgumentPrefix()); @@ -689,7 +696,9 @@ bool Options::HandleOptionCompletion(CompletionRequest &request, for (auto &def : opt_defs) { llvm::StringRef long_option(def.long_option); if (long_option.starts_with(cur_opt_str)) - request.AddCompletion("--" + long_option.str(), def.usage_text); + request.AddCompletion( + "--" + long_option.str(), + ansi::FormatAnsiTerminalCodes(def.usage_text, use_color)); } } return true; diff --git a/lldb/test/API/functionalities/completion/TestCompletion.py b/lldb/test/API/functionalities/completion/TestCompletion.py index e7c53729f2090..45750c7ac0817 100644 --- a/lldb/test/API/functionalities/completion/TestCompletion.py +++ b/lldb/test/API/functionalities/completion/TestCompletion.py @@ -676,8 +676,8 @@ def test_completion_description_command_options(self): self.check_completion_with_desc( "breakpoint set -", [ - ["-h", "Set the breakpoint on exception catcH."], - ["-w", "Set the breakpoint on exception throW."], + ["-h", "Set the breakpoint on exception catch."], + ["-w", "Set the breakpoint on exception throw."], ], ) @@ -685,8 +685,8 @@ def test_completion_description_command_options(self): self.check_completion_with_desc( "breakpoint set --", [ - ["--on-catch", "Set the breakpoint on exception catcH."], - ["--on-throw", "Set the breakpoint on exception throW."], + ["--on-catch", "Set the breakpoint on exception catch."], + ["--on-throw", "Set the breakpoint on exception throw."], ], ) @@ -694,8 +694,8 @@ def test_completion_description_command_options(self): self.check_completion_with_desc( "breakpoint set --on-", [ - ["--on-catch", "Set the breakpoint on exception catcH."], - ["--on-throw", "Set the breakpoint on exception throW."], + ["--on-catch", "Set the breakpoint on exception catch."], + ["--on-throw", "Set the breakpoint on exception throw."], ], ) _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
