Formatting - at once - all the files in a Visual Studio project
Asked Answered
J

7

78

I am interested in formatting all the files in a Visual Studio (ver. 2005) project all at once.

Currently, there is a way to format a single document by doing something like Edit->Advanced->Format Document. However, I don't see a single command to format all the files of a project all at once.

Any idea how to do that?

Janelljanella answered 31/5, 2009 at 5:48 Comment(4)
Resharper provides code cleanup and formatting and that is just a fraction of its functionality...Nymph
Just for future reference, be noted that macros have been dropped from VS 2011.Urey
Phil Haack says: haacked.com/archive/2011/05/22/…Evie
Tim Abell wrote a macro to do this on his blog timwise.blogspot.com/2009/01/…Evie
C
27

Tim Abell wrote a macro to do this on his blog:

Here's a handy macro script for visual studio I knocked together today. It runs "edit, format document" on every document of the listed file types.

You have to keep an eye on it as it's interactive and does sometimes pop up a message and wait for an answer.

You can get the vb file at https://github.com/timabell/vs-formatter-macro More info at https://github.com/timabell/vs-formatter-macro/wiki

The original code is available at the blog post. Note that this is older than the version available on github above.

Confide answered 31/5, 2009 at 5:59 Comment(1)
It seems macro support was removed from recent Visual Studio versions.Isreal
D
37

The Format All Files extension worked for me. Nothing to do, just install and click!

Deth answered 10/11, 2016 at 12:12 Comment(3)
Anyone reading this who wants only selected file types. Read the extension page and it says how to do it.Manns
If you're wondering the package is updated for vs2019 despite being 5 years old.Mauriciomaurie
Doesn't work with Visual Studio 2022 as of now.Spearing
C
29

If anyone is still interested in this question, Visual Studio 2019 brought this functionality through a feature called Code Cleanup!

Just run Code Cleanup for Solution!

And you can also create multiple clean profiles and define which actions happen in each one.

Chemulpo answered 28/4, 2020 at 10:36 Comment(6)
But do not confuse with Resharper "Cleanup Code..."Sela
This is the simplest solution in current versions of VS. You can do it on individual projects too by right-clicking on the project within the Solution Explorer and choosing Analysis and Code Cleanup -> Run Code Cleanup (Profile 1)Francyne
This doesn't work with a CMake project under VS 2022, as it is not a solution...Spearing
Great functionnality but dosent format cshtml filesHayward
And as we have come to expect --no, actually, demand-- from Microsoft, this new "feature" of Visual Studio is only good for demonstration purposes. It seems to work fine if you try it on a tiny solution, but try it on a real solution with thousands of source files, and Visual Studio will be frozen for hours.Rosannarosanne
code cleanup does not fix tabs so it is completely uselessBirthday
C
27

Tim Abell wrote a macro to do this on his blog:

Here's a handy macro script for visual studio I knocked together today. It runs "edit, format document" on every document of the listed file types.

You have to keep an eye on it as it's interactive and does sometimes pop up a message and wait for an answer.

You can get the vb file at https://github.com/timabell/vs-formatter-macro More info at https://github.com/timabell/vs-formatter-macro/wiki

The original code is available at the blog post. Note that this is older than the version available on github above.

Confide answered 31/5, 2009 at 5:59 Comment(1)
It seems macro support was removed from recent Visual Studio versions.Isreal
N
17

Note, that the following solution does not work by itself starting with Visual Studio 2015. You need to apply the answer by Marcus Mangelsdorf as well. Then, this script works in Visual Studio 2015 and 2017.


Phil Haack outlined a good procedure - adding a reusable script to indent the project.

Open your NuGet profile for edition

  1. Open the Package Manager;
  2. Type $profile to see the location of your NuGet profile;
  3. Type mkdir –force (split-path $profile) to create the profile's folder if it does not exist;
  4. Edit the profile with the command notepad $profile.

Add the reusable method to the NuGet profile

Phil used davidfowl's Format-Document method which he found at https://gist.github.com/davidfowl/984358:

# Function to format all documents based on https://gist.github.com/984353
function Format-Document {
    param(
        [parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]]$ProjectName
    )
    Process {
        $ProjectName | %{ 
                        Recurse-Project -ProjectName $_ -Action { param($item)
                        if($item.Type -eq 'Folder' -or !$item.Language) {
                            return
                        }

                        $window = $item.ProjectItem.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}')
                        if ($window) {
                            Write-Host "Processing `"$($item.ProjectItem.Name)`""
                            [System.Threading.Thread]::Sleep(100)
                            $window.Activate()
                            $Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.FormatDocument')
                            $Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.RemoveAndSort')
                            $window.Close(1)
                        }
                    }
        }
    }
}

function Recurse-Project {
    param(
        [parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]]$ProjectName,
        [parameter(Mandatory = $true)]$Action
    )
    Process {
        # Convert project item guid into friendly name
        function Get-Type($kind) {
            switch($kind) {
                '{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}' { 'File' }
                '{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}' { 'Folder' }
                default { $kind }
            }
        }

        # Convert language guid to friendly name
        function Get-Language($item) {
            if(!$item.FileCodeModel) {
                return $null
            }

            $kind = $item.FileCodeModel.Language
            switch($kind) {
                '{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
                '{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
                default { $kind }
            }
        }

        # Walk over all project items running the action on each
        function Recurse-ProjectItems($projectItems, $action) {
            $projectItems | %{
                $obj = New-Object PSObject -Property @{
                    ProjectItem = $_
                    Type = Get-Type $_.Kind
                    Language = Get-Language $_
                }

                & $action $obj

                if($_.ProjectItems) {
                    Recurse-ProjectItems $_.ProjectItems $action
                }
            }
        }

        if($ProjectName) {
            $p = Get-Project $ProjectName
        }
        else {
            $p = Get-Project
        }

        $p | %{ Recurse-ProjectItems $_.ProjectItems $Action } 
    }
}

# Statement completion for project names
Register-TabExpansion 'Recurse-Project' @{
    ProjectName = { Get-Project -All | Select -ExpandProperty Name }
}

Reopen Visual Studio to use the command

When you reopen Visual Studio, the command is available.

Simply run it from the NuGet Package Manager Console: Format-Document This will re-format all files of the selected project.
To apply to the whole solution, use the command Get-Project -All | Format-Document, which lists the projects and then for each of them calls the reformatting command.

As the author put it:

With this in place, you can now indulge your OCD and run the Format-Document command to clean up your entire solution. I just ran it against <Project> and now can become the whitespace Nazi I’ve always wanted to be.

10/10, would run again.

Nymphalid answered 21/7, 2015 at 11:29 Comment(3)
@Methane for me it worked fine, it traversed all C# files of the multiple projects of my solution, but it is WPF and not ASP.NET . What other folders did you expect it to traverse? Maybe there is an issue with the script that I did not run into.Nymphalid
I used ReSharper beta to get this done so no worries, but it basically seems to have missed everything else in the solution directory...Methane
Note that this doesn't work in Visual Studio 2015, see modulexcite's comment on github.Hemidemisemiquaver
H
13

Additional step needed for Visual Studio 2015

Phil Haack's solution posted by ANeves is perfect, but for some reason $item.FileCodeModel.Language always returns null in Visual Studio 2015 making the Format-Document skip all files and effectively do nothing.

To (hackily) work around this limitation you can replace the Get-Language function:

# Convert language guid to friendly name
function Get-Language($item) {
    if(!$item.FileCodeModel) {
        return $null
    }

    $kind = $item.FileCodeModel.Language
    switch($kind) {
        '{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
        '{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
        default { $kind }
    }
}

with the following variant that uses the file's extension instead of the Language GUID:

# Convert file extension to friendly language name
function Get-Language($item) {
   if(!$item.FileCodeModel) {
       return $null
   }

   $filename = $item.Name
   $ext = $filename.substring($filename.lastindexof('.'),
                              ($filename.length - $filename.lastindexof('.')))
   switch($ext) {
       '.cs' { 'C#' }
       '.vb' { 'VB' }
       # If you want to prevent re-formatting files that are not VB or C# source files 
       # (e.g. XML files in your project etc.), replace the following line with 
       # "default { $null }" (thanks to HHenn for this suggestion!)
       default { $ext }
   }            
}
Hemidemisemiquaver answered 24/8, 2016 at 13:36 Comment(2)
I have changed the code in the following way: switch($ext) { '.cs' { 'C#' } '.vb' { 'VB' } default { $null } } to prevent formatting of none VB or C# files.Discursion
Works on 2017 as wellCylindroid
O
12

There's a new way to format all files in a Visual Studio project using the dotnet CLI:

  1. Install dotnet format by running the following command:
    dotnet tool install -g dotnet-format
  2. Run it, replacing ProjectFile.csproj with the path to your project file, with the following command line:
    dotnet format ProjectFile.csproj
Otolaryngology answered 11/11, 2020 at 2:38 Comment(1)
It does not seem to do .razor files nor .css files, but it does a good job at what it does. This is of course without me diving into the configuration of it.Gi
T
0

I encounter the same problem now. For Visual Studio 2022 17.10.2, installed from marketplace, the plugin FormatAllFilesPlus works for me. Right click on solution or project, then select "Format All Files" in the menu.

Teammate answered 27/6 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.