崇高文本 - 修改 tm主题文件



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

它不是一个正则表达式;它是一个从 TextMate 借来的范围选择器。

可以 AND、OR 和减去范围选择器,例如:(a | b) & c - d会选择与 d 不匹配且与 c 和 a 或 b 匹配的范围。

在崇高文本中,您可以通过Tools转到菜单 ->Developer->Show Scope Name在光标右侧找到字符的范围。


对于测试选择器,您可以使用Sublime Text控制台(View菜单->Show Console)中的view.match_selectorview.find_by_selectorAPI。

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

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

操作员

这些是布尔逻辑运算符:

  • -:"not",在选择器中的任何位置使用(需要明确的是,这是一个被空格包围的破折号,因为破折号可以出现在范围名称的中间)
  • &:"and",在作用域中的任何位置使用(在 XML 的.tmTheme文件中,除非在 CDATA 节点内,否则应将&转义为&amp;
  • |,:"或",在作用域中的任何位置使用
  • (......)可用于将选择器分组在一起

还有一个分层运算符:

  • (空格):"之后"必须位于前面的范围之后(即右侧)。
    • 这在(分组)后不起作用。

笔记

  • 作用域应该只包含字母数字字符和点(.),因此永远不会发生与运算符的冲突。
  • 作用域由单个空格分隔。
  • 运算符周围的空格不是必需的。(在评估之前修剪/剥离空格。即string | commentstring|comment相同。
  • 在评估前导点和尾随点之前,也会从范围选择器中删除
  • 连续空格被视为单个空格。
  • 所有范围都按.拆分,并从一开始就匹配选择器具有的级别数。a.b将匹配a.b.c.d.
  • 作用域选择器中没有通配符运算符。因此,您无法使用.python*.python等匹配范围source.python
  • 一个完全空的选择器匹配所有内容。但当操作员跟随时就不行了。即|本身会失败,|source也会失败. 然而,source|有效。-source -将失败。
  • 如果不确定运算符优先级,请将表达式的某些部分括在括号中以使其清晰。请记住在分组后使用空格以外的运算符,否则分组后直接使用的作用域将被忽略。

在下面的 Python 代码片段中,使用语法测试格式,所有测试都将通过,因此它作为选择器工作原理的演示:

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

请注意,由于范围选择器特异性似乎忽略了一些更高级的构造,您可能会发现使用范围选择器创建.tmTheme规则在您可能意想不到的情况下适用或不适用。

最新更新