How to format all files in Visual Studio 2012?

后端 未结 6 863
-上瘾入骨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) }
    

提交回复
热议问题