要确认文件被正确识别为特定语法,需执行Ctrl+Shift+P→Editor: Log Cursor Scope,若末级scope为source.xml/source.protobuf/source.nginx即成功;若仅为text.plain或text.html,则Atom未解析为代码,后续高亮配置均无效。

怎么确认当前文件真被识别为目标语法
全灰无色不是主题问题,是 Atom 根本没把文件当代码解析。执行 Ctrl+Shift+P → 输入并运行 Editor: Log Cursor Scope,看控制台输出最末一级 scope:要是 source.xml、source.protobuf 或 source.nginx 才算成功;若只显示 text.plain 或 text.html,后续所有高亮配置都白搭。
自定义语法必须建 package + grammar 文件
Atom 不支持直接在 UI 里写正则配色,必须手动建插件包:
- 进
~/.atom/packages目录,新建文件夹如language-protobuf - 在该目录下创建
package.json,确保含"name": "language-proto"和"engines": {"atom": "*"} - 建
grammars/protobuf.cson,内容至少包含:scopeName: 'source.protobuf'、fileTypes: ['proto']、patterns: [{match: '\bmessage\b', name: 'keyword.control.protobuf'}] - 保存后重启 Atom 或重载窗口(
Ctrl+Shift+F5),打开.proto文件,右下角应显示 “Protobuf”
fileTypes 映射错一个字符就失效
core.fileTypes 是新建未保存文件默认语法的唯一入口,但 scope 名必须完全匹配语言插件注册值:
- 打开一个已正确高亮的文件 → 运行
Editor: Log Cursor Scope→ 复制第一行的完整 scope(如source.vue,不是text.html.vue) - 编辑
~/.atom/config.cson,在core:下加:fileTypes: '.proto': 'source.protobuf' - 空字符串
''可设无后缀新文件默认语法,但慎用——它会覆盖所有 untitled 标签页 - 改完不用重启,新开标签页立即生效;已有标签页需手动点右下角切换
styles.less 里改颜色必须写对作用域链
语法主题不认你写的 .keyword,只响应完整 scope 路径:
- 查作用域链:光标停在关键字上 →
Editor: Log Cursor Scope→ 复制类似source.protobuf keyword.control.protobuf的整行 - 在
~/.atom/styles.less中写:.syntax--source.syntax--protobuf .syntax--keyword.syntax--control.protobuf { color: #ff6b6b !important; } - 少一个
.syntax--前缀或拼错 scope 名,样式就无效 - 别用
!important覆盖整个.syntax--string,那会破坏所有语言的字符串样式
真正难的不是写正则,是让 Atom 在正确时机、用正确 scope、加载正确 grammar —— 三者缺一,styles.less 再多代码也只配给灰色文本描边。











