Scott Kuhl

You Will Be Assimilated

  Home  |   Contact  |   Syndication    |   Login
  748 Posts | 1 Stories | 538 Comments | 364 Trackbacks

News


Search My Blog

Search GWB



Help CMTA!

Twitter












Tag Cloud


Archives

Post Categories

Image Galleries

My Sites

Visual Studio has this annoying habit of expanding the solution folder tree, especially when synchronizing with Visual SourceSafe.  Even without that problem I sometimes find myself with many expanded tree nodes in the Solution Explorer and I want a quick way to collapse them all.

So I searched for a macro based solution to the problem and came up with the VS.Net Macro: Collapse All from Captain LoadTest.  This macro was not designed to collapse a non-expanded folder's sub items.  So I made a few quick changes to get the macro to walk the entire tree and collapse every item that was expanded.

But is was not working.  Some items just were not collapsing, so I ended up with more items expanded than when I started because Visual Studio will expand the items while you walk the tree.  It turns out I ran into a bug in Visual Studio 2005 that should have been fixed in SP1 and that was marked as fixed by Microsoft, but still is not fixed.  Carlos J. Quintero mentioned a work-around in a Microsoft forum that seems to do the job.

The code to collapse everything does take longer to run (10 seconds across a very large solution, compared to 1 second with the original script) but it does work now.

So now that everything is working here is the code:

 

    Sub CollapseAll()

        ' Get the the Solution Explorer tree
        Dim solutionExplorer As UIHierarchy
        solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

        ' Check if there is any open solution
        If (solutionExplorer.UIHierarchyItems.Count = 0) Then
            Return
        End If

        ' Get the top node (the name of the solution)
        Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
        rootNode.DTE.SuppressUI = True

        ' Collapse each project node
        Collapse(rootNode, solutionExplorer)

        ' Select the solution node, or else when you click 
        ' on the solution window
        ' scrollbar, it will synchronize the open document 
        ' with the tree and pop
        ' out the corresponding node which is probably not what you want.

        rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
        rootNode.DTE.SuppressUI = False

    End Sub

    Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)

        For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
            If innerItem.UIHierarchyItems.Count > 0 Then

                ' Re-cursive call
                Collapse(innerItem, solutionExplorer)

                ' Collapse
                If innerItem.UIHierarchyItems.Expanded Then
                    innerItem.UIHierarchyItems.Expanded = False
                    If innerItem.UIHierarchyItems.Expanded = True Then
                        ' Bug in VS 2005
                        innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
                        solutionExplorer.DoDefaultAction()
                    End If
                End If

            End If
        Next

    End Sub
posted on Monday, April 09, 2007 10:43 AM

Feedback

# re: Visual Studio 2005 Collapse All Macro 5/10/2007 12:20 AM Amazing
This rocks my world. Works great!

# re: Visual Studio 2005 Collapse All Macro 6/10/2007 11:46 PM Umaima
Thanks a ton..I've been searching this since a long time...

You've helped a lot!!

Cheers!!

# re: Visual Studio 2005 Collapse All Macro 6/21/2007 8:39 PM Mr.V.Grateful
Marvellous - the standard CollapseAll macro that is quoted elsewhere means that sub-nodes are still expanded when a node is reopened.

But this works properly - all nodes are recursively collapsed fully.

Thanks so much !!


# re: Visual Studio 2005 Collapse All Macro 8/2/2007 11:46 AM JT
Does not work for me. I run the macro and nothing happens. No error message. No projects collapsed. Nothing.

# re: Visual Studio 2005 Collapse All Macro 8/3/2007 8:36 AM JT
Got it working. Need SP1 to fix macro bug in Vs2005. One downside with this macro is that Visual Studio calls my source control to check the check-in status of EVERY SINGLE file in the solution after the macro runs. This takes 2-3 minutes to do!!!!!!!!!

# re: Visual Studio 2005 Collapse All Macro 8/21/2007 5:15 AM JStuart
Great little macro. However, I had the same issue as JT above where it took several minutes to run as the status of EVERY file was checked. To get around this, I created another Macro and made a very minor alteration so that it only looks into a node if that node is expanded. Obviously this won't collapse nodes that are underneath an already collapsed node, but for my purposes I'll use it more often. The only change I made was within the Collapse sub routine just after the looping begins, instead of just:

If innerItem.UIHierarchyItems.Count > 0 Then

I made it:

If innerItem.UIHierarchyItems.Count > 0 And innerItem.UIHierarchyItems.Expanded Then

This resulted in a decent speed boost for me.

Again, thanks for the nice, recursive macro.

# re: Visual Studio 2005 Collapse All Macro 9/18/2007 9:38 PM Helge
Seriously ... i could collapse the list manually in less time...

I dont mind it runnning through all nodes, but in my oppinion it shoudl be able to sort out wether an item is already collapsed or not.


# re: Visual Studio 2005 Collapse All Macro 12/5/2007 1:59 PM sansay
I was working at editing another macro to do this, but after wasting a couple of hours I decided that somebody else would probably have figured it out. And sure enough here it is!
Thank you very much for this nice macro.

# re: Visual Studio 2005 Collapse All Macro 12/7/2007 3:35 AM nayr
I found this macro to be extremely useful, although somewhat slow at time due to our extremely large solutions.

I modified this macro (or a similar variant found on another website) to recursively collapse down to a user specified depth. The code I work with has such extensive folder structures that often do not need to be collapsed since I never visit most of it anyways.

I found that a depth of 2 is often very fast and usually suitable, but set it to 3 to cover all of the bases. But hey, you can select your own depth!

Enjoy.

Sub Collapse()

'Set the depth of collapsing
Dim depth As Integer
depth = 3

' Get the the Solution Explorer tree
Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (solutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If

' Get the top node (the name of the solution)
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI = True

' Collapse each project node
Collapse(rootNode, solutionExplorer, 0, depth)

' Select the solution node, or else when you click
' on the solution window
' scrollbar, it will synchronize the open document
' with the tree and pop
' out the corresponding node which is probably not what you want.

rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False

End Sub

Private Sub Collapse(ByVal item As UIHierarchyItem, _
ByRef solutionExplorer As UIHierarchy, _
ByRef Level As Integer, ByRef Depth As Integer)

'Collapse to the right Depth
If Level < Depth Then

For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then

' Re-cursive call
Collapse(innerItem, solutionExplorer, Level + 1, Depth)

' Collapse
If innerItem.UIHierarchyItems.Expanded Then
innerItem.UIHierarchyItems.Expanded = False
If innerItem.UIHierarchyItems.Expanded = True Then
' Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()
End If
End If

End If
Next

End If
End Sub

# Much faster solution 1/9/2008 1:54 AM Robert
CoolCommands contains a command for collapsing the solution explorer nodes. It is very fast.
http://weblogs.asp.net/gmilano/archive/2006/05/10/446010.aspx

# re: Visual Studio 2005 Collapse All Macro 5/16/2008 1:34 AM Pete
Dude, you ROCK!! I have been looking for a way to do this for a very long time. This is going to save me so much time. Much appreciated!

Thanks,
Pete Soheil
DigiOz Multimedia

# re: Visual Studio 2008 ExpandAll Macro? 8/15/2008 2:02 PM John
How to do the reverse? I want a macro to expand all projects and sub-items (including references, properties etc). I tried changing the logic in the above and do the opposite but that is not expanding sub-items. :-(

# re: Visual Studio 2005 Collapse All Macro 8/15/2008 5:00 PM Scott Kuhl
Expanding should work pretty much the same. Double check your code.

# re: Visual Studio 2005 Collapse All Macro 8/19/2008 7:06 AM Eden Ridgway
Thanks a lot for this. This is a real time saver!

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 7 and 1 and type the answer here: