Visual Studio宏以格式化解决方案中的所有文件

时间:2010-10-15 14:05:21

标签: visual-studio-2008 macros

我最近为我的代码确定了一种新格式样式,并希望替换现有代码。唯一的问题是,如果不是我的解决方案中的1000个文件,那就是100个,而且我不想单独格式化每个文件。

我想知道如何创建一个宏来打开具有.cs文件扩展名的解决方案中的每个文件,只需选择所有文本然后剪切并粘贴它(相应地格式化它)。如果能够对使用语句进行排序和删除也会很棒,但这并不重要,因为我认为这会有点困难。

我很感激你的帮助。感谢

3 个答案:

答案 0 :(得分:8)

问题解决了!以下宏做了任何人都感兴趣的技巧:

   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

答案 1 :(得分:1)

对于Visual Studio 2008,您必须更改格式语句,因为FormatDocument不可用:

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

所以脚本应该是:

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

答案 2 :(得分:1)

有一种新的方法可以格式化解决方案中的所有文件,而无需使用dotnet CLI:

  1. 通过运行以下命令来安装dotnet format
    dotnet tool install -g dotnet-format
  2. 运行它,并使用以下命令行将SolutionFile.sln替换为解决方案文件的路径:
    dotnet format SolutionFile.sln