Pretty Printers / Format Code / for C# VB.NET

4. March 2008 09:49 by Mrojas in General  //  Tags: , , ,   //   Comments (0)

In my past life I spent a few eons writing Java code. And it wasn't bad. We had nice tools like Jalopy! that allowed us to have
code in a very standard way.

And I missed that. I've been looking around for something similar but I havent found anything like that :(

Until I found a great post from Chris Eargle, he improved the original solution from Kelvinpinch

Well here's the code.

Public Sub FormatSolution()
Dim sol As Solution = DTE.Solution
For i As Integer = 1 To sol.Projects.Count
End Sub 
Private Sub FormatProject(ByVal proj as Project)
     For i As Integer = 1 To proj.ProjectItems.Count
End Sub
Private Sub FormatProjectItem(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)
End If
End If
'Be sure to format all of the ProjectItems.
If Not projectItem.ProjectItems Is Nothing Then
For i As Integer = 1 To projectItem.ProjectItems.Count
End If
'Format the SubProject if it exists.
     If Not projectItem.SubProject Is Nothing Then
End If
End Sub

To use it perform the following steps:

  •  Go to the VS IDE Tools Option
  • Then Select the Macros option and select Macros IDE...
  • This will open the macros IDE
  • In the Macros IDE navigate to the Module1, and Insert the code

To run the Macro go to Tools\Macros\Macro Explorer

And select FormatAll :)

And last but not least if you want to runit from the command line just do:

devenv /command "Macros.MyMacros.Module1.FormalAll" MyProject.csproj or

devenv /command "Macros.MyMacros.Module1.FormalAll" MySol.sln or