skip to Main Content

VS Code’s Fold All Block Comments command seemed to randomly decide whether or not to work on any given python file I was editing until I was able to work out that The number of import statements at the top of the file was influencing the behavior of the command.

The command works fine on a file with no imports:

""" This is a docstring that should fold
when VS Code's Fold All Block Comments command
is executed on this file. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna"""

# This folds

Also works on a file with one import:

import math

""" This is a docstring that should fold
when VS Code's Fold All Block Comments command
is executed on this file. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna"""

# Also folds

But with a second input, it no longer folds with the command…

import math
import tkinter

""" This is a docstring that should fold
when VS Code's Fold All Block Comments command
is executed on this file. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna"""

# Does not fold with Fold All Block Comments command.

Just for fun, the folding functionality returns if the imports are separated by a docstring:

import math

""" This is a docstring that should fold
when VS Code's Fold All Block Comments command
is executed on this file. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna"""
# I fold!

import tkinter

""" This is a docstring that should fold
when VS Code's Fold All Block Comments command
is executed on this file. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna"""
# I fold too!

The docstring is still recognized as a folding region in all examples, but doesn’t fold automatically with the command. What to do?

2

Answers


  1. I can reproduce this on VS Code 1.81.0 with Python extension v2023.14.0. According to Fold all block comments not working for Python docstrings #20330, this is by design, since docstrings are not block comments, so it’s interesting that the Fold All Block Comments affects docstrings at all.

    See also this feature request: Add a "fold all docstrings" command #4677. Give it a thumbs up to show support for it, and subscribe to it to get notified of discussion and progress. Please avoid making noisy comments like "+1" / "bump".

    For your reference / learning purposes, I found the above issue tickets by googling "github vscode python issues "fold all block comments"".

    Login or Signup to reply.
  2. Docstrings are not comments, it looks like there is some problem here. starball’s answer provides some working GitHub link.

    Now if you want to fold them, the alternative is to use the Fold All or Fold All Regions Except Seleted commands.

    enter image description here

    Unfold is Unfold ...

    Here is the relevant explanation of the folding part in VScode.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search