Add to Faves Add to MyAOL Add to Simpy Add to Delicious Add to Live Add to Digg Add to Newsvine Add to Reddit Add to Multiply Add to Blogmarks Add to Yahoo MyWeb Add to Slashdot Add to Mister Wong Add to Spurl Add to Furl Add to Link-a-Gogo Add to Yahoo Bookmarks Add to Twitter Add to Facebook Add to Diigo Add to Mixx Add to Segnalo Add to StumbleUpon Add to Ask Add to Backflip Add to Terchnorati Add to Google Bookmarks Add to MySpace
BlogProductsOpen SourceForumsAbout
Blog > October 2010 > C# Code Formatting Template for Resharper

C# Code Formatting Template for Resharper

If you are an avid user of Resharper, you may want to give this Type Members Layout template a try to improve the formatting of your code when using the Code Cleanup command:

<?xml version="1.0" encoding="utf-8" ?>

<!--
I. Overall

I.1 Each pattern can have <Match>....</Match> element. For the given type declaration, the pattern with the match, evaluated to 'true' with the largest weight, will be used 
I.2 Each pattern consists of the sequence of <Entry>...</Entry> elements. Type member declarations are distributed between entries
I.3 If pattern has RemoveAllRegions="true" attribute, then all regions will be cleared prior to reordering. Otherwise, only auto-generated regions will be cleared
I.4 The contents of each entry is sorted by given keys (First key is primary,  next key is secondary, etc). Then the declarations are grouped and en-regioned by given property

II. Available match operands

Each operand may have Weight="..." attribute. This weight will be added to the match weight if the operand is evaluated to 'true'.
The default weight is 1

II.1 Boolean functions:
II.1.1 <And>....</And>
II.1.2 <Or>....</Or>
II.1.3 <Not>....</Not>

II.2 Operands
II.2.1 <Kind Is="..."/>. Kinds are: class, struct, interface, enum, delegate, type, constructor, destructor, property, indexer, method, operator, field, constant, event, member
II.2.2 <Name Is="..." [IgnoreCase="true/false"] />. The 'Is' attribute contains regular expression
II.2.3 <HasAttribute CLRName="..." [Inherit="true/false"] />. The 'CLRName' attribute contains regular expression
II.2.4 <Access Is="..."/>. The 'Is' values are: public, protected, internal, protected internal, private
II.2.5 <Static/>
II.2.6 <Abstract/>
II.2.7 <Virtual/>
II.2.8 <Override/>
II.2.9 <Sealed/>
II.2.10 <Readonly/>
II.2.11 <ImplementsInterface CLRName="..."/>. The 'CLRName' attribute contains regular expression
II.2.12 <HandlesEvent />
-->

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">

  <!--Do not reorder COM interfaces or structs with StructLayout attribute-->
  <Pattern>
    <Match>
      <Or Weight="100">
        <And>
          <Kind Is="interface"/>
          <HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute"/>
        </And>
        <HasAttribute CLRName="System.Runtime.InteropServices.StructLayoutAttribute"/>
      </Or>
    </Match>
  </Pattern>

  <!--Special formatting of NUnit test fixture-->
  <Pattern RemoveAllRegions="true">
    <Match>
      <And Weight="100">
        <Kind Is="class"/>
        <HasAttribute CLRName="NUnit.Framework.TestFixtureAttribute" Inherit="true"/>
      </And>
    </Match>

    <!--Setup/Teardow-->
    <Entry>
      <Match>
        <And>
          <Kind Is="method"/>
          <Or>
            <HasAttribute CLRName="NUnit.Framework.SetUpAttribute" Inherit="true"/>
            <HasAttribute CLRName="NUnit.Framework.TearDownAttribute" Inherit="true"/>
            <HasAttribute CLRName="NUnit.Framework.FixtureSetUpAttribute" Inherit="true"/>
            <HasAttribute CLRName="NUnit.Framework.FixtureTearDownAttribute" Inherit="true"/>
          </Or>
        </And>
      </Match>
      <Group Region="Setup/Teardown"/>
    </Entry>

    <!--All other members-->
    <Entry/>

    <!--Test methods-->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="method"/>
          <HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false"/>
        </And>
      </Match>
      <Sort>
        <Name/>
      </Sort>
    </Entry>
  </Pattern>

  <!--Default pattern-->
  <Pattern RemoveAllRegions="true">

    <!--public delegate-->
    <Entry>
      <Match>
        <And Weight="100">
          <Access Is="public"/>
          <Kind Is="delegate"/>
        </And>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group Region="Delegates"/>
    </Entry>

    <!--public enum-->
    <Entry>
      <Match>
        <Kind Is="enum"/>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Name Region="Enums"/>
      </Group>
    </Entry>

    <!--constants-->
    <Entry>
      <Match>
        <Kind Is="constant"/>
      </Match>
      <Sort>
        <Kind Order="constant"/>
	<Access Order="public" />
        <Static/>
        <Readonly/>
        <Name/>
      </Sort>
      <Group>
        <Access Region="Constants"/>
      </Group>
    </Entry>

    <!--static fields-->
    <Entry>
      <Match>
        <And>
          <Static />
          <Kind Is="field"/>
        </And>
      </Match>
      <Sort>
        <Kind Order="constant"/>
	<Access Order="public" />
        <Static/>
        <Readonly/>
        <Name/>
      </Sort>
      <Group>
        <Access Region="Static Fields (${Access})"/>
      </Group>
    </Entry>

    <!--fields-->
    <Entry>
      <Match>
        <Kind Is="field"/>
      </Match>
      <Sort>
        <Kind Order="constant"/>
	<Access Order="public" />
        <Static/>
        <Readonly/>
        <Name/>
      </Sort>
      <Group>
        <Access Region="Fields (${Access})"/>
      </Group>
    </Entry>

    <!-- Events-->
    <Entry>
      <Match>
        <Kind Is="event"/>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Access Region="Events (${Access})"/>
      </Group>
    </Entry>

    <!--Constructors. Place static one first-->
    <Entry>
      <Match>
        <Kind Is="constructor"/>
      </Match>
      <Sort>
        <Static/>
      </Sort>
      <Group Region="Constructors"/>
    </Entry>

    <!--Deconstructor-->
    <Entry>
      <Match>
        <Kind Is="destructor"/>
      </Match>
      <Sort>
        <Static/>
      </Sort>
      <Group Region="Deconstructor"/>
    </Entry>

    <!--properties, indexers-->
    <Entry>
      <Match>
        <Or>
          <Kind Is="property"/>
          <Kind Is="indexer"/>
        </Or>
      </Match>
      <Sort>
        <Kind Order="property"/>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Access Region="Properties (${Access})"/>
      </Group>
    </Entry>

    <!--static properties, indexers-->
    <Entry>
      <Match>
	<And>
          <Static />
          <Or>
            <Kind Is="property"/>
            <Kind Is="indexer"/>
          </Or>
        </And>
      </Match>
      <Sort>
        <Kind Order="property"/>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Access Region="Static Properties (${Access})"/>
      </Group>
    </Entry>

    <!--interface implementations-->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="event"/>
          <ImplementsInterface/>
        </And>
      </Match>
      <Sort>
        <ImplementsInterface Immediate="true"/>
	<Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <ImplementsInterface Immediate="true" Region="${ImplementsInterface} Events"/>
      </Group>
    </Entry>

    <!--interface implementations-->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="member"/>
          <ImplementsInterface/>
          <Or>
            <Kind Is="property"/>
            <Kind Is="indexer"/>
          </Or>
        </And>
      </Match>
      <Sort>
        <ImplementsInterface Immediate="true"/>
        <Kind Order="property"/>
        <Name/>
      </Sort>
      <Group>
        <ImplementsInterface Immediate="true" Region="${ImplementsInterface} Properties"/>
      </Group>
    </Entry>


    <!--interface implementations-->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="member"/>
          <ImplementsInterface/>
        </And>
      </Match>
      <Sort>
        <ImplementsInterface Immediate="true"/>
	<Access Order="public" />
        <Kind Order="property"/>
        <Name/>
      </Sort>
      <Group>
        <ImplementsInterface Immediate="true" Region="${ImplementsInterface} Methods"/>
      </Group>
    </Entry>

    <Entry>
      <Match>
        <HandlesEvent />
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Access Region="Event Handlers (${Access})"/>
      </Group>
    </Entry>

    <!-- Console's Main Entry Point Static Method -->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="method"/>
          <Static />
          <Name Is="Main"/>
        </And>
      </Match>
      <Sort>
	<Access Order="public" />        
	<Name />
      </Sort>
      <Group>
        <Access Region="Main Static Methods (${Access})"/>
      </Group>
    </Entry>

    <!-- Static Methods -->
    <Entry>
      <Match>
        <And Weight="100">
          <Kind Is="method"/>
          <Static />
        </And>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name />
      </Sort>
      <Group>
        <Access Region="Static Methods (${Access})"/>
      </Group>
    </Entry>

    <!-- Abstract Methods -->
    <Entry>
      <Match>
        <And Weight="100">
	  <Kind Is="method"/>
          <Abstract/>
        </And>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group Region="Abstract Methods"/>
    </Entry>


    <!-- Methods -->
    <Entry>
      <Match>
        <Kind Is="method"/>
      </Match>
      <Sort>
        <Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Access Region="Methods (${Access})"/>
      </Group>
    </Entry>

    <!--all other members-->
    <Entry/>

    <!--nested types-->
    <Entry>
      <Match>
        <Kind Is="type"/>
      </Match>
      <Sort>
	<Access Order="public" />
        <Name/>
      </Sort>
      <Group>
        <Name Region="Nested type: ${Name}"/>
      </Group>
    </Entry>
  </Pattern>

</Patterns>

Enjoy!

Posted: 10/12/2010 10:20:09 AM by Mark Smeltzer | with 0 comments
Filed under: OpenSource, VS, .NET


Comments
Blog post currently doesn't have any comments.
Leave comment Subscribe



 Security code

Recent Comments

10/25/2010
Mark Smeltzer
Beej, I received similar feedback from Antoine. Based on...

10/25/2010
Beej
In my WPF 4 project I was getting error: "Cannot set a...

10/25/2010
Saheer
Hi Mark, Will this work in Silverlight? I think Freezable...

8/24/2010
Mark Smeltzer
When you say that you are getting errors, are they compilat...

8/24/2010
Antoine
Thanks for your quick reply! I renamed the class to Inte...

8/23/2010
Mark Smeltzer
I have not had any problems with that. Could you post your ...

8/23/2010
Antoine
Hi, Thanks a lot for this, I found it interesting and tr...

7/31/2010
Blog - Attaching Behaviors from the Expression Blend SDK using Styles (Part 2)
In the previous post in this series, I introduce the reader...

6/17/2010
Mark Smeltzer
Werner, The latest release, 0.9.7.4, should fix the prob...

5/31/2010
Mark Smeltzer
Werner, Are you using AnkhSVN? I have created a patch...

|< <  1   2   > >| Results 1 - 10 of 11

Syndication

RSS
Blog > October 2010 > C# Code Formatting Template for Resharper Login | Site map | Privacy | Copyright (C) 2010 Living Agile, Inc.
This web site uses Kentico CMS, the content management system for ASP.NET developers.