【问题标题】:Formatting - at once - all the files in a Visual Studio project一次格式化 Visual Studio 项目中的所有文件
【发布时间】:2010-10-30 04:56:18
【问题描述】:

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

目前,有一种方法可以通过 Edit->Advanced->Format Document 来格式化单个文档。但是,我看不到一个命令可以一次格式化项目的所有文件。

知道怎么做吗?

【问题讨论】:

标签: c# visual-studio visual-studio-2005 formatting


【解决方案1】:

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

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

【讨论】:

  • 它似乎没有处理.razor 文件和.css 文件,但它在它的工作上做得很好。这当然不是我深入研究它的配置。
【解决方案2】:

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

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

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

【讨论】:

  • 但不要与 Resharper "Cleanup Code..." 混淆
  • 这是当前 VS 版本中最简单的解决方案。您也可以通过右键单击解决方案资源管理器中的项目并选择 Analysis and Code Cleanup -> Run Code Cleanup (Profile 1) 对单个项目执行此操作
【解决方案3】:

Tim Abell 在他的 blog 上编写了一个宏来执行此操作:

这是我今天拼凑的一个方便的 Visual Studio 宏脚本。 它对列出的文件类型的每个文档运行“编辑、格式化文档”。

您必须密切注意它,因为它是交互式的,有时会弹出一条消息并等待答复。

你可以在https://github.com/timabell/vs-formatter-macro获取vb文件 更多信息https://github.com/timabell/vs-formatter-macro/wiki

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

【讨论】:

  • 最近的 Visual Studio 版本中似乎删除了宏支持。
【解决方案4】:

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


Phil Haack 概述了一个很好的程序 - adding a reusable script to indent the project

打开版本的 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 }
}

重新打开 Visual Studio 以使用命令

当您重新打开 Visual Studio 时,该命令可用。

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

正如作者所说:

有了这个,您现在可以尽情享受您的强迫症并运行 Format-Document 命令来清理您的整个解决方案。我刚刚针对 运行它,现在可以成为我一直想成为的空白纳粹。

10/10,将再次运行。

【讨论】:

  • @Kristopher 对我来说效果很好,它遍历了我的解决方案的多个项目的所有 C# 文件,但它是 WPF 而不是 ASP.NET 。您希望它遍历哪些其他文件夹?也许我没有遇到的脚本有问题。
  • 我使用 ReSharper beta 完成了这项工作,所以不用担心,但它基本上似乎错过了解决方案目录中的所有其他内容......
  • 请注意,这在 Visual Studio 2015 中不起作用,请参阅 github 上的 modulexcite's comment
【解决方案5】:

Format All Files 扩展对我有用。什么都不做,安装点击即可!

【讨论】:

  • 阅读此文且只需要选定文件类型的任何人。阅读扩展页面,它说明了如何做到这一点。
  • 如果您想知道该软件包是否已更新为 vs2019,尽管它已经 5 岁了。
【解决方案6】:

Visual Studio 2015 需要额外的步骤

Phil Haack's solution posted by ANeves 是完美的,但由于某种原因,$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 }
    }
}

以下变体使用文件的扩展名而不是语言 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 }
   }            
}

【讨论】:

  • 我已按以下方式更改代码:switch($ext) { '.cs' { 'C#' } '.vb' { 'VB' } default { $null } } 以防止格式化无 VB 或 C# 文件。
  • 也适用于 2017 年
猜你喜欢
  • 1970-01-01
  • 2011-07-05
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 2010-10-19
相关资源
最近更新 更多