用于格式化解决方案中所有文件的 Visual Studio 宏 [英] Visual Studio Macro to Format all Files in a Solution
问题描述
我最近为我的代码确定了一种新的格式样式,并想替换现有的代码.唯一的问题是我的解决方案中有 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.
非常感谢您的帮助.谢谢
I'd appreciate your help. Thanks
推荐答案
问题解决了!如果有人感兴趣,下面的宏就起到了作用:
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屋!