Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008 XAML code formatter

Tags:

In my team, the code style in .xaml files are currently not very consistent. We looked over the Visual Studio auto formatter to make it format the code into something we like. However, for one option we lack an extra condition. The options Im talking about are found under Tools -> Options -> Text Editor -> XAML -> Formatting.

We want each attribute on a separate line. Initially, we also wanted the first attribute on a new line (below the start of the tag) like so:

<MyFooBarButton
    Attrib1="a"
    Attrib2="b" />

But we quickly realized that running with those rules would make tags that only set one attribute look horrible, especially if the were nested:

<MyFooBarButton
    Attrib1="a" />
    <NestedFoo
        Attrib="b" />
        <NestedFoo2
            Attrib="c" />

So we tried positioning the first attribute on the same line as the start tag. While still keeping the two important rules (one attribute per line, vertically aligned). It looked decent in both cases:

<MyFooBarButton Attrib1="a"
                Attrib2="b" />

<MyFooBarButton Attrib1="a" />
    <NestedFoo Attrib="b" />
        <NestedFoo2 Attrib="c" />

The problem now is that Visual Studio seems to lack the condition to have both. I.e.:

  • If only one attribute is set: keep it on one line.
  • If more than one attribute is set: place first attribute on a new line, one attribute per line and align them vertically.

Can Visual Studio 2008 be made to do this? And if not, could the code formatter in ReSharper do it (might make it worth the cost)?

like image 434
Mizipzor Avatar asked Feb 10 '10 09:02

Mizipzor


2 Answers

Try http://xamlstyler.codeplex.com/, this xaml formatter won't break attributes into different lines, if an element has only 2 or less than 2 attributes, also, this formatter has the capability to sort the attributes of an element in a predefined rule, which make s the look of your markup much better.

like image 65
chris Avatar answered Nov 15 '22 04:11

chris


maybe these will help you:

http://weblogs.asp.net/fmarguerie/archive/2008/06/07/xaml-markup-formatting-in-visual-studio.aspx

http://geekswithblogs.net/lbugnion/archive/2007/09/29/Cleaning-up-XAML-code-and-improving-parsing-time-with-Blend.aspx#lbu_note2

http://dimebrain.com/2008/05/automating-read.html

(last two links are mentioned in text under first link)

EDIT: I checked options in ReSharper and I couldn't find any options just for XAML formatting (there are for c#, vb.net and XML)

EDIT2: I checked formatting options in VS and there is something that seems exactly what you need. In "Spacing" there is option "Position each attribute on a separate line" and it has checkbox titled "Position first attribute on the same line as start tag". In my opinion it should do what you need.

like image 23
grapkulec Avatar answered Nov 15 '22 06:11

grapkulec