AngryHacker
AngryHacker

Reputation: 61606

How to reorder type members with Resharper?

Typical scenario: a class that a lot of people have worked on. I'd like to sort methods, properties, etc... in alphabetical order.

I'd like to be able to do this within the region or globally in the class.

I see the feature in Resharper to do it, but it does not seem to do anything.

Upvotes: 62

Views: 42937

Answers (8)

TrueWill
TrueWill

Reputation: 25523

An alternative to consider is Regionerate. We use and like ReSharper, but Regionerate fits our needs for creating regions and sorting/rearranging members. And it's all customizable, of course.

UPDATE: We've started using ReSharper's Code Cleanup for this instead.

Upvotes: 5

Robin Bennett
Robin Bennett

Reputation: 3231

jgauffin's answer is close, but I found that (with R# 2017) to reorder Properties I needed to click the 'XAML' option in the header of the File Layout dialog and change

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
</Entry>

to

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
  <Entry.SortBy>
    <Name />
  </Entry.SortBy>
</Entry>

The 'Sort By' property was empty and read-only, which makes sense because it's only used for items with the same name (and all properties should be uniquely named)

Upvotes: 0

birdamongmen
birdamongmen

Reputation: 2090

If you are reordering parameters on specific methods, you can use the Refactor > Change Signature if your cursor is on a method name. I use the IntelliJ shortcuts, so for me, the command is Ctrl+Shift+R followed by Ctrl+F6.

Refactor context menu

After doing so, a dialog will pop up which allows you the reorder method parameters. It will even refactor any implementations of an interface.

Upvotes: 0

jgauffin
jgauffin

Reputation: 101150

Sorting is not activated by default. You can activate it by opening the resharper options and then go here:

enter image description here

Upvotes: 66

Doğan Etkin
Doğan Etkin

Reputation: 77

From Visual Studio menu;

ReSharper > Options > Environment > IntelliSense > Completion Behaviour > Sort Items(Alphabetically)

Upvotes: -2

Ian Griffiths
Ian Griffiths

Reputation: 14547

For the benefit of people, like me, who landed on this question through a web search but found that the detail of the question wasn't quite what they were expecting, you might like to know that you can move individual members up and down within the file by holding down Ctrl-Alt-Shift and then pressing the up or down arrows.

(Obviously that's not the automated arrangement by alphabetical order being asked for in the body of the question, but it was the answer I was hoping I would find for the question in the title.)

Upvotes: 48

Clint StLaurent
Clint StLaurent

Reputation: 1268

Two things: There is a known (but not heavily documented) condition where pre-compile conditionals (#if DEBUG for example) will stop type member reordering. http://youtrack.jetbrains.com/issue/RSRP-336643#tab=Comments In other words if you have #IF DEBUG then it won't reorder.

I also recently noticed that in ReSharper 8.0.1 (and probably earlier versions) that the button to revert the XML template back to DEFAULT WITH REGIONS doesn't really have any statements to include #REGION grouping. So I took a StyleCop friendly template that includes sorting and added #REGION-ing to each type member. If you select CUSTOM TEMPLATE then paste in this XML it should work.

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

<!--  Do not reorder COM interfaces  -->
<Pattern>
    <Match>
        <And Weight="100">
            <Kind Is="interface" />
            <HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute" />
        </And>
    </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 RemoveAllRegions="false">
    <!--  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>
        <Group Region="Delegates" />
    </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>
        <Group Region="Fields" />
    </Entry>

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

    <!--  Constructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="constructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Constructors" />
    </Entry>

    <!--  Destructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="destructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Destructors" />
    </Entry>


    <!--  Events  -->
    <Entry>
        <Match>
            <Kind Is="event" />
        </Match>

        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Events" />
    </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>
        <Group Region="Properties" />
    </Entry>

    <!--  Indexers  -->
    <Entry>
        <Match>
            <Kind Is="indexer" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Indexers" />
    </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>
        <Group Region="Methods" />
    </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>
        <Group Region="Nested Types" />
    </Entry>
</Pattern>

Upvotes: 3

Lucero
Lucero

Reputation: 60190

Use the "Cleanup Code" functionality.

The order of the members can be set up in the ReSharper options in Languages, C#, Type Members Layout. This is a well documented XML layout specification which ReSharper uses when reordering members.

Upvotes: 97

Related Questions