[lldb] Add a setting to change the autosuggestion ANSI escape codes

I'm a big fan of the autosuggestion feature but my terminal/color scheme
doesn't display faint any differently than regular lldb output, which
makes the feature a little confusing. This patch add a setting to change
the autosuggestion ANSI escape codes.

For example, to display the autosuggestion in italic, you can add this
to your ~/.lldbinit

  settings set show-autosuggestion-ansi-prefix ${ansi.italic}
  setting set show-autosuggestion-ansi-suffix ${ansi.normal}

Differential revision: https://reviews.llvm.org/D121064
This commit is contained in:
Jonas Devlieghere 2022-03-07 08:37:42 -08:00
parent 5917219438
commit 080635ef27
No known key found for this signature in database
GPG Key ID: 49CC0BD90FDEED4D
7 changed files with 70 additions and 13 deletions

View File

@ -331,6 +331,10 @@ public:
bool GetUseAutosuggestion() const;
llvm::StringRef GetAutosuggestionAnsiPrefix() const;
llvm::StringRef GetAutosuggestionAnsiSuffix() const;
bool GetUseSourceCache() const;
bool SetUseSourceCache(bool use_source_cache);

View File

@ -210,6 +210,14 @@ public:
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 @@ private:
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;
};

View File

@ -163,4 +163,12 @@ let Definition = "debugger" in {
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.">;
}

View File

@ -384,6 +384,16 @@ bool Debugger::GetUseAutosuggestion() const {
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(

View File

@ -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 @@ IOHandlerEditline::IOHandlerEditline(
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();

View File

@ -1080,8 +1080,13 @@ unsigned char Editline::TypedCharacter(int ch) {
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<std::string> 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();

View File

@ -16,6 +16,11 @@ class TestCase(PExpectTest):
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 @@ class TestCase(PExpectTest):
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 @@ class TestCase(PExpectTest):
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 <subcommand>"
@ -52,7 +52,7 @@ class TestCase(PExpectTest):
# 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 @@ class TestCase(PExpectTest):
# 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 @@ class TestCase(PExpectTest):
# 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 @@ class TestCase(PExpectTest):
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()