internal static IEnumerable <UIHierarchyItem> MapToProjects(UIHierarchyItem[] selectedItems) { if (selectedItems == null) { yield break; } // Note that you can build a single project from the build menu, but your options are limited // to the project you have selected in Solution Explorer. Here 'project you have selected' can // mean the project a selected item is in. If you ctrl-click items in two projects the menu option // changes to 'Build Selection', meaning build both. This logic replicates that. HashSet <string> seenPaths = new HashSet <string>(); foreach (UIHierarchyItem selectedItem in selectedItems) { if (selectedItem.Object is ProjectItem projectItem && projectItem.ContainingProject?.GetRootFolder() is string projectItemRootFolder && !seenPaths.Contains(projectItemRootFolder)) { seenPaths.Add(projectItemRootFolder); UIHierarchyItems uiHierarchyItems = WebLinterPackage.Dte.ToolWindows.SolutionExplorer.UIHierarchyItems; UIHierarchyItem containingProjectHierarchyItem = GetHierarchyItemForProject(projectItemRootFolder, uiHierarchyItems); if (containingProjectHierarchyItem != null) { yield return(containingProjectHierarchyItem); } }
static object FindItem(UIHierarchyItems children, string fileName, out string solutionExplorerPath) { foreach (UIHierarchyItem CurrentItem in children) { if (CurrentItem.Object is EnvDTE.ProjectItem projectitem) { short i = 1; while (i <= projectitem.FileCount) { if (projectitem.get_FileNames(i) == fileName) { solutionExplorerPath = CurrentItem.Name; return(CurrentItem); } i = (short)(i + 1); } } if (FindItem(CurrentItem.UIHierarchyItems, fileName, out solutionExplorerPath) is UIHierarchyItem childItem) { solutionExplorerPath = CurrentItem.Name + "\\" + solutionExplorerPath; return(childItem); } } solutionExplorerPath = ""; return(null); }
private static UIHierarchyItem LocateInUICollection(UIHierarchyItems items, object target, Dictionary <UIHierarchyItems, bool> expandedItems) { if (items == null) { return(null); } foreach (UIHierarchyItem item in items) { if (IsVsSetupProject(item)) { continue; } ProjectItem prjItem = item.Object as ProjectItem; if (item.Object == target || (prjItem != null && prjItem.Object == target)) { item.Select(vsUISelectionType.vsUISelectionTypeSelect); return(item); } UIHierarchyItem child = LocateInUICollection(item.UIHierarchyItems, target, expandedItems); if (child != null) { return(child); } } expandedItems.Add(items, items.Expanded); return(null); }
private static void DumpUIHierarchyItems(UIHierarchyItems items, IndentedTextWriter tw) { tw.Indent++; foreach (UIHierarchyItem item in items) { tw.Write(item.Name); if (item.Object is ProjectItem) { tw.WriteLine(" (ProjectItem)"); } else if (item.Object is Project) { tw.WriteLine(" (Project)"); } else { tw.WriteLine(" (Unknown)"); } if (item.UIHierarchyItems != null) { DumpUIHierarchyItems(item.UIHierarchyItems, tw); } } tw.Indent--; }
/// <summary> /// 折叠全部 /// </summary> public void CollapseAll() { List <UIHierarchyItem> itemNodes = this.GetProjectNodes(); if (itemNodes != null && itemNodes.Count > 0) { for (int i = 0; i < itemNodes.Count; i++) { Project proj = itemNodes[i].Object as Project; //项目为非顶级项目 if (proj == null) { ProjectItem projItem = itemNodes[i].Object as ProjectItem; if (projItem != null && itemNodes[i].UIHierarchyItems.Expanded) { itemNodes[i].Select(vsUISelectionType.vsUISelectionTypeSelect); _rootNode.DoDefaultAction(); } } else { //项目为顶级项目 itemNodes[i].UIHierarchyItems.Expanded = false; } } } //折叠顶级项目 string solutionName = _app.Solution.Properties.Item("Name").Value.ToString(); UIHierarchyItems items = _rootNode.GetItem(solutionName).UIHierarchyItems; foreach (UIHierarchyItem topItem in items) { topItem.UIHierarchyItems.Expanded = false; } }
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); }
/// <summary> /// Searches given UIHieararchy recursively for UIHierarchyItem with specified path /// </summary> public static UIHierarchyItem FindUIHierarchyItem(UIHierarchyItems list, string path) { if (list == null) return null; if (!list.Expanded) list.Expanded = true; UIHierarchyItem result = null; foreach (UIHierarchyItem item in list) { if (item.Object is ProjectItem) { ProjectItem p = (ProjectItem)item.Object; result = ComparePathsSearch(p.GetFullPath(), path, item); } else if (item.Object is Project) { Project p = (Project)item.Object; result = ComparePathsSearch(p.FileName, path, item); } else if (item.Object is Solution) { Solution s = (Solution)item.Object; result = ComparePathsSearch(s.FileName, path, item); } else if (item.Object is UIHierarchyItem) { ComparePathsSearch(item.Name, path, item); } else if (item.Object is Reference) { } else throw new Exception(item.Object.GetVisualBasicType()); if (result != null) break; } return result; }
private void EnumUIHierarchyItems(UIHierarchyItems items, Func <UIHierarchyItem, bool> func, ref bool skip) { if (skip) { return; } if (items != null && items.Count > 0) { foreach (UIHierarchyItem item in items) { bool result = func(item); if (result) { skip = true; break; } else { EnumUIHierarchyItems(item.UIHierarchyItems, func, ref skip); } } } return; }
static object FindItem(UIHierarchyItems children, string fileName, out string solutionExplorerPath) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); foreach (UIHierarchyItem CurrentItem in children) { if (CurrentItem.Object is ProjectItem projectItem) { short i = 1; while (i <= projectItem.FileCount) { if (projectItem.FileNames[i] == fileName) { solutionExplorerPath = CurrentItem.Name; return(CurrentItem); } i = (short)(i + 1); } } if (!(FindItem(CurrentItem.UIHierarchyItems, fileName, out solutionExplorerPath) is UIHierarchyItem childItem)) { continue; } solutionExplorerPath = CurrentItem.Name + "\\" + solutionExplorerPath; return(childItem); } solutionExplorerPath = ""; return(null); }
private void SelectLastAdded() { if (lastAdded != null) { List <UIHierarchyItems> itemStack = new List <UIHierarchyItems>(); EnvDTE80.DTE2 _applicationObject = GetGlobalService(typeof(DTE)) as EnvDTE80.DTE2; UIHierarchy solutionExplorer = _applicationObject.ToolWindows.SolutionExplorer; UIHierarchyItems items = solutionExplorer.UIHierarchyItems; itemStack.Add(solutionExplorer.UIHierarchyItems); while (itemStack.Count > 0) { int lastIndex = itemStack.Count - 1; items = itemStack[lastIndex]; itemStack.RemoveAt(lastIndex); foreach (UIHierarchyItem item in items) { if (item.Object == lastAdded) { item.Select(vsUISelectionType.vsUISelectionTypeSelect); return; } if (item.UIHierarchyItems != null) { itemStack.Add(item.UIHierarchyItems); } } } } }
protected List <Object> getUIHierarchyItems(UIHierarchyItems uiHItems) { EnvDTE80.DTE2 dte2 = m_serviceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2; List <Object> docContainer = new List <Object>(); foreach (UIHierarchyItem hItem in uiHItems) { ProjectItem prjItem = null; try { prjItem = hItem.Object as ProjectItem; if (prjItem.FileCodeModel != null) { docContainer.AddRange(getProjectItem(prjItem)); } else { docContainer.AddRange(getUIHierarchyItems(hItem.UIHierarchyItems)); } } catch (InvalidCastException /*e*/) { docContainer.AddRange(getUIHierarchyItems(hItem.UIHierarchyItems)); } catch (Exception e) { m_logger.Log("Can't determine project item", OptionsGeneral.LoggerPriority.Medium); m_logger.Log(e); } } return(docContainer); }
public string FindProject(UIHierarchyItems h, Project p) { foreach (UIHierarchyItem xUiHierarchyItem in h) { if (xUiHierarchyItem.Name == p.Name) { dynamic solutionNode = xUiHierarchyItem.Object; dynamic project = p.Object; if (solutionNode.Project.ProjectIDGuid == project.ProjectIDGuid) { return(p.Name); } } var xPartOfName = FindProject(xUiHierarchyItem.UIHierarchyItems, p); if (xPartOfName.Length > 0) { if (xUiHierarchyItem.Object is SolutionClass solution) { return(xUiHierarchyItem.Name + "\\" + xPartOfName); } return(xUiHierarchyItem.Name + "\\" + xPartOfName); } } return(string.Empty); }
/// <summary> /// Initializes a new instance of the <see cref="UIHierarchyItemIterator"/> class. /// </summary> /// <param name="items">The items.</param> public UIHierarchyItemIterator(UIHierarchyItems items) { if (items == null) { throw new ArgumentNullException("items"); } this.items = items; }
/// <summary> /// /// </summary> /// <param name="group"></param> /// <param name="item"></param> private static void AddSubItemsToItemGroup(ItemGroup group, UIHierarchyItem item) { UIHierarchyItems subitems = item.UIHierarchyItems; foreach (UIHierarchyItem subitem in subitems) { group.Items.Add(subitem); } }
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 void LoadAllProjectNodes() { string solutionName = _app.Solution.Properties.Item("Name").Value.ToString(); UIHierarchyItems items = _rootNode.GetItem(solutionName).UIHierarchyItems; foreach (UIHierarchyItem topItem in items) { topItem.UIHierarchyItems.Expanded = true; } }
public static UIHierarchyItem FindUiHierarchyItem(UIHierarchyItems items, Project item) { foreach (UIHierarchyItem child in items) { if (child.Object == item) return child; var result = FindUiHierarchyItem(child.UIHierarchyItems, item); if (result != null) return result; } return null; }
/// <summary> /// 展开解决方案的所有项目 /// </summary> /// <param name="sln">解决方案COM</param> public static void ExpandAllProject(this Solution sln) { string solutionName = sln.Properties.Item("Name").Value.ToString(); UIHierarchyItems items = (sln.DTE as DTE2).ToolWindows.SolutionExplorer.GetItem(solutionName).UIHierarchyItems; foreach (UIHierarchyItem topItem in items) { topItem.UIHierarchyItems.Expanded = true; } }
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; } } }
/// <summary> /// Enumerating children recursive would work, but it may be slow on large solution. /// This tries to be smarter and faster /// </summary> private static UIHierarchyItem FindHierarchyItem(UIHierarchyItems items, object item) { // This creates the full hierarchy for the given item var itemHierarchy = new Stack(); CreateItemHierarchy(itemHierarchy, item); // Now that we have the items complete hierarchy, we assume that the item's hierarchy is a subset of the full heirarchy of the given // items variable. So we are going to go through every level of the given items and compare it to the matching level of itemHierarchy UIHierarchyItem last = null; while (itemHierarchy.Count != 0) { // Visual Studio would sometimes not recognize the children of a node in the hierarchy since its not expanded and thus not loaded. if (!items.Expanded) { items.Expanded = true; } if (!items.Expanded) { //Expand dont always work without this fix var parent = ((UIHierarchyItem)items.Parent); parent.Select(vsUISelectionType.vsUISelectionTypeSelect); //VisualStudioServices.Dte.ToolWindows.SolutionExplorer.DoDefaultAction(); } // We're popping the top ancestors first and each time going deeper until we reach the original item var itemOrParent = itemHierarchy.Pop(); last = null; foreach (UIHierarchyItem child in items) { if (child.Object == itemOrParent) { last = child; items = child.UIHierarchyItems; break; } } } return(last); }
private void CollapseHierarchy(UIHierarchyItems items) { foreach (var item in items.Cast <UIHierarchyItem>().Where(item => item.UIHierarchyItems.Count > 0)) { CollapseHierarchy(item.UIHierarchyItems); if (ShouldCollapse(item)) { item.UIHierarchyItems.Expanded = false; } } }
private UIHierarchyItem Find(UIHierarchyItems items, string name) { foreach (UIHierarchyItem item in items) { if (string.Compare(item.Name, name, StringComparison.CurrentCultureIgnoreCase) == 0) { return(item); } } return(null); }
private IEnumerable <UIHierarchyItem> Enumerate(UIHierarchyItems items) { foreach (UIHierarchyItem item in items) { yield return(item); foreach (UIHierarchyItem subItem in Enumerate(item.UIHierarchyItems)) { yield return(subItem); } } }
private void CloseFolderWithoutRunningDocuments(List <string> docs, UIHierarchyItems items) { foreach (var item in items.Cast <UIHierarchyItem>().Where(item => item.UIHierarchyItems.Count > 0)) { CloseFolderWithoutRunningDocuments(docs, item.UIHierarchyItems); if (ShouldCloseFolder(docs, item)) { item.UIHierarchyItems.Expanded = false; } } }
private static UIHierarchyItem[] RecurseUIHierarchyItems(UIHierarchyItems items) { List<UIHierarchyItem> list = new List<UIHierarchyItem>(); if (items != null) { foreach (UIHierarchyItem hierItem in items) { list.Add(hierItem); list.AddRange(RecurseUIHierarchyItems(hierItem.UIHierarchyItems)); } } return list.ToArray(); }
private static UIHierarchyItem GetUIHierarchySolutionItem() { UIHierarchyItems uiHierarchyItems = WebLinterPackage.Dte.ToolWindows.SolutionExplorer.UIHierarchyItems; foreach (UIHierarchyItem item in uiHierarchyItems) { if (item.Object is Solution) { return(item); } } return(null); }
/// <summary> /// Recursivly group all items ending with .csql together. /// </summary> /// <param name="items"></param> private void GroupItems(UIHierarchyItems items) { ItemGroups groups = new ItemGroups(); // Build the groups foreach (UIHierarchyItem item in items) { bool isCSqlFile = false; if (IsFile(item)) { string fileName = GetItemFileName(item).ToLower().Trim(); if (fileName.Length > ".csql".Length && fileName.EndsWith(".csql")) { isCSqlFile = true; } } if (isCSqlFile) { string name = item.Name; int firstDot = name.IndexOf('.'); string groupName = name.Substring(0, firstDot); ItemGroup group = groups.GetGroup(groupName); group.Items.Add(item); AddSubItemsToItemGroup(group, item); } else { GroupItems(item.UIHierarchyItems); } } // Restructure the project hierarchy. foreach (ItemGroup group in groups.Values) { if (group.MasterItem == null) { continue; } UIHierarchyItem master = group.MasterItem; IEnumerable <UIHierarchyItem> childs = group.ChildItems; ProjectItem masterItem = (ProjectItem)master.Object; foreach (UIHierarchyItem item in childs) { ProjectItem childItem = (ProjectItem)item.Object; AddItemToMasterItem(masterItem, childItem); } } }
private static void DumpHierarchyItems(UIHierarchyItems items, StreamWriter log, int level) { foreach (UIHierarchyItem item in items) { log.Write(" ".PadRight(level * 4)); bool canContainProject = false; string type = "unknown"; var obj = item.Object; if (obj is Solution) { type = "solution"; canContainProject = true; } else if (obj is Project) { var project = (Project)obj; if (project.Kind == Constants.vsProjectKindMisc) { type = "Misc folder"; } else if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder) { type = "Solution folder"; canContainProject = true; } else { type = "project " + project.UniqueName;; } } else if (obj is ProjectItem) { type = "projectItem"; var projItem = (ProjectItem)obj; if (projItem.SubProject != null) { var project = (Project)projItem.SubProject; type += "project " + project.UniqueName; } } log.WriteLine("Name = {0}, Level = {1}, Type= {2}", item.Name, level, type); //item. if (canContainProject) { DumpHierarchyItems(item.UIHierarchyItems, log, level + 1); } } }
private string GetPackageManifestPath(UIHierarchyItems items) { foreach (var uiHierarchyItem in items) { var typedItem = uiHierarchyItem as UIHierarchyItem; if (typedItem != null && typedItem.Name.ToLower().Equals("package.appxmanifest")) { var projectItem = typedItem.Object as ProjectItem; return(projectItem?.Properties.Item("FullPath").Value.ToString()); } } return(null); }
private string GetSolutionExplorerPath([NotNull] UIHierarchyItems hierarchyItems, [NotNull] string path) { Debug.ArgumentNotNull(hierarchyItems, nameof(hierarchyItems)); Debug.ArgumentNotNull(path, nameof(path)); foreach (var item in hierarchyItems) { var hierarchyItem = item as UIHierarchyItem; if (hierarchyItem == null) { continue; } var projectItem = hierarchyItem.Object as EnvDTE.ProjectItem; if (projectItem != null) { var fileName = projectItem.GetFileName(); if (string.Compare(fileName, path, StringComparison.InvariantCultureIgnoreCase) == 0) { return(hierarchyItem.Name); } for (short n = 0; n < projectItem.FileCount; n++) { try { if (string.Compare(projectItem.FileNames[n], path, StringComparison.InvariantCultureIgnoreCase) == 0) { return(hierarchyItem.Name); } } // ReSharper disable once EmptyGeneralCatchClause catch { // silent } } } var child = GetSolutionExplorerPath(hierarchyItem.UIHierarchyItems, path); if (child != null) { return(hierarchyItem.Name + @"\" + child); } } return(null); }
private static UIHierarchyItem[] RecurseUIHierarchyItems(UIHierarchyItems items) { List <UIHierarchyItem> list = new List <UIHierarchyItem>(); if (items != null) { foreach (UIHierarchyItem hierItem in items) { list.Add(hierItem); list.AddRange(RecurseUIHierarchyItems(hierItem.UIHierarchyItems)); } } return(list.ToArray()); }
internal UIHierarchyItem FindHierarchyItem(UIHierarchyItems items, object item) { // // Enumerating children recursive would work, but it may be slow on large solution. // This tries to be smarter and faster // Stack s = new Stack(); CreateItemsStack(s, item); UIHierarchyItem last = null; while (s.Count != 0) { if (!items.Expanded) items.Expanded = true; if (!items.Expanded) { //bug: expand dont always work... UIHierarchyItem parent = ((UIHierarchyItem)items.Parent); parent.Select(vsUISelectionType.vsUISelectionTypeSelect); UIHierarchy tree = items.DTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Object as UIHierarchy; tree.DoDefaultAction(); //_DTE.ToolWindows.SolutionExplorer.DoDefaultAction(); } object o = s.Pop(); last = null; foreach (UIHierarchyItem child in items) if (child.Object == o) { last = child; items = child.UIHierarchyItems; break; } } return last; }
private void EnumUIHierarchyItems(UIHierarchyItems items, Func<UIHierarchyItem, bool> func, ref bool skip) { if (skip) return; if (items != null && items.Count > 0) { foreach (UIHierarchyItem item in items) { bool result = func(item); if (result) { skip = true; break; } else { EnumUIHierarchyItems(item.UIHierarchyItems, func, ref skip); } } } return; }
//private bool AddExistingFolder(Project proj, string folder) //{ // var dte2 = ((DTE2)proj.DTE); // var items = dte2.ToolWindows.SolutionExplorer.UIHierarchyItems; // dte2.ExecuteCommand("Project.ShowAllFiles"); // try // { // UIHierarchyItem item = RecursiveFind(items.Item(proj.Name).UIHierarchyItems, // proj.Name + "\\" + folder); // if (item == null) // return false; // var oldSelection = (object[])dte2.ToolWindows.SolutionExplorer.SelectedItems; // item.Select(vsUISelectionType.vsUISelectionTypeSelect); // dte2.ExecuteCommand("Project.IncludeInProject"); // item.Select(vsUISelectionType.vsUISelectionTypeToggle); // foreach (UIHierarchyItem sel in oldSelection) // { // sel.Select(vsUISelectionType.vsUISelectionTypeToggle); // } // } // finally // { // } // return true; //} private UIHierarchyItem RecursiveFind(UIHierarchyItems root, string name) { var tmp = name.Split('\\'); for (int i = 0; i < tmp.Length - 1; i++) { var item = Find(root, tmp[i]); if (item == null) return null; root = item.UIHierarchyItems; } return Find(root, tmp[tmp.Length - 1]); }
private UIHierarchyItem Find(UIHierarchyItems items, string name) { foreach (UIHierarchyItem item in items) { if (string.Compare(item.Name, name, StringComparison.CurrentCultureIgnoreCase) == 0) return item; } return null; }
public List<UIHierarchyItem> GetProjectNodes(UIHierarchyItems topLevelItems) { var projects = new List<UIHierarchyItem>(); foreach (UIHierarchyItem item in topLevelItems) { if (IsProjectNode(item)) { projects.Add(item); } else if (IsSolutionFolder(item)) { projects.AddRange(GetProjectNodesInSolutionFolder(item)); } } return projects; }
/// <summary> /// UIHierarchy.GetItem does not always work :S, so I do it by hand here. /// </summary> private static UIHierarchyItem FindHierarchyItemByPath(UIHierarchyItems items, string[] paths, int index, Dictionary<UIHierarchyItems, bool> expandedItems) { foreach(UIHierarchyItem item in items) { if(item.Name == paths[index]) { if(index == paths.Length - 1) { // We reached the item we were looking for. return item; } else { // Otherwise, keep processing. return FindHierarchyItemByPath(item.UIHierarchyItems, paths, ++index, expandedItems); } } } expandedItems.Add(items, items.Expanded); // Item wasn't found. return null; }
private static UIHierarchyItem LocateInUICollection(UIHierarchyItems items, object target, Dictionary<UIHierarchyItems, bool> expandedItems) { if(items == null) return null; foreach(UIHierarchyItem item in items) { if(IsVsSetupProject(item)) { continue; } ProjectItem prjItem = item.Object as ProjectItem; if(item.Object == target || (prjItem != null && prjItem.Object == target)) { item.Select(vsUISelectionType.vsUISelectionTypeSelect); return item; } UIHierarchyItem child = LocateInUICollection(item.UIHierarchyItems, target, expandedItems); if(child != null) return child; } expandedItems.Add(items, items.Expanded); return null; }
/// <summary> /// Enumerating children recursive would work, but it may be slow on large solution. /// This tries to be smarter and faster /// </summary> private static UIHierarchyItem FindHierarchyItem(UIHierarchyItems items, object item) { // This creates the full hierarchy for the given item var itemHierarchy = new Stack(); CreateItemHierarchy(itemHierarchy, item); // Now that we have the items complete hierarchy, we assume that the item's hierarchy is a subset of the full heirarchy of the given // items variable. So we are going to go through every level of the given items and compare it to the matching level of itemHierarchy UIHierarchyItem last = null; while (itemHierarchy.Count != 0) { // Visual Studio would sometimes not recognize the children of a node in the hierarchy since its not expanded and thus not loaded. if (!items.Expanded) { items.Expanded = true; } if (!items.Expanded) { //Expand dont always work without this fix var parent = ((UIHierarchyItem) items.Parent); parent.Select(vsUISelectionType.vsUISelectionTypeSelect); //VisualStudioServices.Dte.ToolWindows.SolutionExplorer.DoDefaultAction(); } // We're popping the top ancestors first and each time going deeper until we reach the original item var itemOrParent = itemHierarchy.Pop(); last = null; foreach (UIHierarchyItem child in items) { if (child.Object == itemOrParent) { last = child; items = child.UIHierarchyItems; break; } } } return last; }
private UIHierarchyItem FindHierarchyItem(UIHierarchyItems items, object item) { // Enumerating children recursive would work, but it may be slow on large solution. // This tries to be smarter and faster Stack stack = new Stack(); CreateItemsStack(stack, item); UIHierarchyItem last = null; while (stack.Count != 0) { if (!items.Expanded) { items.Expanded = true; } if (!items.Expanded) { // bug: expand doesn't always work... UIHierarchyItem parent = ((UIHierarchyItem)items.Parent); parent.Select(vsUISelectionType.vsUISelectionTypeSelect); dte.ToolWindows.SolutionExplorer.DoDefaultAction(); } object o = stack.Pop(); last = null; foreach (UIHierarchyItem child in items) { // There is a bug here - we cant match some custom project types e.g. wix if (child.Object == o) { last = child; items = child.UIHierarchyItems; break; } } } return last; }