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
- Open the Package Manager;
- Type
$profile
to see the location of your NuGet profile;
- Type
mkdir –force (split-path $profile)
to create the profile's folder if it does not exist;
- 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.