Sublime Forum

Add hightlighting rule for ~ character

#4

File extension does not need to be quoted
And for the scope it is related to what your word is representing (a variable, a keyword, a function, a class type, …) and incidentally the color you want for this word. You can have a look at the end of https://manual.macromates.com/en/language_grammars describing the naming convention (more or less respected) of each scope.
You can also display the scope of other existing keyword in your document using ctrl+alt+shift+p (or in the menu tools->developer->Show scope) to know the scope associated to a word and assigned it to your word.

2 Likes

#5

Still no luck, this is what I have:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
  - txt
scope: source.cs storage.type.source.cs
contexts:
  main:
    - match: ""
      push: "Packages/C#/C#.sublime-syntax"
      with_prototype:
        - match: ~.+?~
          scope: source.cs storage.type.source.cs

do I need to do anything to get it to load it? I’ve been saving and reopening sublime?

0 Likes

#6

In the status bar at the bottom right of the window you can select the syntax. From there you can say Open all files with current extention with (I guess the txt extension is already affected to another syntax)

Also you don’t need storage.type.source.cs in the first scope

1 Like

#7

 
Does this mean that sublime-syntax files are additive?   Or is it only possible to have one syntax enabled at a time?

0 Likes

#8

Yes you could theoretically add more than one sublime-syntax (the push syntax can accept multiple context/syntax file) but you would very likely have conflict between them since they were not written to work at the same time.

1 Like

#9

one way of visualizing it is:

  • the view has a sublime-syntax (or tmLanguage) file assigned to it
  • inside the sublime-syntax file, there can be many contexts
  • a context might define the rules for matching attributes inside a html tag for example, which could also state that it needs to be followed by a different context such as an end of tag character
  • contexts are held on a stack, such that only the context at the top of the stack is active at once (although any meta_scopes from the other contexts are applied)
  • by default, the main context is the only context on the stack
  • it is possible to directly set or push a specific context, even from a different definition file
  • the stack can have new contexts pushed onto or off it at any time, or the top-most context can be replaced with/set to another
  • a prototype is a special context that will always take precedence over the top context on the stack (unless specified not to for the particular context)
  • this allows a php definition to include the html definition while still controlling when to pop it back off the stack, and for that to reference javascript and css when applicable etc. (see the default packages for how they do this) :slightly_smiling:
  • regex’s that consume characters, consume characters so that the next regexs evaluated would need a lookbehind (not compatible with the new regex engine) if they want to assert those characters come immediately before it - not necessary if you push a new context
  • regex’s can only match one line at a time
  • only consumed characters can be assigned a scope, unless a meta scope is applied to the context

therefore, pushing two definitions at once is unlikely to confuse things because it will only look at the top most context on the stack unless something (like a prototype) tells it to pop off. A syntax definition would rarely ever have reason to pop it’s own main context off… I guess multiple active prototypes at once could get interesting… something for you to experiment with! :smiley:

1 Like

[solved] New Syntax - highlighting function in conflict with keywords
#10

Thanks for the explanation!   Seems a bit over my head for now.   :sweat_smile:   Def will spend some time figuring it out soon though, got a project in my queue that will require a custom syntax.

0 Likes

#11

Its working, I was right I was being thick.

I was still selecting the C# syntax rather than the custom cs one.

Many thanks for the help Clams

0 Likes

#12

Now I can do this I’d like to extend it to highlight certain words but use a different colour to any existing ones. Is that easy enough to do? I guess I need to define a new scope and set the colour it’ll use?

0 Likes

#13

Correct - you set the color it will use by editing the .tmTheme file that corresponds to the color scheme you are using, and including your new scope there. I recommend using https://packagecontrol.io/packages/PackageResourceViewer to edit it.

0 Likes

#14

OK got that working. Issue I now face is I want to highlight variables. To set a variable you either do set var to 1 or x := 1

I’ve got that to work with regex but can I have it then apply that scope to all other instances of that variable in the current file?

IE the variable is highlighted when its defined but not when its used at the moment

Many thanks for all your help on this!

0 Likes

#15

You cannot really support this kind of stuff.

0 Likes

#16

Hi,

I would like to do a similar thing. I would like ti highlight Strings of the type “%---------------”. So i made a file called “LaTeX improved.sublime-syntax” in the User folder, with following contents:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
scope: source.cs storage.type.source.cs
contexts:
  main:
    - match: ""
      push: "Packages/LaTeX/LaTeX.sublime-syntax"
      with_prototype:
        - match: "%--------------"
          scope: source.cs storage.type.source.cs

But when I manually change the syntax to “LaTeX improved”, the highlighting gets completely broken. What am I doing wrong?

0 Likes

#17

try changing the first scope at the top of your file to the default LaTeX scope, which is text.tex.latex

0 Likes

#18

Technically you can support highlighting of defined scopes when they are used, but it is a much harder problem to solve and it cannot be done with just the synatx highlight definitions, requires writing a sublime plugin.

I am currently doing exactly that, it works by using the syntax definition file to mark the symbols in the current file and the plugin code which extracts the symbols and applies a scope to wherever thay are used.

I can’t remember the original thread, but it is based on ColorCoder idea - which is needed if you want to highlight text programmatically.

1 Like

#19

Hi @kingkeith

I tried your suggestion. However, it does not work; the file is highlighted for the say first 200 lines, and then suddenly the highlighting is broken and all text stays white. Do I have to adjust something else in my file?

0 Likes

#20

hmm, sounds like too many contexts are being pushed onto the stack, which makes Sublime stop attempting to apply scopes to the rest of the file. Are you able to share your latex file that shows the problem? It will be easier for me to investigate that way :slight_smile:

0 Likes

#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