Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash syntax highlighting broken with $ sign inside double quotes #180

Closed
ifschulz opened this issue Jul 11, 2023 · 6 comments
Closed

bash syntax highlighting broken with $ sign inside double quotes #180

ifschulz opened this issue Jul 11, 2023 · 6 comments
Labels
bash Caused by the bash lexer committed Issue fixed in repository but not in release

Comments

@ifschulz
Copy link

There seems to be an issue with bash syntax highlighting when using a ‘$’ as the last character of a double quoted string. Up to notepad++-v8.5.3 everything was fine, but with v8.5.4 it breaks the syntax highlighting to the end of the document:

v8.5.3:

2023-07-10 23_11_59-_new 1 - Notepad++

v8.5.4:

2023-07-10 23_25_04-_new 4 - Notepad++

This can of course be fixed with escaping the ‘$’ sign:

2023-07-10 23_25_42-_new 4 - Notepad++

Even though using an unescaped ‘$’ sign inside double quotes (when not referencing a variable) is not best practice, it should still not break the syntax highlighting.

@nyamatongwe nyamatongwe added the bash Caused by the bash lexer label Jul 11, 2023
@zufuliu
Copy link
Contributor

zufuliu commented Jul 11, 2023

It's due following code, which should be disabled for String, LString, and HereDoc:

lexilla/lexers/LexBash.cxx

Lines 331 to 336 in f157108

} else if (sc.ch == '\'') {
style = QuoteStyle::CString;
sc.ChangeState(SCE_SH_STRING);
} else if (sc.ch == '"') {
style = QuoteStyle::LString;
sc.ChangeState(SCE_SH_STRING);

it can be fixed by passing a parameter to Expand() or added a method to QuoteStackCls, not sure what would be a good name.

@zufuliu
Copy link
Contributor

zufuliu commented Jul 11, 2023

A simple fix:

diff --git a/lexers/LexBash.cxx b/lexers/LexBash.cxx
index 4945ef70..bc7cff71 100644
--- a/lexers/LexBash.cxx
+++ b/lexers/LexBash.cxx
@@ -864,7 +864,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 							if (stylingInside) {
 								sc.SetState(SCE_SH_BACKTICKS | insideCommand);
 							}
-						} else if (sc.ch == '$') {
+						} else if (sc.ch == '$' && !AnyOf(sc.chNext, '\"', '\'')) {
 							QuoteStack.Expand(sc, cmdState, stylingInside);
 							continue;
 						}

zufuliu added a commit to zufuliu/notepad4 that referenced this issue Jul 11, 2023
@nyamatongwe
Copy link
Member

Example as text:

egrep '^[A-Z]$'
test=foobar

egrep "^[A-Z]$"
test=foobar

egrep "^[A-Z]\$"
test=foobar

Check in Linux for:

echo '$'
echo "$"
echo "$"
echo "$"x""

Result:

>bash 180example.sh
$
$
$
$x
>Exit code: 0

@zufuliu
Copy link
Contributor

zufuliu commented Jul 11, 2023

C-String test case:

echo x$'\t'y
echo "x$'\t'y"
echo "x\ty"

zufuliu added a commit to zufuliu/lexilla that referenced this issue Jul 13, 2023
@zufuliu
Copy link
Contributor

zufuliu commented Jul 13, 2023

It seems to fully fix this we need a list of all special variables (the && !AnyOf(sc.chNext, '\"', '\'') part is still needed for string/heredoc block), current code teat all punctuation and white spaces as single character special variable. there is another bug reported at zufuliu/notepad4#684:
image

Bash special variables are list at https://www.gnu.org/software/bash/manual/bash.html#Variable-Index

@nyamatongwe nyamatongwe added the committed Issue fixed in repository but not in release label Jul 14, 2023
@zufuliu
Copy link
Contributor

zufuliu commented Jul 14, 2023

It seems to fully fix this we need a list of all special variables (the && !AnyOf(sc.chNext, '\"', '\'') part is still needed for string/heredoc block), current code teat all punctuation and white spaces as single character special variable. there is another bug reported at zufuliu/notepad2#684: image

Created #184 as separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bash Caused by the bash lexer committed Issue fixed in repository but not in release
Projects
None yet
Development

No branches or pull requests

3 participants