ScintillaOrg / lexilla

A library of language lexers for use with Scintilla
https://www.scintilla.org/Lexilla.html
Other
187 stars 67 forks source link

[Bash] Correct line continuation handling #195

Closed zufuliu closed 1 year ago

zufuliu commented 1 year ago

following code should check whether previous line ends with odd number of backslashes (where last one colored as SCE_SH_OPERATOR): https://github.com/ScintillaOrg/lexilla/blob/9b45aaf0c4938b2a44fd22a256cdea574a1628cb/lexers/LexBash.cxx#L603-L610

test case:

#backslash1\
echo 1
#backslash2\\
echo 2

if [ 1 ]; then
    backslash1=A\
fi
    backslash2=B\\
fi

echo $backslash1, $backslash2

C shell equivalent for above if block:

if (1) then
    set backslash1=A\
endif
    set backslash2=B\\
endif

bash-line-continuation.patch

The patch can be committed before #194 to avoid overlapped changes. also it doesn't change comment line handing (https://sourceforge.net/p/scintilla/bugs/2226/).

nyamatongwe commented 1 year ago

This patch changes several portions of code and some of the change seems unrelated and result in different interpretations of input. The hunk for case SCE_SH_IDENTIFIER changes the styling for this expression involving zsh arithmetic extensions,

(( #var+10 ))
{7}(({0} {8}#var{7}+{3}10{0} {7})){0}

Changes to

{7}(({0} {8}#var+10{0} {7})){0}
zufuliu commented 1 year ago

bash-line-continuation-0813.patch

Reverted removing of (cmdState == CmdState::Arithmetic && !setWordStart.Contains(sc.ch)), that cause the failure.

zufuliu commented 1 year ago

Fixed zsh print $(( ##\M-\C-x+0 )) regression. bash-line-continuation-0816.patch

nyamatongwe commented 1 year ago

The case of $(( ##\M-\C-x+0 )) wasn't really a regression as it fails both before and after. Key definitions seem rare enough to not be worth implementing.

zufuliu commented 1 year ago

bash-comment-line-continuation.patch Patch to remove line continuation from comment line:

diff --git a/lexers/LexBash.cxx b/lexers/LexBash.cxx
index d9506690..a183cb8a 100644
--- a/lexers/LexBash.cxx
+++ b/lexers/LexBash.cxx
@@ -173,7 +173,7 @@ bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
 }

 constexpr bool StyleForceBacktrack(int state) noexcept {
-   return AnyOf(state, SCE_SH_CHARACTER, SCE_SH_STRING, SCE_SH_BACKTICKS, SCE_SH_HERE_Q, SCE_SH_PARAM, SCE_SH_COMMENTLINE);
+   return AnyOf(state, SCE_SH_CHARACTER, SCE_SH_STRING, SCE_SH_BACKTICKS, SCE_SH_HERE_Q, SCE_SH_PARAM);
 }

 struct OptionsBash {
@@ -770,7 +770,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
                sc.SetState(SCE_SH_DEFAULT | insideCommand);
                break;
            case SCE_SH_COMMENTLINE:
-               if (sc.MatchLineEnd() && sc.chPrev != '\\') {
+               if (sc.MatchLineEnd()) {
                    sc.SetState(SCE_SH_DEFAULT | insideCommand);
                }
                break;