jump to navigation

VS Solution Explorer – Collapse All May 15, 2009

Posted by codinglifestyle in CodeProject, Visual Studio.
Tags: , , , , ,
trackback

I never knew I needed this until I saw it was a feature in a ReSharper demo.  After a quick search and I found a macro that has been around for years.  Slap to the head!  The amount of time I spend 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.

  1. Open Tools->Macros->Macro Explorer (Alt-F8)
  2. Right click MyMacros
  3. Select New Module
  4. Select default template and name to VSMacros
  5. Right-click VSMacros and click New Macro
  6. 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 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

    ‘Find selected node

    Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)

    ‘ Collapse each node

    Collapse(selectedNode, solutionExplorer)

    ‘ Select the selceted 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

     

  7. 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…
  8. Click Tools->Options->Environment->Keyboard
  9. Search for your macro in the Show commands containing textbox
  10. Select your macro, keep scope global, and in the assign macro keys textbox enter your preferred shortcut (I used “ALT-CTRL-\”)
  11. Click ASSIGN and then OK
  12. 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: