microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.58k stars 28.66k forks source link

[folding] Collapse ending brace to the same line #3352

Open Tyriar opened 8 years ago

Tyriar commented 8 years ago

Consider a large JavaScript file

function a() {
  ...
}

function b() {
  ...
}

function c() {
  ...
}

and so on...

Collapsing all currently yields:

function a() { ...
}

function b() { ...
}

function c() { ...
}

and so on...

This would be more readable if the closing brace was on the same line:

function a() { ... }

function b() { ... }

function c() { ... }

and so on...

Here is how other prominent editors handle the above:

Atom

image

Brackets

image

Sublime Text 3

image


VS Code

image

mqnc commented 4 years ago

+1

cepm-nate commented 4 years ago

https://github.com/zokugun/vscode-explicit-folding does work. Here's what we are wishing for: The "Fold last line" to be a GLOBAL setting in VisualStudio that could override extension settings. That's all. :-)

antonpegov commented 4 years ago

Another year past. Is there any LIFE on the VSCODE planet? I'll go buy IntellyJ IDE finally, can't wait next 3 years to your wake up, guys... Yea give me more dislikes, I will be continue adding one comment per year anyway )))

MarceloLaser commented 4 years ago

Thank you to those who recommended explicit-folding, fixes the problem to a sufficient degree. Unfortunate that the VSCode team decided to support the horrendous indentation-based rubbish languages instead of focusing on support for languages with stronger, more versatile syntax. Hopefully they'll come around to it eventually, but for now, again, grateful for the recommendations and explanations on use of explicit-folding.

aminroosta commented 3 years ago

Is there any willingness to merge a user configurable editor.foldEndPatttern: RegExp, similar to #9605 ? If so, i can open one.

aeschli commented 3 years ago

@aminroosta Maybe open a new issue to describe your suggestion in detail and discuss the proposal.

aminroosta commented 3 years ago

Update: created #111731 not to clog this thread.

michael-lumley commented 3 years ago

I was using vscode-explicit-folding as described above to successfully mitigate this issue, however the new release of that extension broke this functionality. Reverting the extension to 0.9.0 locally successfully resolved my issue.

GaddMaster commented 3 years ago

vscode-explicit-folding doesnt work for me :(

daiyam commented 3 years ago

Thanks to the help of @sleepylemur, I was able to fix vscode-explicit-folding for your case. It was very simple mistake (a misread)... The 0.9.2 is already available and is fixing the issue mentioned by @michael-lumley.

daiyam commented 3 years ago

It might be useful for some. I've added the setting editor.showFoldingLastLine to my custom VSCode (MrCode) It determines if the last line of the folding range is shown or not.

GaddMaster commented 3 years ago

@daiyam the download this link shows git 404 for me

daiyam commented 3 years ago

@GaddMaster oops It's fixed. Thx

Detnator-Neo commented 3 years ago

+1 for this feature, except also noting that this is actually two features (as summarized by @aeschli above, here and here):

  1. Folding up to one line instead of two (or hiding the last line as well as all the in between lines, instead of just the in between lines).
  2. Displaying the closing bracket/tag/whatever else (that currently otherwise shows on the second line), at the end of the folded up line with the graphical ellipsis in between.

For 1, I'll say +1 for vscode-explicit-folding. (Thanks @daiyam 😊). It does appear to work at least for me in JS so far, though it requires some configuration. For 2... well... aeschli is saying "no plans to implement this" and so, ok, so be it. Not that big a deal if at least 1 works.

I found this issue only after posting my own #121556. I've made comments on that issue to point it here. Posting this comment here in case it helps anyone else looking for this functionality - to mention the solution that's working for me so far (essentially confirming others above saying similar).

icetbr commented 3 years ago

vscode-explicit-folding is great. I think this does the trick

"[javascript]": {
    "explicitFolding.rules": [
            {
                "begin": "{/*",
                "end": "*/}"
            }
    ]
},

For my case, I want to turn this

it('some test 1', async () => {
...
});

it('some test 2', async () => {
...
});

into this

it('some test 1', async () => {})
it('some test 2', async () => {})

This is the code I use

"[javascript]": {
    "explicitFolding.rules": [
        {
            "separator": "it(",
        }
    ]
},

Then just add your favorite shortcut for folding the desired level (3 in my case, with a describe surrounding block)

Cyberavater commented 3 years ago

So, it's an unsolved issue since 2016, and here I opened a StackOverflow question, thinking that it's possible. https://stackoverflow.com/questions/68227011/finish-code-folding-in-one-line-and-keep-folding-sign-visible-all-the-time

es50678 commented 3 years ago

also having the same need. would like for blocks to collapse to a single line

Sanjay-lakhera commented 2 years ago

I have similar need to work on Powershell.

muthu322 commented 2 years ago

+1 for collapse of ending to same line and also show number of lines folded

jonlepage commented 2 years ago

visual studio have this feature, and true is give nice intuitive reading flow +1 for productivity because we save time for reading fast ! image

tresabhi commented 2 years ago

@djmisterjon how did you do this?

daiyam commented 2 years ago

@TresAbhi The screenshot is from Microsoft Visual Studio and not from Visual Studio Code...

stevemcmillen commented 2 years ago

I wish I could fold to a single line as suggested. Currently that last curly brace just takes up an extra line with no apparent benefit.

I did some testing and it seems that the code folding is strictly based on indentation. So there is one workaround which is no too ugly (though reduces code readability and clarity). Specifically put the end curly brace on same line as code as in:

function myfun() {
    this='that'
    console.log(that)
 }

Notice the single space before closing curly brace. This ends up folding to:

function myfun() { ...

I'd still really like to have normal indentation and have the curly brace show up in the folded variant as in:

function myfun() { ... }

Please consider raising priority of this ticket.

btw: due to face that code folding is based on indentation, you need to avoid invalid indentation (i.e. sometimes I add comment for code I intend to delete at start of line in order to make it obvious and easy to find - that totally breaks code folding.

Detnator-Neo commented 2 years ago

@stevemcmillen: Code folding can be based on one of two things: (1) indentation (2) language specific rules. It is set in the settings with editor.foldingStrategy, to either "indentation" or "auto" (auto = language specific rules).

Or in the settings UI:

Screen Shot 2022-02-27 at 3 36 18 PM

That, arguably makes this issue a little more complicated than you're suggesting, however I still agree that it should be a higher priority than it seems to be.

Every decent IDE does this. I for one don't understand why it's apparently so difficult and not a priority for VSCode -- well other than the fact that the VSCode devs are devs like us, and as with any dev project, we juggle priorities based on many factors, including demand, ease/difficulty of implementation, and others.

Presumably if this thread grows and more people are +1-ing it, then that's increased "demand" and will hopefully push it up.

Detnator-Neo commented 2 years ago

After my comment above, I feel a need to add the following:

There's a number of requests here, and maybe this should be broken out into a few separate "issues"...?
Recap:

  1. Option to consume last line of code block when folding (the original issue, but make it optional)
  2. When code block is folded, show number of consumed/hidden/folded lines (here and here)
  3. Ability to fold from the bottom as well as from the top (here)
  4. When unfolded code has opening bracket (or other code block symbol) on line after function (or whatever) name, then option to move that opening bracket to same line as function name when folding up. (here and here)
  5. Ability to fold code within a single line (here and here)

I think that's everything. If anyone thinks I missed anything please feel free to let me know.

To the VSCode devs (@joaomoreno ?, @aeschli ?): The explicit-folding extension gets some of this right -- without having access to internal code. You guys did a pretty good job with bracket pair colorization, improving on and acknowledging CoenraadS's work. Is there anything significant stopping you from doing the same with daiyam's?**.

And it's not like you have to do it all at once. Is there anything stopping you at least implementing natively what daiyam has done, and then adding to it bit by bit over time from there?

I understand your point about it being up to the language extension providers, so ok... what about the language extensions provided with VSCode/by Microsoft (JS, TS, C++, to name a few)? Please correct me if I've misunderstood the connection between VSCode and those extensions. If not you guys, who do we talk to about those particular languages?

( ** @daiyam I seem to recall you stating you'd be happy for them to do that, but can't find where I saw that. Please correct me if I'm wrong).

angelozerr commented 2 years ago

In our XML Language Server we decide to support this issue, but it provides this bug https://github.com/redhat-developer/vscode-xml/issues/690#issue-1189157838

In other words, if you have this XML:

<note>
    <foo>

        bla bla bla

    </foo>
</note>

and you fold the root note , and you select (without Ctrl+A) the <note> and you type space, it doesn't remove the whole content and teh XML becomes an invalid XML:

    <foo>

        bla bla bla

    </foo>
</note>

In Eclipse IDE, it solves the problem by opening the fold, when you try to update it.

FoldingWithVScode

FoldingWithEclipseIDE

daiyam commented 2 years ago

@angelozerr I think your issue is different from the original issue. You should open a new one.

angelozerr commented 2 years ago

@angelozerr I think your issue is different from the original issue. You should open a new one.

I would like to highlight a problem with the "Collapse ending brace to the same line" issue but if you think it's better to create a new issue, I can do that.

n-gist commented 2 years ago

Explicit Folding extension does the trick.

But while I was reading this thread.

As @aeschli mentioned:

Since a while we allow extensions to contribute folding range providers. If none is present we fall back to indentation based folding ranges.

I checked it in .js file for this piece of code (} is aligned to return for this test)

if (folding) {
    return;
    }

And it turned out that it folds differently based on editor.foldingStrategy. It folds to

if (folding) { ...
    }

For auto, and to

if (folding) { ...

For indentation

This could mean that, unlike in the past, now indentation-based stategy is not the only one VS Code has, and there is already some bracket-based or language-specific strategies under the hood, or indentation-based add-ons that weren't thought of as a possibility before. In any case, it looks like there is now room for the code that can solve the issue.

jonlepage commented 2 years ago

r

not work at all for me

mtbaqer commented 1 year ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here:

https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding

A demo of the extension in action:

https://user-images.githubusercontent.com/32401657/214104135-9c6d3984-cc80-437e-bbc1-bc5b9edaf4f3.mov

jonlepage commented 1 year ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here:

https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding

A demo of the extension in action:

Screen.Recording.2023-01-23.at.11.29.10.mov

My Hero !!

AlekseyPolishchuk commented 1 year ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here:

https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding

A demo of the extension in action:

Screen.Recording.2023-01-23.at.11.29.10.mov

Cool stuff! But I have one problem, folding stops working in html

mtbaqer commented 1 year ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here: https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding A demo of the extension in action: Screen.Recording.2023-01-23.at.11.29.10.mov

Cool stuff! But I have one problem, folding stops working in html

HTML, JSX and TSX support coming soon. You can see the full list here

AlekseyPolishchuk commented 1 year ago

HTML, JSX and TSX support coming soon. You can see the full list here

This is great! Thanks a lot. I'm looking forward to it.

tresabhi commented 1 year ago

@mtbaqer that's amazing! Could you also publish this on Open VSX?

Cyberavater commented 1 year ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here:

https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding

A demo of the extension in action:

Screen.Recording.2023-01-23.at.11.29.10.mov

Thanks man, I was waiting for it for a long time.

niczoom commented 1 year ago

Thanks for your hard work, the folding extension is fantastic!

realfresh commented 6 months ago

Hey everyone, I managed to make an extension that solves this problem along with other many requested folding-related issues. You can find it here:

https://marketplace.visualstudio.com/items?itemName=MohammadBaqer.better-folding

A demo of the extension in action:

Screen.Recording.2023-01-23.at.11.29.10.mov

Mate, you are a bloody legend!

Mr-D-Anderson commented 6 months ago

image It still shows the closing bracket for me. Didn't change any settings.

ScottBeeson commented 4 months ago

@mtbaqer 's extension "Better Folding" works great! Until you restart vscode. Then it reverts to the default behavior.

Immediately after installation: image

After restarting vscode: image