ScintillaOrg / lexilla

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

Missing shebang support in JavaScript #253

Closed ghost closed 4 months ago

ghost commented 5 months ago

I don't have much to say, another user previously reported this in detail in the Notepad++ directory, which uses Lexilla: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/14706

References:

  1. Official specification used by JavaScript, ECMA-262. (begin of page 11 of the PDF) https://ecma-international.org/wp-content/uploads/ECMA-262_14th_edition_june_2023.pdf
  2. W3Schools. (scroll down the article) https://www.w3schools.com/js/js_2023.asp
zufuliu commented 5 months ago

A simple change is following (#! has no valid meaning in all C/C++ like languages):

diff --git a/lexers/LexCPP.cxx b/lexers/LexCPP.cxx
index f5ab166c..3c1bc74a 100644
--- a/lexers/LexCPP.cxx
+++ b/lexers/LexCPP.cxx
@@ -889,6 +889,11 @@ void SCI_METHOD LexerCPP::Lex(Sci_PositionU startPos, Sci_Position length, int i
    const WordClassifier &classifierDocKeyWords = subStyles.Classifier(SCE_C_COMMENTDOCKEYWORD);

    Sci_PositionU lineEndNext = styler.LineEnd(lineCurrent);
+   if (sc.currentPos == 0 && sc.Match('#', '!')) {
+       // Shell Shebang at beginning of file
+       sc.SetState(SCE_C_COMMENTLINE);
+       sc.Forward();
+   }

    for (; sc.More();) {