You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode扩展开发中能否自定义tokenColors且不修改当前主题?

在VSCode扩展中自定义Token颜色(不修改现有主题)

你可以通过在扩展的package.json中配置contributes.tokenColors来实现需求,这个配置会直接叠加在当前激活的主题之上,无需修改默认主题,也不会导致其他高亮扩展失效。

具体实现步骤

  • 打开扩展的package.json,添加contributes字段(已有则直接补充),在其中定义tokenColors数组:
    {
      "contributes": {
        "tokenColors": [
          {
            "name": "自定义函数高亮",
            "scope": "support.function.my-custom-language",
            "settings": {
              "foreground": "#FF4500",
              "fontStyle": "bold italic"
            }
          },
          {
            "name": "自定义注释高亮",
            "scope": "comment.block.my-custom",
            "settings": {
              "foreground": "#00FF00",
              "background": "#1A1A1A"
            }
          }
        ]
      }
    }
    
  • scope需要匹配你在TextMate语法定义中指定的token scope,若要覆盖主题默认规则,可使用更具体的层级选择器,比如source.js support.function.my-custom。
  • settings支持foreground、background、fontStyle(可选值:bold/italic/underline,可组合)等属性。

进阶动态控制(可选)

如果需要在运行时根据条件调整高亮,可使用VSCode文本装饰API:

// 创建装饰类型
const customDecorationType = vscode.window.createTextEditorDecorationType({
  color: "#FF00FF",
  fontWeight: "bold"
});

// 应用到指定文本范围
const editor = vscode.window.activeTextEditor;
if (editor) {
  const range = new vscode.Range(0, 0, 0, 10);
  editor.setDecorations(customDecorationType, [range]);
}

这种方式适合非TextMate token驱动的自定义高亮场景,比如特定关键词、动态内容的高亮。

内容的提问来源于stack exchange,提问作者tao gu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:42:44