Remove highlight_universal

This was an attempt to offer syntax highlighting for `read` when shell
highlighting is not enabled, but it hardly did anything.
This commit is contained in:
ridiculousfish 2020-08-03 13:41:41 -07:00
parent f94a6a74f0
commit 18c7c46657
3 changed files with 17 additions and 117 deletions

View File

@ -1321,95 +1321,3 @@ void highlight_shell_no_io(const wcstring &buff, std::vector<highlight_spec_t> &
color = highlighter.highlight();
}
/// Perform quote and parenthesis highlighting on the specified string.
static void highlight_universal_internal(const wcstring &buffstr,
std::vector<highlight_spec_t> &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<size_t> 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<size_t>(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<highlight_spec_t> &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);
}

View File

@ -90,18 +90,6 @@ void highlight_shell(const wcstring &buffstr, std::vector<highlight_spec_t> &col
void highlight_shell_no_io(const wcstring &buffstr, std::vector<highlight_spec_t> &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<highlight_spec_t> &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);

View File

@ -517,7 +517,7 @@ class reader_data_t : public std::enable_shared_from_this<reader_data_t> {
/// 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<highlight_result_t(void)> 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();