views:

133

answers:

2

I would love to configure Visual Studio/ReSharper to run "Code cleanup" whenever I save a file.

A bonus would be to configure this only for C# files, as I sometimes find that the cleanup on ASP.NET files does not work without introducing errors.

+3  A: 

Are you sure you really want to trust ReSharper to correctly change your code right before saving the file? You really shouldn't count on it to do the right thing for you 100% of the time.

Instead, how about making a habit of coding in ways that the code cleanup action has little to nothing to do for you?

Mike Atlas
Well... isn't that up to me to decide? :-)We are programming in a staticly typed language, and tools can be trusted. I also trust ReSharpers refactroing.We have been running ReSharper's code clean up on the entire solution for 2 years... and it works great. But lets just for argument sayt hat ReSharper has a bug, I'd rather take the hit up-front before I even test my code, than at some random time when I clean up the entire solution. The problem is that when devs forget to run clean up it often becomes a nightmare to merge and we get production bugs. So I'd rather take the hit up front.
Thomas Jespersen
This smells funny to me. Why are you not testing the code before it goes into production? I'd also argue against blindly running "cleanup" on a whole solution as well! I'd also suggest that by extension, other devs should also be coding in a consistent manner that requires little post-facto cleanup by a 3rd party tool. You really shouldn't need a 3rd party tool to get new/updated code to merge cleanly. Perhaps you can you draft a coding convention document and get the devs to all agree on standard formatting, naming, syntax, etc?
Mike Atlas
We have a perfect coding standard: ReSharper and FxCop! Why would you rather do that manually? This setup effectly kills every discution about placment of {}, prefix of variable, when to use var, how to case private methods, Int32 vs. int. And that it's orderId not orderID! Who said we do it blindly and don't test our code? In fact we review every single line ReSharper changes. My comment about production bugs was more about merging in general. Eg. ReSharper reorders fields and method. When you have a merge conflict in one of those methods, it's easy to miss a comma, and miss that in test.
Thomas Jespersen
I see. Sounds like you're fine then!
Mike Atlas
+5  A: 

You could record a macro(Ctrl+E, Ctrl+C,Run, Ctrl+S). Then run that instead of saving. Then all you need to do is assign CTRL+S to your macro.

Public Module RecordingModule
    Sub CLEAN_AND_SAVE()
    DTE.ExecuteCommand ("ReSharper.ReSharper_CleanupCode")
    DTE.ActiveDocument.Save
    End Sub
End Module

This method will show the code clean-up dialogue box where you will have to select Run.

To remove the user interaction you will have to select a profile to run when Code Cleanup is invoked. You can configure this by going into ReSharper | Options | Tools | Code Cleanup and selecting the profile in "Profile to use with silent clean-up" drop down. Its also here where you can create a custom profile to specify what changes to your code to make. In 4.5 however it does not allow you to omit aspx pages. The only differentiator is C# and VB.Net.

Useful link: http://www.jetbrains.com/resharper/features/code_formatting.html

John Nolan
if you want to run the clean up silently then you need to `ExecuteCommand("ReSharper_SilentCleanupCode")` instead and have set a silent code clean-up option
Sam Holder