如何在 VS code 中自定义 python 语法高亮?

问题描述 投票:0回答:5

我使用 Anaconda 安装了 Visual Studio Code,并且想要自定义语法突出显示。我使用默认的深色主题,这很好,但 python 内置函数和方法的颜色却不是。

我找到“开发人员:从当前设置生成颜色主题”并尝试找到要更改的位置。 (我不确定改变语法高亮颜色是否是正确的文件)

我该怎么办?

python visual-studio-code
5个回答
22
投票

在 Visual Studio Code 中,您可以使用内置的颜色主题、安装社区创建的新颜色主题并上传到 Marketplace 或编辑所有已存在的颜色主题。如果您只想自定义语法的特定颜色,例如函数名称,需要编辑settings.json文件。

为此,请转到 文件 > 首选项 > 设置 > 工作台 > 外观,并在 颜色自定义 部分中单击 在 settings.json 中编辑

现在您需要通过在此文件中添加代码来指定您想要自定义的内容,然后保存它。

此代码会将函数名称的颜色更改为橙色:

"editor.tokenColorCustomizations": {
"functions": "#FF9900"

如果您想更改一些其他设置,例如变量、字符串、数字遵循以下模式:

"editor.tokenColorCustomizations": {
"what_you_want_to_customize" : "hex_value_of_color"

如果你想在调用方法时改变颜色,你需要指定范围(在同一个settings.json文件中):

"editor.tokenColorCustomizations": {
        "textMateRules": [
            {
                "scope": "meta.function-call",
                "settings": {
                    "foreground": "#FF9900"
                }
            }

现在,当您在某些对象中调用函数时,它会显示为橙色。

这是使用 pandas.DataFrame():

的样子

如果您在对象中创建自己的方法,它也将是您选择的颜色。

这就是将这两个设置结合起来时的样子。

我刚刚在创建函数时将颜色更改为红色,在调用函数时将颜色更改为橙色,以获得更好的解释。

还有官方文档供进一步阅读和更多设置以使其自定义外观(文本、栏、按钮)


3
投票

Stoockbroker 答案的正确形式。 (已更正缺少的括号。)

  "editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": "meta.function-call.generic.python",
        "settings": {
          "foreground": "#FF0000"
        }
      }
    ]
  },

https://github.com/MagicStack/MagicPython/issues/127


1
投票

我使用这个答案使用VSCodium(VSCode sans Microsoft)为Python创建了carbon.now.sh“One Dark”主题的副本

主题由 settings.json 文件中的这段代码产生:

"workbench.colorCustomizations":
    {
        "editor.background": "#282C34"
    },
"editor.tokenColorCustomizations":
    {
        "textMateRules": 
            [
                {
                     "scope": "meta.function-call",
                     "settings":
                         {
                             "foreground": "#5BB6C1"
                         }
                },
                {
                     "scope": "string.quoted.single.python",
                     "settings": 
                         {
                             "foreground": "#94BC79"
                         }
                },
                {
                     "scope": "source.python",
                     "settings":
                         {
                         "foreground": "#DE6D77",
                         }
                }
            ]
        }

这是我想要复制的代码示例,由 carbon.now.sh 生成:

上面的主题设置帮助我在 VSCodium / VSCode 中为我正在为 Frappe 工作的项目复制它。

与 CodeSnap 扩展结合使用时,您可以创建与 Carbon.now.sh 相同的外观,但离线:

这提高了我创建具有多个代码片段的文档、视频时的工作效率。另外,我可以输入有用的代码一次,而不必将其复制粘贴到其他地方,或者只需获取现有代码的风格化图像片段。


1
投票
这里有一个简短的答案和一个例子。

您必须编辑settings.json。 为此,请单击:文件->首选项->设置->工作台->颜色自定义(或者只需在设置中搜索“颜色自定义”)。

在此 json 中,您将找到“editor.tokenColorCustomizations”和“workbench.colorCustomizations”。这些嵌套的 JSON 就是您正在寻找的。

要了解如何自定义每个令牌,请查看

这个类似问题中的Joey的惊人答案。

但是如果你很懒(像我一样)并且不想读很多书,这里有一个实际的例子。只需将这两个元素粘贴到

settings.json 的根 json 中即可覆盖深色主题。我评论了最重要的部分。享受吧

"workbench.colorCustomizations": { "[*Dark*]": { // terminal colors "terminal.background": "#181818", "terminal.foreground": "#00ff00", "terminalCursor.background": "#D8D8D8", "terminalCursor.foreground": "#D8D8D8", "terminal.ansiBlack": "#181818", "terminal.ansiBlue": "#7CAFC2", "terminal.ansiBrightBlack": "#585858", "terminal.ansiBrightBlue": "#7CAFC2", "terminal.ansiBrightCyan": "#86C1B9", "terminal.ansiBrightGreen": "#A1B56C", "terminal.ansiBrightMagenta": "#BA8BAF", "terminal.ansiBrightRed": "#AB4642", "terminal.ansiBrightWhite": "#F8F8F8", "terminal.ansiBrightYellow": "#F7CA88", "terminal.ansiCyan": "#86C1B9", "terminal.ansiGreen": "#A1B56C", "terminal.ansiMagenta": "#BA8BAF", "terminal.ansiRed": "#AB4642", "terminal.ansiWhite": "#D8D8D8", "terminal.ansiYellow": "#F7CA88", // background "editor.background": "#0f0f0f", // brackets "editorBracketHighlight.foreground1": "#FAE734", "editorBracketHighlight.foreground2": "#DE6257", "editorBracketHighlight.foreground3": "#F58A6B", "editorBracketHighlight.foreground4": "#DE8657", "editorBracketHighlight.foreground5": "#FAA75A", "editorBracketHighlight.foreground6": "#abb2c0", // various errors "editorBracketHighlight.unexpectedBracket.foreground": "#ff0008", "editor.rangeHighlightBackground": "#ff0008", } }, // custom "editor.tokenColorCustomizations": { "[*Dark*]": { "strings": "#4AFF43", "numbers": "#1cdfdf", "functions": "#a856ff", "variables": "#F5AB76", "comments": "#1ABEE8", "textMateRules": [ { // function parameters "scope": "variable.parameter", "settings": { "fontStyle": "", "foreground": "#F5762A" } }, { // language constants such as bools "scope": "constant.language", "settings": { "fontStyle": "", "foreground": "#FF299E" } } ] } }, "editor.semanticTokenColorCustomizations": { "enabled": true, },
    

0
投票
我在settings.json中使用了下一个配置来显示Python代码,就像VSCode中的正常语言语法一样:

{ "editor.tokenColorCustomizations": { "textMateRules": [ { "scope": "meta.function-call", "settings": { "foreground": "#DCDCAA" } }, { "scope": "string.quoted.single.python", "settings": { "foreground": "#CE9178" } }, { "scope": "source.python", "settings": { "foreground": "#9CDCFE", } } ] } }
Psdt:

https://carbon.now.sh/帮助我选择正确的颜色。

© www.soinside.com 2019 - 2024. All rights reserved.