如何在Visual Studio Code中单独修改指定关键词的颜色?
解决方案
VS Code的语法高亮依赖TextMate的作用域(scope),你遇到的问题是entity.name.function.preprocessor这个作用域同时覆盖了bool和目标词汇。解决核心是找到各自的精确作用域,分别设置。
第一步:查看精确作用域
打开VS Code,按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入并执行Developer: Inspect Editor Tokens and Scopes命令。把光标移到bool、true、false、NULL上,就能看到它们各自的作用域列表(最上方的是最精确的作用域)。
两种实现方式:
方式1:单独修改bool的颜色,保留true/false/NULL的颜色
假设你已经用entity.name.function.preprocessor设置了true/false/NULL的颜色,现在给bool单独设置颜色来覆盖原有规则:
{ "editor.tokenColorCustomizations": { "textMateRules": [ // 原有设置:修改true/false/NULL的颜色 { "scope": "entity.name.function.preprocessor", "settings": { "foreground": "#ff4444" // 你的目标颜色 } }, // 单独设置bool的颜色,优先级高于上方规则 { "scope": "storage.type.bool", // 替换成你用Inspect工具查到的bool精确作用域 "settings": { "foreground": "#44ff44" // 你想要的bool颜色 } } ] } }
方式2:只修改true/false/NULL,不影响bool
放弃使用entity.name.function.preprocessor,改用true/false/NULL各自的精确作用域单独设置:
{ "editor.tokenColorCustomizations": { "textMateRules": [ // 设置true/false的颜色 { "scope": "constant.language.boolean", // 替换成查到的true/false精确作用域 "settings": { "foreground": "#ff4444" } }, // 设置NULL的颜色 { "scope": "constant.language.null", // 替换成查到的NULL精确作用域 "settings": { "foreground": "#ff4444" } } ] } }
注意:不同编程语言的作用域可能不同,比如Python中bool的作用域是builtin.type,一定要用Inspect工具确认精确值。
内容的提问来源于stack exchange,提问作者Luca De Notta
相关产品推荐
相关产品推荐

