How do I change the way the RAD Studio (2010 and later) IDE formats my code?
Asked Answered
I

1

21

How do I configure the RAD Studio IDE to format my code with begin on either the same or next line when I use the Format Source option?

One of the most commonly debated preferences is the position of begin - on the same line or a line of its own. How do you set Delphi to format your source with begin on one line or the other in Delphi XE2? This is referring to when you go to Edit > Format Source

Intine answered 2/6, 2012 at 1:11 Comment(6)
+1. Nice question and answer combination - upvote for each of them.Jodijodie
PS - Any input on this setting in prior versions of RAD Studio?Intine
There is no "prior version setting". The code formatter was added in RAD Studio XE, IIRC (may have been 2010, though). Before that, you had to use a third-party solution like the JEDI code formatter or the one in GExperts - see Delphi code formatter for info.Jodijodie
Just checked - it was added in 2010, and it's in the same exact place and has (AFAICT) option settings.Jodijodie
@Warren - except the info applies to D2010 as well, and not only to Delphi but to C++ Builder (which is why I removed Delphi in the first place). :-)Jodijodie
The question talks about Begin though. So if you want it to be general, then please make the question also clearly about C++ and Delphi formatting. The answer is about Delphi begin/end formatting.Osteopath
I
20

I am answering this question Q&A style.

  1. Go to Tools > Options
  2. Find the Formatter section in the tree on the left
  3. Inside that node, select Delphi > Line Breaks
  4. Locate the section on the right labeled Insert line breaks for Begin and Single instructions
  5. Inside here, find the setting labeled Line breaks before Begin in control statements
  6. Switch this setting between Yes or No depending on your preference.

Switch between Yes and No to change how begin is positioned

Yes will produce this:

if (Foo = Bar) then
begin

end;

No will produce this:

if (Foo = Bar) then begin

end;

By default, this setting is set to Yes which means when you use Format Source, it will always bring begin to the next line.

There are many settings in this section which can define how Format Source will treat your code. This option is available in at least RAD Studio 2010 and above. Not sure of which exact editions.

Note: Ctrl + D is a keyboard shortcut to format your source, a quicker alternative.

Intine answered 2/6, 2012 at 1:11 Comment(2)
Also note that Ctrl-D acts on the current selection only if there is any.Cesspool
@UweRaabe Took me 4 years to respond to your comment, but Yes, that is true - and by default, there's a confirmation dialog which comes up regarding this, which has a "Don't show me again" option (of some sort, I'm not looking at it now). Once you tell it that, you never see that dialog again, it just proceeds with formatting. I don't like disabling dialogs like that, because in case I accidentally hit the wrong keyboard shortcut, I don't want my source (selected or all) to change from how I wrote it. Not arguing, just an an observation :-)Intine

© 2022 - 2024 — McMap. All rights reserved.