Geeks With Blogs
Path Notes of a Kodefu Master blog

There's a cool option in Visual Studio 2005 to format the current document (ctrl-e,d). But what do you do when you have thousands of files in your C# solution? I'm much too lazy to open every file individually to make sure other developers on the project are formatting their code properly.

The first idea was to change the build script. Doing this would ensure the code is always formatted (unless someone has a lock). Unfortunately, I could find no task do this in MSBuild. Naturally, I endeavored to build my own, but after trying to figure out the VS SDK I decided I should review other options. I chose to look into a macro since the command is already present within Visual Studio.

A quick google search brought me to Kevin Pilch-Bisson's blog explaining why the VS team didn't give a "Format Solution" option: "we figured that if you were consistently using VS, there wouldn’t be much need, since the formatting should be right already." I guess they didn't calculate large projects with developers who may not format habitually.

Kevin goes on to explain how he ended up needing the functionality and coded a macro for it. I tried using his macro, but it didn't like the way the solution was structured. In the solution I'm working on, there are several projects in subfolders within the solution. When stepping through the code I discovered you had to drill down into the SubProject property of some projects.

Here's my fixed version:

    Public Sub FormatSolution()

        Dim sol As Solution = DTE.Solution

        For i As Integer = 1 To sol.Projects.Count

            FormatProject(sol.Projects.Item(i))

        Next

    End Sub

    Private Sub FormatProject(ByVal proj as Project)

        For i As Integer = 1 To proj.ProjectItems.Count

            FormatProjectItem(proj.ProjectItems.Item(i))

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

                window.Activate()

                projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")

                window.Close(vsSaveChanges.vsSaveChangesYes)

            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

                FormatProjectItem(projectItem.ProjectItems.Item(i))

            Next
        End If


        'Format the SubProject if it exists.
        If Not projectItem.SubProject Is Nothing Then

            FormatProject(projectItem.SubProject)

        End If

    End Sub

 

 

The macro ended up modifying 600 files out of around 1600... not too shabby. I can only imagine what my poor wrists would have experienced had I done it manually. Posted on Tuesday, August 14, 2007 2:14 PM Kodefu | Back to top


Comments on this post: Format Solution

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Chris Eargle | Powered by: GeeksWithBlogs.net