Wednesday, February 11, 2009

[VS] Collapse all projects in a solution + How to setup Macro

This is not about increasing productivity - it's about being awesome.

How many times have you been collapsing manually all the projects in your solution ranting about the fact there's no option to do that automagically? Loadza times is the answer - unless you only work on small projects (which is not a crime btw). 

Anyway - after swearing at the screen a number of times I decided it was about time to do something about it and I googled it.

I found loads of awesome VS Macros to do what I was looking for but I hadn't a clue about how to create a VS Macro so I thought I'd share the whole thing.

First thing you need to go to Visual Studio Tools --> Macros --> Macro Explorer. Once you got that right click on MyMacros and create a new module (I called it CollapseAll).

Now edit the new module (double-click on it) erase whatever is in there and paste this stuff into it (if you want you can even try to understand what it does - but it's optional):


'Awesome Macros ripped-off
'from http://www.milkcarton.com/blog/

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module CollapseAll

Public Sub CollapseTopLevel()
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()

Dim solutionWindow As EnvDTE.Window = DTE.ActiveWindow
solutionWindow.Visible = False
Dim solution As UIHierarchyItem = GetSolution()

CollapseHierarchy(solution.UIHierarchyItems, True, True)
solutionWindow.Visible = True
DTE.StatusBar.Clear()
DTE.StatusBar.Progress(False)
End Sub

Private Sub CollapseHierarchy(ByRef items As UIHierarchyItems, ByVal IsRoot As Boolean, ByVal OnlyCollapseRootLevel As Boolean)
For i As Int32 = 1 To items.Count
If IsRoot Then DTE.StatusBar.Progress(True, "Collapsing", i, items.Count)
If (items.Item(i).UIHierarchyItems.Count > 0 And Not OnlyCollapseRootLevel) Then
DTE.StatusBar.Text = "Collapsing " & items.Item(i).Name
CollapseHierarchy(items.Item(i).UIHierarchyItems, False, False)
End If
items.Item(i).UIHierarchyItems.Expanded = False
Next
End Sub

Private Function GetSolution() As UIHierarchyItem
Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
Dim uih As UIHierarchy = win.Object
GetSolution = uih.UIHierarchyItems.Item(1)
End Function

End Module


Now, the hard work is done - You'll see your CollapseTopLevel as a child of the CollapseAll module in you Macro Explorer and you can run it double clicking on it but we all know that sucks so the cool thing to do now is to assign an hotkey chord/combination to our kick-ass macro.

To do this you need to go to Tools-->Options-->Environment-->Keyboard. Pick your macro from the listBox with all the default VS stuff (remember it will be there like MyMacros.CollapseAll.CollapseTopLevel) and then assign a hotkey combination or chord to it (I used CTRL+K, CTRL+Y since it's not used by VS) and save. 

That's all.

P.S. I'd really like to thank Dan, who wrote the snippets I brutally ripped-off. The original article is awesome and there's plenty of nitty-gritty cut and paste potential there for developers in need.

kick it on DotNetKicks.com

6 comments:

Dan Morphis said...

I had a feeling when I saw the title of your post that you were using my macros. Thank you for pointing that out in your article, and linking back. I think your blog post adds value to what I did. I'll link back to your post from mine.

Johnny Idol said...

@Dan

I meant to leave note on your article saying that I was using your stuff but then I noticed I could not comment.

I hope you won't mind if I brutally ripped the Macros off - I figured mentioning you and throwing a bunch of links on the post would've been a decent compensation!

Anonymous said...

Johnny Idol shamelessly ripping off another man's code?! Now there's a surprise... :) I would have done the same but just not given any credit. You're a man of high standards, Johnny.

Johnny Idol said...

the trick is to set very low standards in order to look good

Nexii Malthus said...

Erm, im a newbie and new to visual studio but was quick to figure out how to right click -> Outlining menu to quickly collapse all, so I guess I must be smart to find something other people have to find silly ways around?

Johnny Idol said...

@Nexii

not clear which 'outlining' context menu you're referring to.

If you're talking about Edit-->Outlining that's not gonna collapse your projects in the solution explorer.

If you have a simpler way anyway plz share so we can get rid of the "silly way around" :-)