Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format all files in Visual Studio 2012?

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?

like image 647
Jay Bazuzi Avatar asked Mar 17 '13 06:03

Jay Bazuzi


People also ask

What is the fastest way to format code in Visual Studio?

In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F. To format a range, in an already opened project, open the document that you want to modify, select the specific range to format, right-click, and select Format Selection.

How do I tidy up code in Visual Studio?

Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.


2 Answers

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) } 
like image 76
Jay Bazuzi Avatar answered Sep 19 '22 21:09

Jay Bazuzi


Here's an update to the existing script which works on very large solutions.

It opens each file, formats it, then saves and closes the file rather than leaving it open. It also skips ".designer." files, since these should generally be left alone.

In recent versions of Visual Studio (after 2017), copy the script to a ps1 file, then in the Package Manager Console run . C:\path\to\the.ps1 to invoke it. (It works in Visual Studio 2012 and 2013 to copy and paste it directly into the Package Manager Console.)

BE WARNED: Pasting this code into your console will immediately open and format every C# file in your entire solution, saving each modified file without asking. It might be a good idea to branch first...

function FormatItems($projectItems) {     $projectItems |     % {         # Write-Host "    Examining item: $($_.Name)";          if ($_.Name -and $_.Name.ToLower().EndsWith(".cs") `             -and (-not $_.Name.ToLower().Contains(".designer."))) {              $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}');             $win.Activate();              $dte.ExecuteCommand('Edit.FormatDocument');              if (!$_.Saved) {                 Write-Host "    Saving modified file: $($_.Name)";                 $dte.ExecuteCommand('File.SaveSelectedItems');             }              $dte.ExecuteCommand('Window.CloseDocumentWindow');         }          if ($_.ProjectItems -and ($_.ProjectItems.Count -gt 0)) {             # Write-Host "    Opening sub-items of $($_.Name)";              FormatItems($_.ProjectItems);         }     }; }  $dte.Solution.Projects | % {     Write-Host "-- Project: $($_.Name)";      FormatItems($_.ProjectItems) } ; 
like image 23
Daryl Avatar answered Sep 21 '22 21:09

Daryl