Sublime Text - Modifying tmTheme file

It's not a regular expression; it's a scope selector which was borrowed from TextMate.

it's possible to AND, OR, and subtract scope selectors, e.g.: (a | b) & c - d would select the scope which is not matched by d, and matched by both c, and a or b.

In Sublime Text, you can find the scope of the character to the right of the cursor by going to Tools menu -> Developer -> Show Scope Name.


For testing selectors, you can use the view.match_selector or view.find_by_selector APIs in the Sublime Text console (View menu -> Show Console).

Example to see if the scope at the first cursor matches the selector from your first example:

view.match_selector(view.sel()[0].begin(), 'entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)')

Operators

These are boolean logic operators:

  • -: "not," used anywhere in the selector (to be clear, this is a dash surrounded by spaces, as a dash can appear in the middle of a scope name)
  • &: "and," used anywhere in the scope (in a .tmTheme file, which is XML, the & should be escaped to &, unless inside a CDATA node.)
  • | and ,: "or," used anywhere in the scope
  • () can be used to group selectors together

There is also a hierarchical operator:

  • (space): "after," must come after (i.e. to the right of) the preceding scope.
    • This does not work after a (grouping).

Notes

  • Scopes should only ever consist of alpha numeric characters and dots (.), therefore conflicts with the operators will never occur.
  • Scopes are separated by a single space.
  • Whitespace around the operators are not necessary. (Whitespace is trimmed/stripped before evaluation.) i.e. string | comment is the same as string|comment.
  • Leading and trailing dots are also stripped from scope selectors before they are evaluated
  • Consecutive whitespace is treated as a single space.
  • All scopes are split by . and matched from the beginning for as many levels as your selector has. a.b will match a.b.c.d.
  • There are no wildcard operators in scope selectors. Therefore, you cannot match the scope source.python using .python or *.python etc.
  • A completely empty selector matches everything. But not when followed by an operator. i.e. | on its own will fail, as will |source. source| works, however. - and source - will fail.
  • If you are not sure about operator precedence, enclose parts of the expression in parentheses to make it clear. Remember to use an operator other than space after grouping, or the scopes directly after the grouping will be ignored.

Example

In the following Python snippet, using the syntax test format, all the tests will pass, and thus it serves as a demonstration of how selectors work:

a = "hello world" # comment
#   ^^^^^^^^^^^^^ string.quoted.double
#   ^^^^^^^^^^^^^ string
#   ^^^^^^^^^^^^^ string.quoted
#   ^^^^^^^^^^^^^ string.quoted.
#   ^^^^^^^^^^^^^ - quoted.double
#   ^^^^^^^^^^^^^ string - comment
#   ^^^^^^^^^^^^^ string, comment
#   ^^^^^^^^^^^^^ string | comment
#   ^^^^^^^^^^^^^ string & - comment
#   ^^^^^^^^^^^^^ string & - comment
#   ^^^^^^^^^^^^^ source string
#   ^^^^^^^^^^^^^ source & (string - comment)
#   ^^^^^^^^^^^^^ source - (string & comment)
#   ^^^^^^^^^^^^^ string & source
#   ^ source.python string.quoted.double.block.python punctuation.definition.string.begin.python
#   ^ source & string & punctuation.definition.string.begin.python
#   ^ string & punctuation & source
#   ^ string punctuation & source
#   ^ source punctuation & string
#   ^ source string punctuation - (punctuation string)
#   ^ string - source comment - punctuation source
#   ^ string - source comment - comment
#   ^ source - python
#   ^ source - (source & python)
#   ^ source - (source python)
#   ^ source.python - source.python.string
#   ^ source.python.. ..string..
#                 ^ comment - string
#                 ^ comment
#                 ^ comment, string
#   ^^^^^^^^^^^^^^^^^^^ comment, string | source
#   ^ (punctuation | string) & source.python - comment
#   ^ (punctuation & string) & source.python - comment

Note that due to how scope selector specificity seems to ignore some of the more advanced constructs, you might find that .tmTheme rules you create with scope selectors apply or don't apply in cases you might not expect.