title: Formatting type declarations that include many [Attributes] for readability
body: <p>Lately I found myself relying on custom [Attributes] allot. I'm on a quest for creating RTTI and Generics based variants of the VCL controls I most often use; My intention is to reduce the amount of boilerplate code to a minimum, and I find RTTI, Anonymous methods and Attributes to be <em>awesome</em>.</p>
  <p>I've got an unusual problem. Since I end up using lots of [Attributes], I need a <strong>readable</strong> way to <em>format</em> this code. Might sound like an insignificant problem but I'm very obsessive with my formatting and indenting. The problem with attributes is that they're not formatted in a special way by the IDE. In a block of multiple field declarations, inter-wind with [Attribute] declarations, you'd have a hard time guessing what's a type, what's an attribute, what attribute applies to what field. Here's a minimal example:</p>
  <pre><code>type
    [OneAttribute('SomeParam', 7), AnOtherAttribute]
    TSomething = class
    public
      [OneAttribute('SomeParam', 7), AnOtherAttribute]
      FieldName: string;
      [OneAttribute('SomeParam', 7), AnOtherAttribute]
      OtherField: Integer;
      [OneAttribute('SomeParam', 7), AnOtherAttribute]
      function Sum(a,b:Integer):Currency;
      [OneAttribute('SomeParam', 7), AnOtherAttribute]
      property Name:string read FieldName write FieldName;
    end;
  </code></pre>
  <p>The way I formated those [Attributes] in the example above is the way most often seen in <code>C#</code> code; Unfortunately I have seen no example of Delphi code using attributes, except my own. This works slightly better for <code>C#</code> because you're less likely to apply attributes to actual fields and more likely to apply them to <code>properties</code> or <code>functions</code>; Since in <code>C#</code> the function implementation immediately follows it's declaration (the same for properties) you don't get the same kind of block as with Delphi; functions and property declarations are naturally separated so glancing over the code is not hindered by the use of attributes.</p>
  <p>I tried the obvious source for delphi-related &quot;style&quot; information. The code formater in Delphi 2010 doesn't understand property declarations at all! In the above example, if I remove the <code>public</code> keyword and run the built-in code formater then the first line of Attributes declarations is moved one line up behind the <code>class</code> keyword as if it's declaring an ancestor class.</p>
  <p>I grepped around the Delphi 2010 source code. Either my grep-fu is failing me badly or there's absolutely no use of attributes in the whole of the Delphi 2010 sources. Just to be clear, after trying to grep for the actual uses of attributes (difficult because square brackets are used allot and they rarely mean it's an attribute declaration) I started grepping for <code>TCustomAttribute</code>: there's no descendant of <code>TCustomAttribute</code> anywhere, so obviously there's no use of attributes.</p>
  <p>Finaly to the question:</p>
  <h1>How do you format [Attributes] for readability?</h1>
  <p>My latest solution looks like this:</p>
  <pre><code>type
   [OneAttribute('SomeParam', 7), AnOtherAttribute]
    TSomething = class
    public
     [OneAttribute('SomeParam', 7), AnOtherAttribute]
      FieldName: string;
     [OneAttribute('SomeParam', 7), AnOtherAttribute]
      OtherField: Integer;
     [OneAttribute('SomeParam', 7), AnOtherAttribute]
      function Sum(a,b:Integer):Currency;
     [OneAttribute('SomeParam', 7), AnOtherAttribute]
      property Name:string read FieldName write FieldName;
    end;
  </code></pre>
  <p>I'm normally using two-column indent; For attributes I'm thinking of moving the attribute declaration one-to-the-left; There's always space for that since my types, fields, methods and properties always have a minimum of 2 columns right-indent (for types) or 4 for class members. Left-indenting like <em>seems</em> to blend nicely with existing declarations that do not use attributes, and I want the attributes to &quot;stand out&quot;, so left indent is better then right indent.</p>
  <p>What do you think? Have you got better ideas?</p>