views:

272

answers:

3

Resharper offers a very useful introduce and initialize field xxx action when you specify a new parameter in a constructor like:

Constructor (int parameter)

The only (minor) nuisance is that it puts the new field at the beginning of the class - and I'm a fan of putting private parts as far away as possible from the prying eyes of strangers ;).

If, however, you already have some private fields in the class, Resharper will put the new field "correctly" (note the quotes, I don't want to start a flame war over this issue) next to those, even if they are at the end of the class.

Is there a way to force Resharper to always put new fields at the end of the class?

UPDATE: OK, I forgot to mention I know about the "Type Members Layout in Options" feature, but some concrete help on how to modify the template to achieve fields placement would be nice.

A: 

Use a custom "Type Members Layout" in Resharper options.

In the "Default Pattern" section of the XML layout definition, just move the entry element that groups your fields to the end of the section, after methods, events, properties, etc.

Romain Verdier
+3  A: 

In the "Type Members Layout" you can find and move the following block of XML

<!--fields and constants-->
<Entry>
  <Match>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="field"/>
    </Or>
  </Match>
  <Sort>
    <Kind Order="constant field"/>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>

To the bottom of the default pattern. Basically two lines up form the bottom of the file

    <!--HERE-->

  </Pattern>      
</Patterns>

Unfortunately a quick test shows that this doesn't affect new fields created through the option you describe, but it will make the code cleanup move them to the right place. (Provided you have "Reorder type members" turned on)


If you are in the habit of using public fields and you only want the private ones moved to the bottom then change the match above to this:

<Match>
  <And>
    <Access Is="private"/>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="field"/>
    </Or>
  </And>
</Match>

Then you can copy the whole block again, change the "Access Is" value to "public" and put the new public block wherever you want the public fields to go - near the top, where it came from originally I'd guess.

Martin Harris
I tried that before asking the question and got the same result you got. Still, +1 point from me for making the effort, thanks
Igor Brejc
+1  A: 

If you haven't already, you should lodge a bug report at the ReSharper Jira site:

http://www.jetbrains.net/jira/browse/RSRP

Have a look first to see if someone has already reported this. If so, you can vote on their issue and be notified of any changes to it.

Drew Noakes

related questions