Code folding is a very cool feature that helps to keep your workspace clean and your code targets easy to find. I have yet to put it to any significant use -but- now that I've played with the short-cut keys a bit, I expect it will be something I use a lot.
Using the Default mappings, the key combination of code folding is based on Ctrl-Shift-K + ? where the final key is A, C, E, G, M, N, O, P, R, T, or U ... yea, that is a lot of code folding variations. I'm sure we could create some clever mnemonic or acronym with all those letters, but not today. With a few exceptions the letter used will give some hint as to the action that will take place. For the odd ones out ... you'll have to create something that helps you remember it.
Having to hit four keys in some semblance of order might seem to be more involved than simply clicking the little icon to the left of the first line of the foldable section but once you get working with both hands at the keyboard using the mouse becomes less and less attractive - there just seems to be too much effort and distraction involved in reaching for it. In playing around with these features I've found that not all of the keys have to be hit at the same time. You can hold down Ctrl-Shift, hit the K and then hit the final key and the combination [in my tests] will still work. This actually works out pretty well for me, as a southpaw I tend to always use the Ctrl-Shift on the left side of the keyboard and use my right hand to peck out the other two keys. I have had a few oddities happen though, like macro recorder starting/stopping [Ctrl-Shift-R], GUID's being generated and inserted [Ctrl-Shift-G] ... so you need to pay attention to your results.
Starting from the least likely [for me] to be frequently used ...
The Master Switch:
- Ctrl-Shift-K+O Toggles between On and Off ... enabling and disabling Code Folding. Toggling it Off will expand all collapsed code whereas toggling it back On will make folding available. It doesn't remember previous state if you had some sections collapsed prior to turning it off, all will remain expanded.
Combinations - I'll Seldom Use:
- Ctrl-Shift-K+N Collapses Namespace/Unit. It isn't a toggle though, which would have been nice since it does remember state if you use the mouse to expand the collapsed block. I can't imagine me using this one [at all] it simply hides too much.
- Ctrl-Shift-K+R Collapses all Regions. I assumed from the name that it would collapse every collapsible block up to and including the Namespace/Unit. I never did manage to get this to work -but- that didn't concern me too much as it's not likely one I would use ... providing my assumption proves to be true. If you have been using this one, let me know what it actually does and I'll edit this section. Personally, I'm hoping [the defining element Regions, as found in the help file, isn't very clear] it's a combination of Ctrl-Shift-K+C and Ctrl-Shift-K+M that I get to later on.
- Ctrl-Shift-K+G Collapses down to primary Groups [Interface/Implementation] ... everything down to a simplified Namespace/Unit without collapsing the Namespace/Unit itself - typically three lines. This one too would have been nice as a toggle since it too remembers state if you use the mouse to expand the collapsed block. Again, not likely one I would use much.
- Ctrl-Shift-K+A Expands All blocks of code. Everything gets expanded. If you've used any of the above shot-cuts this is the only way, that I've found so far, to open any of them without using the mouse ... the problem being - it's all or nothing. If you're into extreme folding this might be one you use frequently.
- Ctrl-Shift-K+E [which collapses a block of code] and Ctrl-Shift-K+U [which expands a block of code] will likely both be easily forgotten ...
- because neither has a memorable key-function relationship and
- either is replaced with Ctrl-Shift-K+T which Toggles the current block open/closed.
Combinations - I'll Use Consistently:
- Ctrl-Shift-K+C Collapses all Classes. For either cleaning up at the end of work in a unit or just whenever things get too messy, invoking this key combination is likely to become common practice for me. This "tidy-up" function being equally applicable to it's partner Ctrl-Shift-K+M [found next].
- Ctrl-Shift-K+M Collapses all Methods.
- Ctrl-Shift-K+P Collapses nested Procedures. I haven't used a lot of nested procedures but being able to collapse the code within to get an overview of the entire workings will make this one memorable. I probably would have given this one the N for Nested and used Ctrl-Shift-K+U [for Unit] in place of the existing Ctrl-Shift-K+N [Namespace] -but- what we have is fine.
- Ctrl-Shift-K+T Toggles the current block between collapsed and expanded. This one is bound to be the one that gets used most.
Thanks for stopping by, -- Dave
Edit1: Code folding [as has been pointed out in the comments section] is not all roses for all users. It does require system resources to work and has been know to slow the IDE. There are a few articles on how to deal with this issue if you need to ... most boil down to The Master Switch: [see above] - turn it off. It has been well reported to CodeGear, hopefully there will be a fix and every-one will be able to enjoy this cool feature.
Edit2: As indicated in the comments below, it is also possible to set up your own code folding {$REGION}. The article New IDE features since Delphi 7 found on the DN at CodeGear shows how to do this. The Code Folding {$REGION} example is about half way down.
That was a great post!
ReplyDeleteI didn't use them at all, now, lol, Im forcing myself to, it is simply a great way to keep things visually organized.
Why thank you Esteban, I'm glad you found it interesting.
ReplyDeleteTurning the code folding off speeds up the IDE considerably if you have units bigger than the biolife demo. If you are a VM, it can be like an order of magnitude faster.
ReplyDeleteFrankly, I will not be using much of code folding until they fix the performance issues.
Xepol,
ReplyDeleteI did read about some issues with code folding and IDE speed ... both happened to be CG articles. Code folding is not the only feature involved in these IDE resource problems.
I understand that it can be frustrating for those who work on large projects where some of the coolest IDE features have to be disabled in order to increase performance.
I expect there are also many users where the feature does not affect IDE speed -or- they're willing to live with the hit to gain the benefit.
Sorry it isn't working out for you, it is such a cool feature.
It is a cool feature, and it IS very annoying that it has to be turned off.
ReplyDeleteI have recently had problems with the IDE throwing endless loops of exceptions from something related to "structure". I assumed it was the structure panel, but the exceptions would keep happening when I turned off the panel.
Since I have turned off the code folding, I have not seen it once, so I suspect that the code folding code (say that 5 times fast) has more problems than just performance.
I definitely enjoyed the code folding in Visual Studio, so I would hope that one day they can fix the problems. I suspect that the artificial node count limit is probably related somehow.
region collapsing is used for
ReplyDelete{$REGION 'REGION NAME'}
some code
{$ENDREGION}
tags. They can be very useful for building well structured code.
The first thing I do when open a unit with a lot of code is Ctr+Shift+K+C, Ctr+Shift+K+M :) It would be nice to make some shortcut which do these two steps as one.
ReplyDeletehttp://dn.codegear.com/article/34323/
ReplyDeletefor region example (search code folding from table)
Anonymous,
ReplyDeleteNice catch, I had actually forgotten to include the info on creating your own {$REGION} ... shouldn't have.
iSkomorokh,
ReplyDeleteYea that would be excellent.
Perhaps "Ctrl-Shift-K+R Collapses all Regions" could be made customizable to collapse the regions the user designates as "all regions" ... or anything at all [for that matter] on my keyboard :)