private string TraverseItems(UIHierarchyItems items) { string returnVal = null; UIHierarchyItem item; for (int i = 0; i < items.Count; i++) { item = items.Item(i+1); string name = item.Name; if (name.EndsWith(".zprj", StringComparison.CurrentCultureIgnoreCase)) { ProjectItem prj = item.Object as ProjectItem; for (short j = 0; j < prj.FileCount; j++) { returnVal = prj.get_FileNames(j); break; } if (returnVal != null) break; } if ((returnVal == null) && (item.UIHierarchyItems != null)) { returnVal = TraverseItems(item.UIHierarchyItems); } if (returnVal != null) break; } return returnVal; }
/// <summary> /// Gets the <see cref="UIHierarchyItem"/> from dte with the given name /// </summary> /// <param name="hierarchyItems">Current hierarchy items to look for</param> /// <param name="name">Name of the item</param> /// <returns>null if not found otherwise returns the found item</returns> public static UIHierarchyItem GetHierarchyItem(this UIHierarchyItems hierarchyItems, string name) { // Get count int count = hierarchyItems.Count; // Loop for (int i = 1; i <= count; i++) { // Get inner item UIHierarchyItem hierarchyItem = hierarchyItems.Item(i); if (hierarchyItem.Name.Equals(name)) { return(hierarchyItem); } if (hierarchyItem.UIHierarchyItems.Count != 0) { UIHierarchyItem childItem = hierarchyItem.UIHierarchyItems.GetHierarchyItem(name); if (childItem != null) { return(childItem); } } } return(null); }
internal void SelectProjectItem(ProjectItem projectItem) { UIHierarchy tree = projectItem.DTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Object as UIHierarchy; UIHierarchyItems solutionItems = tree.UIHierarchyItems; // // ExpandView dont (always) work it item is inside solution folders. // The hack below worked, but it expanded all solution folders, and since we walk the solution explorer // in FindHierarchyItem we might as well expand at the same time // //try //{ // proItem.ExpandView(); //} //catch (Exception) //{ // //bug: expand project dont work if a soultion folder is parent and it hasnt been expanded yet // LoadSolutionItems(items); // proItem.ExpandView(); //} //check if we have a solution if (solutionItems.Count != 1) { return; } //FindHierarchyItem expands nodes as well (it must do so, because children arent loaded until expanded) UIHierarchyItem uiItem = FindHierarchyItem(solutionItems.Item(1).UIHierarchyItems, projectItem); if (uiItem != null) { //if we called DoDefaultAction in FindHierarchyItem, solution explorer will have focus //set it back to the dicument //projectItem.Document.Activate(); //didnt find another way to make the item gray (while not having focus) //dte.ExecuteCommand("View.TrackActivityinSolutionExplorer", "True"); //if item is already selected, View.TrackActivityinSolutionExplorer wont make the item grey, //so deselect it first if (uiItem.IsSelected) { uiItem.Select(vsUISelectionType.vsUISelectionTypeToggle); } //selecting while View.TrackActivityinSolutionExplorer selects and makes it grey uiItem.Select(vsUISelectionType.vsUISelectionTypeSelect); //done with it now //dte.ExecuteCommand("View.TrackActivityinSolutionExplorer", "False"); } else { } }
public static void SelectInSolutionExplorer(string selected) { UIHierarchy solutionExplorer = (UIHierarchy)DTE.Windows.Item(EnvDTE.Constants.vsext_wk_SProjectWindow).Object; UIHierarchyItem rootNode = solutionExplorer.UIHierarchyItems.Item(1); Stack <Tuple <UIHierarchyItems, int, bool> > parents = new Stack <Tuple <UIHierarchyItems, int, bool> >(); ProjectItem targetItem = DTE.Solution.FindProjectItem(selected); if (targetItem == null) { return; } UIHierarchyItems collection = rootNode.UIHierarchyItems; int cursor = 1; bool oldExpand = collection.Expanded; while (cursor <= collection.Count || parents.Count > 0) { while (cursor > collection.Count && parents.Count > 0) { collection.Expanded = oldExpand; Tuple <UIHierarchyItems, int, bool> parent = parents.Pop(); collection = parent.Item1; cursor = parent.Item2; oldExpand = parent.Item3; } if (cursor > collection.Count) { break; } UIHierarchyItem result = collection.Item(cursor); ProjectItem item = result.Object as ProjectItem; if (item == targetItem) { result.Select(vsUISelectionType.vsUISelectionTypeSelect); return; } ++cursor; bool oldOldExpand = oldExpand; oldExpand = result.UIHierarchyItems.Expanded; result.UIHierarchyItems.Expanded = true; if (result.UIHierarchyItems.Count > 0) { parents.Push(Tuple.Create(collection, cursor, oldOldExpand)); collection = result.UIHierarchyItems; cursor = 1; } } }
private UIHierarchyItem LocateHierarchyItem(UIHierarchyItems items, object item) { var dte = ServiceProvider.GetDte(); var solutionExplorer = dte.ToolWindows.SolutionExplorer; var stack = CreateItemHierarchyStack(item); UIHierarchyItem projectHierarchyItem = null; while (stack.Count > 0) { if (!items.Expanded) { items.Expanded = true; } if (!items.Expanded) { var parent = (UIHierarchyItem)items.Parent; parent.Select(vsUISelectionType.vsUISelectionTypeSelect); solutionExplorer.DoDefaultAction(); } var o = stack.Pop(); var project1 = o as Project; var projectItem1 = o as ProjectItem; for (var i = 0; i < items.Count; i++) { var hierarchyItem = items.Item(i + 1); var project2 = hierarchyItem.Object as Project; var projectItem2 = hierarchyItem.Object as ProjectItem; if ((project1 != null && project2 != null && project1.Object == project2.Object) || (projectItem1 != null && projectItem2 != null && projectItem1.Object == projectItem2.Object) ) { projectHierarchyItem = hierarchyItem; items = hierarchyItem.UIHierarchyItems; } } } return(projectHierarchyItem); }
/// <summary> /// Select the current active document in the solution explorer, taken from /// http://social.msdn.microsoft.com/Forums/vstudio/en-US/1f9d7cb2-cfbc-44b7-88e5-6faf564cdc74/uihierarchyitem-from-a-projectitem /// </summary> public void FindCurrentActiveDocumentInSolutionExplorer() { ProjectItem projectItem = dte.ActiveDocument.ProjectItem; UIHierarchyItems solutionItems = dte.ToolWindows.SolutionExplorer.UIHierarchyItems; // check if we have a solution if (solutionItems.Count != 1) { return; } // FindHierarchyItem expands nodes as well (it must do so, because children arent loaded until expanded) UIHierarchyItem uiItem = FindHierarchyItem(solutionItems.Item(1).UIHierarchyItems, projectItem); if (uiItem != null) { dte.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate(); uiItem.Select(vsUISelectionType.vsUISelectionTypeSelect); } }
private string TraverseItems(UIHierarchyItems items) { string returnVal = null; UIHierarchyItem item; for (int i = 0; i < items.Count; i++) { item = items.Item(i + 1); string name = item.Name; if (name.EndsWith(".zprj", StringComparison.CurrentCultureIgnoreCase)) { ProjectItem prj = item.Object as ProjectItem; for (short j = 0; j < prj.FileCount; j++) { returnVal = prj.get_FileNames(j); break; } if (returnVal != null) { break; } } if ((returnVal == null) && (item.UIHierarchyItems != null)) { returnVal = TraverseItems(item.UIHierarchyItems); } if (returnVal != null) { break; } } return(returnVal); }
public void LocateAndSelectCurrentFile() { Document activeDocument = this.application.ActiveDocument; if (activeDocument == null) { // TODO: message return; } ProjectItem projectItem = activeDocument.ProjectItem; if (projectItem == null) { // Most probably reason: The current document is not included in any of project // i.e. the user opened an external file. // TODO: message return; } UIHierarchyItems solutionItems = application.ToolWindows.SolutionExplorer.UIHierarchyItems; if (solutionItems.Count != 1) { // No open solution return; } // FindHierarchyItem expands nodes as well (it must do so, because children arent loaded until expanded) UIHierarchyItem uiItem = FindHierarchyItem(solutionItems.Item(1).UIHierarchyItems, projectItem); if (uiItem != null) { uiItem.Select(vsUISelectionType.vsUISelectionTypeSelect); application.ToolWindows.SolutionExplorer.Parent.Activate(); } }
private void SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { if (!this.ViewModel.SyncToSolutionExplorer) { return; } FileTreeModel selected = e.NewValue as FileTreeModel; if (selected != null) { UIHierarchy solutionExplorer = (UIHierarchy)IgnorePackage.DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object; UIHierarchyItem rootNode = solutionExplorer.UIHierarchyItems.Item(1); Stack <Tuple <UIHierarchyItems, int, bool> > parents = new Stack <Tuple <UIHierarchyItems, int, bool> >(); ProjectItem targetItem = IgnorePackage.DTE.Solution.FindProjectItem(selected.FullPath); if (targetItem == null) { return; } UIHierarchyItems collection = rootNode.UIHierarchyItems; int cursor = 1; bool oldExpand = collection.Expanded; while (cursor <= collection.Count || parents.Count > 0) { while (cursor > collection.Count && parents.Count > 0) { collection.Expanded = oldExpand; Tuple <UIHierarchyItems, int, bool> parent = parents.Pop(); collection = parent.Item1; cursor = parent.Item2; oldExpand = parent.Item3; } if (cursor > collection.Count) { break; } UIHierarchyItem result = collection.Item(cursor); ProjectItem item = result.Object as ProjectItem; if (item == targetItem) { result.Select(vsUISelectionType.vsUISelectionTypeSelect); return; } ++cursor; bool oldOldExpand = oldExpand; oldExpand = result.UIHierarchyItems.Expanded; result.UIHierarchyItems.Expanded = true; if (result.UIHierarchyItems.Count > 0) { parents.Push(Tuple.Create(collection, cursor, oldOldExpand)); collection = result.UIHierarchyItems; cursor = 1; } } } }