如何在 VS Code 中为多个主题自定义 textMateRules?

How to customise the textMateRules for multiple themes in VS Code?

VS Code 中,我想为多个主题自定义一些相同的 textMateRules。例如,对于 Atom One DarkDefault Dark+ 但不影响任何其他主题,我想将 keyword 设为 斜体 。我可以通过为每个主题分别复制相同的设置两次来实现这一点,如下所示

  "editor.tokenColorCustomizations": {
    "[Atom One Dark]": {
      "textMateRules": [
        {
          "scope": [ "keyword" ],
          "settings": { "fontStyle": "italic" }
        }
      ]
    },
    "[Default Dark+]": {
      "textMateRules": [
        {
          "scope": [ "keyword" ],
          "settings": { "fontStyle": "italic" }
        }
      ]
    }
  }

我如何只需要为两者设置一次,而不重复规则,尤其是当多个主题有很多相同的规则时?类似于下面的内容(但是它不起作用

  "editor.tokenColorCustomizations": {
    "[Atom One Dark] [Default Dark+]": {
      "textMateRules": [
        {
          "scope": [ "keyword" ],
          "settings": { "fontStyle": "italic" }
        }
      ]
    }
  }

来自 this Github page

首先将此代码复制到用户设置。

"editor.tokenColorCustomizations": {
  "textMateRules": [
    {
      "scope": [
        //following will be in italic (=FlottFlott)
        "comment",
        "entity.name.type.class", //class names
        "keyword", //import, export, return…
        "constant", //String, Number, Boolean…, this, super
        "storage.modifier", //static keyword
        "storage.type.class.js", //class keyword
      ],
      "settings": {
        "fontStyle": "italic"
      }
    },
    {
      "scope": [
        //following will be excluded from italics (VSCode has some defaults for italics)
        "invalid",
        "keyword.operator",
        "constant.numeric.css",
        "keyword.other.unit.px.css",
        "constant.numeric.decimal.js",
        "constant.numeric.json"
      ],
      "settings": {
        "fontStyle": ""
      }
    }
  ]
}

现在仔细观察,在 textMateRules 中你必须定义一个范围。 在 scope 数组中,你必须提到你想要修改哪些选项,然后,在相同范围的设置数组中你可以添加你的样式,例如,你想要添加 fontStyle italic。 像这样。

"editor.tokenColorCustomizations": {
  "textMateRules": [
    {
      "scope": [
        //following will be in italic (=FlottFlott)
        "comment",
        "entity.name.type.class", //class names
        "keyword", //import, export, return…
        "constant", //String, Number, Boolean…, this, super
        "storage.modifier", //static keyword
        "storage.type.class.js", //class keyword
      ],
      "settings": {
        "fontStyle": "italic"
      }
    }
  ]
}

查看 Mr.Shuvo 的回答。您可以像这样使其特定于加载的主题:

{
"folders": [
    {
        "path": "C:\Users\Envs\django2"
    }
],
"settings": {
    "workbench.colorCustomizations": {
        "activityBar.background": "#580A42",
        "titleBar.activeBackground": "#7B0E5D",
        "titleBar.activeForeground": "#FFFCFE"
    },
    "editor.tabCompletion": "on",
    "editor.tokenColorCustomizations": {
            "[Monokai Classic]": { 
                "textMateRules": 
                [
                    {
                        "scope": "string",
                        "settings": {
                                    "foreground": "#FF9900" ,}
                    }
                ]
            },
        }
}

}

如果 microsoft/vscode#103694 获得足够多的支持,Microsoft 将添加此功能。除非您自定义每个主题,否则您想要的行为是不可能的。

{
  "editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": [ "keyword" ],
        "settings": { "fontStyle": "italic" }
      }
    ]
  }
}

VSCode 1.59开始,它完全支持指定的方式现在的问题:),加上支持通配符*。参见 Extended theme customization syntax

  "editor.tokenColorCustomizations": {
    "[Atom One Dark] [Default Dark+] [*Monokai*]": {
      "textMateRules": [
        {
          "scope": [ "keyword" ],
          "settings": { "fontStyle": "italic" }
        }
      ]
    }
  }