microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.5k stars 28.65k 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

tht13 commented 8 years ago

This would not work for languages which do not use punctuation to signify code blocks (i.e python)

Code Folding at the moment uses indentation and so will close everything up to the next line on the same indentation level as the folding line. The implementation would have to change to fold the closing bracket as this would cause issues in some languages

joaomoreno commented 8 years ago

:+1:

joaomoreno commented 8 years ago

ping @alexandrudima

pom421 commented 8 years ago

👍 I like the Atom's way. We save a line and the ending curly brace is not virtually on the same line than the beginning one (unlike Bracket).

danjoa commented 7 years ago

👍 definitely, would help so much

g5codyswartz commented 7 years ago

I like how Jetbrains does theirs which matches "bracket" image

I also love being able to fold not only from the first bracket but also the last bracket.

They also have essentially peeking for brackets when they're not on screen so you can see what they belong to. I used to comment what ending brackets belonged to, then I stopped due to the IDE, now I'm feeling lost again w/o it. image

I also really like how notepad++ puts the trailing line as mentioned in Alternative Designs #2

Spown commented 7 years ago

I'm for the current model, but since people still think there is something to be corrected I think a preference is in order, something like editor.foldingConsumeLastLine: bool

renestalder commented 6 years ago

Also would prefer a way to make it one-line. I often use the folding in CSS and liked the way the Jetbrains IDEs put the collapsed selectors on one line.

gabrielmaximo commented 6 years ago

Can you solve it? I have this same problem uncomfortable, I read the other post about it, but I did not see anything finished that can help me, someone there knows how to solve ?? it really is very uncomfortable working with multiple collapsed lines, because with this line more than "}" it is difficult and confusing to work.

manyfew commented 6 years ago

I like the 'Brackets' example, looks clean

jeasonstudio commented 6 years ago

image

It is a good choice to show the number of folded code lines like vim, and make the folded code more visible.

faustinoaq commented 6 years ago

Hi people!

I think would be nice to see vscode folding with info about amount of lines folded like Netbeans:

screenshot_20170620_215124

BTW: Thank you for this awesome code editor! :smile:

Related issue here: https://github.com/Microsoft/vscode/issues/29155

ArthaTi commented 6 years ago

I would really like to see this implemented. But, in the meantime, is there an extension that does that?

garpunkal commented 6 years ago

I would to see this implemented soon. It's a right pain. Visual Studio working perfectly for collapse and expanding structure.

RueDeRennes commented 6 years ago

+1

aeschli commented 6 years ago

54351 suggests to always show an opening bracket on the first line, even if it's on the next line in the original code

NSExceptional commented 5 years ago

+1 for many of the ideas here! To recap:

To add to this, the folded braces { ... } should change state when you hover over it, like a button, and should unfold when clicked.

Also, I would really not like it to look like the Visual Studio folding style. I really want to see those braces surrounding the ellipses.

haykam821 commented 5 years ago

for large blocks only

@NSExceptional, how much would you consider that to be? 8 lines? 15? 100+? Or maybe a config option?

NSExceptional commented 5 years ago

10 I guess. Or configurable from 10, sure.

antonpegov commented 5 years ago

Hey DEVS! Don't you tired of merging similar requests from your users? I have been waiting for this incredibly difficult in the implementation feature for two years now, I've got anything, but not what I really want. If this is something fundamental, not relevant to your philosophy, then just let us know.

slinkardbrandon commented 5 years ago

Any update on this?

eeasoftware commented 5 years ago

Why did this all the sudden change. Until a week ago, the collapse '-' was on the declaration line of my function. Now it is on the line of the opening '{' (which for me is under the declaration) and it no longer hides the closing bracket.

How can I change it back. I liked it the other way. A main reason I switched to VSCode was the clean collapsing of functions on long files. Now the same collapse takes up 3 times more space.

Is there a setting I can use to change it back? I can't stand the new change.

aeschli commented 5 years ago

@eeasoftware Please file a detailed issue with a sample code (please name the language (JavaScript. TypeScript, C#...)

eeasoftware commented 5 years ago

I am writing in PHP

Prior to the update methods, if/etc statements in a class collapsed like the following: Uncollapsed: [-] public function TestMethod() { Return 'Test'; }

[-] if (1==1) { $i = 10; }

Collapsed: [+] public function TestMethod() ... [+] if (1==1) ...

After the update methods, if/etc statements in a class collapsed like this: Uncollapsed: [-] public function TestMethod() { Return 'Test'; }

[-] if (1==1) { $i = 10; }

Collapsed: [+] public function TestMethod() { ... }

[+] if (1==1) { ... }

The blocks now take up much more room collapsed because the brackets don't collapse.

bittnkr commented 5 years ago

This would not work for languages which do not use punctuation to signify code blocks (i.e python)

Code Folding at the moment uses indentation and so will close everything up to the next line on the same indentation level as the folding line. The implementation would have to change to fold the closing bracket as this would cause issues in some languages

What about a simple approach like this:

if( /[};]+/.test(line) ) // if line has only braces or semicolons, 
   hide_line()

I'm sure this would not affect the collapsing of any other language than those which use curly braces and semicolons ;-}

aeschli commented 5 years ago

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

@eeasoftware We don't have a built-in folding provider for PHP, so what ypu ahve seen is a folding provder from a PHP extension. Maybe you disabled or installed the extension that added the feature or the exension has changed its behaviour. In that case, file an issue against the PHP extension that you use.

NSExceptional commented 5 years ago

@aeschli I am experiencing @eeasoftware's issue as well. Code folding was same-line and now it's back to two lines. Shouldn't matter what language it is... But for the record, I'm using C.

aeschli commented 5 years ago

@NSExceptional See my comment https://github.com/Microsoft/vscode/issues/3352#issuecomment-456027450. For C, out of the box, without any extensions installed, we use indentation based folding: A folding region starts when a line has a smaller indent than one or more following lines, and ends when there is a line with the same or smaller indent. There haven't been any changes on that code for quite a long time. If you see otherwise please file a new issue with a code sample.

eeasoftware commented 5 years ago

@aeschli @bittnkr I appreciate your comments back to me, but what I'm trying to tell you is that I experienced the change when VSCode updated, not the plugin.

It worked just fine as I have described (same line) before the update and now it does not. So I understand what you are saying, but it is not what I am experiencing.

I will check with the extension provider (PHPTools, Devsense) again, but there wasn't a change until VSCode updated.

cepm-nate commented 5 years ago

Does anyone see folding occur with a closing bracket into the same line, in any language? ... When the closing bracket is NOT indented?

Devs, any update on this? Since VSCode is smart enough to highlight the opening bracket, one would think that it should be able to fold inclusively.

Tiedye commented 5 years ago

50840 is required to implement this

eeasoftware commented 5 years ago

@Tiedye

What people seem to be forgetting is:


This was already working (See my post from January 7th)


Everyone keeps saying, "we have no plans to implement", or "it can't be done in its current form, major rewrite", etc.

Folding to the same line was already being done. It stopped working like it was. Something changed. It's not a new feature I'm requesting, it's putting back an old feature.

I'm just trying to figure out why it changed and if it's possible to put it back.

Tiedye commented 5 years ago

@eeasoftware When they removed this feature as it was (if it ever existed), it was because they replaced if with an extensible api (so people could make extensions to support code folding in any language). This meant that JavaScript and other c like languages had to use their new API... which doesn't support having the closing brace on the same line.

There is no way they're going to undo that and break support for all the new folding features they've added since then, so the current solution is to improve the folding API by supporting folding on a character by character basis instead of just on a line by line basis.

At this time, it is a new feature you are requesting, and it depends on that API being implemented.

(I too would really love it to get implemented because this bracket thing has been basically my most significant gripe with VSCode for a year)

aeschli commented 5 years ago

@Tiedye @eeasoftware Sorry, we never supported folding ranges inside lines, and there hasn't been any features removed. We allowed extensions to add folding range providers, but you can always switch back to the original indentation based folding strategy using the settings.

The change of behaviour you have seen was caused by the change in the PHP extension. Extensions update automatically in the background without interactions. A restart of VSCode would cause the latest version of the extension to be used.

Please let's stop discussing this 'change of behaviour' issue here, there hasn't been any changes recently. This issue is about allowing the indentation based folding strategy to also fold the closing brackets and that brackets can be rendered at the end of the folded line. { .... }. The first request can already be implemented by a extension

eeasoftware commented 5 years ago

@Tiedye Thank you for a good explanation. It's all I am really looking for. @aeschli I'm just a fellow dev trying to figure out what happened. No one could give an explanation, and I've spent hours searching, not knowing where to look. All I would have required is a point in the right direction.

dnecklesportfolio commented 5 years ago

Hi!! Im working in html, can we the end tag on the same line, please. In the second screenshot I posted how chrome views the same html. This would help so much! image

image

irvnriir commented 5 years ago

Just please do not make this for an indentation based folding (it would be an obviously horrible idea, but just in case)

antonpegov commented 5 years ago

Plus one more year waiting for this feature. I don't need anything, I'm happy with current VSCode version. Just add normal folding. Please.

AgamlaRage commented 5 years ago

Is that possible to create an extension that could do that ?

Could we have more informations about what needs to be done to achieve this feature please ?

Thx

aeschli commented 5 years ago

As discussed above, folding range provider contributed by extensions can control which lines are folded, say whether the closing bracket is part of that or not. You can write your own folding provider for you favourite language, or if you already use an extension for that language, create an issue against that extension to add an setting to support both.

To have the editor render the bracket after the ... image requires a new feature in the editor: https://github.com/microsoft/vscode/issues/70794 There are currently no plans to work on this.

AgamlaRage commented 5 years ago

@aeschli Thx for your answer.

For those who are just looking for One line code folding (without closing brace on the same line). I found an extension, where you can provide Regexp to create Code folding range by languages:

Explicit folding

With this kind of configuration

// vscode settings.json
"folding": {
    "*": {
        "beginRegex": "\\{\\{\\{",
        "endRegex": "\\}\\}\\}"
    },
    "typescript": {
        "beginRegex": "#region",
        "endRegex": "#endregion"
    },
    "javascriptreact": [
        {
            "beginRegex": "\\{/\\*",
            "endRegex": "\\*/\\}"
        },
        {
            "begin": "<",
            "end": "/>"
        }
    ]
}
waldyrious commented 5 years ago

@AgamlaRage Thanks for the pointer, that's quite helpful!

For reference, here's the simplest configuration that implements one-line folding on braces, {}:

"folding": {
    "*": {
        "begin": "{",
        "end": "}"
    },
}

Multiple blocks can be provided to include other symbols, e.g. []. See @Soaku's comment below for an example (I had one here but it was incorrect).

ArthaTi commented 5 years ago

@waldyrious Shouldn't the second match { ] too? According to the extension info the valid solution would be:

"folding": {
    "*": [
        {
            "begin": "{",
            "end": "}",
        },
        {
            "begin": "[",
            "end": "]",
        }
    ],
},

By the way, the extension doesn't seem to work for me.

AgamlaRage commented 5 years ago

@waldyrious , @Soaku I think if we want to continue discuss about it we should go to the Explicit Folding Repository because like @aeschli says, this issue (#3352) is more talking about the closing brace on the same line.

@Soaku For now, the extension still have an issue The indentation breaks if beginRegexp and endRegexp are on the same line, but for language like CSS, SCSS, if your code is linted, the extension works 100%.

Now it's up to us to help the Explicit Folding owner to make it works everywhere.

He helped me find the SCSS regexp yesterday, and it's working like a charm.

    "scss": [
      {
        "beginRegex": " \\{\\s*$",
        "endRegex": "^\\s*\\}"
      },
    ],
antonpegov commented 5 years ago

This extension broke my folding completely... Actually I fell like vscode devs sent us somevere fare away...

antonpegov commented 4 years ago

Is there is a life on Mars?

PassTheMayo commented 4 years ago

This has been open for over 3 years now, and no progress on getting this implemented. It's sad that such an amazing editor doesn't have this feature...

Edit: It has also been in the backlog queue for 3 years, and it's one of the oldest to be there...

Immortalyzy commented 4 years ago

very much expecting this feature

tuledev commented 4 years ago

Looking for this feature also. Thank VSCode team for a great editor.

Ryacinse commented 4 years ago

Looking for this feature also.