visual-studio-codetext-editor

VSCode: how to fold arbitrary/manual lines of code?


By "VS Code" I mean the lightweight text editor not the monolithic IDE, unfortunately searching this on google will bring up many pieces of irrelevant information about how to do this in Visual Studio.

To the question itself, Anybody knows how to hide arbitrary selected lines of code in "Visual Studio Code", preferably into a plus sign like collapsing does?

Note: this is different than collapsing nested code which probably could be achieved by Ctrl+K,Ctrl+<num> , what I need here is to hide specific block of code of choice, no matter nested or not.

EDIT: I see there are people who don't understand my requirements.

for example, you may think what I want is this:

before hiding:

for i in j:
    for k in i:
        for l in k:
            somestuff...

after hiding:

[+] for i in j: ...

What I actually want is this:

before hiding:

# doing stuff about a
a = ClassA()
a.bar()
a.i = 2
a.j = 3
a.k = 5

after hiding:

[+] ...  ( doing stuff about a )

2017.10.17 EDIT: turns out VS Code implemented a very similar feature called "Folding Regions" in VS Code 1.17.1 Update. link


Solution

  • In the Insiders Build v1.70 now is the functionality and command to truly hide arbitrary lines of code. The command is

    Create Manual Folding Range from Selection
    editor.createFoldingRangeFromSelection
    

    It is bound to Ctrl+K Ctrl+, by default. Select any lines you want to fold.

    You can unfold those lines either by clicking the gutter folding controls or this command when the cursor is somewhere on the folded line:

    Remove Manual Folding Ranges
    editor.removeManualFoldingRanges
    

    The above command is bound to Ctrl+K Ctrl+. by default.

    fold selected lines demo