格式化 - 立即 - Visual Studio项目中的所有文件

Dan*_*Dan 52 c# formatting visual-studio-2005 visual-studio

我有兴趣一次性格式化Visual Studio(版本2005)项目中的所有文件.

目前,有一种方法可以通过执行Edit-> Advanced-> Format Document等操作来格式化单个文档.但是,我没有看到一个命令来同时格式化项目的所有文件.

知道怎么做吗?

Mik*_*ail 28

格式所有文件的扩展为我工作.无所事事,只需安装并点击即可!

  • 目前不适用于 Visual Studio 2022。 (8认同)
  • 阅读此内容的人只需要选择的文件类型.阅读扩展页面,它说明了如何做到这一点. (2认同)

Dan*_*ler 26

蒂姆阿贝尔在他的博客上写了一个宏来做到这一点:

这是一个方便的宏观脚本,我今天在一起的视觉工作室.它在列出的文件类型的每个文档上运行"编辑,格式化文档".

您必须密切关注它,因为它是交互式的,有时会弹出消息并等待答案.

您可以访问http://github.com/timabell/vs-formatter-macro获取vb文件. 更多信息请访问http://wiki.github.com/timabell/vs-formatter-macro

原始代码可在博客文章中找到.请注意,这比上面的github上提供的版本旧.

  • 似乎从最近的 Visual Studio 版本中删除了宏支持。 (3认同)
  • 以上https://github.com/timabell/vs-formatter-macro/wiki的维基页面 (2认同)

ANe*_*ves 16

请注意,从Visual Studio 2015开始,以下解决方案本身不起作用.您还需要应用Marcus Mangelsdorf的答案.然后,此脚本适用于Visual Studio 2015和2017.


Phil Haack概述了一个很好的程序 - 添加一个可重复使用的脚本来缩进项目.

打开您的NuGet配置文件以进行编辑

  1. 打开包管理器;
  2. 输入$profile以查看NuGet个人资料的位置;
  3. 键入mkdir –force (split-path $profile)以创建配置文件的文件夹(如果它不存在);
  4. 使用命令编辑配置文件notepad $profile.

将可重用方法添加到NuGet配置文件

Phil使用davidfowl的Format-Document方法,他在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 }
}
Run Code Online (Sandbox Code Playgroud)

重新打开Visual Studio以使用该命令

重新打开Visual Studio时,该命令可用.

只需从NuGet包管理器控制台运行它:Format-Document这将重新格式化所选项目的所有文件.
要应用于整个解决方案,请使用命令Get-Project -All | Format-Document列出项目,然后为每个项目调用重新格式化命令.

正如作者所说:

有了这个,您现在可以放纵您的OCD并运行Format-Document命令来清理整个解决方案.我只是针对<Project>运行它,现在可以成为我一直想成为的空白纳粹.

10/10,会再次运行.

  • 请注意,这在Visual Studio 2015中不起作用,请参阅github上的[modulexcite的评论](https://gist.github.com/davidfowl/984358#gistcomment-1807675). (2认同)

Mar*_*orf 13

Visual Studio 2015所需的其他步骤

由ANeves发布的Phil Haack的解决方案是完美的,但由于某种原因,$item.FileCodeModel.LanguageVisual Studio 2015中总是返回null,使得Format-Document跳过所有文件并且实际上什么都不做.

为了(hackily)解决这个限制,你可以替换这个Get-Language功能:

# 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 }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用以下使用文件扩展名而不是语言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 }
   }            
}
Run Code Online (Sandbox Code Playgroud)


Ram*_* A. 12

有一种使用dotnetCLI 格式化 Visual Studio 项目中所有文件的新方法:

  1. 通过运行以下命令安装dotnet 格式:
    dotnet tool install -g dotnet-format
  2. 运行它,ProjectFile.csproj使用以下命令行替换项目文件的路径:
    dotnet format ProjectFile.csproj


Cha*_*man 8

如果有人仍然对这个问题感兴趣,Visual Studio 2019 通过一个名为Code Cleanup的功能带来了这个功能!

只需运行代码清理解决方案!

您还可以创建多个干净的配置文件并定义每个配置文件中发生的操作。

  • 这不适用于 VS 2022 下的 CMake 项目,因为它不是解决方案...... (2认同)