views:

382

answers:

5

I have inherited a large source tree, C#, Visual Studio 2008.

It has many quality issues, one of them is that the code is generally badly formatted.

I am looking for a tool, preferably a plugin for Visual Studio, that will go over the whole solution and apply the same basic formatting that Visual Studio itself applies when, for example, you close a curly brace around a block of code.

Any suggestions?

+5  A: 

In vs2005, you can do Edit->Advanced->Format Document for a single file, which will (I believe) do what you're asking. I assume vs2008 has a similar function somewhere.

Andy Mikula
the chord for this is Ctrl+K, Ctrl+D. In resharper, use Ctrl+E, C.
Michael Meadows
There's another chord under C# developer settings: Ctrl+E, D.
configurator
VS only does it for a single file doesn't it? Not the whole solution like the OP is asking?
Ray
Yep - I need a tool that will work solution-wide. 18 projects!!
tomfanning
Why not just format each file when you open it for the first time?
Ed Swangren
Because doing the reformatting all at once is much cleaner from a source control point of view. You have one check-in which is Reformat Code. Rather than check-ins that are reformat and fix bugs which makes it much harder to see what's changed.
Ray
+3  A: 

ReSharper should do what you're looking for. Have a look at their Code Cleanup Feature. This can be applied to multiple files including the whole solution.

Ray
Yet another reason to covet ReSharper. :)
Randolpho
Expensive tool for one job :-( To buck a trend, I'm not the world's greatest fan of Resharper overall.
tomfanning
Well, yes it's expensive just to do that, but for $250 you do get a whole lot else.
Ray
Well, only $150 if you go for the personal license.
Ray
Not cheap, and not legitimate for my intended use. Shame :-(
tomfanning
A: 

if you have already set your preferences in the vs editor options, you should be able to hit CTRL K, CTRL D which will tidy the code based upon your preferences

Matt
Works for one file only. I need something that will work solution-wide.
tomfanning
+5  A: 

ok, so why not try a macro, if you want to do the entire solution, there is a vb example here

http://blogs.msdn.com/kevinpilchbisson/archive/2004/05/17/133371.aspx

it opens each file and applies the same formatting that VS does, but the macro will work across an entire solution (may not be that wise to run it if your solution is huge), beyond reflecting the internals of the format document code in VS and inling it into some sort of stream, this appears to be the easiest way to make use of what i consider to be a very useful feature in visual studio

Matt
Awesome, this looks like what I'm looking for. Thanks very much.
tomfanning
A: 

Here's the simple way (for me)...

I have my Visual Studio set to reformat on "Paste"

Tools | Options | Text Editor | C# | Formatting

**Automatically format on paste** (checked)

When I hit an ugly file, I do CTRL-A, CTRL-X, CTRL-V -- poof, instant format :)

Hope this helps.

Kevin

peiklk
Again, not solution-wide. Thanks though.
tomfanning