naz
naz

Reputation: 2072

VSCode fold docstrings Python MacOS

I have tried using the command:

>Fold Level 2 and it is causing too much folding.

And

>Fold Level 3 does not fold the methods' docstrings.

My primary goal is to fold the docstrings and nothing else.

def test(a, b, c):
    """A lot of multiline
    docstrings here
    that dont get folded
    """
    return ...

would turn into:

def test(a, b, c):
    return ...

Is there a way to achieve this?

Upvotes: 4

Views: 2410

Answers (2)

Isabella
Isabella

Reputation: 401

By default VS Code's cold folding is indentation-based, unaware of the programming language used. Aside from Brett's answer, one hack is to indent the docstring.

def test(a, b, c):
    """[short summary]

        [indented long summary parameters returns etc.]
    ^^^^
    """

    return ...

This is clearly a bad idea for anything professional, but it works.


Towards the end of Brett's link one can find a new comment that would allow the Python extension to fold docstrings properly. The comment contains instructions for MacOS. On my Ubuntu 20.04.1 LTS machine, I did the following instead:

  1. Run which code. I got /usr/bin/code, but that was linked to /usr/share/code/bin/code.
  2. Copy the directory /usr/share/code/resources/app/extensions/python to ~/.vscode/extensions.
  3. Add the regex below to ~/.vscode/extensions/python/language-configuration.json.
{
  "folding": {
    "offSide": true,
    "markers": {
      "start": "^\\s*#\\s*region\\b|^\\ *\"{3}(?!.*\"{3}).+",
      "end": "^\\s*#\\s*endregion\\b|^\\ *\"{3}$"
    }
  }
}

Upvotes: 3

Brett Cannon
Brett Cannon

Reputation: 16000

Not at the moment. Please upvote the feature request if you would like to see this supported.

Upvotes: 2

Related Questions