How can I configure ReSharper's code cleanup o

2019-01-17 09:47发布

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.

6条回答
2楼-- · 2019-01-17 10:06

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

查看更多
3楼-- · 2019-01-17 10:06

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-17 10:08

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/.

查看更多
神经病院院长
5楼-- · 2019-01-17 10:08

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
    
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-17 10:17

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).

查看更多
姐就是有狂的资本
7楼-- · 2019-01-17 10:29

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
查看更多
登录 后发表回答