Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VS Solution Explorer – Collapse All

0.00/5 (No votes)
2 Apr 2011 1  
VS Solution Explorer – Collapse All

I never knew I needed this until I saw it was a feature in a ReSharper demo. After a quick search, I found a macro that has been around for years. Slap to the head! The amount of time I spent collapsing all my solution folders and this existed the whole time!

I never use macros, but even I was able to quickly get this working.

  • Open Tools->Macros->Macro Explorer (Alt-F8)
  • Right click MyMacros
  • Select New Module
  • Select default template and name to VSMacros
  • Right-click VSMacros and click New Macro

Copy and paste this over the opened macro file.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module VSMacros
Sub SolutionCollapseAll()

‘ Get 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 IfGet the top node (the name of the solution)
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI = True
‘Find selected node
Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)

‘ Collapse each node
Collapse(selectedNode, solutionExplorer)
‘ Select the selected node
selectedNode.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
If innerItem.UIHierarchyItems.Expanded Then
‘ Re-cursive call
Collapse(innerItem, solutionExplorer)
End If

‘ 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
Private Function FindSelectedNode(ByVal item As UIHierarchyItem, 
    ByRef solutionExplorer As UIHierarchy) As UIHierarchyItem

If item.IsSelected Then
Return item
End If
For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then
If innerItem.UIHierarchyItems.Expanded Then

‘ Re-cursive call
Dim result As UIHierarchyItem
result = FindSelectedNode(innerItem, solutionExplorer)
If Not result Is Nothing Then
Return result
End If
End If

If innerItem.IsSelected Then
Return innerItem
End If
End If
Next
Return Nothing

End Function
End Module
  • You may now execute the macro via the Macro Explorer. Click the folder you wish to collapse (or the Solution root to collapse everything). Right click and run the macro. To set a shortcut, continue on with the steps below…
  • Click Tools->Options->Environment->Keyboard
  • Search for your macro in the Show commands containing textbox.
  • Select your macro, keep scope global, and in the assign macro keys textbox, enter your preferred shortcut (I used “ALT-CTRL-\”)
  • Click ASSIGN and then OK
  • To use, select a node and then ALT-CTRL-\. To collapse the entire solution, select the root of the solution.

Reference: http://www.codeproject.com/KB/macros/collapseall.aspx?df=100&forumid=7565&exp=0&select=396167

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here