Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Monday, May 17, 2004 10:40 AM by kevinpilch-bisson

Comments

# re: Formatting all C# files in a solution

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.
Monday, May 17, 2004 11:07 AM by Jacob Morgan

# re: Formatting all C# files in a solution

If I have such a problem I usually convert tabs to spaces by find/replace simple regexps (\t) in my VS2003.
Monday, May 17, 2004 2:07 PM by Anatoly Lubarsky

# re: Formatting all C# files in a solution

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.
Monday, May 17, 2004 2:19 PM by Kevin Pilch-Bisson

# RE: Formatting all C# files in a solution

I wonder some day SQL team will present us with such a gift: Identation, proper captalization, etc...
Monday, May 17, 2004 3:07 PM by Luciano Evaristo Guerche

# Whidbey indentation changes

Tuesday, May 18, 2004 4:42 PM by Kevin Dente's RantLog

# re: Formatting all C# files in a solution

Conversion code/wizards, are the bane of our industry.. how to make them satisfy everyone.. keep up the good work!!
Monday, June 14, 2004 5:44 PM by Ron Erwin

# Format Solution

Format Solution

Tuesday, August 14, 2007 3:14 PM by Chris Eargle

# Format Solution

Tuesday, September 18, 2007 6:05 PM by Format Solution

# Pretty Printers for C#

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

Tuesday, March 04, 2008 5:46 PM by Mauricio Rojas Blog
New Comments to this post are disabled
 
Page view tracker