Formatting all C# files in a solution

Formatting All Files in a Solution

One of the features I’ve worked on quite a bit during Whidbey is automatic formatting of source code. We’ve done quite a bit of work in this area including:

  • Making formatting affect more than just indentation
  • Adding many more options about how code is formatted.
  • Making sure that code which is generated by VS is formatted according to your settings.

Because of this, we also added the Format Document command, which will format the entire current document, instead of just the selection.

One of the things we thought about adding a command to format the entire project, or the entire solution. We decided not to do it, because we figured that if you were consistently using VS, there wouldn’t be much need, since the formatting should be right already.

So then, a little while ago, the C# profile changed its default so that spaces are used for indentation instead of tabs. I personally prefer this, but most of my side projects had been written using tabs, because that was the old default, and when I was working on a lot of those projects, the Import/Export Settings feature wasn’t working, so I just left the defaults. Please, I don’t need a bunch of comments about the default options in the profile. Joe or Anson’s blogs are much better places for that.

So now I have a solution where all the new code has spaces, but the old code has tabs. I wanted to convert all of the files to use spaces, but didn’t want to manually open and issue a Format Document command for about 100 files.

Instead, I broke out the VS Macro IDE, and decided to write a macro to do it. After struggling with VB’s variable declaration syntax for about an hour, I finally came up with this:

    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")

                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

To use it, launch VS, then go to Tools->Macros->Macros IDE. Paste the code inside the Module, and then close the Macros IDE. Next go to Tools->Macros->Macros Explorer, and double click the “FormatAll” macro.

Presto: All files in the solution formatted according to your settings.

NOTE: This macro has been tested on exactly 1 solution. It will close all files during processing, and it may or may not work if your projects are under source control. Use at your own risk.