29

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?

4

6 回答 6

28

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) }
于 2013-03-17T06:57:28.760 回答
11

这是对适用于非常大的解决方案的现有脚本的更新。

它打开每个文件,对其进行格式化,然后保存并关闭文件,而不是让它保持打开状态。它还会跳过“.designer”。文件,因为这些文件通常应该单独放置。

在最新版本的 Visual Studio(2017 年之后)中,将脚本复制到 ps1 文件中,然后在包管理器控制台中运行. C:\path\to\the.ps1以调用它。(它可以在 Visual Studio 2012 和 2013 中直接复制并粘贴到包管理器控制台中。)

警告:将此代码粘贴到您的控制台将立即打开并格式化整个解决方案中的每个 C# 文件,无需询问即可保存每个修改过的文件。首先分支可能是个好主意...

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)
}
;
于 2014-01-13T18:57:52.900 回答
2

你可以使用CodeMaid,一个 VS2012 的免费插件。这让你可以清理、重组你的代码。在使用 StyleCop 检查我的代码之前,我总是使用它

于 2013-03-17T10:22:34.413 回答
1

您可以使用ReSharper 的 Code Cleanup。看起来这可以立即解决整个解决方案。

于 2013-03-17T09:58:41.793 回答
0

这是前两个答案的另一个变体,用户可能会觉得有帮助......绝对可以进一步改进和简化。这不仅可以格式化.cs文件,还可以格式化.json.cshtml.js.css.

function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.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) }

function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".json" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }

$dte.Solution.Projects | % { f($_.ProjectItems) }

function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".cshtml" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }

$dte.Solution.Projects | % { f($_.ProjectItems) }

function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".js" ) -and (-not $_.Properties.Item("FullPath").Value.Contains("common")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Content")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Scripts")) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }

$dte.Solution.Projects | % { f($_.ProjectItems) }

function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".css" ) -and (-not $_.Properties.Item("FullPath").Value.Contains("common")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Content")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Scripts")) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }

$dte.Solution.Projects | % { f($_.ProjectItems) }
于 2017-03-21T18:26:13.100 回答
-4

转到-->工具-->>选项-->>文本编辑器-->> XAML-->>格式-->>间距-->>检查每个属性在单独行上的位置!而已

于 2014-03-10T15:57:01.663 回答