From 18c7c4665748fd29b0fc5f5c00cf8768bd1a8f4d Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 3 Aug 2020 13:41:41 -0700 Subject: [PATCH] Remove highlight_universal This was an attempt to offer syntax highlighting for `read` when shell highlighting is not enabled, but it hardly did anything. --- src/highlight.cpp | 92 ----------------------------------------------- src/highlight.h | 12 ------- src/reader.cpp | 30 +++++++++------- 3 files changed, 17 insertions(+), 117 deletions(-) diff --git a/src/highlight.cpp b/src/highlight.cpp index 658b64b4b..01b95e0f1 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -1321,95 +1321,3 @@ void highlight_shell_no_io(const wcstring &buff, std::vector & color = highlighter.highlight(); } -/// Perform quote and parenthesis highlighting on the specified string. -static void highlight_universal_internal(const wcstring &buffstr, - std::vector &color, size_t pos) { - assert(buffstr.size() == color.size()); - if (pos < buffstr.size()) { - // Highlight matching quotes. - if ((buffstr.at(pos) == L'\'') || (buffstr.at(pos) == L'\"')) { - std::vector lst; - int level = 0; - wchar_t prev_q = 0; - const wchar_t *const buff = buffstr.c_str(); - const wchar_t *str = buff; - bool match_found = false; - - while (*str) { - switch (*str) { - case L'\\': { - str++; - break; - } - case L'\"': - case L'\'': { - if (level == 0) { - level++; - lst.push_back(str - buff); - prev_q = *str; - } else { - if (prev_q == *str) { - size_t pos1, pos2; - - level--; - pos1 = lst.back(); - pos2 = str - buff; - if (pos1 == pos || pos2 == pos) { - color.at(pos1).background = highlight_role_t::normal; - color.at(pos2).background = highlight_role_t::normal; - match_found = true; - } - prev_q = *str == L'\"' ? L'\'' : L'\"'; - } else { - level++; - lst.push_back(str - buff); - prev_q = *str; - } - } - break; - } - default: { - break; // we ignore all other characters - } - } - if ((*str == L'\0')) break; - str++; - } - - if (!match_found) color.at(pos).background = highlight_role_t::error; - } - - // Highlight matching parenthesis. - const wchar_t c = buffstr.at(pos); - if (std::wcschr(L"()[]{}", c)) { - int step = std::wcschr(L"({[", c) ? 1 : -1; - wchar_t dec_char = *(std::wcschr(L"()[]{}", c) + step); - wchar_t inc_char = c; - int level = 0; - bool match_found = false; - for (long i = pos; i >= 0 && static_cast(i) < buffstr.size(); i += step) { - const wchar_t test_char = buffstr.at(i); - if (test_char == inc_char) level++; - if (test_char == dec_char) level--; - if (level == 0) { - long pos2 = i; - color.at(pos).background = highlight_role_t::normal; - color.at(pos2).background = highlight_role_t::normal; - match_found = true; - break; - } - } - - if (!match_found) - color.at(pos) = highlight_spec_t::make_background(highlight_role_t::error); - } - } -} - -void highlight_universal(const wcstring &buff, std::vector &color, size_t pos, - const operation_context_t &ctx) { - UNUSED(ctx); - assert(buff.size() == color.size()); - std::fill(color.begin(), color.end(), highlight_spec_t{}); - highlight_universal_internal(buff, color, pos); -} diff --git a/src/highlight.h b/src/highlight.h index 7a20d3a88..42e829fc6 100644 --- a/src/highlight.h +++ b/src/highlight.h @@ -90,18 +90,6 @@ void highlight_shell(const wcstring &buffstr, std::vector &col void highlight_shell_no_io(const wcstring &buffstr, std::vector &color, size_t pos, const operation_context_t &ctx); -/// Perform syntax highlighting for the text in buff. Matching quotes and parenthesis are -/// highlighted. The result is stored in the color array as a color_code from the HIGHLIGHT_ enum -/// for each character in buff. -/// -/// \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 cancellation and other environment for this operation. This is unused. -void highlight_universal(const wcstring &buffstr, std::vector &color, size_t pos, - const operation_context_t &ctx); - /// \return an RGB color for a given highlight spec. rgb_color_t highlight_get_color(const highlight_spec_t &highlight, bool is_background); diff --git a/src/reader.cpp b/src/reader.cpp index b8d7e8720..222176023 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -517,7 +517,7 @@ class reader_data_t : public std::enable_shared_from_this { /// Whether tab completion is allowed. bool complete_ok{false}; /// Function for syntax highlighting. - highlight_function_t highlight_func{highlight_universal}; + highlight_function_t highlight_func{nullptr}; /// Function for testing if the string can be returned. test_function_t test_func{default_test}; /// If this is true, exit reader even if there are running jobs. This happens if we press e.g. @@ -2280,6 +2280,8 @@ void reader_data_t::highlight_complete(highlight_result_t result) { static std::function get_highlight_performer( parser_t &parser, const wcstring &text, long match_highlight_pos, highlight_function_t highlight_func) { + if (!highlight_func) return {}; + auto vars = parser.vars().snapshot(); unsigned generation_count = read_generation_count(); return [=]() -> highlight_result_t { @@ -2307,18 +2309,20 @@ void reader_data_t::super_highlight_me_plenty(int match_highlight_pos_adjust, bo sanity_check(); - auto highlight_performer = get_highlight_performer( - parser(), el->text(), match_highlight_pos, no_io ? highlight_shell_no_io : highlight_func); - if (no_io) { - // Highlighting without IO, we just do it. - highlight_complete(highlight_performer()); - } else { - // Highlighting including I/O proceeds in the background. - auto shared_this = this->shared_from_this(); - debounce_highlighting().perform(highlight_performer, - [shared_this](highlight_result_t result) { - shared_this->highlight_complete(std::move(result)); - }); + if (auto highlight_performer = + get_highlight_performer(parser(), el->text(), match_highlight_pos, + no_io ? highlight_shell_no_io : highlight_func)) { + if (no_io) { + // Highlighting without IO, we just do it. + highlight_complete(highlight_performer()); + } else { + // Highlighting including I/O proceeds in the background. + auto shared_this = this->shared_from_this(); + debounce_highlighting().perform(highlight_performer, + [shared_this](highlight_result_t result) { + shared_this->highlight_complete(std::move(result)); + }); + } } highlight_search();