|
Thank you for posting this. It has been a big help.
|
|
|
|
|
please reference:
http://www.usysware.com/DPack/Default.aspx
Right click the solution, there's a menu item "Collapse all projects".
in VS2005, there's also a short cut mapped to this: SHIFT + ALT + C
DPack do a boundle of wonderful thing to improve developers' productvity, worth a try.
But, DPack's solution in vs2003 looks very slow compared to your macro's way.
I think it's better to use the macro for real large .sln file.
|
|
|
|
|
There is a bug in VS 2005 that keeps some nodes from collapsing. The fix and a code enhancement to collapse all sub-nodes can be found here: http://geekswithblogs.net/scottkuhl/archive/2007/04/09/111195.aspx
|
|
|
|
|
 I have made some improvements to Scott's code. My code can collapse selected item and runs faster than Scott's code on a very large solution.
<br />
Imports System<br />
Imports EnvDTE<br />
Imports EnvDTE80<br />
Imports System.Diagnostics<br />
<br />
Public Module Collapse<br />
<br />
Sub CollapseItem()<br />
<br />
' Get the the Solution Explorer tree<br />
Dim solutionExplorer As UIHierarchy<br />
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()<br />
<br />
' Check if there is any open solution<br />
If (solutionExplorer.UIHierarchyItems.Count = 0) Then<br />
Return<br />
End If<br />
<br />
' Get the top node (the name of the solution)<br />
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)<br />
rootNode.DTE.SuppressUI = True<br />
<br />
'Find selected node<br />
Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)<br />
<br />
' Collapse each node<br />
Collapse(selectedNode, solutionExplorer)<br />
<br />
' Select the selceted node<br />
selectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect)<br />
rootNode.DTE.SuppressUI = False<br />
<br />
End Sub<br />
<br />
Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)<br />
<br />
For Each innerItem As UIHierarchyItem In item.UIHierarchyItems<br />
If innerItem.UIHierarchyItems.Count > 0 Then<br />
<br />
If innerItem.UIHierarchyItems.Expanded Then<br />
' Re-cursive call<br />
Collapse(innerItem, solutionExplorer)<br />
End If<br />
<br />
' Collapse<br />
If innerItem.UIHierarchyItems.Expanded Then<br />
innerItem.UIHierarchyItems.Expanded = False<br />
If innerItem.UIHierarchyItems.Expanded = True Then<br />
' Bug in VS 2005<br />
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)<br />
solutionExplorer.DoDefaultAction()<br />
End If<br />
End If<br />
<br />
End If<br />
Next<br />
<br />
End Sub<br />
<br />
Private Function FindSelectedNode(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy) As UIHierarchyItem<br />
<br />
If item.IsSelected Then<br />
Return item<br />
End If<br />
For Each innerItem As UIHierarchyItem In item.UIHierarchyItems<br />
If innerItem.UIHierarchyItems.Count > 0 Then<br />
<br />
If innerItem.UIHierarchyItems.Expanded Then<br />
' Re-cursive call<br />
Dim result As UIHierarchyItem<br />
result = FindSelectedNode(innerItem, solutionExplorer)<br />
If Not result Is Nothing Then<br />
Return result<br />
End If<br />
End If<br />
<br />
If innerItem.IsSelected Then<br />
Return innerItem<br />
End If<br />
<br />
End If<br />
Next<br />
Return Nothing<br />
<br />
End Function<br />
<br />
<br />
End Module<br />
|
|
|
|
|
A simple but very useful utility, thanks!
|
|
|
|
|
I sometimes need to collapse more than just the top level. Here's some code to configure the depth.
Sub CollapseTop()
Collapse(3)
End Sub
Sub CollapseAll()
Collapse(-1)
End Sub
Sub Collapse(ByVal maxDepth As Integer)
' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item( _
Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
CollapseLevel(UIHItem, maxDepth)
Next
' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Sub CollapseLevel(ByVal node As UIHierarchyItem, ByVal maxDepth As Integer, Optional ByVal currentLevel As Integer = 0)
'would like to be able to determine if this was a folder. But didn't see the option on the object.
If (maxDepth = -1 Or currentLevel < maxDepth) Then
currentLevel = currentLevel + 1
node.UIHierarchyItems.Expanded = False
Dim UIHItem As UIHierarchyItem
For Each UIHItem In node.UIHierarchyItems
CollapseLevel(UIHItem, maxDepth, currentLevel)
Next
End If
End Sub
-- modified at 17:36 Tuesday 14th February, 2006
|
|
|
|
|
Sub CollapseAll()
' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item( _
Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
CollapseRec(UIHSolutionRootNode)
' Collapse each project node
' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Sub CollapseRec(ByVal UIHParent As UIHierarchyItem)
Dim UIHItem As UIHierarchyItem
If UIHParent Is Nothing Then
Return
End If
' Check if there is any open solution
If (UIHParent.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
For Each UIHItem In UIHParent.UIHierarchyItems
CollapseRec(UIHItem)
UIHItem.UIHierarchyItems.Expanded = False
Next
End Sub
Bnaya Eshet
Chief Architect at
Wise Mobility
www.wisemobility.com
|
|
|
|
|
Great makro. Works in VS 2003 as well.
M
|
|
|
|
|
while i am not a total fan of having the solution explorer follow the open code, i find i need to keep it turned on since it causes more problems turning it off.
as a result a rather large tree ends up open all over the place, making a complete mess. this is ideal
zen is the art of being at one with the two'ness
|
|
|
|
|
I modified the macro slightly so it will collapse all subfolders in the solution tree as well...
'------------------------------------------------------------------------------
Sub CollapseMe(oRootItem As UIHierarchyItem)
Dim oChildItem As UIHierarchyItem
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next
oRootItem.UIHierarchyItems.Expanded = False
End Sub
'------------------------------------------------------------------------------
Sub CollapseAll()
'DESCRIPTION: Colapse all the nodes in the project tree
' Get the the Solution Explorer tree
Dim oSolutionExplorer As UIHierarchy
oSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (oSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
' Get the top node (the name of the solution)
Dim oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem
' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next
' 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.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Charles
|
|
|
|
|
Hi Edwin,
Have you tried applying your macro to ClassView? For some reason doing this
DTE.Windows.Item(Constants.vsext_wk_ClassView).Object()
always gives you Nothing.
I've also tried:
DTE.Windows.Item(Constants.vsWindowKindClassView).Object()
Same result.
Referencing the ClassView window is OK though. It's only when you apply .Object that you get Nothing.
Kevin
Kevin
|
|
|
|
|
Apparently the classview does not implement anything at all to be able to access the tree ,
but here is a trick that could help you out :
There are commands to switch the type of treeview within the classview :
1. View.ClassViewGroupByType
2. View.SortAlphabetically
3. View.SortByAccess
4. View.SortByType
If you call 1. and 4. using
dte.ExecuteCommand("View.ClassViewGroupByType", "");
dte.ExecuteCommand("View.ClassViewSortByType", "");
The tree is collapsed , sadly enough i did not find a way yet to test which one is
active so it could be that you end up with enother sort then you started off with
Brgds,
Erlend
Erlend Robaye
Fluxys N.V.
Belgium
Erland.Robaye@fluxys.net
|
|
|
|
|
Great macro.
How did you add the ToolButton which says "Collapse All" in the Solution Explorer's toolbar?
Thanks.
PKP
|
|
|
|
|
Here is a explanation of how to add a button to the toolbar: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q318651#4. Start with step 6.
If you want to do this programmatically, here is what you could do: (I wrote an Add-In version of this same feature in C#)
Command command = commands.AddNamedCommand(addInInstance, "CollapseAll", "CollapseAll", "Collapses all the project nodes in the Solution Explorer", true, <br />
41,
ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);<br />
<br />
CommandBar commandBar = (CommandBar)commandBars["Standard"];<br />
int nPositionEnd = commandBar.Controls.Count + 1; <br />
CommandBarControl commandBarControl = command.AddControl(commandBar, nPositionEnd);
-Edwin
|
|
|
|
|
Hi Edwin,
Thanks for the reply.
I knew how to add a CommandBarControl to a toolbar (via code or using Tools|Customize. However in your screenshot you had added a "CollapseAll" button to a toolbar which seems like part of "Solution Explorer". That toolbar does not seem to be customizable using Tools|Customize.
So how did you do it? I will be incorporating this in an add-in.
You did mention how can I add a toolbutton to the "Standard" toolbar. But how can I add it to a Solution Explorer toolbar? Is there a string name for that toolbar?
I will appreciate your help in this regard.
Thanks.
Paresh
|
|
|
|
|
If you look at the pictures again, you'll see that Edwin's button is *not* inside the Solution Explorer.
|
|
|
|
|
This is a great macro, but I wouldn't call the auto-sync feature in the Solution Explorer a "great feature" .
Thanks,
- Stephen Jones
|
|
|
|
|
|
It was great, but not quite what I was looking for. Here's my modifications, which allow the macro to collapse all subfolders of expanded folders as well. It does not deal with expanded subfolders in collapsed folders, as that takes much too long (it actually seemed to hang my VS).
Enjoy!
Sub CollapseNode(ByRef item As UIHierarchyItem)
Dim subitem As UIHierarchyItem
For Each subitem In item.UIHierarchyItems
If (subitem.UIHierarchyItems.Expanded = True) Then
CollapseNode(subitem)
subitem.UIHierarchyItems.Expanded = False
End If
Next
End Sub
Sub CollapseAll()
' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
CollapseNode(UIHSolutionRootNode)
' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
|
|
|
|
|
Hey, thanks Booga! That's just what I need with the new project I'm working on.
-Edwin
|
|
|
|
|
Thanks, exactly what I needed.
|
|
|
|