Visual Studio宏以格式化解决方案中的所有文件 [英] Visual Studio Macro to Format all Files in a Solution

查看:99
本文介绍了Visual Studio宏以格式化解决方案中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 and i don't fancy formatting each one individually.

我想知道如何创建一个宏来打开解决方案中具有.cs文件扩展名的每个文件,并简单地选择所有文本,然后剪切并粘贴(将对其进行相应的格式化).如果它也可以排序和删除using语句,那也很好,但这并不重要,因为我想这会更难一些.

I was wondering how to create a macro to open every file in the solution that has the .cs file extension and simply select all the text and then cut and paste it (which would format it accordingly). It would also be great if it could sort and removing the using statements aswell but this is not that important as i'd imagine this would be a little harder.

感谢您的帮助.谢谢

推荐答案

问题已解决!如果有人感兴趣,可以使用下面的宏进行欺骗:

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

这篇关于Visual Studio宏以格式化解决方案中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆