Resharper Clean-up Code - how to affect sorting of methods?
Asked Answered
P

3

49

I've got a customized clean-up and it's almost 'there'.

However, R# appears to want to sort the member methods, but at least it does not appear to be alphabetically.

Is there a way to force that sorting?

Preoccupy answered 2/10, 2009 at 12:31 Comment(2)
See alphabetizing-methods-in-visual-studioLipetsk
Added tag, "Rider" because the answers here also apply directly to Rider, which uses the same code formatting engine as ReSharper.Unesco
F
92

Customizing the layout can indeed be accomplished with Resharper. Go to:

Resharper->Options->Languages->C#->Formatting Style->Type Members Layout

ReSharper 2017

Resharper -> Options-> Code Editing -> C# -> File Layout -> Interface Implementations/All Other Members

and uncheck the "Use Default Patterns" option.

Now you'll want to edit the xml in the "Custom Patterns" box. I'd recommend copying it out to an editor that can properly hi-light the xml (notepad++ or visual studio should work fine).

Now, find the section near the bottom:

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

and change it to:

    <!--all other members-->
    <Entry>
      <Match>
        <Kind Is="method"/>
      </Match>
      <Sort>
        <Name/>
      </Sort>
    </Entry>

Now, make sure that your cleanup profile has "Reorder type members", and then right click on the filename in solution explorer and do "Cleanup code...". I've just tried this myself and it does order the methods alphabetically.

If you want to also sort by access type, you can add this under the <Sort> element:

<Access Order="public protected internal private" />

Here's an article to learn more.

Frontpage answered 5/10, 2009 at 17:2 Comment(5)
Thank you James for a very fine answer. As it turned out, it did not work at first until I realialized that a special handling is applied for interface implementations - which is what I was trying to sort. In my case the described edit should take place under the comment <!--interface implementations-->. Your answer was exactly right, though, for the problem as I phrased it in my question.Preoccupy
Excellent, clear answer. So many places online claiming that resharper does this automatically when it obviously doesn't.Jennettejenni
spot on @AndersJuul, in my case i wanted to always have my public methods first, whether they are interface implementations or not, and i simply removed the whole section for interface implementations.Stepson
how about resharper 9.1?Rimmer
This is no longer accurate in R# 9Killarney
R
26

Resharper->Options->Languages->C#->File Layout

Select "All other members" and select "Sort by name" (highlighted in bottom right of pic)

Options screen

Rimmer answered 30/7, 2015 at 21:16 Comment(8)
It appears ReSharper 9.x has broken this feature, but once it is fixed this should work.Rimmer
It makes me nervous when a tool I use for greater code quality has quality control issues.Rimmer
Update for R# 9.2: Resharper->Options->Code Editing->C#->File Layout->All Other Members -> Sort By Name Save or Save To Right Click in File, With Default Full Cleanup selected, Click RunApril
Yes, it looks like they fixed it!Rimmer
The profile that you use when doing Code Cleanup must have 'Apply file layout' set to 'On' in order for methods to be sorted.Eckhart
@Eckhart Where exactly do I set this?Loose
@problemofficer R# Options->Code Editing->Code Cleanup, click +Add, enter a profile name, under the top level C# item there should be an 'Apply file layout' option that is unchecked. Check that option and save your profile ( the only other thing I have checked is under the top level XAML item for 'Collapse empty tags' ). Now when you are in your C# file and you run Code Cleanup you can select the new profile you created and when you hit run the file should be sorted by the method name.Eckhart
I don't see any way to place private methods after public ones. Can you elaborate, please?Climax
C
0

In the XML file layout you have to add the element Entry.SortBy For example in All other members:

 <Entry DisplayName="All other members">
  <Entry.SortBy>
    <Name />
  </Entry.SortBy>
</Entry>

The problem with the 9.2 is that when the Entry.SortBy is missing from XML the selected value from the designer is not saved.

Cundiff answered 9/3, 2016 at 16:16 Comment(1)
This applies in JetBrains Rider, too. Checked in 2022.Unesco

© 2022 - 2024 — McMap. All rights reserved.