views:

419

answers:

3

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?

+6  A: 

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.

François
@François: I was typing the same answer, when yours was posted.
Cesar Romero
Castalia does no code folding AFAIR.
dummzeuch
+5  A: 

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.

Rob Kennedy
Good point. For demonstration purposes though on the merging point, I probably should have called the latter call FinallyWrapUpTHATBit. ;-)Thanks for the reply.
Jamo
A: 

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.

skamradt