Cyber Avater
Cyber Avater

Reputation: 2147

Finish code folding in one line (and keep folding sign visible all the time)

Code folding in VScode doesn't seem as good as IntelliJ's.

eg. code block

def g(a):
    print(a)
    print(a)
    print(a)
    print(a)
    print(a)

Or, for (...),{...},[...] (indentation isn't actually the real concern.)

eg code sample from dart(flutter):

OutlinedButton(
              onPressed: () {
                Navigator.pushNamed(
                  context,
                  SecondPage.id,
                );
              },
              child: Text("Page 2"),
            )
  1. So, now if I fold this code and try to duplicate the folded line, it doesn't copy the entire fold as it does in IntelliJ. Is there an extension or settings, with which I can achieve the same as IntelliJ? I've found a comment which says I need to copy till the beginning of the next line which is not what I want. https://github.com/microsoft/vscode/issues/51232#issuecomment-395190470
  1. VScode only shows the folding sign when I hover the mouse over it, I want it visible all the time.

Upvotes: 0

Views: 1366

Answers (2)

DavidT
DavidT

Reputation: 767

Three points. Two addressed by others already but I'll include them here for completeness, with the third being my own contribution.

  1. Fundamentally, Code's philosophy seems to be that the end of the line containing all the folded code seems to be considered the end of the first line of the folded code, not the end of all the folded code. The folded code exists between the start of the first line of the code in question, and the start of the next visible line (the first line after the folded section). Therefore:
  • As per daiyam's answer, press Shift+Down at the start of the folded code to select everything between it and the next visible line. This then includes all the folded code, which you can then copy/paste.

  • My own contribution here: Similarly, to add a new line after the folded code, either press Down from the start of the folded code line, or press Right from the end of that line, to get to the start of the next line. Then press Enter to add a new line there. Then Up to get to the start of the new line.^^

  1. As per mark's comment set the Editor: Show Folding Controls to always.

^^ Some potentially overkill analysis 😉 ...

Yes, that seems like a lot of steps for something you're used to doing in one step in IntelliJ, except it's only two extra (arrow) key presses. If you're switching away from IntelliJ, to Code, then your muscle memory will likely adapt pretty quick. That might only be a problem if you're switching back and forth between them frequently.

Also, whatever keys or mouse click you've done to get to the start or end of that first folding line in the first place could be replaced by a different click or key presses to get you to the start of that next line instead. So the only extra really is the Up key after the Enter, plus the (presumably pretty tiny) learning curve to remember to go to that next line start instead of previous line end.

I suppose if that's a big deal, you could use any of the Macro applications to combine those extra arrow keystrokes and the Enter to one keystroke (say Shift+Enter or something). I find myself doing that sort of thing a lot to bring consistency to my use of different IDE's etc.

Just a couple of ideas in case they help.

Upvotes: 1

daiyam
daiyam

Reputation: 21

I'm assuming that the code language is Python.

So, now if I fold this code and try to duplicate the folded line, it doesn't copy the entire fold as it does in IntelliJ

You need to select the whole region. To do that, simply do a Shift+Down, then you can copy/paste the region.

If I press Enter after folding, it creates a new line in the folding section, I actually want it to create the line after the folding section.

Python's folding regions are based on the indentation. So when pressing Enter after a folding, the new line is tabbed so it is inside the region. You need to delete the tab so the line isn't in the region.

Finish code folding in one line

To fold on a single line, you need to use the extension Explicit Folding and the following configuration:

"[python]": {
    "explicitFolding.rules": [
        {
            "beginRegex": "\"\"\"",
            "endRegex": "\"\"\"",
        },
        {
            "indentation": true,
            "offSide": true
        }
    ]
}

Upvotes: 2

Related Questions