34

I quite often use the ReSharper "Clean Up Code" command to format my code to our coding style before checking it into source control. This works well in general, but some bits of code are better formatted manually (eg. because of the indenting rules in ReSharper, things like chained linq methods or multi-line ternary operators have a strange indent that pushes them way to the right).

Is there any way to mark up parts of a file to tell ReSharper not to format that area? I'm hoping for some kind of markup similar to how ReSharper suppresses other warnings/features. If not, is there some way of changing a combination of settings to get ReSharper to format the indenting correctly?

EDIT:

I have found this post from the ReSharper forums that says that generated code sections (as defined in the ReSharper options page) are ignored in code cleanup. Having tried it though, it doesn't seem to get ignored.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • 4
    Do you really want to "pollute" your code with R# specific markup? Yuck yuck yuck... – Oded Feb 01 '10 at 12:02
  • 5
    @Oded: Not really, no - but I'd prefer that to code that is unreadable because of its layout. – adrianbanks Feb 01 '10 at 12:03
  • You can select the code you want to cleanup, it will only cleanup the things in the selection. But probably you already knew this. Anyhow, I kind of have this same problem, but I've submitted to the wills of ReSharper, and correct the strange indentation manually afterwards. – Skurmedel Feb 01 '10 at 12:07
  • @Skurmedel: I did know that. By the sounds of it, you are doing the same as me. It's a pain though having to do it every time. – adrianbanks Feb 01 '10 at 12:09
  • 1
    Yeah, it keeps pushing my collection initializers and lambda bodies to the right. It can be mitigated somewhat if you tweak the formatting settings but insists on indenting at least two steps too much. – Skurmedel Feb 01 '10 at 12:22
  • 2
    The absolutely crazY reformatting behaviour of ReSharper was one of the big reasons I dropped it in favour of CodeRush. I shouldn't have to fight against my refactoring tool in order to get the results I want – ZombieSheep Feb 01 '10 at 15:27
  • Doesn't sound like this exists. Contacting their support and putting in a feature request might be your only hope. – Greg Feb 01 '10 at 17:36
  • 2
    Well I found a fix to my problem under "Other" and "Align multiline constructs" in the formatting settings for C#, probably doesn't fix your problems though. – Skurmedel Feb 10 '10 at 10:21

3 Answers3

21

Resharper>Options>Languages>C#>Formatting Style>Other>

Uncheck "Indent anonymous method body" and "Indent array, object and collection initilizer blocks" and anything else that strikes your fancy.

Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
  • The "anonymous method body" option works (showing their slightly strange `DoSomethog()` method as an example), but R# still formats the other blocks with large indents. – adrianbanks Mar 23 '10 at 19:13
  • @adrianbanks - by unchecking "Indent array, object and collection initilizer blocks" my arrays and initializers are left alone. Not sure what DoSomethog() you are referring to though. – Sky Sanders Mar 23 '10 at 19:22
  • @adrianbanks - also, if your blocks are already pushed out from previous reformatting, r# is going to leave them alone. Set the first open brace where you want it, CTRL-D to format the rest, and subsequent code cleanup will not touch it. – Sky Sanders Mar 23 '10 at 19:24
3

As a last resort, if you've got legacy code that you don't want to format but you want additions to the class to be nicely formatted, then make the class partial and put new code in the new file.

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
2

Check out this question I asked that involves the same issue: Resharper formatting code into a single line

The answer I got there work really good for me.

Community
  • 1
  • 1
chafnan
  • 457
  • 1
  • 7
  • 13