How to reorder type members with Resharper?

前端 未结 8 465
名媛妹妹
名媛妹妹 2021-02-01 12:48

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 wit

相关标签:
8条回答
  • 2021-02-01 12:52

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

    0 讨论(0)
  • 2021-02-01 12:52

    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.

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

    0 讨论(0)
  • 2021-02-01 12:58

    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.

    0 讨论(0)
  • 2021-02-01 13:04

    From Visual Studio menu;

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

    0 讨论(0)
  • 2021-02-01 13:05

    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>
    

    0 讨论(0)
  • 2021-02-01 13:11

    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.

    0 讨论(0)
提交回复
热议问题