How to format all files in Visual Studio 2012?
c#, formatting, visual-studio-2012
Solution
Open Tools->Library Package Manager->Package Manager Console, and run the command below. At the end, all documents will be open in the IDE. (Low-RAM machines will have problems with large solutions.) Changed files will be modified in the IDE, and not saved to disk. You can Save All, then Close All if you're ready.
VS2012 removed the VB-like macro language that existed in previous version of Visual Studio. However, the underlying DTE interface is still there, and you can reach it via PowerShell, in the Package Manager Console
The weird GUID passed to `ProjectItem.Open` is `Constants.vsViewKindCode`.
Normally I'd split this in to multiple lines, but the Package Manager Console doesn't support line continuation.
You can find the latest version at https://gist.github.com/JayBazuzi/9e0de544cdfe0c7a4358
function f($projectItems) { $projectItems | ? { $_.Name.EndsWith( ".cs" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }
Problem
With previous versions of Visual Studio, I used Kevin Pilch-Bisson's script to format all C# files in my solution. VS2012 dropped macro support, so that doesn't work any more. How can I easily format all my documents in VS2012?