开发者

auto format all files in solution in visual studio 2010

开发者 https://www.devze.com 2023-01-24 07:02 出处:网络
I\'m trying to auto-format all files in my solution in vs2010. I\'ve seen there is no built-in option to do it. I\'ve tried to write macro to do it, but I\'ve stucked.

I'm trying to auto-format all files in my solution in vs2010. I've seen there is no built-in option to do it. I've tried to write macro to do it, but I've stucked.

When I iterates any ProjectItem in my projects, I don't know how to open designable-files in source-code mode, or how to distinguish between sourceable f开发者_如何学Pythoniles and icourceable files (such as png-s or such).

Do you have any suggestions?

thanks.


Well instead of writing a macro check out a Code Maid (free on visualstudiogallery) has these 2 features:

  • Cleanup a single file, all selected items or the entire solution
  • Cleanup option to run automatically on save

With a number of options for what CleanUp does


I think the following should work for you it's based on other file iterating that I have done in Visual Studio. If you want to format other document types change the FormatFile routine to support other file extensions.

Sub IterateFiles()
    Dim solution As Solution = DTE.Solution
    For Each prj As Project In solution.Projects
        IterateProjectFiles(prj.ProjectItems)
    Next
End Sub

Private Sub IterateProjectFiles(ByVal prjItms As ProjectItems)
    For Each file As ProjectItem In prjItms
        If file.SubProject IsNot Nothing Then
            FormatFile(file)
            IterateProjectFiles(file.ProjectItems)
        ElseIf file.ProjectItems IsNot Nothing AndAlso file.ProjectItems.Count > 0 Then
            FormatFile(file)
            IterateProjectFiles(file.ProjectItems)
        Else
            FormatFile(file)
        End If
    Next
End Sub

Private Sub FormatFile(ByVal file As ProjectItem)
    DTE.ExecuteCommand("View.SolutionExplorer")
    If file.Name.EndsWith(".cs") OrElse file.Name.EndsWith(".vb") Then
        file.Open()
        file.Document.Activate()

        DTE.ExecuteCommand("Edit.FormatDocument", "")

        file.Document.Save()
        file.Document.Close()
    End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消