Visual Studio Macro to Format all Files in a Solution

后端 未结 3 1916
青春惊慌失措
青春惊慌失措 2020-12-10 19:38

I\'ve recently settled on a new format style for my code and want to replace the existing code. The only problem is there\'s 100\'s if not 1000\'s of files in my solution a

相关标签:
3条回答
  • 2020-12-10 20:09

    For Visual Studio 2008 you have to change the format statements, because FormatDocument is not available:

    projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
    projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
    

    So the script should be:

    Public Sub FormatAll()
        Dim sol As Solution = DTE.Solution
        For i As Integer = 1 To sol.Projects.Count
            Dim proj As Project = sol.Projects.Item(i)
            For j As Integer = 1 To proj.ProjectItems.Count
                FormatSome(proj.ProjectItems.Item(j))
            Next
        Next
    End Sub
    
    Private Sub FormatSome(ByVal projectItem As ProjectItem)
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If projectItem.Name.LastIndexOf(".cpp") = projectItem.Name.Length - 4 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            ElseIf projectItem.Name.LastIndexOf(".h") = projectItem.Name.Length - 2 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End If
    
        For i As Integer = 1 To projectItem.ProjectItems.Count
            FormatSome(projectItem.ProjectItems.Item(i))
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-10 20:18

    There's a new way to format all files in a solution without using a macro using the dotnet CLI:

    1. Install dotnet format by running the following command:
      dotnet tool install -g dotnet-format
    2. Run it, replacing SolutionFile.sln with the path to your solution file, with the following command line:
      dotnet format SolutionFile.sln
    0 讨论(0)
  • 2020-12-10 20:21

    Problem solved! The following macro did the trick incase anyone is interested:

       Public Module FormatAll
            Public Sub FormatAll()
                Dim sol As Solution = DTE.Solution
                For i As Integer = 1 To sol.Projects.Count
                    Dim proj As Project = sol.Projects.Item(i)
                    For j As Integer = 1 To proj.ProjectItems.Count
                        FormatSome(proj.ProjectItems.Item(j))
                    Next
                Next
            End Sub
    
            Private Sub FormatSome(ByVal projectItem As ProjectItem)
                If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
                    If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                        Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                        window.Activate()
                        projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
                        projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
                        window.Close(vsSaveChanges.vsSaveChangesYes)
                    End If
                End If
    
                For i As Integer = 1 To projectItem.ProjectItems.Count
                    FormatSome(projectItem.ProjectItems.Item(i))
                Next
            End Sub
        End Module
    
    0 讨论(0)
提交回复
热议问题