Sublime Forum

Add hightlighting rule for ~ character

#21

Hi @kingkeith,

yup, I have uploaded my file here:

http://pastebin.com/2Tmuwb8V

And I try to match the long comment lines “%--------------…” in front of the \sections, \chapters and so on such that I can easily see where a new section or chapter starts when I quickly scroll through the document.

(As a plus, later I would like to match the “language=matlab” on line 251, and then switch to the matlab syntax such that I have proper highlighting for matlab code.)

0 Likes

#22

Hi @nesbit,

Here is a working syntax that will mark the comment lines in a different color for you EDIT: and highlight matlab code:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
scope: text.tex.latex.nesbit
name: LaTeX Improved
contexts:
  main:
    - match: '^%-+$'
      scope: source.cs storage.type.source.cs
    - match: '(?:\s*)((\\)begin)(\{)(lstlisting)(\})(?:(\[).*language=matlab*(\]))?(\s*%.*\n?)?'
      captures:
        1: support.function.be.latex
        2: punctuation.definition.function.latex
        3: punctuation.definition.arguments.begin.latex
        4: variable.parameter.function.latex
        5: punctuation.definition.arguments.end.latex
        6: punctuation.definition.arguments.optional.begin.latex
        7: punctuation.definition.arguments.optional.end.latex
        8: comment.line.percentage.latex
      push:
        - meta_scope: meta.function.embedded.generic.latex
        - meta_content_scope: source.generic.embedded
        - match: '((\\)end)(\{)(lstlisting)(\})'
          captures:
            1: support.function.be.latex
            2: punctuation.definition.function.latex
            3: punctuation.definition.arguments.begin.latex
            4: variable.parameter.function.latex
            5: punctuation.definition.arguments.end.latex
            6: punctuation.definition.arguments.optional.begin.latex
            7: punctuation.definition.arguments.optional.end.latex
            8: comment.line.percentage.latex
          pop: true
        - include: scope:source.matlab
    - include: scope:text.tex.latex
0 Likes

#23

Hi @kingkeith,

Thanks man! Your file works just great! However, I don’t exactly get what are the captures needed for. I tried to expand your file a bit and ended up with this one:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html

scope: text.tex.latex.nesbit

contexts:
  main:
    - match: '^%-+$'
      scope: invalid.illegal.string.quoted.single.latex

    - match: '(?:\s*)((\\)begin)(\{)(lstlisting)(\})'
      scope: support.function.be.latex
      # scope: invalid.illegal.string.quoted.single.latex
      push:
        - meta_scope: meta.function.embedded.generic.latex
        - meta_content_scope: source.generic.embedded

        - match: '.*\[.*'
          # scope: invalid.illegal.string.quoted.single.latex

        - match: 'language=matlab'
          # scope: invalid.illegal.string.quoted.single.latex

        - match: '.*\]'
          # scope: invalid.illegal.string.quoted.single.latex
          push:
            - match: '((\\)end)'
              scope: support.function.be.latex
              # scope: invalid.illegal.string.quoted.single.latex
              pop: true

            - include: scope:source.matlab

        - match: '(\{)(lstlisting)(\})'
          scope: support.function.be.latex

          # scope: invalid.illegal.string.quoted.single.latex
          pop: true




    - match: '(?:\s*)((\\)begin)(\{)(gnuplot)(\})'
      scope: support.function.be.latex
      # scope: invalid.illegal.string.quoted.single.latex
      push:
        - meta_scope: meta.function.embedded.generic.latex
        - meta_content_scope: source.generic.embedded

        - match: '.*\[.*'
          # scope: invalid.illegal.string.quoted.single.latex

        - match: '.*\]'
          # scope: invalid.illegal.string.quoted.single.latex
          push:
            - match: '((\\)end)'
              scope: support.function.be.latex
              # scope: invalid.illegal.string.quoted.single.latex
              pop: true

            - include: scope:source.gnuplot

        - match: '(\{)(gnuplot)(\})'
          scope: support.function.be.latex

          # scope: invalid.illegal.string.quoted.single.latex
          pop: true

It does not only know matlab, but also gnuplot, which comes in handy in line 288 :wink:

0 Likes

#24

—[quote=“nesbit, post:23, topic:18660”]
However, I don’t exactly get what are the captures needed for.
[/quote]

captures are to set different scopes to the different capture groups of the regular expression that matched.
A capture group in a regular expression is defined by parenthesis without a ? after the opening parenthesis, and are numbered sequentially, starting from 1 (capture group 0 always refers to the entire match).
for example: - match: '(?:\s*)((\\)begin)(\{)(gnuplot)(\})'
here we have a group 1 for \\, a group 2 for \\begin, a group 3 for \{, a group 4 for gnuplot and a group 5 for \}. Why is this useful? both for semantics, in terms of any snippets, auto-completions or plugins, but also so that the \begin can be highlighted in one color, the { and } characters in other and the gnuplot in yet another different color.

We can see that the original LaTeX syntax definition uses these captures on \begin{figure}, to highlight \begin differently to figure, but where you have removed these captures for gnuplot and lstlisting, they are all one color. I hope that clarifies their use :slightly_smiling:

0 Likes