Condividi tramite


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

  • Anonymous
    May 17, 2004
    The comment has been removed

  • Anonymous
    May 17, 2004
    If I have such a problem I usually convert tabs to spaces by find/replace simple regexps (t) in my VS2003.

  • Anonymous
    May 17, 2004
    The comment has been removed

  • Anonymous
    May 17, 2004
    I wonder some day SQL team will present us with such a gift: Identation, proper captalization, etc...

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

  • Anonymous
    August 14, 2007
    Format Solution

  • Anonymous
    September 18, 2007
    PingBack from http://www.chriseargle.com/post/Format-Solution.aspx

  • Anonymous
    March 04, 2008
    In my past life I spent a few eons writing Java code. And it wasn't bad. We had nice tools like Jalopy

  • Anonymous
    August 15, 2008
    One of the features I worked on for the Visual Studio 2008 release was the new "Organize Usings"

  • Anonymous
    August 24, 2008
    OneofthefeaturesIworkedonfortheVisualStudio2008releasewasthenew

  • Anonymous
    October 10, 2008
    PingBack from http://aisweb.artinsoft.net/aisblogs/mrojas/archive/2008/03/04/pretty-printers-for-c.aspx/