Sublime Forum

Avoid to highlight comments (sublime-syntax)

#1

I’m expanding the the C++ syntax highlight with new functions, classes, constants, etc. I’m following this guide because I haven’t experience with this topic.

I’m matching the new words like this:

match: \b(send|on)\b
scope: entity.name.function.c++

But, if my file has comments with that words, they will be colored too.

how can avoid this?

This is how my syntax file looks like

%YAML 1.2
---
name: newsyntax
file_extensions: [ino, pde]
scope: source.dino

contexts:
  main:
    - match: ''
      push: Packages/C++/C++.sublime-syntax
      with_prototype:
        - match: \b(HIGH|LOW|INPUT|OUTPUT|INPUT_PULLUP|LED_BUILTIN)\b
          scope: constant.language.c++
        - match: \b(boolean|word|String|string|array|byte)\b
          scope: storage.type.c++
        - match: PROGRAM
          scope: storage.modifier.c++
        - match: \b(Serial|Stream|Keyboard|Mouse})\b
          scope: entity.name.class.c++
        - match: \b(send|on)\b
          scope: entity.name.function.c++
0 Likes

#2

All rules under with_prototype: are applied to all scopes on the stack with highest priority before local rules are applied.

You’d need to create your own modified C++.sublime-syntax with your custom keywords added at the right place.

Maybe you can built a sytnax-file with a custom main context which contains your rules and includes selected contexts from C++.sublime-syntax. But I guess this may not be too trivial as you might not gain access to all pushed contexts and it may break if C++ syntax is changed.

1 Like

#3

This is the case @gepd – with a complex syntax where different tokens have different meanings in specific contexts, it isn’t possible to just add a bunch of rules, they really have to be added in the correct place.

Currently there isn’t a way to “extend” a syntax, only include the whole thing or portions. The default C++ and Objective-C++ syntaxes could be an example in this regard. There is a lot of duplication, but also some portions of C++ are included in Objective-C++.

Possibly at some point in the future I may see if I can come up with a consistent, logical way to use an existing syntax as a base and only override or extend specific contexts.

7 Likes

YAML Macros 2.0: Extend existing syntaxes with new functionality
#4

Thanks @deathaxe @wbond,

So I will copy the current C ++ syntax file and edit it, until this is solved.

0 Likes