Sublime Text - Modifying tmTheme file

走远了吗. 提交于 2019-12-18 12:43:15

问题


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>

回答1:


It's not a regular expression; it's a scope selector 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)')

More info:

The following operators can be used:

  • -: without, anywhere in the scope (to be clear, this is a dash surrounded by spaces, as a dash can appear in the middle of a scope name)
  • &: with, anywhere in the scope (in a .tmTheme file, which is XML, the & should be escaped to &amp;, unless inside a CDATA node.)
  • (space): with, must come after (i.e. to the right of) the preceding scope
  • | and ,: or, anywhere in the scope
  • (...) can be used to group selectors together

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 matched from the beginning. 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 it's 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. However, it seems that you need 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.



来源:https://stackoverflow.com/questions/37406338/sublime-text-modifying-tmtheme-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!