Sublime Text自定义语法:如何获取更多着色作用域?
问题
我正在为纯文本文件创建自定义语法定义,使用文件Plain Text Coloured.sublime-syntax,通过匹配特定文本/字符并绑定C语言的作用域(如keyword.control.c、string.quoted.double.c等)实现着色,目前效果尚可,但希望获取更多作用域来添加红色、蓝色配色。我尝试查找Sublime Text的C语言语法定义却未找到,请问这是否是正确的搜索方向?
附当前语法定义代码:
%YAML 1.2 --- # http://www.sublimetext.com/docs/syntax.html name: Plain Text Coloured file_extensions: [txt] scope: source.txt contexts: main: # Purple - match: (MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY|CODEX|SORTIUM|LEXICANUM|GAWK|PCB NONSOMNUS|NONSOMNUS|MUTIM|PCB MUTIM|MUTIM|AQOS) scope: keyword.control.c # Green - match: '==' scope: string.quoted.double.c - match: '--' scope: string.quoted.double. - match: ':' scope: string.quoted.double.c - match: '✔' scope: string.quoted.double.c - match: '→' scope: string.quoted.double.c - match: '←' scope: string.quoted.double.c - match: '↑' scope: string.quoted.double.c - match: '↓' scope: string.quoted.double.c - match: '|' scope: string.quoted.double.c # Orange - match: '~' scope: constant.numeric.c - match: '✗' scope: constant.numeric.c - match: '>=' scope: constant.numeric.c - match: '<=' scope: constant.numeric.c - match: '>' scope: constant.numeric.c - match: '<' scope: constant.numeric.c - match: '±' scope: constant.numeric.c # Red highlight # scope: constant.character.escape.c # - match: '..' # scope: constant.other.markdown # scope: keyword.txt # scope: comment.txt # Potential lexicanum :: Morphims | Alchemy | Necromancy | Conjure | Exorcisim | Rune
回答
你的搜索方向是对的,但Sublime Text内置的C语言语法定义并非以独立文件形式直接存放在可直接访问的目录里,你可以通过以下方式查看:
- 打开任意C文件,按下
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac)调出命令面板,执行View Package File命令,输入C.sublime-syntax即可打开内置的C语法定义文件,里面包含所有C语言对应的作用域标签。
推荐几个能实现红色、蓝色配色的C语言作用域:
红色系作用域
invalid.illegal.c:多数主题默认渲染为红色,用于标记非法语法constant.character.escape.c:C语言转义字符,通常显示为红色comment.line.double-slash.c:单行注释,部分主题会渲染为红色
蓝色系作用域
storage.type.c:C语言类型关键字(如int、char),绝大多数主题显示为蓝色entity.name.function.c:函数名称,不少主题会用蓝色标记variable.parameter.c:函数参数,部分主题为蓝色
另外,你也可以不局限于C语言的作用域,使用通用的TextMate标准作用域:
markup.error:通用错误标记,通常为红色keyword.operator:操作符关键字,多为蓝色constant.language:语言内置常量,常显示为蓝色
最后提个小问题:你当前代码里有一行scope: string.quoted.double.少了后缀.c,建议补上,避免作用域无效。
内容的提问来源于stack exchange,提问作者SphagnumMoss
相关产品推荐
相关产品推荐

