diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -331,6 +331,10 @@ bool GetUseAutosuggestion() const; + llvm::StringRef GetAutosuggestionAnsiPrefix() const; + + llvm::StringRef GetAutosuggestionAnsiSuffix() const; + bool GetUseSourceCache() const; bool SetUseSourceCache(bool use_source_cache); diff --git a/lldb/include/lldb/Host/Editline.h b/lldb/include/lldb/Host/Editline.h --- a/lldb/include/lldb/Host/Editline.h +++ b/lldb/include/lldb/Host/Editline.h @@ -210,6 +210,14 @@ m_fix_indentation_callback_chars = indent_chars; } + void SetSuggestionAnsiPrefix(std::string prefix) { + m_suggestion_ansi_prefix = std::move(prefix); + } + + void SetSuggestionAnsiSuffix(std::string suffix) { + m_suggestion_ansi_suffix = std::move(suffix); + } + /// Prompts for and reads a single line of user input. bool GetLine(std::string &line, bool &interrupted); @@ -388,9 +396,11 @@ const char *m_fix_indentation_callback_chars = nullptr; CompleteCallbackType m_completion_callback; - SuggestionCallbackType m_suggestion_callback; + std::string m_suggestion_ansi_prefix; + std::string m_suggestion_ansi_suffix; + std::size_t m_previous_autosuggestion_size = 0; std::mutex m_output_mutex; }; diff --git a/lldb/source/Core/CoreProperties.td b/lldb/source/Core/CoreProperties.td --- a/lldb/source/Core/CoreProperties.td +++ b/lldb/source/Core/CoreProperties.td @@ -163,4 +163,12 @@ Global, DefaultFalse, Desc<"If true, LLDB will show suggestions to complete the command the user typed. Suggestions may be accepted using Ctrl-F.">; + def ShowAutosuggestionAnsiPrefix: Property<"show-autosuggestion-ansi-prefix", "String">, + Global, + DefaultStringValue<"${ansi.faint}">, + Desc<"When displaying suggestion in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately before the suggestion.">; + def ShowAutosuggestionAnsiSuffix: Property<"show-autosuggestion-ansi-suffix", "String">, + Global, + DefaultStringValue<"${ansi.normal}">, + Desc<"When displaying suggestion in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately after the suggestion.">; } diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -384,6 +384,16 @@ nullptr, idx, g_debugger_properties[idx].default_uint_value != 0); } +llvm::StringRef Debugger::GetAutosuggestionAnsiPrefix() const { + const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix; + return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, ""); +} + +llvm::StringRef Debugger::GetAutosuggestionAnsiSuffix() const { + const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix; + return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, ""); +} + bool Debugger::GetUseSourceCache() const { const uint32_t idx = ePropertyUseSourceCache; return m_collection_sp->GetPropertyAtIndexAsBoolean( diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp --- a/lldb/source/Core/IOHandler.cpp +++ b/lldb/source/Core/IOHandler.cpp @@ -17,6 +17,7 @@ #include "lldb/Core/StreamFile.h" #include "lldb/Host/Config.h" #include "lldb/Host/File.h" +#include "lldb/Utility/AnsiTerminal.h" #include "lldb/Utility/Predicate.h" #include "lldb/Utility/ReproducerProvider.h" #include "lldb/Utility/Status.h" @@ -273,10 +274,14 @@ this->AutoCompleteCallback(request); }); - if (debugger.GetUseAutosuggestion() && debugger.GetUseColor()) { + if (debugger.GetUseAutosuggestion()) { m_editline_up->SetSuggestionCallback([this](llvm::StringRef line) { return this->SuggestionCallback(line); }); + m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes( + debugger.GetAutosuggestionAnsiPrefix())); + m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes( + debugger.GetAutosuggestionAnsiSuffix())); } // See if the delegate supports fixing indentation const char *indent_chars = delegate.IOHandlerGetFixIndentationCharacters(); diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -1080,8 +1080,13 @@ llvm::StringRef line(line_info->buffer, line_info->lastchar - line_info->buffer); + const char *ansi_prefix = + m_color_prompts ? m_suggestion_ansi_prefix.c_str() : ""; + const char *ansi_suffix = + m_color_prompts ? m_suggestion_ansi_suffix.c_str() : ""; + if (llvm::Optional to_add = m_suggestion_callback(line)) { - std::string to_add_color = ANSI_FAINT + to_add.getValue() + ANSI_UNFAINT; + std::string to_add_color = ansi_prefix + to_add.getValue() + ansi_suffix; fputs(typed.c_str(), m_output_file); fputs(to_add_color.c_str(), m_output_file); size_t new_autosuggestion_size = line.size() + to_add->length(); diff --git a/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py b/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py --- a/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py +++ b/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py @@ -16,6 +16,11 @@ mydir = TestBase.compute_mydir(__file__) + ANSI_FAINT = "\x1b[2m" + ANSI_RESET = "\x1b[0m" + ANSI_RED = "\x1b[31m" + ANSI_CYAN = "\x1b[36m" + # PExpect uses many timeouts internally and doesn't play well # under ASAN on a loaded machine.. @skipIfAsan @@ -23,15 +28,12 @@ def test_autosuggestion_add_spaces(self): self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"]) - # Common input codes and escape sequences. - faint_color = "\x1b[2m" - reset = "\x1b[0m" # Check if spaces are added to hide the previous gray characters. self.expect("help frame var") self.expect("help frame info") self.child.send("help frame v") - self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + faint_color + "ar" + reset + " ") + self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + self.ANSI_FAINT + "ar" + self.ANSI_RESET + " ") self.quit() @@ -40,10 +42,8 @@ def test_autosuggestion(self): self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"]) - # Common input codes and escape sequences. + # Common input codes. ctrl_f = "\x06" - faint_color = "\x1b[2m" - reset = "\x1b[0m" delete = chr(127) frame_output_needle = "Syntax: frame " @@ -52,7 +52,7 @@ # Check that LLDB shows the autosuggestion in gray behind the text. self.child.send("hel") - self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p frame" + reset) + self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + self.ANSI_FAINT + "p frame" + self.ANSI_RESET) # Apply the autosuggestion and press enter. This should print the # 'help frame' output if everything went correctly. @@ -80,7 +80,7 @@ # Check that autosuggestion complete to the most recent one. self.child.send("help frame variable\n") self.child.send("help fr") - self.child.expect_exact(faint_color + "ame variable" + reset) + self.child.expect_exact(self.ANSI_FAINT + "ame variable" + self.ANSI_RESET) self.child.send("\n") # Try another command. @@ -90,7 +90,7 @@ # Check that 'hel' should have an autosuggestion for 'help apropos' now. self.child.send("hel") - self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p apropos" + reset) + self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + self.ANSI_FAINT + "p apropos" + self.ANSI_RESET) # Run the command and expect the 'help apropos' output. self.child.send(ctrl_f + "\n") @@ -102,4 +102,19 @@ self.child.expect_exact(breakpoint_output_needle) + self.quit() + + @skipIfAsan + @skipIfEditlineSupportMissing + def test_autosuggestion_custom_ansi_prefix_suffix(self): + self.launch(extra_args=["-o", "settings set show-autosuggestion true", + "-o", "settings set use-color true", + "-o", "settings set show-autosuggestion-ansi-prefix ${ansi.fg.red}", + "-o", "setting set show-autosuggestion-ansi-suffix ${ansi.fg.cyan}"]) + + self.child.send("help frame variable\n") + self.child.send("help fr") + self.child.expect_exact(self.ANSI_RED + "ame variable" + self.ANSI_CYAN) + self.child.send("\n") + self.quit()