Is there an automatic code formatter for C#? [clos

2019-01-30 22:49发布

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.

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.

14条回答
The star\"
2楼-- · 2019-01-30 22:55

Further to @Chris Karcher's answer - you can also automatically format the whole document by hitting Ctrl+K, Ctrl+D.

These formatting features work on a variety of file formats - it works wonders on ugly HTML.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-30 22:59

I you want to do online have a: freecodeformat

查看更多
该账号已被封号
4楼-- · 2019-01-30 23:04

For me, Ctrl+Shift+F maps to Find in Files. When I need to format code, I highlight it and hit 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.

查看更多
Rolldiameter
5楼-- · 2019-01-30 23:06

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 plugin as 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+E C.

In 'vanilla' Visual Studio, current file can be automatically formatted with Crtl+K Crtl+D, and Ctrl+K Ctrl+F formats the selected text.

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.

Some people also use Artistic Style command line tool, although it requires perl and works better with C/C++ code than with C#.

查看更多
走好不送
6楼-- · 2019-01-30 23:06

I understand this is a very late answer, but maybe you could be interested in this free Addin for Visual Studio 2010/2012

查看更多
淡お忘
7楼-- · 2019-01-30 23:08

The .NET Foundation just released their code formatting tool on GitHub

https://github.com/dotnet/codeformatter

It uses the Rosyln compiler services to parse project documents and convert them to their expected formatting conventions. They're applying this to some of the very old CLR code in order to make all the projects consistent.

查看更多
登录 后发表回答