自定义NPC语言VSCode语法高亮失效问题求助
NPC编程语言VSCode语法高亮失效排查与修复
我为自研游戏开发了一门简易NPC编程语言,文件后缀为.npc。使用yo generator创建VSCode语法高亮扩展后,将扩展文件夹放入Users.vscode\extensions目录,多次重启VSCode后,if/return关键字、//注释等语法高亮功能仍未生效。
当前配置文件
npc.tmLanguage.json
{ "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json", "name": "NPC Programming Language", "patterns": [ { "include": "#keywords" }, { "include": "#strings" } ], "repository": { "keywords": { "patterns": [{ "name": "keyword.control.npc", "match": "\\b(if|while|for|return)\\b" }] }, "strings": { "name": "string.quoted.double.npc", "begin": "\"", "end": "\"", "patterns": [ { "name": "constant.character.escape.npc", "match": "\\\\" } ] } }, "scopeName": "source.npc" }
language-configuration.json
{ "comments": { // symbol used for single line comment "lineComment": "//" }, // symbols used as brackets "brackets": [ ["{", "}"], ["[", "]"], ["(", ")"] ], // symbols that are auto closed when typing "autoClosingPairs": [ ["<", ">"], ["{", "}"], ["[", "]"], ["(", ")"], ["'", "'"] ], // symbols that that can be used to surround a selection "surroundingPairs": [ ["<", ">"], ["{", "}"], ["[", "]"], ["(", ")"], ["'", "'"] ] }
排查与修复步骤
1. 检查package.json的关键配置
扩展根目录下的package.json必须正确声明语言贡献,否则VSCode无法识别.npc文件和语法规则。确保包含以下字段:
{ "contributes": { "languages": [ { "id": "npc", "aliases": ["NPC Programming Language", "npc"], "extensions": [".npc"], "configuration": "./language-configuration.json" } ], "grammars": [ { "language": "npc", "scopeName": "source.npc", "path": "./npc.tmLanguage.json" } ] } }
2. 补充注释高亮规则
当前tmLanguage.json缺少注释的高亮规则,需在patterns中添加注释引用,并在repository中新增注释规则。修改后的文件如下:
{ "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json", "name": "NPC Programming Language", "patterns": [ { "include": "#comments" }, { "include": "#keywords" }, { "include": "#strings" } ], "repository": { "keywords": { "patterns": [{ "name": "keyword.control.npc", "match": "\\b(if|while|for|return)\\b" }] }, "strings": { "name": "string.quoted.double.npc", "begin": "\"", "end": "\"", "patterns": [ { "name": "constant.character.escape.npc", "match": "\\\\" } ] }, "comments": { "patterns": [ { "name": "comment.line.double-slash.npc", "match": "//.*$" } ] } }, "scopeName": "source.npc" }
3. 验证扩展加载状态
- 打开命令面板(Ctrl+Shift+P),输入
Extensions: Show Installed Extensions,确认你的NPC语言扩展处于启用状态。 - 打开一个
.npc文件,输入Developer: Inspect Editor Tokens and Scopes,将鼠标移到目标文本上,查看右侧面板是否显示对应的scope(如keyword.control.npc)。如果没有,说明语法规则未被正确加载。
4. 清理VSCode缓存
若以上步骤无效,尝试清理缓存:
- 关闭VSCode
- 删除
%APPDATA%\Code\Cache和%APPDATA%\Code\CachedData目录(Windows系统) - 重新启动VSCode
内容的提问来源于stack exchange,提问作者Creepabob
相关产品推荐
相关产品推荐

