Skip to content

Commit

Permalink
[lldb] Add a setting to change the autosuggestion ANSI escape codes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JDevlieghere committed Mar 7, 2022
1 parent 5917219 commit 080635e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
4 changes: 4 additions & 0 deletions lldb/include/lldb/Core/Debugger.h
Expand Up @@ -331,6 +331,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

bool GetUseAutosuggestion() const;

llvm::StringRef GetAutosuggestionAnsiPrefix() const;

llvm::StringRef GetAutosuggestionAnsiSuffix() const;

bool GetUseSourceCache() const;

bool SetUseSourceCache(bool use_source_cache);
Expand Down
12 changes: 11 additions & 1 deletion lldb/include/lldb/Host/Editline.h
Expand Up @@ -210,6 +210,14 @@ class Editline {
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);

Expand Down Expand Up @@ -388,9 +396,11 @@ class Editline {
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;
};
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Core/CoreProperties.td
Expand Up @@ -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.">;
}
10 changes: 10 additions & 0 deletions lldb/source/Core/Debugger.cpp
Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Core/IOHandler.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Host/common/Editline.cpp
Expand Up @@ -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();
Expand Down
35 changes: 25 additions & 10 deletions lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py
Expand Up @@ -16,22 +16,24 @@ 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
@skipIfEditlineSupportMissing
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()

Expand All @@ -40,10 +42,8 @@ def test_autosuggestion_add_spaces(self):
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>"
Expand All @@ -52,7 +52,7 @@ def test_autosuggestion(self):

# 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.
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_autosuggestion(self):
# 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.
Expand All @@ -90,7 +90,7 @@ def test_autosuggestion(self):

# 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")
Expand All @@ -102,4 +102,19 @@ def test_autosuggestion(self):
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()

0 comments on commit 080635e

Please sign in to comment.