Sublime Text - 修改 tmTheme 文件

Sublime Text - Modifying tmTheme file

.tmTheme 文件中:

<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>

下一个范围字符串:

<string>entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)</string>

是一种正则表达式吗?什么适用于这个定义?在同一文件的另一部分,我可以看到类似这样的内容:

<string>variable.parameter - (source.c | source.c++ | source.objc | source.objc++)</string>

这不是正则表达式;这是 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.

在 Sublime Text 中,您可以通过 Tools 菜单 -> Developer -> Show Scope Name.[=52 找到光标右侧字符的范围=]


要测试选择器,您可以使用 Sublime Text 控制台中的 view.match_selectorview.find_by_selector APIsView 菜单 -> Show Console)。

查看第一个光标处的范围是否与第一个示例中的选择器匹配的示例:

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

运算符

这些是布尔逻辑运算符:

  • -:“not”,用于选择器中的任何地方(要清楚,这是一个被 space 包围的破折号,因为破折号可以出现在范围名称的中间)
  • &:“and”在范围内的任何地方使用(在 .tmTheme 文件中,即 XML,& 应转义为 &amp;,除非在 CDATA 节点内。)
  • |,:“或”在范围内的任何地方使用
  • () 可用于将选择器组合在一起

还有一个层次运算符:

  • (space):“之后”必须在前一个范围之后(即右侧)。
    • 这在 (分组) 后不起作用。

备注

  • 范围应该只包含字母数字字符和点 (.),因此永远不会发生与运算符的冲突。
  • 范围由单个 space.
  • 分隔
  • Whitespace周围的运算符不是必需的。 (Whitespace 在计算之前是 trimmed/stripped。)即 string | comment string|comment.
  • 相同
  • 前导点和尾随点在计算之前也会从范围选择器中删除
  • 连续白色space被视为单个space。
  • 所有范围都按 . 拆分,并从头开始匹配您的选择器所具有的级别。 a.b 将匹配 a.b.c.d.
  • 范围选择器中没有通配符。因此,您不能使用 .python*.python 等匹配范围 source.python
  • 完全空的选择器匹配所有内容。但不是在操作员后面时。即 | 本身会失败,|source 也会失败。然而,source| 有效。 -source - 将失败。
  • 如果您不确定运算符的优先级,请将部分表达式括在括号中以使其清楚。记得在分组后使用运算符而不是space,否则分组后的作用域将被忽略。

例子

在下面的 Python 片段中,使用 syntax test format, 所有测试都会通过,因此它可以作为选择器如何工作的演示:

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

请注意,由于 scope selector specificity 似乎忽略了一些更高级的结构,您可能会发现您使用范围选择器创建的 .tmTheme 规则适用或不适用您可能会遇到的情况没想到。