microsoft / vscode

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

[folding] language-aware folding #3422

Closed aeschli closed 6 years ago

aeschli commented 8 years ago

The current implementation of folding uses an indentation based folding strategy that is unaware of the language it works on. Knowledge on the underlying language allows us to solve the following requests:

MichaelDuppre commented 7 years ago

@acls - above you've mentioned that you don't think that Python-specific folding would be needed, however I'm struggling with the folding in Python when using multi-line strings that shouldn't be indented when printed out:

def print_text():
    print("""
This unindented multi-line text doesn't get collapsed when collapsing the 
function.        
    """)
pr-yemibedu commented 7 years ago

Hello, Is there an update about how we can get users some nice folding working? I do not mean discussion about if it is ready. I am looking for feedback about code presentation, ui adjustments, environment state and editor context. So can we have a gathered thought around:

acls commented 7 years ago

@MichaelD1 I would say multiline text falls under the multiline docstrings or other things not based on indentation category.

My point in my previous comments is that the broken indention-based folding #3353 should not be included in this language-aware folding issue.

3353 should also be removed because it has been fixed by pull request #26258.

Tuumke commented 7 years ago

Just receiver powershell 1.4.1, seems to recognize the region option now. `#region code

endregion`

Is foldable now (do need a tab infront of code)

wsmelton commented 7 years ago

@Tuumke regions are not supported like they should be in the current release of PS Extension or Code.

If you don't indent the region tags it will fold, but most folks will put region tags lined up with the other code...which is supported in ISE.

This currently works:

#region MyReg
    Get-Process -name NotHappening
#endregion

But this is not:

    #region MyReg
    Get-Process -name NotHappening
    #endregion
artisticcheese commented 7 years ago

It's not the region which is working but generally indentation is working. See below.

image

DarkLite1 commented 7 years ago

It would be really helpful to have this feature. Is there any news yet on its implementation?

pr-yemibedu commented 7 years ago

Hello, Can there be more discussion of what thoughts are being kicked around behind the scenes as to allow extensions or users to create language aware editor folding and markers? The interest is in both the implementation path and the final result.

For multi root support there a skype meeting (recorded) and mocks that were kicked around to get feedback and let the community know there is some progress being made. That would be nice here as well. Given how important this is to the primary use case of this software, lets keep it transparent (good or bad).

In the smallest aspect of maintaining discussion, talking about how the coding and concepts of the currently implemented indentation model would give people opportunities for constructive pull requests for new ideas. Thank you. Good day.

langhorst commented 7 years ago

Folding sections within a Markdown document is another example. I'd love to be able to show/hide various Markdown sections as you would in Emacs Org mode.

nilslindemann commented 7 years ago

why not change context?

cb inspire

gulshan commented 7 years ago

Proposal for a new style folding in #26757 is not exactly "language aware". But still may be relevant to this issue.

aeschli commented 7 years ago

You are correct, region folding does not necessarily require language awareness. Therefore I opened #12146 to a separate issue. Please add your thumbs up there so that this feature gets more priority.

Fenweldryn commented 7 years ago

interesting extension that does part of what #region functionality should do: bookmarks maybe it can evolve to what we want here?

DTW-DanWard commented 7 years ago

VS Code is an amazing and awesome free editor. But I will pay $$$ right now to you or the charity of your choice if you please please fix the folding for PowerShell regions. Seriously. It's killing me.

DTW-DanWard commented 7 years ago

THANK YOU for fixing the code folding for PowerShell regions in the 1.17 release! And I am a man of my word: I just donated to the Puerto Rico hurricane Maria relief effort.

DustinCampbell commented 7 years ago

I hope language extensions will get the ability to add custom folding at some point. C# has several other places that we'd like to fold in order to reach parity with VS which require knowledge of the parse tree. For example, "#if", "#else", "#endif", using directives, etc.

rkeithhill commented 7 years ago

@DustinCampbell And don't forget about multiline verbatim strings. PowerShell has a similar feature (here strings) that mess up code folding.

cadayton commented 7 years ago

Tested on Windows 10 and it is working great for me. It is nice that indentation isn't required in between the #region and $endregion pairing in PowerShell. Thanks.

DarkLite1 commented 7 years ago

Totally agree with @rkeithhill . When VS Code can work properly with here strings in PowerShell it will be my default editor. Until that's implemented I still have to use the Powershell ISE. Otherwise some code might break and that's not acceptable in a production environment.

But again, thanks for the addition of region aware folding. It's a first step in getting an amazing editor for Powershell.

antonpegov commented 7 years ago

When I can fold my functions and html tags like I do in VS 2015? Or it's a kind of taboo. Just tell me.

LeThiHyVong commented 7 years ago

Well, the folding in C/C++ still not working correctly. b68e2070-c121-11e6-89b8-80e807c949f4

aeschli commented 7 years ago

No taboo at all, there were just always other issues & features with higher pressures to implement. But we are getting there. Please keep adding 👍 to the description, that's the way we prioritize.

pr-yemibedu commented 7 years ago

Hello, @LeThiHyVong the current folding that was added is based on a loose pair of single markers defined by a regular expression in the language configuration file. It is now mostly just used with language that either directly have a notion of a code region (like C# or VB) or for some other languages that have been using a comment marked version that has similar support in other editors (JavaScript, C, F#). There needs to be more discussion on how to extend to capture more markers and how to name and group them. Thank you. Good day.

KillyMXI commented 7 years ago

What have to be done to make it work with CSS? (To have regions in css file without changing the depth of styles themselves and messing with linter.)

pr-yemibedu commented 7 years ago

Hello, @KillyMXI Remember to look at #12146 for editor region specific folding. It just happens to be that a few languages (like C# and VB.Net) have it as a syntactic element. Most are using a line level regex lookup. By common practice, the scans are against language comments for possible folding regions. It does not know anything special about a language.

I left another comment in the other issue with a straightforward but non user friendly way to add this for yourself. Thank you. Good day.

mvanderlee commented 7 years ago

Love the new region folding. Could you add support for 'bat' files?

As a workaround I added this to my C:\Program Files\Microsoft VS Code\resources\app\extensions\bat\language-configuration.json file

"folding": {
    "markers": {
        "start": "^\\s*::#region",
        "end": "^\\s*::#endregion"
    }
}
aeschli commented 7 years ago

@MichielVanderlee Please make a PR....

pr-yemibedu commented 7 years ago

Hello, @MichielVanderlee please also include the "REM" comment as an or (using "|" in regex) option. Since it is the official way. REM versus :: versus %= =% is worth looking at. Thank you. Good day.

DarkLite1 commented 7 years ago

It's still not working for the opening comment <# and the closing comment #> in PowerShell code.

Example:

function Get-WindowsVersion {  
<#    
.SYNOPSIS    
    List Windows Version from computer.  

.DESCRIPTION  
    List Windows Version from computer. 

.PARAMETER ComputerName 
    Name of server to list Windows Version from remote computer.
#>  
    Write-Output 'Success'
}

Is it possible to add folding for this too?

pr-yemibedu commented 7 years ago

Hello, @DarkLite1 there is still no news about folding because of language syntax nor semantics. So a problem with folding the contents of a single block comment are that it can span multiple lines and some people style it differently like inline starting or trailing marker. Were you looking to fold all block comments, regardless its content?

If you were mentioning about the language agnostic editor folding, then editor folding is in another issue.

DarkLite1 commented 7 years ago

@pr-yemibedu Yes, I was looking to fold everything between <# and #> regardless of its contents. At least, this is what is needed for PowerShell. I don't know about other languages.

pr-yemibedu commented 7 years ago

I would initially consider folding all comments as language aware fold logic. But I also think that this is something very doable with the logic already specified. There would a need to have some style constraints to be good for folding:

That would allow for the code to just attempt to grab the existing markers comments.blockComment array and push the first pair onto an array of patterns:

function escapeRegExp(str) {
  return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}

let comments = { "blockComment": ["(*", "*)"] }

let commentStart = escapeRegExp(comments.blockComment[0])
let commentEnd = escapeRegExp(comments.blockComment[1])
let commentPair = `(^\\s*${commentStart})|(?:${commentEnd}\\s*$)`
let commentPattern = new RegExp(commentPair)
patterns.push(commentPattern)

So the question is when can we get to the current logic to support lists of pattern pairs @aeschli? Hope this does not feel like spam, just think it is doable but involves changes you might want to do yourself instead of by PR.

Halkcyon commented 6 years ago

@pr-yemibedu Your comment doesn't really apply to a lot of cases.

the lead marker can not have non whitespace in front of it

What about inline comment blocks? In PowerShell, for example, it's not uncommon to have a comment based help block indented with the function block.

the tail marker can not have non whitespace after it

In shell languages, redirection of string blocks can occur (again, PowerShell, here-strings)

the lead and tail marker are on different lines

Back to point one, where there can be a open/close comment glyph on the same line

this would only apply to languages that defined block comments and blockComment[0] <> blockComment[1]

Makes sense, but the points above are still valid.

pr-yemibedu commented 6 years ago

Hello, All of the things I mentioned were solely for comments. They were about languages that support block comments using an true syntax construct. It was not about languages that ignore artifacts that people can use as comments.

I just want to make sure we are on the same page of what my proposal was entailing. It was a way to get some of the comments to fold that were not already being handled by another extension.

Please continue to comment to help flesh out use cases and understandings. Also , if you can include small code snippets that demonstrate how you would like things to look would help. Thank you. Good day.

peon501 commented 6 years ago

Do we have progress on #37494 ?

sdir commented 6 years ago

Outside the function of folding failure snipaste_20171204_173034

Ptiloup commented 6 years ago

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="getControleur">
...
//</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 that the existant region markers can be reused ... however it seems Visual studio code does not have a defaultstate ? It would be an advantage to be able to reuse also defaultstate in region folding

aeschli commented 6 years ago

@Ptiloup Please file a new issue. Maybe you are looking for something like #36002.

DarkLite1 commented 6 years ago

Any update on the folding of here strings @" My here string "@ or block comments <# Block comment #> for PowerShell editing? I don't want to be rude, but this question is on the table since February 2016. It would really be nice to see some progress here...

peon501 commented 6 years ago

This place is graveyard.

DanTup commented 6 years ago

This has come up for Dart:

https://github.com/Dart-Code/Dart-Code/issues/583

Multi-line strings tend to go back to column 0 (no indenting) so the folding just stops there. My language server can provide me all of the folding region data, I just don't have a way of providing it to Code to use.

LeThiHyVong commented 6 years ago

Atom's code folding based on indentation. But the next release (1.25) new parsing system Tree-sitter is added. I tried roughly and it's ok on small files. May VSCode follow? https://github.com/atom/atom/pull/16299

Update: Seem like VSCode team plan to work on their own https://code.visualstudio.com/updates/v1_21#_folding

rcjsuen commented 6 years ago

I saw that a new experimental API was introduced in VS Code 1.21. Thank you for looking into this.

Are there any plans to give control as to which line is shown after the content is folded? For example, in VS Code, the source files look like this at the top:

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

Current folding behaviour:

/*--------------------------------------------------------------------------------------------- ...

Proposed folding behaviour:

...  *  Copyright (c) Microsoft Corporation. All rights reserved. ...
export interface FoldingRange {

    /**
     * The start line number
     */
    startLine: number;

    /**
     * The end line number
     */
    endLine: number;

    /**
     * The line to show when the region is folded. If this is
     * unspecified, then startLine will be shown.
     */
    visibleLine?: number;

    /**
     * The actual color value for this folding range.
     */
    type?: FoldingRangeType | string;
}
unional commented 6 years ago

@rcjsuen your request is similar to https://github.com/Microsoft/vscode/issues/19912

rcjsuen commented 6 years ago

Thank you for the info, @unional.

I also found #31966.

pr-yemibedu commented 6 years ago

Hello, There would need to be a really strong formatter to shrink the excessive parts of the comments to get at such useful information. That may not be the exact case now, but I wanted to brain dump it. Most typical, lines with at least one letter or number is a high candidate for keeping to favor that useful preview style. It would handle most scenarios. Thank you. Good day.

SunOfHomeBoy commented 6 years ago

like this

22

code fold logic have a question

aeschli commented 6 years ago

@SunOfHomeBoy Please file a separate issue with the VSCode version and the sample code.

beachdweller commented 6 years ago

Dear all,

I think I have not seen this example here yet. (Please let me know if other post discussed this already)

folding

pr-yemibedu commented 6 years ago

Hello, @autodrive , It not a new notion to the old problem of only having indentation as the folding signal. There is now experimental syntax support possible in the mainline. It needs extension author help for the vast languages out there. Only HTML, JSON, Markdown, CSS, LESS and SCSS come available out of the box. So hopefully there can be a resolution to many of these scenarios issues. Thank you. Good day.