Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[folding] configure initial collapse state #40338

Open
Ptiloup opened this issue Dec 16, 2017 · 31 comments
Open

[folding] configure initial collapse state #40338

Ptiloup opened this issue Dec 16, 2017 · 31 comments
Assignees
Labels
editor-folding Editor code folding issues feature-request Request for new features or functionality
Milestone

Comments

@Ptiloup
Copy link

Ptiloup commented Dec 16, 2017

Maybe I missed something ... I recently migrated my javascript project from netbeans to visual studio code ... in netbeans we use

//<editor-fold defaultstate="collapsed" desc="MySection">
...
//</editor-fold>

for defining regions ... I saw I could modify javascript-language-configuration.json to

	"folding": {
		"markers": {
			"start": "^\\s*//\\s*(?:(?:#?region\\b)|(?:<editor-fold\\b))",
			"end": "^\\s*//\\s*(?:(?:#?endregion\\b)|(?:</editor-fold>))"
		}
	}

SO my request includes 2 questions :

  1. Is it possible to add the editor fold tag to the javascript language configuration (Identical as the configuration for java)
  2. Could a defaultstate be added to regions, so when a file is opened regions will be either collapsed or expanded
@vscodebot vscodebot bot added editor editor-folding Editor code folding issues labels Dec 16, 2017
@aeschli aeschli changed the title Add defaultstate to regions? [folding] configure initial collapse state Dec 18, 2017
@aeschli aeschli added the feature-request Request for new features or functionality label Dec 18, 2017
@aeschli aeschli added this to the Backlog milestone Dec 18, 2017
@aeschli
Copy link
Contributor

aeschli commented Dec 18, 2017

Configuring additional folding markers in the settings is #36002.

@ghost
Copy link

ghost commented Sep 21, 2018

Sorry, but comment-folding wont help me - mostly all docs will be generated from source, more and more; why not completely hide them? I do not want to see any comment in my code when coding. It is never needed! It's only needed after a long break or mostly for others. So, why should we look every day, hour and minute on them? It cannot be a problem to hide them, if folding is already available in an IDE/Editor :|

But - can i actually fold them to only a single char, like »?

And whats about

// line
// line
// line
...
# line
# line
...
/*
...
...
*/

as they are in several documents? Copy & Paste would might break all rules every time i do that (without saying Copy & Paste is accurate - but have a look at all ressources for coders). Nobody would write this by self every day when learning a new framework or software.

@WhatFreshHellIsThis
Copy link

Yes! Regions should be folded by default when you open the editor (assuming that it's impossible to remember the last state of that region between session). They are kind of useless right now to me because I use regions to hide complexity but every time I open a file in c# the regions are all unfolded so it's actually a pointless feature for what I think it was intended to be.

@jabacchetta
Copy link

Would love to have import blocks automatically folded in files with a simple user setting.

@phmatray
Copy link

phmatray commented Feb 3, 2019

Would love to have import blocks automatically folded in files with a simple user setting.

Like in WebStorm.

@mathsalmi
Copy link
Contributor

Is there anything we can do to help get this out?

@qwabra
Copy link

qwabra commented May 31, 2020

just add the attribute to "#region"

e.g. #region collapsed

@MLefebvreICO
Copy link

just add the attribute to "#region"

e.g. #region collapsed

So all your regions will be named collapsed ? 🤔

Normally, we put a name after #region

@qwabra
Copy link

qwabra commented Jun 1, 2020

I mean, the #region tool is already built-in, we just need to extend it.
This technology is already built into "VSCode".

Syntax in this case is secondary, for example, camelCase can be used.

//#region Some name or description
// - ,-
//#regionEnd
//#regionCollapsed Some name or description
// - ,-
//#regionEnd

The current syntax #endregion is a weirder than the one I suggest ...

@nereusz
Copy link

nereusz commented Jun 1, 2020

I mean, the #region tool is already built-in, we just need to extend it.

I don't agree. It makes sense only for very custom regions.

As I mentioned in #84844 I would like to default collapse all inline SVGs. And it's crazy to refactor whole project to add such #region to every SVG in project. If I can fold regions basing on custom regex, then I can fold SVGs without any change in code, as SVG is pretty easy to spot using regex. Using region can be considered only as temporary substitution.

@quicksnap
Copy link

I also do not agree; the use case for auto-collapsed imports is pretty important to many, and wrapping them in an explicit #region would be silly since they are already an implicit foldable region.

@qwabra
Copy link

qwabra commented Jun 2, 2020

Initially, I came here in the search for settings as

  • ctrl + K ctrl + J - editor.unfoldAll
  • ctrl + K ctrl + 2 - editor.foldLevel2

by default.

All I could find was "felicio.vscode-fold" from the marketplace.

In a previous post, I want to say that instead of inventing a new tool and new XML like syntax, it is possible to use the existing "#region" functionality. This applies to the original "issue".

@fbricon
Copy link
Contributor

fbricon commented Jun 4, 2020

Here's an example of the payload returned by the Java language server:

[Trace - 9:52:58 AM] Received response 'textDocument/foldingRange - (822)' in 318ms.
Result: [
    {
        "startLine": 2,
        "endLine": 16,
        "kind": "imports"
    },
    {
        "startLine": 18,
        "endLine": 27
    },
    {
        "startLine": 21,
        "endLine": 23
    },
    {
        "startLine": 25,
        "endLine": 26
    }
]

Everything's already in place in folding.ts to fold blocks based on kind:

setCollapseStateForType(foldingModel, FoldingRangeKind.Comment.value, true);

Then you could expose new settings, so that we could set something like:

editor.folding.initialState: [
 {"imports":true},
 {"comment":true},
 {"region":false},
]

@quicksnap
Copy link

@fbricon Really awesome! Wish I had time to contribute such a setting. Thanks for digging. Hopefully some kind person implements it for TS eventually =)

@MLefebvreICO
Copy link

This could only work based on kind through ..

So if you want to collapsed some long functions in the long run or private functions by default, you wouldn't be able to.

@olemp
Copy link

olemp commented Nov 7, 2020

Would love this! 🚀

@aroman
Copy link

aroman commented Feb 18, 2021

any update on this one? for larger projects, it'd be great to offer the ability to collapse imports by default as many IDEs do

@DerZyklop
Copy link

@aroman

@mjbvz said »Folding of imports is now in vscode with d8e5520«.

TypeScript’s part on this is done, too.

As i under stand it: The feature is already implemented.

Only thing missing: A boolean in the vscode config to enable it.

@aeschli
Copy link
Contributor

aeschli commented Jul 19, 2021

Automatic folding of imports has been added through #128809.
That's for all languages that generate regions of type import (FoldingRangeKind.Imports)

@quicksnap
Copy link

YESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS!

Will this be part of the release notes for the next version of VSCode?

@Kingwl
Copy link
Contributor

Kingwl commented Jul 20, 2021

You can try vscode insiders which has nightly build.

@fbricon
Copy link
Contributor

fbricon commented Jul 20, 2021

@aeschli I tried latest insiders build (1.59.0-insider Commit: b805d2e, Date: 2021-07-20T05:12:13.059Z), But Java imports are still not folded by default, despite:

[Trace - 12:01:58 PM] Received response 'textDocument/foldingRange - (101)' in 218ms.
Result: [
    {
        "startLine": 0,
        "endLine": 10,
        "kind": "comment"
    },
    {
        "startLine": 13,
        "endLine": 17,
        "kind": "imports"
    },
    {
        "startLine": 19,
        "endLine": 31
    },
    {
        "startLine": 23,
        "endLine": 26
    },
    {
        "startLine": 28,
        "endLine": 30
    }
]

@aeschli
Copy link
Contributor

aeschli commented Jul 20, 2021

Yes, you need Version: 1.59.0-insider
Commit: b805d2e
Date: 2021-07-20T05:13:38.717Z

and you need to set "editor.foldingImportsByDefault": true

I tried it with Java and got it working. But yes, it's not yet tested well. For now, it will be off by default. Please file separate issues.

@fbricon
Copy link
Contributor

fbricon commented Jul 20, 2021

Thanks @aeschli , "editor.foldingImportsByDefault": true works!

@idelice
Copy link

idelice commented Sep 4, 2021

Yes, you need Version: 1.59.0-insider
Commit: b805d2e
Date: 2021-07-20T05:13:38.717Z

and you need to set "editor.foldingImportsByDefault": true

I tried it with Java and got it working. But yes, it's not yet tested well. For now, it will be off by default. Please file separate issues.

This works. Thanks!

@nashspence
Copy link

Nice to see the auto-folding of the imports when opening a file as a setting. Thanks much. Still would love to see a setting for auto-folding of regions when opening a file.

@starball5
Copy link

starball5 commented May 4, 2023

#84844 (Automatic folding/collapsing of specified HTML tags (ie. <svg>) on file load) got closed as a duplicate of this one, and I don't see that particular use-case being discussed much here. Please don't forget about that one. Either reopen that issue or make sure it's addressed here. I.e. How is this issue going to allow for specific XML/HTML/SVG elements to be collapsed by default without manual insertion of region folding markers?

Related on Stack Overflow: Is there a way to automatically fold specific XML/HTML/SVG tags in VS Code?

Related extension: https://github.com/moalamri/vscode-inline-fold#svg

@plourenco
Copy link

plourenco commented Jun 13, 2023

Shouldn't the automatic collapse of import regions also include multi-line imports? It seems vscode correctly folds imports when they're in a single line, however, if they span across multi-lines (which is enforced by, e.g. the popular JS formatter, prettier) it considers them as different regions:

With Folding Without Folding
imagem imagem

The expected result is a single folded block (as in Jetbrains).

EDIT: My comment has been resolved by setting the "Folding Strategy" to "auto".

@mctrafik
Copy link

mctrafik commented Oct 4, 2023

+1 to being able to fold regions.

My use-case is for an HTML file that has to be a single file.

 <!-- #region(collapsed) Image -->
  <img
    id="logo"
    src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAA
   ...
   ...
   ... many... many lines.
   ...
   ...
   " />
 <!-- #endregion -->

If only region(collapsed) worked. :(

@mrx23dot
Copy link

mrx23dot commented Nov 2, 2023

What about only folding first comment block, like long legal disclaimer?

Test cases from Eclipse:

/*  */  multiline
/**  */ multiline
// many lines like this
///  many lines like this

also in python and other languages.

Comment is auto detected as foldable, it just needs a trigger to fold the first, as I know VScode stores the fold state for each file, so it would only be one extra line of code to fold the first entry of that.

image

@moseleyi
Copy link

Being able to collapse SVG tags in HTML on load would be a time saver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-folding Editor code folding issues feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests