views:

1140

answers:

11

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.

+2  A: 

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

KiwiBastard
+9  A: 

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

FantaMango77
works well... and it does more than format code!
jle
+2  A: 

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

Zack Peterson
+4  A: 

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.

Chris Karcher
+1  A: 

Also take a look at Microsoft StyleCop

SQLMenace
+4  A: 

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.

Ian Nelson
A: 

I know that ReSharper is a great tool, I definitely plan to try it out. However, Visual Studio Pro + ReSharper cost about two thirds (2/3) of an average programmer's monthly salary here in Poland and won't work on Linux. I was hoping for a tool that would be affordable for open source projects.

skolima
A: 

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.

Jan
A: 

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

Other tools:

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

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

majkinetor
+1  A: 

See this previous question:

Is there any tool for reformatting C# code?

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

Simon Steele
+1  A: 

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