65

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.

Community
  • 1
  • 1
skolima
  • 31,963
  • 27
  • 115
  • 151

14 Answers14

49

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. The keyboard shortcut for code cleanup is Ctrl + E, C.

In 'vanilla' Visual Studio, the current file can be automatically formatted with Ctrl + K, Ctrl + 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 the Artistic Style command line tool, although it requires Perl and works better with C/C++ code than with C#.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FantaMango77
  • 2,407
  • 4
  • 24
  • 27
24

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

https://github.com/dotnet/codeformatter

It uses the Roslyn 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Turner
  • 38,949
  • 15
  • 102
  • 166
15

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.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
6

Another option: NArrange;

  • free
  • console based (so good for commit hooks etc, but can still be used as an "External Tool" in VS)
  • flexible config file
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 4
    I haven't managed to use NArrange for code formatting (yet?). All it seemed to do was randomize the order of members in my source files based on basically arbitrary criteria such as access level or member type, but erroneous indentation and operator spacing within methods was left untouched. In fact, the NArrange docs even say "NArrange does not currently parse to the statement level of constructors, methods and properties" - if this means what I think it means, NArrange may be useful for code reorganization, but doesn't really do much formatting. – O. R. Mapper Jan 23 '13 at 14:59
5

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 Visual Studio.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Karcher
  • 2,252
  • 7
  • 24
  • 31
3

I've heard only good things about ReSharper. It's on my to-learn list.

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
3

http://www.sourceformat.com/

This tool is around (~30$). I tried it and it works nice (with multiple languages).

I like this tool the best because it doesn't check code file for correctness. I can post code snippets from the Internet and it will translate them correctly 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.

Other tools:

http://www.polystyle.com/index.jsp

http://astyle.sourceforge.net/ (open source)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
majkinetor
  • 8,730
  • 9
  • 54
  • 72
2

Not directly, but I use the Agent Smith plugin for R# to do this. Unfortunately, R# isn't free.

JamesSugrue
  • 14,891
  • 10
  • 61
  • 93
2

Also take a look at Microsoft StyleCop

SQLMenace
  • 132,095
  • 25
  • 206
  • 225
1

Maybe you could be interested in this free Addin for Visual Studio 2010/2012.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NinjaCross
  • 784
  • 2
  • 9
  • 20
1

I haven't tried this (found it through Google). It might work. http://www.semdesigns.com/Products/Formatters/CSharpFormatter.html. It's fairly cheap at USD50, but a trial is not available.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jan
  • 4,366
  • 6
  • 22
  • 21
1

See this previous question:

Is there any tool for reformatting C# code?

Searching for [c#] astyle shows up some more previous questions too.

Community
  • 1
  • 1
Simon Steele
  • 11,558
  • 4
  • 45
  • 67
0

Here is an open source code formatting tool which has amazing features

CodeMaid

Vikram
  • 37
  • 5
0

If you want to do it online, have a freecodeformat.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131