From c181de1d7fa46821c7a89633e3ab3639d89fd2a3 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 30 Sep 2014 11:14:57 -0700 Subject: [PATCH] Teach the highlighter about multiple adjacent square bracket expansions Fixes #1627 --- fish_tests.cpp | 13 ++++++++++++- highlight.cpp | 48 +++++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/fish_tests.cpp b/fish_tests.cpp index 0ec8bf7bb..c46dcf7a7 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -3620,8 +3620,19 @@ static void test_highlighting(void) {NULL, -1} }; + const highlight_component_t components13[] = + { + {L"echo", highlight_spec_command}, + {L"$$foo[", highlight_spec_operator}, + {L"1", highlight_spec_param}, + {L"][", highlight_spec_operator}, + {L"2", highlight_spec_param}, + {L"]", highlight_spec_operator}, + {L"[3]", highlight_spec_param}, // two dollar signs, so last one is not an expansion + {NULL, -1} + }; - const highlight_component_t *tests[] = {components1, components2, components3, components4, components5, components6, components7, components8, components9, components10, components11, components12}; + const highlight_component_t *tests[] = {components1, components2, components3, components4, components5, components6, components7, components8, components9, components10, components11, components12, components13}; for (size_t which = 0; which < sizeof tests / sizeof *tests; which++) { const highlight_component_t *components = tests[which]; diff --git a/highlight.cpp b/highlight.cpp index af2392c43..ef57519e4 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -591,6 +591,7 @@ static size_t color_variable(const wchar_t *in, size_t in_len, std::vector slice_begin_idx); - colors[slice_begin_idx] = highlight_spec_operator; - colors[slice_end_idx] = highlight_spec_operator; - break; - } - case -1: - { - // syntax error - // Normally the entire token is colored red for us, but inside a double-quoted string - // that doesn't happen. As such, color the variable + the slice start red. Coloring any - // more than that looks bad, unless we're willing to try and detect where the double-quoted - // string ends, and I'd rather not do that. - std::fill(colors, colors + idx + 1, (highlight_spec_t)highlight_spec_error); - break; - } + size_t slice_begin_idx = slice_begin - in, slice_end_idx = slice_end - in; + assert(slice_end_idx > slice_begin_idx); + colors[slice_begin_idx] = highlight_spec_operator; + colors[slice_end_idx] = highlight_spec_operator; + idx = slice_end_idx + 1; + } + else if (located == 0) + { + // not a slice + break; + } + else + { + assert(located < 0); + // syntax error + // Normally the entire token is colored red for us, but inside a double-quoted string + // that doesn't happen. As such, color the variable + the slice start red. Coloring any + // more than that looks bad, unless we're willing to try and detect where the double-quoted + // string ends, and I'd rather not do that. + std::fill(colors, colors + idx + 1, (highlight_spec_t)highlight_spec_error); + break; } } return idx;