Menu

#982 GitHub style Bash backticks

Committed
closed
nobody
5
2023-06-01
2013-03-31
No

GitHub uses a different style for Bash backticks, example
http://github.com/git/git/blob/master/git-pull.sh

Instead of coloring the entire section they only color the
opening "$(" and closing ")"

This is desirable with Scintilla because keywords within backticks could be highlighted instead of the whole section being one color. I am not asking that this be implemented per se, but if someone could help me along with the code. I was able to do this

--- a/scintilla/lexers/LexBash.cxx
+++ b/scintilla/lexers/LexBash.cxx
@@ -628,7 +628,7 @@ static void ColouriseBashDoc(unsigned int startPos, int length, i
                                sc.SetState(SCE_SH_BACKTICKS);
                                QuoteStack.Start(sc.ch, BASH_DELIM_BACKTICK);
                        } else if (sc.ch == '$') {
-                               if (sc.Match("$((")) {
+                               if (sc.Match("$((") || sc.Match("$(")) {^M
                                        sc.SetState(SCE_SH_OPERATOR);   // handle '((
                                        continue;
                                }

but this doesnt give style to the "$(" and ")", only allow keyword styles within the
backticks.

Discussion

  • Kein-Hong Man

    Kein-Hong Man - 2013-04-01

    Here is a patch that has been casually tested.

    --- a/scintilla/lexers/LexBash.zzz
    +++ b/scintilla/lexers/LexBash.cxx
    @@ -644,8 +644,8 @@
                        sc.ChangeState(SCE_SH_STRING);
                        QuoteStack.Start(sc.ch, BASH_DELIM_LSTRING);
                    } else if (sc.ch == '(') {
    -                   sc.ChangeState(SCE_SH_BACKTICKS);
    -                   QuoteStack.Start(sc.ch, BASH_DELIM_COMMAND);
    +                   sc.ChangeState(SCE_SH_OPERATOR);
    +                   cmdState = BASH_CMD_DELIM;
                    } else if (sc.ch == '`') {  // $` seen in a configure script, valid?
                        sc.ChangeState(SCE_SH_BACKTICKS);
                        QuoteStack.Start(sc.ch, BASH_DELIM_BACKTICK);
    

    The $( and ) are styled as operators, anything else would be non-trivial. The cmdState line tells the lexer the next word may be a keyword.

     
  • Steven Penny

    Steven Penny - 2013-04-01

    Kein-Hong, thanks for you reply. However after looking again it appears my original patch already set $( and ) as operators. This is not desired because I would like to style the backticks independent of operators.

     
  • Kein-Hong Man

    Kein-Hong Man - 2013-04-01

    Assuming you don't want $() to clash with ``, you'll need a new style. Add the style entry in the appropriate places, then duplicate the relevant code running operators for the new $() style.

    BUT... the flaw or problem in the above solution is this: how does the closing parentheses know it needs to use the $( style? Matched $( and ) in its own style cannot be trivially implemented, since our lexer code is lower-level but faster compared to regexp or high-level matchers. So, styling it as operators is the quickest solution, else much more time will have to be invested.

     
  • Zufu Liu

    Zufu Liu - 2022-04-09
    • labels: --> lexilla, bash
     
  • Zufu Liu

    Zufu Liu - 2023-04-24

    Related to [feature-requests:#1033], highlight $( and ) as operator would be the simplest change.

     

    Related

    Feature Requests: #1033

  • Neil Hodgson

    Neil Hodgson - 2023-06-01
    • status: open --> closed
    • Group: Completed --> Committed
     
  • Neil Hodgson

    Neil Hodgson - 2023-06-01

    The new lexer option lexer.bash.command.substitution with value 1 or 2 may be used to display states inside the command substitution.

    https://github.com/ScintillaOrg/lexilla/issues/153

     

Log in to post a comment.