In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

huangapple go评论57阅读模式
英文:

In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

问题

抱歉,我无法提供代码部分的翻译。以下是翻译好的内容:

"我不知道这是怎么发生的。似乎无论我对主题做什么,都没有任何效果。在我重新启动 VS Code 之前,我选择了“Dark modern”主题,而Python的文档字符串与其他字符串的颜色完全相同。但重新启动后,“Dark modern”主题仍然被选中,但现在文档字符串变成了一种难看的深绿色。切换主题并不能恢复原来的颜色。手动修改主题的 JSON 文件也无效,而且这种难看的颜色在任何主题文件中都找不到。禁用和/或重新启用扩展没有效果,唯一的例外是禁用 Python 扩展会从语法高亮中删除几种颜色,但深绿色并不受影响。使用 venv 或不使用都没有效果。

有没有任何想法是怎么发生的或者如何修复这个问题?

编辑:我找到了问题的原因。这个人决定改变主题颜色。任务仍然是将其恢复到这个变更之前的状态。

编辑2:VS Code 似乎没有办法区分块注释和文档字符串。在Python语言中,这两者之间存在语义上的非语法区别。在某种程度上,这个问题无法解决,除非VS Code团队更新他们如何处理这个语义上的区别:它会错误地将文档字符串和块注释标记为相同,因此答案是在错误的行为之间进行选择。"

英文:

I have no idea how this happened. Nothing I do with themes seems to do anything. Before I restarted VS Code, I had the "Dark modern" theme selected, and Python docstrings were exactly the same color as other strings. After I restarted, the "Dark modern" theme was still selected, but now docstrings are an ugly, dark green color. Toggling between themes does not restore the original color. Manually modifying the theme json file does nothing, and this nasty color is not in any of the theme files. Disabling and/or re-enabling extensions has no effect with the exception that disabling the Python extension removes several colors from syntax highlighting, but not the dark green. Using a venv or not has no effect.

Any idea what happened or how to fix this?

Edit: I found the culprit. This guy decided to just change the theme color. The task remains to fix it back to the way it was before this change.

Edit 2: VS Code does not seem to have a way to distinguish between block comments and docstrings. There is a semantic, non-syntactic difference between these in the Python language. To some extent, the problem is incurable without the VS Code team updating how they handle this semantic difference: it will incorrectly highlight docstrings and block comments identically, so the answer is to choose between wrong behaviors.

答案1

得分: 11

以下是翻译好的部分:

注意: 如在问题帖子中提到,此更改已在 PR all color themes: treat comment docstrings as comments too #182162 中进行了更改,并已提出要撤销更改的 PR Revert Python docstring color #184938,该 PR 被标记为 2023 年 7 月发布里程碑(实施 现已完成)。

首先,查找您的主题用于字符串的颜色,您可以通过编写字符串文字,然后使用“Developer: Inspect Editor Tokens and Scopes”命令来执行,然后将以下内容添加到您的 settings.json 文件中:

"editor.tokenColorCustomizations": {
   "[这里填写主题名称]": { // 如果您需要帮助找到主题的名称/ID,请在 [] 中触发建议
      "textMateRules": [
         {
            "scope": "string.quoted.docstring", // 如果只想在 Python 中应用,请使用 string.quoted.docstring.multi.python。
            "settings": {
               "foreground": "#FF0000", // 填写颜色代码
            }
         }
      ]
   }
}

用于破损建议的解决方法

在撰写时,有关各种设置 JSON 文件的架构加载错误的问题仍然存在,这会导致“触发建议”功能失效。如果“触发建议”功能不起作用,请查看以下主题别名列表:

  • "Abyss"
  • "Default Dark+"
  • "Default High Contrast Light"
  • "Default High Contrast"
  • "Default Light Modern"
  • "Default Light+"
  • "Kimbie Dark"
  • "Monokai Dimmed"
  • "Monokai"
  • "Quiet Light"
  • "Red"
  • "Solarized Dark"
  • "Solarized Light"
  • "Tomorrow Night Blue"
  • "Visual Studio 2019 Dark"
  • "Visual Studio 2019 Light"
  • "Visual Studio Dark"
  • "Visual Studio Light"

如果这些方法不起作用,您可以尝试使用星号 [*] 或完全删除主题范围(但要知道,将来自一个主题的颜色硬编码到所有主题中将无法实现所有主题的预期行为):

"editor.tokenColorCustomizations": {
    "textMateRules": [
        {
            "name": "Docstrings",
            "scope": "string.quoted.docstring",
            "settings": {
                "foreground": "#CE9178"
            }
        }
    ]
}
英文:

Note: As mentioned in the question post, this change was made in PR all color themes: treat comment docstrings as comments too #182162, and a PR has been made requesting to revert the change at Revert Python docstring color #184938 which is marked as being in the July 2023 release milestone (implementation now completed).

First find out what colour your theme uses for strings, which you can do by writing a string literal and then using the Developer: Inspect Editor Tokens and Scopes command, and then add the following to your settings.json file:

"editor.tokenColorCustomizations": {
   "[Theme Name Here]": { // TODO trigger suggestions inside the [] if you need help finding your theme's name/ID
      "textMateRules": [
         {
            "scope": "string.quoted.docstring", // use string.quoted.docstring.multi.python if you only want it to apply for python.
            "settings": {
               "foreground": "#FF0000", // TODO
            }
         }
      ]
   }
}

Workarounds for broken suggestions

At time of writing, there are issues open for bugs regarding the schema loading for various settings json files, which leads to the trigger suggestions feature breaking. In case the trigger suggestions feature does not work, here is a list of theme aliases:

  • "Abyss"
  • "Default Dark+"
  • "Default High Contrast Light"
  • "Default High Contrast"
  • "Default Light Modern"
  • "Default Light+"
  • "Kimbie Dark"
  • "Monokai Dimmed"
  • "Monokai"
  • "Quiet Light"
  • "Red"
  • "Solarized Dark"
  • "Solarized Light"
  • "Tomorrow Night Blue"
  • "Visual Studio 2019 Dark"
  • "Visual Studio 2019 Light"
  • "Visual Studio Dark"
  • "Visual Studio Light"

If these do not work, you can try an asterisk "[*]" or remove the theme scoping entirely (but know that hardcoding a colour from one theme for all themes will not achieve the desired behaviour for all themes):

    "editor.tokenColorCustomizations": {
        "textMateRules": [
            {
                "name": "Docstrings",
                "scope": "string.quoted.docstring",
                "settings": {
                    "foreground": "#CE9178"
                }
            }
        ]
    }

答案2

得分: 4

Sure, here's the translated content:

如何根据您的喜好着色 Python 文档字符串;例如:像字符串一样

使用此技巧找到要设置 Python 文档字符串的颜色。在 VSCode 中的默认设置是大多数颜色主题都将文档字符串着色与字符串相同。因此,找到一个字符串,使用下面解释的“Developer: Inspect Editor Tokens and Scopes”工具检查其颜色,然后将其用作您的用户“settings.json”文件中的“foreground”颜色。

注意:如果您进行此更改,然后单击 VSCode 底部左侧的小人图标,并使用 GitHub(或其他登录选项之一)登录并打开同步,它将应用此更改到您拥有的所有计算机上的所有 VSCode 实例。

您需要将以下内容添加到您的“settings.json”文件中。

有关如何从命令面板中打开“settings.json”以及其在您的操作系统上的位置,请参阅此处:如何打开 Visual Studio Code 的 'settings.json' 文件?。简而言之,只需按<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>以打开命令面板,然后键入并搜索“Preferences: Open User Settings (JSON)”来打开它。在该用户设置 JSON 文件中,按照下面的说明更新文件:

{
    // 您的其他设置在此处

    // 用于修复 Python 文档字符串颜色/格式的新设置:

    "workbench.colorTheme": "Monokai", // <<< 在此处填写您的颜色主题 <<<
    "editor.tokenColorCustomizations": {
        "textMateRules": [
            {
                "scope": [
                    // 这包括 Python 文档字符串
                    "string.quoted.docstring", 
                ],
                "settings": {
                    // >>> 在此处设置您要用于 Python 文档字符串的颜色 <<<
                    // 例如:在 Monokai 中将文档字符串着色为字符串
                    "foreground": "#E6DB74",
                    // 例如:在 Monokai 中将文档字符串着色为注释
                    // "foreground": "#88846F",
                }
            }
        ]
    },
}

要找出上述文本中“settings”部分的“foreground”颜色的“scope”为“string.quoted.docstring”的“"”符号,您可以使用 VSCode 中的标记和范围检查器,如下所示:

按<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>以打开命令面板,然后键入并搜索“Developer: Inspect Editor Tokens and Scopes”,如下所示:

In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

选择它。现在,您可以单击不同的语法以查看语法高亮范围和信息,就像这样。在下面的图像中,当我将光标放在 Python 文档字符串块开头的“"""”字符上时,您可以看到“string.quoted.docstring.multi.python”范围。在上面的“settings.json”文件中,我已经从中提取了“string.quoted.docstring”部分。在下面的图像底部,您可以看到“#E6DB74”的“foreground”颜色。因此,如果您喜欢该颜色用于 Python 文档字符串,请在上面的设置中使用它。同样,您需要为您的确切颜色主题执行此操作,因为我的颜色主题设置为“Monokai”。

In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

完成检查颜色后,按<kbd>Esc</kbd>退出标记和范围检查模式。

附加详细信息

来自我在此处的评论

> 为了帮助人们回到以前的状态:
>
> 任何希望自定义主题以返回到其语法高亮显示器以前状态的人,都可以阅读我的答案以了解如何执行此操作。它包含有关查找您喜欢的部分的名称和颜色以及如何更改您不喜欢的部分的信息:

> 如何将 Python 文档字符串在 VSCode 中降级以看起来像 Sublime Text 的 Monokai

在我的上面的链接中,我解释了如何使用标记和范围检查器,然后我说:

> 使用此技巧自定义其他范围和颜色,以满足您的喜好,或者查看其他范围当前具有的颜色和格式,如果您想复制它们。

@starball 也有一个很棒的答案。有了这个答案以及我链接中的信息和截图,每个人都可以根据需要调整其语法高亮显示器。


顺便说一下,我是创建了该 PR 的人,VSCode 的维护者要求我大规模扩展我的 PR,以覆盖 所有 文档字符串,而不仅仅是 Python 文档字符串,以及 所有 语法高亮显示器,而不仅仅是我使用的 Monokai 语法高亮显示器。这个想法是使文档字符串,这在 Python 中像注释和 Doxygen 在其他语言中使用的方式一样,并且实际上更属于注释类别而不是字符串,与注释一样由语法高亮显示器处理,而

英文:

How to colorize Python docstrings to your liking; ex: like strings

Use this technique to find the color you want to set Python docstrings to. The previous default in VSCode was to colorize docstrings the same as strings for most color themes. So, find a string, inspect its color using the Developer: Inspect Editor Tokens and Scopes tool explained below, and use that as the foreground color in your user settings.json file below.

Note: if you do this change and then click the little person icon in the bottom-left of VSCode, and sign in with GitHub (or one of the other sign-in options) and turn on sync, it will apply this change to all instances of VSCode you have, on all computers you have.

You need to add the following to your settings.json file.

See here for how to open settings.json from the command palette, and for where it's located on your OS: How can I open Visual Studio Code's 'settings.json' file?. In short, just press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> to open up the command palette, and then type and search for Preferences: Open User Settings (JSON). In that user settings JSON file, update the file as explained here:

{
    // your other settings here

    // new settings to fix the Python docstrings color/formatting:

    &quot;workbench.colorTheme&quot;: &quot;Monokai&quot;, // &lt;&lt;&lt; YOUR COLOR THEME GOES HERE &lt;&lt;&lt;
    &quot;editor.tokenColorCustomizations&quot;: {
        &quot;textMateRules&quot;: [
            {
                &quot;scope&quot;: [
                    // this includes Python docstrings
                    &quot;string.quoted.docstring&quot;, 
                ],
                &quot;settings&quot;: {
                    // &gt;&gt;&gt; YOU SET YOUR COLOR YOU WANT FOR PYTHON DOCSTRINGS
                    // HERE &lt;&lt;&lt;
                    // Ex: to color docstrings like strings in Monokai
                    &quot;foreground&quot;: &quot;#E6DB74&quot;,
                    // Ex: to color docstrings like comments in Monokai
                    // &quot;foreground&quot;: &quot;#88846F&quot;,
                }
            }
        ]
    },
}

In order to find out what to use for the &quot;foreground&quot; color in the &quot;settings&quot; section for the textmate &quot;scope&quot; of &quot;string.quoted.docstring&quot; above, use the tokens and scopes inspector in VSCode, as follows:

Press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> to open up the command palette, and then type and search for Developer: Inspect Editor Tokens and Scopes, as shown here:

In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

Select it. Now, you can click around on different syntax to see the syntax highlighting scope and information, like this. You can see the &quot;string.quoted.docstring.multi.python&quot; scope in the image below, when I have my cursor on the &quot;&quot;&quot; characters at the start of a Python docstring block. And, in the settings.json file above, I have taken from that the string.quoted.docstring part. At the bottom of the image below, you can see the foreground color of #E6DB74. So, if you like that color for Python docstrings, use it in the settings above. Again, you'll need to do this for your exact color theme, as my color theme is set to Monokai.

In VS Code 1.79.0-1.80.2, Python docstrings are colored like comments. How do I return it to the string color?

When done inspecting colors, hit <kbd>Esc</kbd> to exit the tokens and scopes inspection mode.

Additional details

From my comment on the PR here:

> To help people go back:
>
> For anyone wishing to customize your theme to go back to how it was for your syntax highlighter, read my answer here to learn how to do that. It contains info about finding the names and colors of sections you like and how to change the ones you don't:
>
> How to subdue Python docstrings in VSCode in order to look like Sublime Text's Monokai
>
> In my link above, I explain how to use the token and scope inspector, and then I say:
>
> > Use this technique to customize other scopes and colors to your liking, or to see what colors and formatting other scopes currently have if you want to copy them.

@starball also has a great answer to this question. With that answer plus the info. and screenshots in my link above, everyone can tweak their syntax highlighters as desired.


By the way, I am the one who created that PR, and VSCode's maintainers are the ones who requested that I massively expand my PR to cover all docstrings, not just Python docstrings, and all syntax highlighters, not just the Monokai syntax highlighter I was using. The idea is to make docstrings, which are used in Python like comments and Doxygen are used in other languages, and which are really more in the category of comments, not strings, be treated by the syntax highlighters the same as comments, not strings. VSCode agreed, and asked me to expand my PR.

But overall, I think though now painful for some, the change is really a benefit to most, and the right default behavior for all going forward.

References

  1. My answer here, and all references I have at the bottom of that answer: How do I change color of comments in visual studio code?

答案3

得分: 0

我弄清楚了。颜色来自dark_vs.json主题文件,无论您是否选择了该主题。问题在于"tokenColors"数组中有以下对象:

{
    "name": "Comments",
    "scope": ["comment", "string.quoted.docstring"],
    "settings": { "foreground": "#6a9955" }
},

为了将所有暗主题恢复到几小时前的状态,我用以下内容替换了它:

{
    "name": "Docstrings",
    "scope": ["string.quoted.docstring"],
    "settings": { "foreground": "#ce9178" }
},
{
    "name": "Comments",
    "scope": ["comment"],
    "settings": { "foreground": "#6a9955" }
},

此更改需要重新启动才能生效。

英文:

I figured it out. The color is coming from the dark_vs.json theme file, whether or not you have that theme selected. The problem was that in the "tokenColors" array was the following object:

        {
            &quot;name&quot;: &quot;Comments&quot;,
            &quot;scope&quot;: [&quot;comment&quot;, &quot;string.quoted.docstring&quot;],
            &quot;settings&quot;: { &quot;foreground&quot;: &quot;#6a9955&quot; }
        },

To restore all dark themes back to the way they were a couple hours ago, I replaced it with the following:

        {
            &quot;name&quot;: &quot;Docstrings&quot;,
            &quot;scope&quot;: [&quot;string.quoted.docstring&quot;],
            &quot;settings&quot;: { &quot;foreground&quot;: &quot;#ce9178&quot; }
        },
        {
            &quot;name&quot;: &quot;Comments&quot;,
            &quot;scope&quot;: [&quot;comment&quot;],
            &quot;settings&quot;: { &quot;foreground&quot;: &quot;#6a9955&quot; }
        },

This change requires a restart to take effect.

huangapple
  • 本文由 发表于 2023年6月9日 09:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定