LoginSignup
1
0

More than 5 years have passed since last update.

Visual Studio2010以前でソリューション内のC#ファイルを再フォーマットする

Last updated at Posted at 2013-02-07

こんなマクロでできたっぽい。
デザイナが生成したファイルは.Designer.csで終わることを条件にして無視してます。
問題があるとしたら、2012からはマクロが消えたので使えない、というところですかね。
誰か拡張機能つくってくだしあ。

Imports System
Imports EnvDTE
Imports EnvDTE80

Public Module Module1
    Sub FormatAllLines(ByVal item As ProjectItem)
        Dim win As Window = item.Open()
        win.Activate()
        DTE.ExecuteCommand("Edit.FormatDocument")
        ' usingの整理もする場合は下の行を有効にしてね!
        ' DTE.ExecuteCommand("Edit.RemoveAndSort")
        item.Save()
        win.Close()
    End Sub

    Sub FormatProjectItems(ByVal items As ProjectItems)
        For Each item As ProjectItem In items
            If item.Name.EndsWith(".cs") AndAlso Not item.Name.EndsWith(".Designer.cs") Then
                FormatAllLines(item)
            End If

            If Not IsNothing(item.ProjectItems) Then
                FormatProjectItems(item.ProjectItems)
            End If
        Next
    End Sub

    Sub FormatAllProjectItem(ByVal proj As Project)
        If proj.Kind = ProjectKinds.vsProjectKindSolutionFolder Then
            For Each pitem In proj.ProjectItems
                FormatAllProjectItem(CType(pitem.Object, Project))
            Next
        Else
            FormatProjectItems(proj.ProjectItems)
        End If
    End Sub

    Sub TemporaryMacro()
        For Each proj In DTE.Solution.Projects
            FormatAllProjectItem(proj)
        Next
    End Sub
End Module
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0