这不是正则表达式;这是一个scope selector,它是borrowed from TextMate。
可以对范围选择器进行 AND、OR 和减法,例如:(a | b) & c - d 将选择 d 不匹配的范围,但 c、a 或 b 都匹配。
在 Sublime Text 中,您可以通过 Tools 菜单 -> Developer -> Show Scope Name 找到光标右侧字符的范围。
对于测试选择器,您可以在 Sublime Text 控制台中使用 view.match_selector 或 view.find_by_selector APIs(View 菜单 -> Show Console)。
查看第一个光标处的范围是否与第一个示例中的选择器匹配的示例:
view.match_selector(view.sel()[0].begin(), 'entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)')
运营商
这些是布尔逻辑运算符:
-
-: "not" 在选择器中的任何位置使用(要清楚,这是一个由空格包围的破折号,因为破折号可以出现在范围名称的中间)
-
&:“和”,在范围内的任何位置使用(在 .tmTheme 文件中,即 XML,& 应转义为 &,除非在 CDATA 节点内。)
-
| 和 ,:“或”在范围内的任何位置使用
-
(…) 可用于将选择器组合在一起
还有一个层次运算符:
-
(space): "after," 必须出现在前面的作用域之后(即右边)。
注意事项
- 范围只能由字母数字字符和点 (
.) 组成,因此不会发生与运算符的冲突。
- 范围由一个空格分隔。
- 运算符周围的空格不是必需的。 (空白在评估之前被修剪/去除。)即
string | comment 与string|comment 相同。
- 在对范围选择器进行评估之前,前导点和尾随点也会从范围选择器中去除
- 连续的空格被视为一个空格。
- 所有范围都由
. 分割,并从一开始就匹配您的选择器拥有的尽可能多的级别。 a.b 将匹配 a.b.c.d。
- 范围选择器中没有通配符运算符。因此,您无法使用
.python 或 *.python 等匹配范围 source.python。
- 完全为空的选择器匹配所有内容。但不是在操作员之后。即
| 本身会失败,|source 也会失败。但是,source| 有效。 - 和 source - 将失败。
- 如果您不确定运算符的优先级,请将表达式的部分内容括在括号中以使其清楚。分组后记得使用空格以外的运算符,否则分组后的范围会被忽略。
示例
在下面的 Python sn-p 中,使用 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 规则适用于或不适用于您可能未预料到的情况。