How to format all files in Visual Studio 2012?

后端 未结 6 772
-上瘾入骨i
-上瘾入骨i 2021-01-30 17:37

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 a

相关标签:
6条回答
  • 2021-01-30 17:57

    Here's another variant of the previous two answers that users may find helpful... definitely could be further improved and simplified. This not only formats .cs files but also .json, .cshtml, .js, and .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) }
    
    0 讨论(0)
  • 2021-01-30 17:59

    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) }
    
    0 讨论(0)
  • 2021-01-30 18:02

    You can use CodeMaid, a free plugin for VS2012. That allow you to clean, reorganize your code. I always use it before checking my code with StyleCop

    0 讨论(0)
  • 2021-01-30 18:06

    You could use ReSharper's Code Cleanup. Looks like this can work on a whole solution at once.

    0 讨论(0)
  • 2021-01-30 18:14

    Goto --> Tools -->> Option -->> TextEditor -->> XAML -->> Formatting -->> Spacing -->> Check the Postion Each Attribute on Separate Line! Thats it

    0 讨论(0)
  • 2021-01-30 18:20

    Here's an update to the existing script which works on very large solutions.

    It opens each file, formats it, then saves and closes the file rather than leaving it open. It also skips ".designer." files, since these should generally be left alone.

    In recent versions of Visual Studio (after 2017), copy the script to a ps1 file, then in the Package Manager Console run . C:\path\to\the.ps1 to invoke it. (It works in Visual Studio 2012 and 2013 to copy and paste it directly into the Package Manager Console.)

    BE WARNED: Pasting this code into your console will immediately open and format every C# file in your entire solution, saving each modified file without asking. It might be a good idea to branch first...

    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)
    }
    ;
    
    0 讨论(0)
提交回复
热议问题