Remove the cursor position from highlighting

This used to be used to determine which token contained the cursor, so
as to highlight potential paths. But now we highlight all potential paths,
so we can remove the field.
This commit is contained in:
ridiculousfish 2020-08-11 15:42:22 -07:00
parent 84b9cc5c01
commit 57102caba6
7 changed files with 21 additions and 45 deletions

View File

@ -389,8 +389,7 @@ maybe_t<int> builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t *
// colorize if interactive
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
std::vector<highlight_spec_t> colors;
size_t len = repr.size();
highlight_shell(repr, colors, len, parser.context());
highlight_shell(repr, colors, parser.context());
streams.out.append(str2wcstring(colorize(repr, colors, parser.vars())));
} else {
streams.out.append(repr);

View File

@ -262,7 +262,7 @@ static int report_function_metadata(const wchar_t *funcname, bool verbose, io_st
append_format(comment, L"# Defined in %ls @ line %d\n", path, line_number);
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
std::vector<highlight_spec_t> colors;
highlight_shell(comment, colors, comment.size(), parser.context());
highlight_shell(comment, colors, parser.context());
streams.out.append(str2wcstring(colorize(comment, colors, parser.vars())));
} else {
streams.out.append(comment);
@ -440,7 +440,7 @@ maybe_t<int> builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
std::vector<highlight_spec_t> colors;
highlight_shell(def, colors, def.size(), parser.context());
highlight_shell(def, colors, parser.context());
streams.out.append(str2wcstring(colorize(def, colors, parser.vars())));
} else {
streams.out.append(def);

View File

@ -685,7 +685,7 @@ static const char *highlight_role_to_string(highlight_role_t role) {
static std::string make_pygments_csv(const wcstring &src) {
const size_t len = src.size();
std::vector<highlight_spec_t> colors;
highlight_shell(src, colors, src.size(), operation_context_t::globals());
highlight_shell(src, colors, operation_context_t::globals());
assert(colors.size() == len && "Colors and src should have same size");
struct token_range_t {
@ -995,8 +995,7 @@ int main(int argc, char *argv[]) {
// Maybe colorize.
std::vector<highlight_spec_t> colors;
if (output_type != output_type_plain_text) {
highlight_shell(output_wtext, colors, output_wtext.size(),
operation_context_t::globals());
highlight_shell(output_wtext, colors, operation_context_t::globals());
}
std::string colored_output;

View File

@ -4999,7 +4999,7 @@ static void test_highlighting() {
do_test(expected_colors.size() == text.size());
std::vector<highlight_spec_t> colors(text.size());
highlight_shell(text, colors, 20, operation_context_t{vars}, true /* io_ok */);
highlight_shell(text, colors, operation_context_t{vars}, true /* io_ok */);
if (expected_colors.size() != colors.size()) {
err(L"Color vector has wrong size! Expected %lu, actual %lu", expected_colors.size(),

View File

@ -772,8 +772,6 @@ class highlighter_t {
// The string we're highlighting. Note this is a reference memmber variable (to avoid copying)!
// We must not outlive this!
const wcstring &buff;
// Cursor position.
const size_t cursor_pos;
// The operation context. Again, a reference member variable!
const operation_context_t &ctx;
// Whether it's OK to do I/O.
@ -831,10 +829,8 @@ class highlighter_t {
void visit(const ast::node_t &node) { visit_children(node); }
// Constructor
highlighter_t(const wcstring &str, size_t pos, const operation_context_t &ctx, wcstring wd,
bool can_do_io)
highlighter_t(const wcstring &str, const operation_context_t &ctx, wcstring wd, bool can_do_io)
: buff(str),
cursor_pos(pos),
ctx(ctx),
io_ok(can_do_io),
working_directory(std::move(wd)),
@ -904,18 +900,9 @@ void highlighter_t::color_as_argument(const ast::node_t &node) {
if (arg_subcmd_end < this->buff.size())
this->color_array.at(arg_subcmd_end) = highlight_role_t::operat;
// Compute the cursor's position within the cmdsub. We must be past the open paren (hence >)
// but can be at the end of the string or closed paren (hence <=).
size_t cursor_subpos = CURSOR_POSITION_INVALID;
if (cursor_pos != CURSOR_POSITION_INVALID && cursor_pos > arg_subcmd_start &&
cursor_pos <= arg_subcmd_end) {
// The -1 because the cmdsub_contents does not include the open paren.
cursor_subpos = cursor_pos - arg_subcmd_start - 1;
}
// Highlight it recursively.
highlighter_t cmdsub_highlighter(cmdsub_contents, cursor_subpos, this->ctx,
this->working_directory, this->io_ok);
highlighter_t cmdsub_highlighter(cmdsub_contents, this->ctx, this->working_directory,
this->io_ok);
const color_array_t &subcolors = cmdsub_highlighter.highlight();
// Copy out the subcolors back into our array.
@ -1321,9 +1308,9 @@ std::string colorize(const wcstring &text, const std::vector<highlight_spec_t> &
return outp.contents();
}
void highlight_shell(const wcstring &buff, std::vector<highlight_spec_t> &color, size_t pos,
void highlight_shell(const wcstring &buff, std::vector<highlight_spec_t> &color,
const operation_context_t &ctx, bool io_ok) {
const wcstring working_directory = ctx.vars.get_pwd_slash();
highlighter_t highlighter(buff, pos, ctx, working_directory, io_ok);
highlighter_t highlighter(buff, ctx, working_directory, io_ok);
color = highlighter.highlight();
}

View File

@ -95,11 +95,10 @@ std::string colorize(const wcstring &text, const std::vector<highlight_spec_t> &
/// \param buffstr The buffer on which to perform syntax highlighting
/// \param color The array in which to store the color codes. The first 8 bits are used for fg
/// color, the next 8 bits for bg color.
/// \param pos the cursor position. Used for quote matching, etc.
/// \param ctx The variables and cancellation check for this operation.
/// \param io_ok If set, allow IO which may block. This means that e.g. invalid commands may be
/// detected.
void highlight_shell(const wcstring &buffstr, std::vector<highlight_spec_t> &color, size_t pos,
void highlight_shell(const wcstring &buffstr, std::vector<highlight_spec_t> &color,
const operation_context_t &ctx, bool io_ok = false);
/// highlight_color_resolver_t resolves highlight specs (like "a command") to actual RGB colors.

View File

@ -586,7 +586,7 @@ class reader_data_t : public std::enable_shared_from_this<reader_data_t> {
void update_autosuggestion();
void accept_autosuggestion(bool full, bool single = false,
move_word_style_t style = move_word_style_punctuation);
void super_highlight_me_plenty(int highlight_pos_adjust = 0, bool no_io = false);
void super_highlight_me_plenty(bool no_io = false);
void highlight_search();
void highlight_complete(highlight_result_t result);
@ -1307,7 +1307,7 @@ bool reader_data_t::insert_string(editable_line_t *el, const wcstring &str) {
// Syntax highlight. Note we must have that buff_pos > 0 because we just added something
// nonzero to its length.
assert(el->position() > 0);
super_highlight_me_plenty(-1);
super_highlight_me_plenty();
}
repaint();
@ -2261,11 +2261,10 @@ void reader_data_t::highlight_complete(highlight_result_t result) {
}
}
// Given text, bracket matching position, and whether IO is allowed,
// return a function that performs highlighting. The function may be invoked on a background thread.
// Given text and whether IO is allowed, return a function that performs highlighting. The function
// may be invoked on a background thread.
static std::function<highlight_result_t(void)> get_highlight_performer(parser_t &parser,
const wcstring &text,
long match_highlight_pos,
bool io_ok) {
auto vars = parser.vars().snapshot();
unsigned generation_count = read_generation_count();
@ -2273,8 +2272,8 @@ static std::function<highlight_result_t(void)> get_highlight_performer(parser_t
if (text.empty()) return {};
operation_context_t ctx = get_bg_context(vars, generation_count);
std::vector<highlight_spec_t> colors(text.size(), highlight_spec_t{});
highlight_shell(text, colors, match_highlight_pos, ctx, io_ok);
return {std::move(colors), text};
highlight_shell(text, colors, ctx, io_ok);
return highlight_result_t{std::move(colors), text};
};
}
@ -2282,22 +2281,15 @@ static std::function<highlight_result_t(void)> get_highlight_performer(parser_t
/// background color under the cursor to avoid repaint issues on terminals where e.g. syntax
/// highlighting maykes characters under the sursor unreadable.
///
/// \param match_highlight_pos_adjust the adjustment to the position to use for bracket matching.
/// This is added to the current cursor position and may be negative.
/// \param no_io if true, do a highlight that does not perform I/O, synchronously. If false, perform
/// an asynchronous highlight in the background, which may perform disk I/O.
void reader_data_t::super_highlight_me_plenty(int match_highlight_pos_adjust, bool no_io) {
void reader_data_t::super_highlight_me_plenty(bool no_io) {
if (!conf.highlight_ok) return;
const editable_line_t *el = &command_line;
assert(el != nullptr);
long match_highlight_pos = static_cast<long>(el->position()) + match_highlight_pos_adjust;
assert(match_highlight_pos >= 0);
sanity_check();
auto highlight_performer =
get_highlight_performer(parser(), el->text(), match_highlight_pos, !no_io);
auto highlight_performer = get_highlight_performer(parser(), el->text(), !no_io);
if (no_io) {
// Highlighting without IO, we just do it.
highlight_complete(highlight_performer());
@ -3002,7 +2994,7 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat
// syntax highlighting, but a synchronous variant that performs no I/O, so
// as not to block the user.
bool skip_io = (command_test_result == 0);
super_highlight_me_plenty(0, skip_io);
super_highlight_me_plenty(skip_io);
}
}