Delphi Handbooks Collection


Delphi XE Handbook


Delphi 2010 Handbook


August 12, 2010

Delphi XE Beta Blogging: Code Formatting

In this first "beta blog" I'll discuss an IDE feature that's not covered in the first beta video, improvements in code formatting.

In this first "beta blog" I'll discuss an IDE feature that's not covered in the first beta video, namely improvements in code formatting . I picked this specific topic not because it it the most important feature of the product, but because it is a tiny gem that I can easily discuss in a blog post. Covering something like the extensive subversion integration in a blog post is quite hard, I have to say.

In case you don't know, "Delphi beta blogs" are authorized blog posts by independent bloggers covering features of a beta version of Delphi. This means I was given a specific permission to post about this topic and I'm discussing a feature of a pre-release version. Notice also that the official RAD Studio XE Preview page is at http://www.embarcadero.com/rad-studio-xe-preview . Since this is a pre-release, there is no guarantee it will work the same in the final version (or will actually be in the product).

Source Code Formatting

Source code formatting was introduced in the Delphi 2010, but it had a couple of weaknesses that limited its usefulness. First, you could format the selected lines of a unit or a full unit, but not all of the units of a project at once. Second, if you customized the format settings there was no obvious way to copy the settings to another computer or share them with the other members of your team, to keep the source code format consistent.

New: Format Project

Delphi XE addresses both issues, adding some further formatting options. The first and more obvious change is that you can now use the Project | Format Project Sources menu item or the corresponding entry of the Project Manager local menu (on a project node). As the name implies, this applies the current source format settings to all of the units of the current project, after showing a confirmation dialog.

As you reformat a project you get some status information on a dialog which you can barely glance to (more or less like when you compile), but this is all added to a specific tab of the Messages pane, like in the following simple case:

      Starting format sources in project C:\Users\Marco\Documents\RAD Studio\Projects\Project5.dpro 
        Formatting C:\Users\Marco\Documents\RAD Studio\Projects\Project5.dpr ... 
        Formatting C:\Users\Marco\Documents\RAD Studio\Projects\Unit8.pas ... 
      Format project sources finished
    

If you format an individual unit, instead, the dialog below will let you disable further confirmations and won't output its status to the Messages pane:

 

Formatter Profiles

The Formatter | Profiles and Status page of the Options dialog box mentioned in the message box above contains the specified settings and also the option to disable the code formatter, removing the corresponding menu items.

Its main goal, though, is to let you manage the active settings of the formatter, picking one of the predefined options, updating it, and saving the current settings to a custom file. If you save the files in the Delphi user's “roaming” folder, like in my case:

C:\Users\Marco\AppData\Roaming\Embarcadero\BDS\8.0

and it's named with the pattern “Formatter_*.config” it will be picked by the IDE and listed in combo box at the top of the page, if not you can still locate it with a File Open Dialog. The top of the page is shown below:

Interestingly enough, a formatter profile is a rather plain XML file you might even edit or process directly. At the beginning there are the global settings, followed by the specific settings for each language, grouped by category using some comments:

That's all for this first post. Will find time for a few more in the coming days and weeks.

PS. Before you ask about the overall features of Delphi XE and the potentially missing features, we'll have to wait for the road map update that Product Manager Mike Rozlog said he's close to release in this thread. Until that information becomes public, there is nothing I can comment upon.





 

16 Comments

Delphi XE Beta Blogging Code Formatting 

 Do they use Code Formatter to their own codes? If
they don't, what the point of introducing something
that is buggy and useless!!!!

Cheers
Comment by apz28 on August 12, 15:41

Delphi XE Beta Blogging Code Formatting 

Dear apz28 (using a name would be better),

  Delphi source code is formatted along with those 
guidelines and I used it a lot over the last year. I 
think it is very flexible, and I like to use it to clean 
up my code. The added features make it much nicer. 

What do you mean by "buggy and useless" I don't 
understand (and won't accept a follow up post with such 
a generic and gratuitous attack).
Comment by Marco Cantu [http://www.marcocantu.com] on August 12, 15:45

Delphi XE Beta Blogging Code Formatting 

Agree Marco

It makes sense to wait until the last video is shown. I 
think this was originally announced for this release. 
More and more it pops up that lots of interesting detail 
outside the IDE are here.
Comment by Michael Thuma on August 12, 16:03

Delphi XE Beta Blogging Code Formatting 

The formatter is anything but useless even in D2010.
It is one of the more often features I use since it
was introduced.
Comment by Uwe Raabe [http://www.uweraabe.de] on August 12, 16:09

Delphi XE Beta Blogging Code Formatting 

As long as you can configure:

if ... then  Begin

end;

vs.

if .. then
  begin
  end;

Then I'm a happy camper with this feature!!!
Comment by Medbob on August 12, 16:32

Delphi XE Beta Blogging Code Formatting 

 I look forward to a video on it.

Personally, my long standing unanswered angst it the 
ability of a code formatter to make begin/end pairs 
manditory or optional.

ie :
  if condidition then
    true_event
  else 
    false_event;

 convert into
  
  if condition then
  begin
    true_event;
  end
  else
  begin
    false_event;
  end;

or the reverse case of :

  if condition then
  begin
    true_event;
  end
  else
  begin
    false_event1;
    false_event2;
    false_event3;
  end;

into

  if condition then
    true_event
  else
  begin
    false_event1;
    false_event2;
    false_event3;
  end;

Because I know for a fact that begin/end opinions 
vary greatly in more than just indenting, 
particularilly once you start getting nested logic. 
(I'm a huge fan of manditory begin/end pairs.  Once 
you start nesting, going any other way can make it 
very hard to read, esp. if the indenting is somewhat 
randomized)
Comment by Xepol on August 12, 17:09

Code Formatting and source control 

Hmm..  Would be great to clean up some code.. now, how
do one avoid that the new code formatting does not
f... up the source control. I wonder if there's a
diff/merge program that can be set up to ignore the
change in formatting (like uppercase/lowercase, white
spaces, tabs and new lines) 
Comment by Lasse [] on August 12, 17:34

Delphi XE Beta Blogging Code Formatting 

@Medbob,

I used to format my Delphi begins that way.

You can do this in Delphi 2010.  Set "Line break
before Begin in structured statement" to false in
formatting options.

Comment by Bruce McGee on August 12, 17:39

Delphi XE Beta Blogging Code Formatting 

/me hopes for great generics support in the code 
formatter; the Delphi 2010 version didn't do that well.

Currently, I'm mixing usage of the GExperts 
experimental builds (that have the DelForEx formatter) 
and the Delphi one.

Both have their drawbacks, but together it usually is 
OK.
It helps keeping your units short though.

--jeroen 
Comment by Jeroen Pluimers [http://wiert.wordpress.com] on August 12, 18:23

Delphi XE Beta Blogging Code Formatting 

And as long as I can fix the formatting that Medbob 
applied I'm happy too.  :-)
Comment by Wouter on August 12, 19:20

Delphi XE Beta Blogging Code Formatting 

For many years (since delphi 6) I've used the
"experimental" GExperts plugin that includes the
DelForExp formatter. It's so great I can't imagine
coding without it now. I type type type, and then
poof: perfectly formatted and indented code! I can
even save personalized capitalizations.

So this IDE feature from CodeGear is yet another thing
that I don't really need to buy an upgrade for, as my
(free) 3rd party plugins work just as well already.
Just like my existing SVN and diff plugins...
Comment by Brad Phipps on August 12, 21:08

Delphi XE Beta Blogging Code Formatting 

@Lasse: I have diff technology that I developed which 
can recognise when code changes do not affect the 
logic, e.g. it will see all of these as being the 
same:

  if (a = b)
   and (b=c) then begin

  if (A = B) and (B = C) then begin

  if (A = B) and (B = C) then
  begin

  if (A = B) and
     (B = C) then
  begin


Unfortunately the diff technology is built on top of a 
dictionary based tokeniser technology that I developed 
which makes extensive use of low-level ANSI string 
manipulation and characteristics in order to provide 
BLAZINGLY fast tokenising performance.

All of which was broken by Unicode and I haven't 
gotten around to updating it yet (no-one pays me to do 
this stuff).
Comment by Jolyon Smith [http://www.deltics.co.nz/blog] on August 12, 21:28

Delphi XE Beta Blogging Code Formatting 

I also welcome code formatting as a productivity booster. 

In M$ Visual Studio using C#, it saves me time. A bit
inflexible perhaps, but it works reliably and that's
more important.
Comment by Ken Knopfli on August 13, 11:34

Delphi XE Beta Blogging Code Formatting 

 Are you working on a new book? Delphi xe? An intraweb xi book 
would be great also with topics like jquery, son/JavaScript calling 
methods, skinning/templates, database apps, custom controls. 
There is very little intraweb info and xi looks like it's finally heading 
in the right direction.
Comment by Andrew Tierney [http://www.castlesoft.com.au] on August 13, 12:51

Delphi XE Beta Blogging Code Formatting 

Andrew,
  yes, I'm working on a book on XE, but I'm still not 
100% sure about the scope. For now IW looks out of the 
picture, but it depends on the time it will take me to 
write the core book.

I'll keep everyone updated on my blog.
Comment by Marco Cantu [http://www.marcocantu.com] on August 13, 13:36

Delphi XE Beta Blogging Code Formatting 

It would be more useful if it could easily use a 
shared profile stored on a remote server (with ACL to 
avoid non allowed modification), and be integrated 
with the VCS system to ensure code gets formatted 
before being committed. Also being able to format 
someone else code remotely would be a welcome 
feature... ;)
Comment by Luigi D. Sandon on August 14, 18:53


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.