Suppress sorting of members with a comment (File Layout)


Is there a resharper comment command to suppress sorting of fileds/properties/methods? I have setup a custom "File Layout" and it works wonderfully, 99% ot the time. There are those 1% of times, when I want a particular section of the file to not be auto sorted. It could be that I want the fields or properties or methods left alone and not sorted alphabetically. Or at times, I want part of the file to be left alone completely. I mean file layout/sorting wise, not other code inspections.
So does resharper have a comment to enable/disable the code layout and sorting?

Also, where can I find a list of all the resharper's enable/disable comment commands? I would really want to know all the resharper's options, that I can micro-manage in code. Yes, it's "cluttering the code", but when you get used to it, it's the best way to manage things, as it stays with the code.

Thanks in advance for any help.

Comment actions Permalink

You can define a region that maps to a "generated code" region, via the Code Inspection | Generated Code options page. Then you could add "#region do not reorder/#endregion" around the area of code that you don't want to reorder. If it's a class, interface or struct that you don't want to reorder, you could use the "NoReorder" attribute in the JetBrains annotations nuget package to mark a class as not being reordered - you do need to explicitly add this as a rule in your file layouts, though. Alternatively, define your own attribute and add that to your code and patterns.

Comment actions Permalink

Thank you for an answer.

1. The first way, using the region didn't work for me, sadly. I would very much preffer using this solution. Here are the screens:

I tried to use a region Designer generated code, which did not work (left - before code cleanup, right - after). As you can see, the properties have been sorted alphabetically:

Than I tried adding my own region:

Did not work either:

2. About the second way of doing this, would you, please, elaborate what should I do? Should I install this nuget package into every project, in which I want to be able to use [NoReorder] attribute on a class?
Also should I install this?

Thanks in advance for any help.

Comment actions Permalink

I have just installed the JetBrains ReSharper Annotations 10.0.0 nuget package into my project and used the [NoReorder] attribute and:

it doesn't work!

The properties and methods are still being sorted alphabetically on silent cleanup, which I use constantly. What the hell? ReSharper is completely ignoring everything - both ways do not work for me - do I need to change something in the settings or what?

Help, please. This is driving me up the wall.

Comment actions Permalink

Looks like you've uncovered a couple of bugs in ReSharper, unfortunately.

If the region is *inside* the class, it works as expected, and the properties are not reordered. Like this:

public class Class1
  #region Designer generated code

  public string FooF { get; set; }
  public string FooD { get; set; }
  public string FooH { get; set; }
  public string FooE { get; set; }
  public string FooB { get; set; }
  public string FooA { get; set; }
  public string FooI { get; set; }
  public string FooG { get; set; }
  public string FooC { get; set; }


If the region is outside the class definition, the properties are reordered (but any classes inside the region aren't!). Here's the bug to track, vote and comment: RSRP-451938.

You're referencing the NoReorder attribute correctly by adding a NuGet reference to each project (see this blog post for lots of details about how to reference them). You don't need the community annotations - that adds extra annotations for various third party assemblies.

To use the NoReorder attribute, it has to be included in the file layout patterns, so if you're editing it, you have to make sure it's there. Unfortunately, the default patterns don't include this! Here's the bug: RSRP-451939.

On the positive side, it's easy to add:

  • Add a new Type Pattern, after "COM interfaces or structs", call it something like "NoReorder annotation"
  • Double click on "NoReorder annotation"
  • Switch to constraints view via the button in the top left of the "NoReorder annotation" box
  • Drag "Has Attribute" from the toolbox and add `JetBrains.Annotations.NoReorder` - don't put `NoReorderAttribute`, as the type is actually just called `NoReorder`, and not `NoReorderAttribute`.
  • Click away from the text box so that it loses focus, or the value isn't saved (RSRP-451942)
  • Leave the formatting rules empty. ReSharper will match on the constraint and apply any rules - if they're empty, nothing gets changed.
  • Click save

The `NoReorder` attribute will now stop items being reordered. Of course, you can go through the same steps with your own attribute - just create an empty attribute class, and use that name instead in the rule.

Comment actions Permalink

@Matt Ellis, thank you.
I just checked it works both ways. Region inside the class and NoReorder annotation both work. Over my frustration I forgot to add the rule in the file layout options.
Thank you so much.


Please sign in to leave a comment.