In VS Code + Python, is there a way to fold only functions (and not if/else statements)?

67 Views Asked by At

I'm new to Python, and I'm trying to fold my code in a way that's similar to regular Visual Studio with C#.

In VS, you can press Ctrl+M, Ctrl+O to fold all methods and #regions, and then press Ctrl+M, Ctrl+P to unfold.

VS Code has a similar feature with Ctrl+K, Ctrl+0 and Ctrl+K, Ctrl+J. However, this feature folds a lot of the contents of the functions, like if/else statements.

EDIT: Looking at the attempted answers on the duplicate question, I think the correct answer is simply that it isn't possible to fold Python code to only the function names.

1

There are 1 best solutions below

1
MingJie-MSFT On

If you are familiar with #region you can still use this tag in vscode.

You could add #region and #endregion before and after the code that needs to be folder, such as:

# region
print("HelloWorld")

#endregion

enter image description here

You can manually click on the arrows, or use the shortcuts ctr+8 and ctrl+9 to collapse or expand.