Reputation: 525
In the .tmTheme
file:
<dict>
<key>name</key>
<string>Entity name</string>
<key>scope</key>
<string>entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)</string>
<key>settings</key>
<dict>
<key>fontStyle</key>
<string></string>
<key>foreground</key>
<string>#A6E22E</string>
</dict>
</dict>
The next scope string:
<string>entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)</string>
Is a kind of regular expression? What applies with this definition? In another part of the same file I can see something like this:
<string>variable.parameter - (source.c | source.c++ | source.objc | source.objc++)</string>
Upvotes: 2
Views: 890
Reputation: 16065
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)')
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 togetherThere is also a hierarchical operator:
(space): "after," must come after (i.e. to the right of) the preceding scope.
(
grouping)
..
), therefore conflicts with the operators will never occur. string | comment
is the same as string|comment
..
and matched from the beginning for as many levels as your selector has. a.b
will match a.b.c.d
.source.python
using .python
or *.python
etc.|
on its own will fail, as will |source
. source|
works, however. -
and source -
will fail.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.
Upvotes: 9