Sublime Forum

[SOLVED] Multiline syntax highlighting with .sublime-syntax?

#1

Greetings. I am trying to do syntax highlighting on a multi-line shell script.
But I find it different from what I just think.
Here’s my .sublime-syntax and a testcase (an ‘echo’ with some 'cd’s).

[code]%YAML1.2

See http://www.sublimetext.com/docs/3/syntax.html

file_extensions:

  • myTest
    scope: source.myTest
    contexts:
    main:
    • match: ‘echo’
      scope: support.function.builtin.shell
      push: echoMatched
    • match: ‘cd’
      scope: support.function.external.shell
      echoMatched:
    • match: ‘\$’
      scope: keyword
    • match: ‘(?<!\)$’
      pop: true[/code]echo cd \ cd cd

Here’s what I think.
At first, we meet an ‘echo’ which matches ‘echo’ in the ‘main’ context, so ‘echo’ is highlighted due to ‘support.function.builtin.shell’ and ‘echoMatched’ is now pushed to the top of the stack.
After that, we meet a ‘cd’. It’s not matched in the ‘echoMatched’ context, so it won’t be highlighted.
After that, we meet a backslash at the line ending which matches ‘\$’ in the ‘echoMatched’ context so it would be highlighted due to ‘keyword.’
After that, we go to the second line and meet the 2nd ‘cd’ but we still in the ‘echoMatched’ context, it won’t be highlighted.
After that, we meet the end of the 2nd line which matches ‘(?<!\)$’, so ‘echoMatched’ is popped and ‘main’ is on the top of the stack.
After that, we go to the third line and meet the 3rd ‘cd’ which matches ‘cd’ in the ‘main’ context so it would be highlighted.

As a result, it should look like this.

But actually I got this.

What do I misunderstand?
How to make it in the right way?

0 Likes

#2

Change \$ by \$\n

0 Likes

#3

Thanks for your reply but it seems not working. (dev build 3094)

0 Likes

#4

Do the same on the pop one ((?<!\)$\n).
You don’t even need the look behind here in fact (just $\n is enough)

0 Likes

#5

[quote=“Clams”]Do the same on the pop one ((?<!\)$\n).
You don’t even need the look behind here in fact (just $\n is enough)[/quote]

Thanks! This works like a charm.
But why the original syntax definition won’t work correctly?
The ‘\n’ triggers ‘$’ matching again?

0 Likes

#6

Yes and in fact i think you could even remove the $, and it would work the same

0 Likes

#7

Confirmed. Thanks again :smiley:

0 Likes