2
0
Fork 0
mirror of https://github.com/fish-shell/fish-shell synced 2025-02-15 05:28:41 +00:00

Correct syntax highlighting for variables spanning multiple lines

A variable may be broken across multiple lines with a backslash, for
example:

     > echo $FISH_\
       VERSION

Teach syntax highlighting about this line breaking. Fixes 
This commit is contained in:
ridiculousfish 2021-11-15 22:52:12 -08:00
parent 3773baf1f3
commit a47f498516
2 changed files with 17 additions and 2 deletions

View file

@ -5525,6 +5525,13 @@ static void test_highlighting() {
{L"=", highlight_role_t::operat, ns},
});
// Highlighting works across escaped line breaks (#8444).
highlight_tests.push_back({
{L"echo", highlight_role_t::command},
{L"$FISH_\\\n", highlight_role_t::operat},
{L"VERSION", highlight_role_t::operat, ns},
});
auto &vars = parser_t::principal_parser().vars();
// Verify variables and wildcards in commands using /bin/cat.
vars.set(L"VARIABLE_IN_COMMAND", ENV_LOCAL, {L"a"});

View file

@ -489,8 +489,16 @@ static size_t color_variable(const wchar_t *in, size_t in_len,
}
// Handle a sequence of variable characters.
while (valid_var_name_char(in[idx])) {
colors[idx++] = highlight_role_t::operat;
// It may contain an escaped newline - see #8444.
for (;;) {
if (valid_var_name_char(in[idx])) {
colors[idx++] = highlight_role_t::operat;
} else if (in[idx] == L'\\' && in[idx + 1] == L'\n') {
colors[idx++] = highlight_role_t::operat;
colors[idx++] = highlight_role_t::operat;
} else {
break;
}
}
// Handle a slice, up to dollar_count of them. Note that we currently don't do any validation of