visual-studio 如何在保存时配置 ReSharper 的代码清理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3071953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I configure ReSharper's code cleanup on save?
提问by Thomas Jespersen
I would love to configure Visual Studio/ReSharper to run "Code cleanup" whenever I save a file.
我很想将 Visual Studio/ReSharper 配置为在我保存文件时运行“代码清理”。
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.
一个额外的好处是只为 C# 文件配置这个,因为我有时发现 ASP.NET 文件的清理不会在不引入错误的情况下工作。
采纳答案by Johnno Nolan
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+Sto your macro.
你可以录制一个宏(Ctrl+ E, Ctrl+ C,Run, Ctrl+ S)。然后运行它而不是保存。然后您需要做的就是将CTRL+分配S给您的宏。
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 interactionyou 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 profileto 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.
要删除用户交互,您必须选择在调用代码清理时运行的配置文件。您可以通过进入 ReSharper 进行配置 | 选项 | 工具 | 代码清理并在“用于静默清理的配置文件”下拉列表中选择配置文件。您还可以在此处创建自定义配置文件以指定要对代码进行哪些更改。但是在 4.5 中它不允许您省略 aspx 页面。唯一的区别是 C# 和 VB.Net。
Useful link: http://www.jetbrains.com/resharper/features/code_formatting.html
有用的链接:http: //www.jetbrains.com/resharper/features/code_formatting.html
回答by Pedro Pombeiro
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/.
我刚刚发布了一个免费的 Visual Studio 扩展,它可以自动化类似的脚本,以便于设置。您可能想在http://blog.pedropombeiro.com/keeping-code-formatted-the-easy-way/ 上尝试一下。
回答by Lech Osiński
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):
问题是关于 R#,但你也提到了 ( Visual Studio/ReSharper),所以也许我的提示会对某人有所帮助。在名为“Productivity Power Tools”的 Visual Studio 扩展中,有两个选项(在工具 -> 选项 -> Productivity Power Tools -> PowerCommands:General):
- Format code on save.
- 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).
我发现 PPT 很好,即使安装了 R#。您可以从Visual Studio Gallery(2012 版,但也有 2010 和 2013 版)获取它们。
回答by FriendlyGuy
I've created an extension to automatically invoke ReSharper Silent Cleanup on file save: https://visualstudiogallery.msdn.microsoft.com/43be6ead-dabf-4bb1-b019-1e361efd8410
我创建了一个扩展来在文件保存时自动调用 ReSharper Silent Cleanup:https: //visualstudiogallery.msdn.microsoft.com/43be6ead-dabf-4bb1-b019-1e361efd8410
It only supports ReSharper silent cleanup, but it works.
它仅支持 ReSharper 静默清理,但它有效。
2018 Note: This stopped working in later ReSharper versions due to ReSharper api changes
2018 年注意:由于 ReSharper api 更改,这在更高的 ReSharper 版本中停止工作
回答by VitalyB
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:
如果您使用的是 VS2012,并且无法使用宏解决方案,则可以使用 AutoHotKey(或类似方法)来自动化它,而不是使用宏:
- In Visual Studio:
- Assign a key to the ReSharper_SilentCleanupCode command, e.g: CONTRL+SHIFT+C
- Change the save key from being CONTROL+S to something else, e.g: CONTRL+SHIFT+S
In AutoHotKey create a snippet that looks something like that:
^s:: Send, ^+c Sleep, 300 Send, ^+s return
- 在 Visual Studio 中:
- 为 ReSharper_SilentCleanupCode 命令分配一个键,例如:CONTRL+SHIFT+C
- 将保存键从 CONTROL+S 更改为其他内容,例如:CONTRL+SHIFT+S
在 AutoHotKey 中创建一个看起来像这样的片段:
^s:: Send, ^+c Sleep, 300 Send, ^+s return
回答by Marcel Dutt
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:
也许这会在未来帮助其他人。我真的很喜欢宏观的想法,所以我采用了它。但这对我来说还不够。我想一次保存所有未保存的打开文件,并且仍然可以享受 ReSharper 清理功能。所以我想出了这个宏:
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

