tags:

views:

16

answers:

3

I use this pattern for Type Members Layout:

<?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 />
-->

<!-- Changed pattern in (more) accordance to StyleCop from: http://blog.mijalko.com/2009/01/resharper-stylecop-and-type-members.html -->

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">
  <!--Do not reorder COM interfaces and structs marked by StructLayout attribute-->
  <Pattern>
    <Match>
      <Or Weight="100">
        <And>
          <Kind Is="interface"/>
          <Or>
            <HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute"/>
            <HasAttribute CLRName="System.Runtime.InteropServices.ComImport"/>
          </Or>
        </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>
    </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>
    <!--Delegates-->
    <Entry>
      <Match>
        <And Weight="100">
          <Access Is="public"/>
          <Kind Is="delegate"/>
        </And>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Name/>
      </Sort>
    </Entry>
    <!--Fields and constants-->
    <Entry>
      <Match>
        <Or>
          <Kind Is="field"/>
          <Kind Is="constant"/>
        </Or>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Kind Order="constant"/>
        <Readonly/>
        <Static/>
        <Name/>
      </Sort>
    </Entry>
    <!--Enums-->
    <Entry>
      <Match>
        <Kind Is="enum"/>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Name/>
      </Sort>
    </Entry>
    <!--Constructors. Place static one first-->
    <Entry>
      <Match>
        <Kind Is="constructor"/>
      </Match>
      <Sort>
        <Static/>
        <Access Order="public internal protected-internal protected private"/>
      </Sort>
    </Entry>
    <!--Destructors. Place static one first-->
    <Entry>
      <Match>
        <Kind Is="destructor"/>
      </Match>
      <Sort>
        <Static/>
        <Access Order="public internal protected-internal protected private"/>
      </Sort>
    </Entry>
    <!-- Events -->
    <Entry>
      <Match>
        <Kind Is="event"/>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Name/>
      </Sort>
    </Entry>
    <!--Properties-->
    <Entry>
      <Match>
        <And>
          <Kind Is="property"/>
          <Not>
            <Kind Is="indexer"/>
          </Not>
        </And>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Static/>
        <Abstract/>
        <Virtual/>
        <Override/>
        <Name/>
      </Sort>
    </Entry>
    <!--Indexers-->
    <Entry>
      <Match>
        <Kind Is="indexer"/>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Static/>
        <Abstract/>
        <Virtual/>
        <Override/>
        <Name/>
      </Sort>
    </Entry>
    <!--Methods-->
    <Entry>
      <Match>
        <And>
          <Or>
            <Kind Is="method"/>
            <Kind Is="operator"/>
            <HandlesEvent/>
          </Or>
          <Not>
            <Kind Is="destructor"/>
          </Not>
        </And>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Static/>
        <Abstract/>
        <Virtual/>
        <Override/>
        <Name/>
      </Sort>
    </Entry>
    <!--all other members-->
    <Entry/>
    <!--nested types-->
    <Entry>
      <Match>
        <Kind Is="type"/>
      </Match>
      <Sort>
        <Access Order="public internal protected-internal protected private"/>
        <Static/>
        <Abstract/>
        <Virtual/>
        <Override/>
        <Name/>
      </Sort>
    </Entry>
  </Pattern>
</Patterns>

It works pretty well. But if I Clean Code on a solution I get a one thing which I think should be possible to solve using the pattern.

  • SA1201: All enums must be placed after all constructors.

How do I make all public enums come after the (public) constructors?

A: 

The easiest way is to install the StyleCopForResharper plugin which will redefine all of these rules for you..

http://stylecopforresharper.codeplex.com/releases

chrismilleruk
I've actually tried this. And it (using the ReSharper rules which it comes with) doesn't work, for me at least. I can't import them. I've reported it as a bug.
Binary255
A: 

NB: My recommendation is the StyleCopForResharper plugin as per my first answer

Otherwise, you can move the Enums section so it appears after Constructors and Destructors:

<!--Constructors. Place static one first-->
<Entry>
  <Match>
    <Kind Is="constructor"/>
  </Match>
  <Sort>
    <Static/>
    <Access Order="public internal protected-internal protected private"/>
  </Sort>
</Entry>
<!--Destructors. Place static one first-->
<Entry>
  <Match>
    <Kind Is="destructor"/>
  </Match>
  <Sort>
    <Static/>
    <Access Order="public internal protected-internal protected private"/>
  </Sort>
</Entry>
<!--Enums-->
<Entry>
  <Match>
    <Kind Is="enum"/>
  </Match>
  <Sort>
    <Access Order="public internal protected-internal protected private"/>
    <Name/>
  </Sort>
</Entry>

If you want to limit it to public enums only, you will need two rules, one with a modified match constraint:

<Match>
     <And>
         <Kind Is="enum"/>
         <Access Is="public"/>
     </And>
 </Match>
chrismilleruk
I've actually tried this. And it (using the ReSharper rules which it comes with) doesn't work, for me at least. I can't import them. I've reported it as a bug.
Binary255
A: 

The following works with Resharper 5.1.1727. First, have a public constructor match

<!--public Constructors -->
<Entry>
  <Match>
<And>
    <Kind Is="constructor"/>
      <Access Is="public"/>
</And>
  </Match>
  <Sort>
    <Static/>
  </Sort>
</Entry>

Then a public enum match.

<!--public enum-->
<Entry>
  <Match>
    <And Weight="100">
      <Access Is="public"/>
      <Kind Is="enum"/>
    </And>
  </Match>
  <Sort>
    <Name/>
  </Sort>
</Entry>    

Then a catch-all constructor match.

<Entry>
  <Match>
    <Kind Is="constructor"/>
  </Match>
  <Sort>
    <Static/>
  </Sort>
</Entry>

gets the following ordering

public class Child
{
    public Child(int id)
    {
    }

    public enum PublicParent
    {
        A = 1
    }

    private Child()
    {
    }

    private enum Sibling
    {
        B = 4
    }
}
Handcraftsman

related questions