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
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
There's a new way to format all files in a solution without using a macro using the dotnet
CLI:
dotnet tool install -g dotnet-format
SolutionFile.sln
with the path to your solution file, with the following command line:dotnet format SolutionFile.sln
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