Tutorial :Collapsing If-Then-Else statements in code editor (Delphi 2007)



Question:

Every once in a while I'm editing some long pair of if-then-else statements (or worse, nested if-then-else statements) , like, say, this:

  if A < B then     begin      DoSomething;       DoSomethingElse;      {...and more statements going on and on and on...}      FinallyWrapUpThisBit;    end     else     begin      DoThis;      DoThat;      {...and more statements going on and on and on...}      FinallyWrapUpThisBit;    end;  

...and I find myself wanting to "collapse" the first begin-end pair, to bring up the lower "else" part (usually because I'm referring to something above the if-then statemnent. Maybe so it would just say "begin..." and have [+} sign to the left of it to expand it out again.

I've explored the "fold" functions in the IDE, but none of the commands seem to do this. It seems like my CodeRush for my old D6 did this, but I could be imagining things. (I have a very active imagination...).

Do any of the IDE plug-ins like Castalia (or some other one) do this?


Solution:1

With plain Delphi out of the box, you would have to surround your begin...end with

  {$region 'begin...end'}    ....     {$endregion}  

which can be done through a code template...

I remember Castalia for the nice colored visualization of code blocks (begin..end) but I don't remember if it was foldable.


Solution:2

Use the refactoring tools to move the conditional branches' code into separate functions. Then you won't need to fold anything. You might also find that you can merge code that's common to the two branches, such as that call to FinallyWrapUpThisBit.


Solution:3

Another big helper here would be CNPack. It is a wizard which installs into Delphi and will colorize your begin/end pairs, making it MUCH easier to follow the code. It doesn't exactly do code folding, for that you need to use the {$REGION} {$ENDREGION} tags.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »