Chris Eargle

Life Student of the Kodefu Arts

Format Solution

by chris 14. August 2007 19:14

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.

My 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.

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

visual studio, macro, vbscript

Kodefu

E-mail | Kick it! | DZone it! | del.icio.us
Permalink | Comments (5) | Post RSSRSS comment feed

Related posts

WCF 3.5 Security GuidelinesThe patterns & practices WCF Security Guidance project has released the the WCF 3.5 Security Guideli...Vista UX Guidelines in PDFMicrosoft has released the Vista UX Guidelines in the pdf format.ADO.NET Data Services CTPADO.NET Data Services is the new name for project "Astoria".

Comments

October 17. 2007 22:31

Tony Evans

Just what I was looking for. Thanks! :o)

Tony Evans us

March 4. 2008 04:46

pingback

Pingback from blogs.artinsoft.net

Pretty Printers for C# - Mauricio Rojas Blog

blogs.artinsoft.net

April 17. 2008 03:47

Joe

I added a couple of lines to FormatProjectItem() to avoid closing files that are initially open when the macro is run.

Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)
Dim fileIsOpen As Boolean = False
If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
'If this is a c# file
If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
'Set flag to true if file is already open
fileIsOpen = projectItem.IsOpen
Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
'Only close the file if it was not already open
If Not fileIsOpen Then
window.Close(vsSaveChanges.vsSaveChangesYes)
End If
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

Joe us

August 15. 2008 12:55

pingback

Pingback from blogs.msdn.com

Park Place : Organize Usings Across Your Entire Solution

blogs.msdn.com

Saving the comment

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 19. 2008 12:24

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Chris Eargle Chris Eargle
Enterprise .NET developer in Columbia, SC.

E-mail me Send mail

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Pages

    Recent posts

    • Create Vista Icons in VS 2008Comments: 0Rating: 0 / 0
    • String to StreamComments: 0Rating: 0 / 0
    • Hung Database RestoreComments: 0Rating: 0 / 0
    • MSI Compilation ErrorComments: 0Rating: 0 / 0
    • Moving Parts and TimeoutsComments: 0Rating: 0 / 0
    • C# 3.0 PresentationComments: 1Rating: 0 / 0
    • Augusta Meeting TonightComments: 0Rating: 5 / 3
    • WCF 3.5 Security GuidelinesComments: 0Rating: 0 / 0
    • .NET 3.5 Enhancements Training KitComments: 0Rating: 0 / 0
    • Sessions GaloreComments: 1Rating: 0 / 0

    Recent comments

    • C# 3.0 Presentation (1)
      vijay wrote: Good post Thanks, Vijay [More]
    • Format Solution (4)
      Joe wrote: I added a couple of lines to FormatProjectItem() t… [More]
    • Sessions Galore (1)
      Lou wrote: I'll have to get you down here soon - I'll e-mail … [More]
    • Redeemed (3)
      Fred Beiderbecke wrote: It wasn't you, it was some of the others in the ro… [More]
    • South Florida Code Camp (1)
      Jason Meridth wrote: You've mentioned the only latest difference betwee… [More]
    • Redeemed (3)
      Chris Eargle wrote: Hmm, the only thing derisive I remember saying abo… [More]
    • Redeemed (3)
      Fred Beiderbecke wrote: I was in the afternoon session and enjoyed it (exc… [More]
    • Format Solution (4)
      Tony Evans wrote: Just what I was looking for. Thanks! :o) [More]
    • XML Nugget of Joy (2)
      Chris Eargle wrote: How do you retrieve the description, and in what c… [More]
    • XML Nugget of Joy (2)
      Wesley Wilson wrote: That's pretty neat. I've just started using attrib… [More]

    Archive

    • 2008
      • August (3)
      • July (1)
      • June (1)
      • April (4)
      • March (1)
      • February (4)
      • January (5)
    • 2007
      • December (5)
      • November (1)
      • October (6)
      • September (3)
      • August (1)
      • June (1)

    Authors

    • RSS feed for chrischris (36)

    Tags

    • activex
    • addin
    • ado.net data services
    • ajax
    • architecture
    • astoria
    • beta
    • c#
    • code camp
    • com
    • consolas
    • continuous integration
    • ctp
    • database
    • deployment
    • design
    • download
    • ebook
    • entity
    • expression blend
    • font
    • framework
    • gadget
    • grid
    • guidelines
    • icon
    • lamdba
    • linq
    • macro
    • msbuild
    • msi
    • mvc
    • powertoy
    • preview
    • ray ozzie
    • regasm
    • security
    • serialization
    • silverlight
    • source code
    • sql server
    • starter kit
    • stream
    • string
    • trial
    • usability
    • ux
    • vbscript
    • vista
    • visual studio
    • vs2008
    • wcf
    • web
    • winforms
    • wpf
    • xml

    Categories

    • RSS feed for Bleeding EdgeBleeding Edge (3)
    • RSS feed for GeneralGeneral (1)
    • RSS feed for KodefuKodefu (8)
    • RSS feed for Path NotesPath Notes (6)
    • RSS feed for PresentationPresentation (2)
    • RSS feed for TechniquesTechniques (2)
    • RSS feed for TrainingTraining (5)
    • RSS feed for WeaponsWeapons (4)
    • RSS feed for ZenZen (4)

    Archive

    Blogroll

    • RSS feed for Structure Too BigStructure Too Big
      • MSDN Event Thursday in...
      • WorldMaps Update
      • ASP.NET University!
    • RSS feed for Chris CraftChris Craft
      • Pimp My Phone – D...
      • Pimp My Phone – D...
      • Pimp My Phone – D...
    Download OPML file OPML

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in