48

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.

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
Thomas Jespersen
  • 11,493
  • 14
  • 47
  • 55

9 Answers9

35

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
  • 7
    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 Oct 26 '10 at 17:05
  • 7
    Not for VS2012, support for macro's is dropped (http://stackoverflow.com/a/12065139/23805) – bob May 08 '13 at 13:37
  • You could use [Visual Commander](http://visualstudiogallery.msdn.microsoft.com/deda8ac1-75e6-4068-89ab-b607cee38f2d) for macro. – Dmitriy Dokshin Aug 29 '14 at 06:59
  • I'm trying to use this on VS2019, but execution fails with errors. Is this not relevant anymore on VS 2019? I'm using Vissual Commander to write and run the macro – nahuelarjonadev May 04 '20 at 23:49
19

I just published a free Visual Studio Extension that automates a similar script, for easier setup. You might want to give that a try at

http://blog.pedropombeiro.com/keeping-code-formatted-the-easy-way/

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Pedro Pombeiro
  • 1,654
  • 13
  • 14
  • 4
    It might be interesting to implement it as a ReSharper plugin deployed with the new ReSharper Extension Manager. – Pedro Pombeiro Jun 15 '14 at 01:22
  • just wanted to say thanks for the effort and time you spent building this :) – user230910 Feb 16 '16 at 05:15
  • Had some trouble with this when I tried undoing pending changes in multiple files. TFS undid them, but the add-in went and reformatted again - not what I wanted. – Gordon Slysz Dec 16 '16 at 18:26
  • Unfortunately, this extension is disabled in Visual Studio 2019 v16.1 due to the use of deprecated api functions / synchronous execution. – Nicolas Aug 06 '19 at 12:51
19

It's my first post (hooray!) so excuse me if it's not perfect in any way...

Question is about R#, but you also mentioned (Visual Studio/ReSharper), so maybe my hint will help somebody. In Visual Studio extension called "Productivity Power Tools" there are two options for this (In Tools -> Options -> Productivity Power Tools -> PowerCommands: General):

  1. Format code on save.
  2. Remove and Sort Usings on save.

I find PPT nice to have, even with R# installed. You can get them from Visual Studio Gallery (2012 version, but there are also 2010, and 2013 versions).

Lech Osiński
  • 512
  • 7
  • 14
19

Update Resharper 2020.2

In version 2020.2+ of R# there is now build in option to run cleanup on save:

  1. Select Resharper Options (Alt + R, O)
  2. Code Editing > Code Cleanup > General

R# Code Cleanup Settings

KyleMit
  • 30,350
  • 66
  • 462
  • 664
MSkuta
  • 1,630
  • 13
  • 19
5

I've created an extension to automatically invoke ReSharper Silent Cleanup on file save: https://visualstudiogallery.msdn.microsoft.com/43be6ead-dabf-4bb1-b019-1e361efd8410

It only supports ReSharper silent cleanup, but it works.

2018 Note: This stopped working in later ReSharper versions due to ReSharper api changes

zastrowm
  • 8,017
  • 3
  • 43
  • 63
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11568843) – Bhavesh Odedra Mar 10 '16 at 05:46
  • 2
    @Odedra given that the answer is an extension that performs what the asker wanted, I'm not sure that I can add "the essential parts of the answer here" unless StackOverflow lets me upload the extension directly :: ) – zastrowm Mar 10 '16 at 05:54
  • Had success with this. And works properly when saving multiple files. Also doesn't interfere with undoing pending changes in multiple files. – Gordon Slysz Dec 16 '16 at 18:27
1

If you are on VS2012, and you can't use the macro solution, you could use AutoHotKey (or similar) to automate it, instead of using macros:

  1. In Visual Studio:
    1. Assign a key to the ReSharper_SilentCleanupCode command, e.g: CONTRL+SHIFT+C
    2. Change the save key from being CONTROL+S to something else, e.g: CONTRL+SHIFT+S
  2. In AutoHotKey create a snippet that looks something like that:

    ^s::
        Send, ^+c
        Sleep, 300
        Send, ^+s
    return
    
VitalyB
  • 12,397
  • 9
  • 72
  • 94
0

Maybe this will help someone else out in the future. I really liked the Macro idea so I adopted it. But this was not enough for me. I wanted to save all the unsaved open files at once and still get to enjoy the ReSharper cleanup function. So I came up with this Macro:

Public Module SaveUtils
    Public Sub CleanAndSave()
        DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
        DTE.ActiveDocument.Save()
    End Sub

    Public Sub CleanAndSaveAll()
        For i = 1 To DTE.Documents.Count
            Dim document = DTE.Documents.Item(i)
            If (Not document.Saved) Then
                document.Activate()
                CleanAndSave()
            End If
        Next i
    End Sub
End Module
Marcel Dutt
  • 180
  • 2
  • 10
0

Today I asked in the official resharper++ forum for that feature and received the answer that is already implemented in the 2020.2 EAP (early access program) version. topic

here you can find the related issue: click me

That means, this feature will be available soon for everyone. Otherwise just switch over to the EAP version.

DNKpp
  • 259
  • 1
  • 7
0

Even though I might not 100% agree with running a project-wide code clean-up, there are easy ways to do this, especially in 2023.

The easiest way in my opinion is to download Codemaid, an OSS tool that's plain awesome.

And then in its Settings select the following to Run Reshaper cleanup on pretty much every CodeMaid Code cleanup which can also be configured to run on save.

Go to Extensions>CodeMaid>Options>Third party

To find the below options for silent Resharper cleanups.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
FreakyAli
  • 13,349
  • 3
  • 23
  • 63