helix-editor / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
32.53k stars 2.4k forks source link

Add Code Folding #1840

Open waterlubber opened 2 years ago

waterlubber commented 2 years ago

As far as I can tell, Helix does not (yet) support code folding. Syntax or LSP/Treesitter based code folding is probably my most missed feature from nvim,

It would probably be best to add it in a similar manner to nvim's approach, although initial patches might only support syntax-based folding if it's simpler.

There's a chance I might have missed this feature - I couldn't find it in the editor, documentation, or as an issue, so I figured I'd make the feature request. Apologies if I have missed something, though!

i3elj commented 2 years ago

Just for the matter of visualization:

foldingInNeovim

I think this is a pretty nice UI (it's from neovim)

naveedpash commented 1 year ago

any progress on this?

EriKWDev commented 1 year ago

I find myself really missing this. In rust when you have a bunch of "pub mod foo { / ... / }" it can be very handy to be able to fold them away when not working on them.

crispinb commented 1 year ago

I find emacs-style narrow (aka hoist) more useful than the (more common) fold. Folding complects navigation with focusing, but navigation is already covered by lsp. Narrowing is extremely useful for focusing singly on a chunk of code to be worked on. It's a shame narrowing isn't more widely implemented - perhaps helix has an opportunity here.

Zoybean commented 1 year ago

I've never encountered narrowing before, but from googling it just now, it seems like it might serve a different use case than folding, and maybe they could both be added?

crispinb commented 1 year ago

Sure, they're not incompatible. Emacs has both.

My perspective (no more than that!) is that folding doesn't offer much given its relative UX complexity (multiple operations and keybindings). It does 2-3 things: offers a code overview + helps with navigation, and allows you to focus on one section of code by folding others.

To me, navigation is already taken care of by many existing helix/lsp features. Focus is managed much more simply by narrowing - it's just a single and simple pair of operations (narrow/expand), which fit like a glove with a central existing concept (the selection - to which you can narrow).

That leaves potentially the code overview as folding's one unique use. Seems like a sledgehammer to crack a nut to me, and probably handled better by other (and again simpler) means, but obviously it's pretty subjective. Folding is far more widely implemented than is hoisting, so I guess people must find it useful.

Zoybean commented 1 year ago

Yeah, I admit that from my experience with folding in vim, it does feel clunky even when it's working properly, so I see some value in investigating other possibilities for a code overview feature aside from traditional folding.

SoraTenshi commented 1 year ago

Just for the matter of visualization:

foldingInNeovim

I think this is a pretty nice UI (it's from neovim)

this is only possible once virtual text is implemented.

I believe to implement this (to have it done in a imo nice way) would be to wait once #417 is merged.

emilyyyylime commented 1 year ago

I don't think it makes sense to drop folds in favour of narrowing, but I'd be glad to have narrowing in core. I can imagine it in it's own submenu with equivalents for all tree-sitter movement commands (A-[iopn]), and exiting and entering narrow. If it does get in though, I would like to see it implemented as in https://github.com/Malabarba/fancy-narrow, though at narrowing may be deservant of its own issue.

Do note I have 0 experience with narrowing and I'm saying all of this only after a tiny bit of research into what it even is and why its useful. I think done like in https://github.com/Malabarba/fancy-narrow it would get a very real usecase of helping with focusing on code and let the other commands I've suggested above operate much more smoothly, unlike when just creating a virtual buffer.

creikey commented 1 year ago

Would really love folding so I can navigate big files reading a succinct overview, then have the ability to drill down into parts of code I need to edit/read.

matklad commented 1 year ago

Important subfeature of code folding is "fold function bodies by default" config. Ie, folding some elements by default, based on the "kind" of element, rather than just level.

Eloitor commented 1 year ago

For python it would be useful to be able to fold docstrings

pashadia commented 1 year ago

For python it would be useful to be able to fold docstrings

I would do exactly the opposite: Fold everything BUT the docstrings

frondeus commented 1 year ago

So, #411 is closed now, which I guess was a blocker.

EzekielEnns commented 1 year ago

Lets GOOO!!!

How would this be implemented in codebase?

SoraTenshi commented 1 year ago

How would this be implemented in codebase?

i would assume that we would also create tree-sitter queries for this, and then basically manipulate the text rendering in a way to skip all the lines that are within that context. Also; rendering the "context core information" such as function name, etc. as virtual text.

pascalkuthe commented 1 year ago

A conceal API was excluded from #5420. I have a prototype for that locally but it needs more work. I am doing some small refactors of the vritual text API anyway atm and improved the conceal implementation there but I probably won't include the conceal API with the PR for that that as it's a bit more complex and I want to avoid doing large PRs

In particular we must be careful that vertical/horitzontal movement moves past the concealed text (vrtical movement already does) while any other movement/search etc. that would place a selection inside the concealed text expands the concealed text. This requires centrelazing the way we set selctions which is something we want to do anyway.

AndyJado commented 1 year ago

💡 an edge case usage description

suppose I have a text file with 800 lines, and the one and only 7th line owes 1 million columns.

should it be folded even before I open the file? will that help ?

MrGibus commented 1 year ago

we must be careful that vertical/horitzontal movement moves past the concealed text (vrtical movement already does) while any other movement/search etc.

I think this is a very important aspect. The vim implementation in vscode had a very annoying flaw in that moving vertically past a fold opened it, as well as several other issues related to it, which probably still plague it to this day.

prescientmoon commented 1 year ago

Are there plans to also support marker based folding? That's a pretty important part of my workflow.

87113 commented 1 year ago

Hi there, another use case for folding (vs narrowing) is for markdown.

I like to be able to fold titles or list for example. Like that I can see the file's structure without all the text around. It's a bit like an inside TOC.

stonecharioteer commented 1 year ago

It would also be great to fold merge conflict sections like VS Code does so that you can compare them too across splits. Folding is a very amazing editor feature!

connortsui20 commented 1 year ago

Is there any plan to work on this? There seems to be a lot of additional features on top of basic code folding that would be interesting to have, but there is no basic implementation yet.

From #5798 it seems like a conceal API will not be fully implemented soon, but I'm not sure it would be absolutely necessary for basic code folding.

The way VSCode does folding is simply adding dots to the end of the first line, darkening the line, and removing the folded lines from sight:

image image

Maybe this would be a good first step?

pascalkuthe commented 1 year ago

Code folding and conceal need the same lower level API being able to hide part of the document and replace it with some virtual text.

That API doesn't exist yet and is not easy to implement. When it exists implementing code folding (and conceal) will not be that hard. The hardest parts are already done in #5420 but further work is needed and I don't have the time and motivation to finish that right now

ghost commented 1 year ago

Usually I only want a list of functions in the current displayed buffer. I use vsplit-new and then :pipe grep.... A quicker way to achieve this would be useful.

eliliam commented 1 year ago

A basic version of folding would be amazing, even if it is behind a flag that is disabled by default until it's fully complete. Is there a rough time frame on when work will start on this?

Edit: Why did I get downvote bombed? Is it because I asked about a rough timeline?

AE86Trueno commented 11 months ago

Reaching here when trying to find how to fold in Helix. Fold is one of the Vim function I use all the time. In my use case, I have a text file (currently already 25k lines of text). I break it into section and sub-sections and require Fold for me to navigate.

theunixer commented 11 months ago

Really waiting for these feature.

Eloitor commented 10 months ago

This would be useful for saturn notebook support: https://github.com/mrzv/saturn

camilodlt commented 9 months ago

Hello, I've been using Helix for a week and it's amazing. Thanks! . What about folding with the "region" command.

It's done on vscode and some lsps recognize them apparently:

# Unfolded code

# region set seeds
random.seed(1)
# endregion

# Folded : 

# region set seeds ...

Of course, different languages have different markers for comments vs code regions

prescientmoon commented 9 months ago

@camilodlt In vim I use # {{{ and # }}} comments for folding (this is referred to as "marker based folding" I think), so the nicest option would be having configurable markers (so both our comments would fold as intended)

tgross35 commented 8 months ago

As far as the logic for how to fold: nvim uses folds.scm, with an @fold capture. There are already some of these in the codebase, e.g. https://github.com/helix-editor/helix/blob/7e389b67c24dfe4466112c988b240c807e7e2414/runtime/queries/latex/folds.scm

See also: discussion on standardization of these files https://github.com/nvim-treesitter/nvim-treesitter/issues/3944

luccahuguet commented 7 months ago

Code folding and conceal need the same lower level API being able to hide part of the document and replace it with some virtual text.

That API doesn't exist yet and is not easy to implement. When it exists implementing code folding (and conceal) will not be that hard. The hardest parts are already done in #5420 but further work is needed and I don't have the time and motivation to finish that right now

Hi @pascalkuthe, is there an issue to track this lower level API that you mention, and if not, can I create one?

dawkrish commented 7 months ago

so are there any updates on code-folding ?

saifulapm commented 4 months ago

Any update on code folding ?

ortense commented 4 months ago

I really like this feature and use a lot in other editors but in helix I "indirectly replace" it by using more the symbol picker.

I think that a improvement in this view mixing the idea of folding and symbol picker could create an amazing workflow.

image

pashadia commented 3 months ago

I really like this feature and use a lot in other editors but in helix I "indirectly replace" it by using more the symbol picker.

While I agree that using a symbol picker is preferable when possible, when working with poorly maintained codebases, you sometimes encounter functions spanning hundreds of lines, and several distinct flows.