Sublime Forum

Code Folding

#1

I coded a little plugin this weekend that lets you do code folding. The way it works is you select some text and hit the “fold” shortcut, and the text gets grabbed from the buffer and replaced with a fold marker. If you select some or all of the fold marker and hit “unfold”, the fold marker gets replaced with the text in the buffer. It’s actually kind of cool, if I do say so myself.
It also works nice with saving: whenever you save, the fold markers all get replaced with the original text, and then get folded back again after the save. The file on the disk will always have the full text.
Anyway, check it out here:
github.com/adzenith/Sublime-plu … folding.py

The one known bug is that whenever you save a view, it seems to scroll down about 1 line—it turns out that view.show(view.visible_region(),False) moves the view.

Also, if you’re doing some hacking at home, keep in mind that if you ever save code_folding.py from within Sublime Text, the plugin will be reloaded and it will forget all the text that it’s grabbed from buffers, making it impossible to unfold text.

As always, feedback and comments are appreciated.

0 Likes

#2

What about using something like sqlite or pickle for persistency?

0 Likes

#3

you don’t need to pickle externally

a reloaded python module keeps old variables, it just happens to execute the module again… which usually rewrites all of the variables

I had an issue in sublime lint - a thread was started every time the module was reloaded, which caused things to get pretty slow after a while. now I detect reload using a variable and it’s way nicer to work on.
check this out: github.com/lunixbochs/sublimeli … in.py#L142

0 Likes

#4

[quote=“sublimator”]Creative but seems like a pretty fragile way to implement folding.

Aren’t you supposed to be able to find / replace/ copy etc in folded areas?[/quote]

Yeah, but I can’t think of a way to make that work without reimplementing them in the code folding plugin.

0 Likes

#5

It sounds like this plugin actually removes the folded lines and replaces them when you save. Is this true?

If so it’d be great if the Sublime developer implemented even a basic, programmable folding feature like Hide Lines that could be scripted. Then plugin developers could just specify what lines they want folded.

0 Likes

#6

That is true. The editor currently does not support hiding text.

0 Likes

#7

Rather than just line folding, it would be handy to be able to do index folding. That way you can fold everything between braces, for example. Folding is the only feature I miss from other editors.

0 Likes

#8

That sounds like a really good idea, although I can’t think of many uses for it (if that makes any sense). One example I can think of is as a cheat sheet for a helper class/file with the method signatures easily accessible (then again, regular code folding would do this too for the most part).

What uses did you have in mind for these “composable views”, sublimator?

0 Likes

#9

You’re a passionate man!

Yes, some good ideas here.

0 Likes

#10

github.com/adzenith/Sublime-plu … folding.py

no longer working, is there any chance anyone can host it again? need code folding that saves on exit ;p

0 Likes

#11

[quote=“dannoffs”]https://github.com/adzenith/Sublime-plugins/raw/master/code_folding.py

no longer working, is there any chance anyone can host it again? need code folding that saves on exit ;p[/quote]

It’s git. Nothing is ever lost :wink:
raw.github.com/adzenith/Sublime … folding.py

0 Likes