Is there a way to mark up code to tell ReSharper not to format it?
Asked Answered
G

3

35

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.

Gourmet answered 1/2, 2010 at 11:57 Comment(8)
Do you really want to "pollute" your code with R# specific markup? Yuck yuck yuck...Skate
@Oded: Not really, no - but I'd prefer that to code that is unreadable because of its layout.Gourmet
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.Coinstantaneous
@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.Gourmet
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.Coinstantaneous
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 wantBumboat
Doesn't sound like this exists. Contacting their support and putting in a feature request might be your only hope.Ethel
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.Coinstantaneous
A
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.

Administrative answered 23/3, 2010 at 18:27 Comment(3)
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.Gourmet
@Gourmet - 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.Administrative
@Gourmet - 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.Administrative
T
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.

Trixy answered 1/2, 2010 at 15:18 Comment(0)
P
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.

Perdita answered 7/3, 2012 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.