6

Recently I discovered that the little arrow next to lines in vscode, that allows you to fold parts of the code, had disappeared. I then noticed this was the case only in my Python files. I scoped the internet looking for an answer, but nothing worked I'v tried fixing the setting (by checking that the "folding" setting in the settings UI was ticked) but it did nothing, I tried removing the last extensions I had installed to see if they were interfering or something, but no.

Thanks for the info on #region, but even that doesn't allow me to fold the code. I've tried with the command "fold" from the command palette and with 'Ctrl+Shift+[' and 'Ctrl+Shift+]' but it didn't work

I'm on Arch Linux using VsCode-OSS btw

5 Answers5

6

Sort of expanding on the other answer, I've worked around it by changing settings for my python-specific workspace and changing the "Folding Strategy" to "indentation" instead of "auto", which seems to be a perfect workaround (for me at least) since Python requires proper indentation anyway and this doesn't mess with global settings

image of the settings in question

I'm experiencing the exact same issue in VSCode on Windows (which is how I found this question) - only Python code folding seems to be broken, C++ etc seems to be fine, never noticed it happening before so I think a recent update broke it

mMcFab
  • 61
  • 3
3

Just adding to what has been said above - with some visuals:

Select (bottom left): Manage > Settings > filter on folding

Then make sure Editor: Folding is checked and that Editor: Folding Strategy is set to indentation - as below

enter image description here

Finally - close your session of VS Code

When you open VS Code again the folding settings should work.

Remember to mouseover to the left of class or def in your code to fold - as below:

enter image description here

Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
1

enter image description here

Search folding in settings, and then check the first one.

You can also use the following code to test whether it is valid

# region

# endregion

For example:

# region hi
print("HelloWorld")
# endregion
MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • 1
    For me `Edictor: Folding` was already checked. However, unchecking it, closing settings, opening settings, and checking it back on triggered code folding to work! – user3731622 May 09 '23 at 00:40
1

If none of other answers helps you, you can create your custom folding range using Ctrl+K Ctrl+, on windows (I hope smth like that is in linux also, try searching folding range). Selected lines willbe folded. To delete folding region, use Ctrl+K Ctrl+..

leschtsch
  • 17
  • 4
0

As said by user3731622 in comments, unchecking Edictor: Folding, restarting vs-code, and checking it back, may solve the problem.

saastn
  • 5,717
  • 8
  • 47
  • 78