12

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Jonathan Voss
  • 1,126
  • 1
  • 9
  • 24
  • @GabrielStaples I was in a nerd rage but tried to stay civil. As described in the PR comments, Python docstrings are a semantic feature of the language, which differentiates them from regular block comments that are simply _treated_ as docstrings by IDEs in other languages. A wrong behavior was replaced with another wrong behavior, and it is likely a limitation of VS Code. – Jonathan Voss Jun 10 '23 at 13:01
  • 1
    I actually came to this question from my own question (https://stackoverflow.com/questions/76466400/why-are-docstrings-and-block-comments-suddenly-the-same-color-as-a-single-line-c/76466841#76466841). And initially thought it was an error or setting, but i actually think that all comments (including the docstrings) in one colour is helpful. – D.L Jun 13 '23 at 21:19

3 Answers3

11

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"
                }
            }
        ]
    }
starball
  • 20,030
  • 7
  • 43
  • 238
4

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 Ctrl + Shift + P 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:

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

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

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

enter image description here

Select it. Now, you can click around on different syntax to see the syntax highlighting scope and information, like this. You can see the "string.quoted.docstring.multi.python" scope in the image below, when I have my cursor on the """ 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.

enter image description here

When done inspecting colors, hit Esc 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?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • As I mentioned in the other comment thread, docstrings have semantic meaning in Python as a language feature, so treating them as equivalent to block comments in any other language is incorrect. There is a semantic difference between a docstring and a block comment in Python, even though the syntax is the same. – Jonathan Voss Jun 10 '23 at 13:10
0

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:

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

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

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

This change requires a restart to take effect.

Jonathan Voss
  • 1,126
  • 1
  • 9
  • 24
  • 5
    at the next update you lose these changes, modify the theme colors in the settings, and per theme – rioV8 Jun 09 '23 at 04:45
  • 2
    You really should be modifying the theme colors in your custom user `settings.json` file, as the other answers here point out, not by touching the theme files directly. – Gabriel Staples Jun 09 '23 at 13:43