C# 是否有自动代码格式化程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30101/
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
Is there an automatic code formatter for C#?
提问by skolima
In my work I deal mostly with C# code nowadays, with a sprinkle of java from time to time. What I absolutely love about Eclipse (and I know people using it daily love it even more) is a sophisticated code formatter, able to mould code into any coding standard one might imagine. Is there such a tool for C#? Visual Studio code formatting (Crtl+K, Crtl+D) is subpar and StyleCop only checks the source without fixing it.
在我的工作中,我现在主要处理 C# 代码,不时使用一些 java。我非常喜欢 Eclipse(我知道每天使用它的人更喜欢它)是一个复杂的代码格式化程序,能够将代码塑造成人们可以想象的任何编码标准。C#有这样的工具吗?Visual Studio 代码格式(Crtl+K、Crtl+D)低于标准,StyleCop 只检查源代码而不修复它。
My dream tool would run from console (for easy inclusion in automated builds or pre-commit hooks and for execution on Linux + Mono), have text-file based configuration easy to store in a project repository and a graphical rule editor with preview - just like the Eclipse Code Formatter does.
我梦想中的工具可以从控制台运行(便于包含在自动构建或预提交钩子中,并在 Linux + Mono 上执行),基于文本文件的配置易于存储在项目存储库中,以及带有预览的图形规则编辑器 - 只是就像 Eclipse 代码格式化程序一样。
采纳答案by FantaMango77
For Visual Studio, take a look at ReSharper. It's an awesome tool and a definite must-have. Versions after 4.0 have the code formatting and clean-up feature that you are looking for. There's also plugin integration with StyleCop, including formatting settings file. You'll probably want Agent Smith pluginas well, for spell-checking the identifiers and comments. ReSharper supports per-solution formatting setting files, which can be checked into version control system and shared by the whole team. Keyboard shortcut for code cleanup is Ctrl+EC.
对于 Visual Studio,请查看ReSharper。这是一个很棒的工具,绝对是必备工具。4.0 之后的版本具有您正在寻找的代码格式化和清理功能。还有与 StyleCop的插件集成,包括格式化设置文件。您可能还需要Agent Smith 插件,用于对标识符和注释进行拼写检查。ReSharper 支持 per-solution 格式设置文件,可以签入版本控制系统并由整个团队共享。代码清理的键盘快捷键是Ctrl+EC.
In 'vanilla' Visual Studio, current file can be automatically formatted with Crtl+KCrtl+D, and Ctrl+KCtrl+Fformats the selected text.
在'vanilla' Visual Studio 中,当前文件可以自动格式化为Crtl+KCrtl+D,并Ctrl+KCtrl+F格式化选定的文本。
As for a runs-everywhere command line tool to be used with commit hooks, try NArrange. It's free, can process whole directories at once and runs on Mono as well as on Microsoft.Net.
至于与提交钩子一起使用的无处不在的命令行工具,请尝试NArrange。它是免费的,可以一次处理整个目录,并且可以在 Mono 和 Microsoft.Net 上运行。
Some people also use Artistic Stylecommand line tool, although it requires perl and works better with C/C++ code than with C#.
有些人也使用Artistic Style命令行工具,尽管它需要 perl 并且使用 C/C++ 代码比使用 C# 更好。
回答by JamesSugrue
Not directly, but I use the Agent Smithplugin for R# to do this. Unfortunately, R# isn't free.
不是直接的,但我使用R#的Agent Smith插件来做到这一点。不幸的是,R# 不是免费的。
回答by Zack Peterson
I've heard only good things about ReSharper. It's on my to-learn list.
我只听说过ReSharper 的优点。它在我的学习清单上。
回答by Chris Karcher
For me, Ctrl+Shift+Fmaps to Find in Files. When I need to format code, I highlight it and hit Ctrl+K, Ctrl+F.
对我来说,Ctrl+ Shift+F映射在文件中找到。当我需要格式化代码时,我会突出显示它并点击Ctrl+ K, Ctrl+ F。
I understand this doesn't really address automated formatting, I just wanted to clarify for those who may not know this feature even exists in VS.
我知道这并没有真正解决自动格式化问题,我只是想向那些可能不知道 VS 中甚至存在此功能的人澄清一下。
回答by SQLMenace
Also take a look at Microsoft StyleCop
还可以看看Microsoft StyleCop
回答by Ian Nelson
Further to @Chris Karcher's answer - you can also automatically format the whole documentby hitting Ctrl+K, Ctrl+D.
除了@Chris Karcher 的回答之外 - 您还可以通过点击+ 、+来自动设置整个文档的格式。CtrlKCtrlD
These formatting features work on a variety of file formats - it works wonders on ugly HTML.
这些格式化功能适用于各种文件格式——它在丑陋的 HTML 上产生奇迹。
回答by Jan
I haven't tried this (found it through Google). Might work? http://www.semdesigns.com/Products/Formatters/CSharpFormatter.html. It's fairly cheap at USD50, but no trial available.
我没有试过这个(通过谷歌找到的)。可能有用吗?http://www.semdesigns.com/Products/Formatters/CSharpFormatter.html。它相当便宜,50 美元,但没有试用版。
回答by majkinetor
This tool is around (~30$), I tried it and it works nice (with multiple languages) I like this tool the best cuz it doesn't check code file for correctness. I can post code snippets from the net and it will traslate them correclty no matter if they are in missing parts of the code. Other tools I try complain in that cases. The tool can also be integrated easily into editors as it allows command line driving.
这个工具大约(~30 美元),我试过了,它运行良好(支持多种语言)我最喜欢这个工具,因为它不检查代码文件的正确性。我可以从网上发布代码片段,无论它们是否在代码的缺失部分,它都会正确地转换它们。在这种情况下,我尝试使用的其他工具会抱怨。该工具还可以轻松集成到编辑器中,因为它允许命令行驱动。
Other tools:
其他工具:
http://www.polystyle.com/index.jsp
http://www.polystyle.com/index.jsp
http://astyle.sourceforge.net/(open source)
回答by Simon Steele
See this previous question:
请参阅上一个问题:
Is there any tool for reformatting C# code?
Searching for [c#] astyle shows up some more previous questions too.
搜索 [c#] astyle 也会显示更多以前的问题。