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.


Comments (12)

  1. Jacob Morgan says:

    not only would a "Format Project" option have been nice, a Nant/MSBuild/etc task would have been really nice also.

    In our shop we just get nasty during the coding, and we don’t expect everyone to have the same formatting settings in their IDE. During continuous builds, we run an ant Jalopy task to get everything looking sharp. It saves a lot of time all around. Of course this is just Java. Our .NET stuff is pretty much up to the developer.

  2. If I have such a problem I usually convert tabs to spaces by find/replace simple regexps (t) in my VS2003.

  3. Find/Replace works okay for that specific option, but what if I changed the option to put method/property braces on the same line, while still leaving type and namespace braces on a new line.

    I think this macro would be useful anytime you change options.

  4. I wonder some day SQL team will present us with such a gift: Identation, proper captalization, etc…

  5. Ron Erwin says:

    Conversion code/wizards, are the bane of our industry.. how to make them satisfy everyone.. keep up the good work!!

  6. In my past life I spent a few eons writing Java code. And it wasn't bad. We had nice tools like Jalopy

  7. Park Place says:

    One of the features I worked on for the Visual Studio 2008 release was the new "Organize Usings"

  8. Qingping says:

    OneofthefeaturesIworkedonfortheVisualStudio2008releasewasthenew