/// <summary> /// Toggles all solution folders open temporarily to workaround searches not working inside /// solution folders that have never been expanded. /// </summary> /// <param name="parentItem">The parent item to inspect.</param> private void ToggleSolutionFoldersOpenTemporarily(UIHierarchyItem parentItem) { if (parentItem == null) { throw new ArgumentNullException("parentItem"); } const string solutionFolderGuid = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"; var project = parentItem.Object as Project; bool isCollapsedSolutionFolder = project != null && project.Kind == solutionFolderGuid && !parentItem.UIHierarchyItems.Expanded; // Expand the solution folder temporarily. if (isCollapsedSolutionFolder) { parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); Package.IDE.ToolWindows.SolutionExplorer.DoDefaultAction(); } // Run recursively to children as well for nested solution folders. foreach (UIHierarchyItem childItem in parentItem.UIHierarchyItems) { ToggleSolutionFoldersOpenTemporarily(childItem); } // Collapse the solution folder. if (isCollapsedSolutionFolder) { parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); Package.IDE.ToolWindows.SolutionExplorer.DoDefaultAction(); } }
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 { } }
/// <summary> /// Selects an item in SolutionExplorer windows. /// </summary> /// <param name="item"> /// Item to select. /// </param> private void SelectUIHierarchyItem(UIHierarchyItem item) { m_selectedItemsCount++; if (m_selectedItemsCount > 1) { item.Select(vsUISelectionType.vsUISelectionTypeToggle); } else { item.Select(vsUISelectionType.vsUISelectionTypeSelect); } Debug.Assert(item.IsSelected); }
private void SetStartupFromRow(ParsedRow parsedRow) { ThreadHelper.ThrowIfNotOnUIThread(); if (parsedRow == null || parsedRow.project == null) { return; } try { dynamic vcPrj = (dynamic)parsedRow.project.Object; // is VCProject dynamic vcCfg = vcPrj.ActiveConfiguration; // is VCConfiguration dynamic vcDbg = vcCfg.DebugSettings; // is VCDebugSettings vcDbg.Command = parsedRow.command; vcDbg.CommandArguments = parsedRow.commandArguments; toolwindow.Caption = "Args: " + parsedRow.title; UIHierarchyItem item = Utils.FindUIHierarchyItem(parsedRow.project); item.Select(vsUISelectionType.vsUISelectionTypeSelect); if (dte.Solution.SolutionBuild.BuildState != vsBuildState.vsBuildStateInProgress) { dte.Solution.Properties.Item("StartupProject").Value = parsedRow.project.Name; } } catch (Exception ex) { Utils.PrintMessage("Exception", ex.Message + "\n" + ex.StackTrace, true); } }
private static void RecurseMoveBottom(UIHierarchyItem item, ILogger logger) { ThreadHelper.ThrowIfNotOnUIThread(); if (item == null) { return; } UIHierarchyItem next = null; if (item.UIHierarchyItems.Count > 0 && item.UIHierarchyItems.Expanded) { next = item.UIHierarchyItems.OfType <UIHierarchyItem>().LastOrDefault(); } logger.Log($"Recursing from {item.Name}, next = {next?.Name ?? string.Empty}"); if (next != null) { RecurseMoveBottom(next, logger); } else { item.Select(vsUISelectionType.vsUISelectionTypeSelect); } }
/// <summary> /// Causes the given item and all of its expanded children to be collapsed. This may cause /// selections to change. /// </summary> /// <param name="parentItem">The parent item to collapse from.</param> internal static void CollapseRecursively(UIHierarchyItem parentItem) { if (parentItem == null) { throw new ArgumentNullException(nameof(parentItem)); } if (!parentItem.UIHierarchyItems.Expanded) { return; } // Recurse to all children first. foreach (UIHierarchyItem childItem in parentItem.UIHierarchyItems) { CollapseRecursively(childItem); } if (ShouldCollapseItem(parentItem)) { // Attempt the direct collapse first. parentItem.UIHierarchyItems.Expanded = false; // If failed, solution folder oddity may be at play. Try an alternate path. if (parentItem.UIHierarchyItems.Expanded) { parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); ((DTE2)parentItem.DTE).ToolWindows.SolutionExplorer.DoDefaultAction(); } } }
internal static void CollapseRecursively(UIHierarchyItem parentItem) { if (parentItem == null) { throw new ArgumentNullException("parentItem"); } if (!parentItem.UIHierarchyItems.Expanded) return; // Recurse to all children first. foreach (UIHierarchyItem childItem in parentItem.UIHierarchyItems) { CollapseRecursively(childItem); } if (ShouldCollapseItem(parentItem)) { // Attempt the direct collapse first. parentItem.UIHierarchyItems.Expanded = false; // If failed, solution folder oddity may be at play. Try an alternate path. if (parentItem.UIHierarchyItems.Expanded) { parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); ((DTE2)parentItem.DTE).ToolWindows.SolutionExplorer.DoDefaultAction(); } } }
private static void GetAndSelectSolutionExplorerHierarchy(_DTE vs, out UIHierarchy hier, out UIHierarchyItem sol) { if (vs == null) { throw new ArgumentNullException("vs"); } // Select the parent folder to add the project to it. Window win = vs.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer); if (win == null) { // Can't select as there's no solution explorer open. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } win.Activate(); win.SetFocus(); hier = win.Object as UIHierarchy; sol = hier.UIHierarchyItems.Item(1); if (sol == null) { // No solution is opened. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } sol.Select(vsUISelectionType.vsUISelectionTypeSelect); }
public void RunSchemaExport() { try { if (IsSchemaExportProjectAvailable && m_schemaExportProject != null) { if (m_addInSettings.PassSchemaExportArguments) { Property prop = m_schemaExportProject.Project.ConfigurationManager.ActiveConfiguration.Properties.Item("StartArguments"); prop.Value = m_addInSettings.SchemaExportCommandLine; } string solutionName = VSSolutionUtils.GetSolutionName(m_applicationObject.Solution); // Activate the solution explorer window m_applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate(); // Grab the UIHierarchy object for the schema export project UIHierarchyItem item = ((UIHierarchy)m_applicationObject.ActiveWindow.Object).GetItem(solutionName + "\\" + m_schemaExportProject.Name); // Select the project item.Select(vsUISelectionType.vsUISelectionTypeSelect); // Execute the Debug.StartNewInstance command m_applicationObject.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance", string.Empty); } else { AddInUtils.ShowWarning("The schema export project for this solution could not be found.\nPlease ensure the following variables are set correctly in mda\\project.properties\n\nmaven.andromda.schemaexport.available=true\nmaven.andromda.schemaexport.dir=<path to schema export project>"); } } catch (Exception e) { AddInUtils.ShowError(e.Message); } }
internal void ReloadProject(int n) { ThreadHelper.ThrowIfNotOnUIThread(); DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeof(DTE)); Project proj = dte.Solution.Projects.Item(1); string uniqueName = proj.UniqueName; string name = proj.Name; IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsSolution)); IVsHierarchy hier; Marshal.ThrowExceptionForHR(solutionService.GetProjectOfUniqueName(uniqueName, out hier)); solutionService.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_UnloadProject, hier, 0); Window solutionExplorer = dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer) as Window; solutionExplorer.Activate(); UIHierarchy uiHier = solutionExplorer.Object as UIHierarchy; UIHierarchyItem item = uiHier.GetItem(Path.GetFileNameWithoutExtension(dte.Solution.FileName) + "\\" + name); item.Select(vsUISelectionType.vsUISelectionTypeSelect); dte.ExecuteCommand("Project.ReloadProject", ""); }
private void SelectItemInSolutionExplorer(ProjectItem projectItem) { if (projectItem != null) { var uih = (UIHierarchy)_dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Object; UIHierarchyItem uiHierarchyItem = uih.FindHierarchyItem(projectItem); uiHierarchyItem?.Select(vsUISelectionType.vsUISelectionTypeSelect); } }
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> * /// Selects a project in the solution explorer. * /// </summary> * public static bool SelectProject(Project project) * { * if (project == null) * { * throw new ArgumentNullException("project"); * } * // Select the parent folder to add the project to it. * Window win = project.DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer); * if (win == null) * { * // Can't select as there's no solution explorer open. * throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); * } * win.Activate(); * win.SetFocus(); * UIHierarchy hier = win.Object as UIHierarchy; * UIHierarchyItem sol = hier.UIHierarchyItems.Item(1); * if (sol == null) * { * // No solution is opened. * return false; * } * sol.Select(vsUISelectionType.vsUISelectionTypeSelect); * * // Remove project file name from path. * string name = Path.GetDirectoryName(project.UniqueName); * * // Web projects can't be located through UniqueName. * if (IsWebProject(project)) * { * // Locate by folder relative to solution one. * // WARNING: this will not work if project is NOT inside solution! * name = Path.GetDirectoryName(project.Properties.Item("FullPath").Value.ToString()); * string slnpath = Path.GetDirectoryName(project.DTE.Solution.FullName); * name = name.Substring(name.IndexOf(slnpath) + slnpath.Length + 1); * } * * // Perform selection. * UIHierarchyItem item = null; * try * { * item = hier.GetItem(Path.Combine(sol.Name, name)); * } * catch (ArgumentException) * { * // Retry selection by name (much slower!) * item = FindProjectByName(project.Name, hier.UIHierarchyItems); * } * if (item != null) * { * item.UIHierarchyItems.Expanded = true; * item.Select(vsUISelectionType.vsUISelectionTypeSelect); * return true; * } * else * { * return false; * } * } * * private static UIHierarchyItem FindProjectByName(string name, UIHierarchyItems items) * { * foreach (UIHierarchyItem item in items) * { * if (item.Name == name) * { * ProjectItem pi = item.Object as ProjectItem; * // Check the project name or the subproject name (for ETP). * if (pi.ContainingProject.Name == name || * (pi.SubProject != null && pi.SubProject.Name == name)) * return item; * } * * if (item.UIHierarchyItems != null) * { * UIHierarchyItem uii = FindProjectByName(name, item.UIHierarchyItems); * if (uii != null) return uii; * } * } * * return null; * } * */ #endregion Removed feature - not working quite good /// <summary> /// Selects a solution explorer item, based on /// its relative path with regards to the solution. /// </summary> /// <remarks> /// If selection fails, returned object will be null too. /// </remarks> public static UIHierarchyItem SelectItem(_DTE vs, string path) { if (vs == null) { throw new ArgumentNullException("vs"); } if (path == null) { throw new ArgumentNullException("path"); } // Select the parent folder to add the project to it. Window win = vs.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer); if (win == null) { // Can't select as there's no solution explorer open. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } win.Activate(); win.SetFocus(); UIHierarchy hier = win.Object as UIHierarchy; UIHierarchyItem sol = hier.UIHierarchyItems.Item(1); if (sol == null) { // No solution is opened. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } sol.Select(vsUISelectionType.vsUISelectionTypeSelect); // Perform selection. UIHierarchyItem item = null; try { string slnpath = Path.Combine(sol.Name, path); item = hier.GetItem(slnpath); } catch (ArgumentException) { // Retry selection by name (slower) item = FindHierarchyItemByPath(sol.UIHierarchyItems, path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), 0); } if (item != null) { item.UIHierarchyItems.Expanded = true; item.Select(vsUISelectionType.vsUISelectionTypeSelect); } return(item); }
static UIHierarchyItem GetItemWorkaround(UIHierarchy uh, string item) { var items = item.Split('\\'); UIHierarchyItem uii = null; foreach (var s in items) { uii = uii == null?uh.GetItem(s) : uii.UIHierarchyItems.Item(s); uii.Select(vsUISelectionType.vsUISelectionTypeSelect); } return(uii); }
public void SelectItem(UIHierarchyItem item) { try { item.Select(vsUISelectionType.vsUISelectionTypeSelect); Wait(); } catch (Exception e) { throw new Failed( "can't select item {0}, {1}", item.Name, e.Message); } }
private void ExpandCollapseNodes(UIHierarchyItem node, bool expanded, UIHierarchy tree) { foreach (UIHierarchyItem subNode in node.UIHierarchyItems) { ExpandCollapseNodes(subNode, expanded, tree); } if (node.Object is SolutionFolder) { node.UIHierarchyItems.Expanded = true; } else if (node.Object is Solution) { node.UIHierarchyItems.Expanded = true; } else if (node.Object is Project) { if (((Project)node.Object).Object is SolutionFolder) { //are there projects in the solution folder SolutionFolder f = ((Project)node.Object).Object as SolutionFolder; bool expandit = false; foreach (ProjectItem pi in f.Parent.ProjectItems) { if (pi.Object is Project) { //solutionfolder contains a child project, dont close expandit = true; } } node.UIHierarchyItems.Expanded = expandit; } else { node.UIHierarchyItems.Expanded = false; } } else { node.UIHierarchyItems.Expanded = false; //bug in VS //if still expanded if (node.UIHierarchyItems.Expanded == true) { node.Select(vsUISelectionType.vsUISelectionTypeSelect); tree.DoDefaultAction(); } } }
public static void Collapse(this DTE2 dte) { UIHierarchy solutionExplorer = dte.ToolWindows.SolutionExplorer; if (solutionExplorer.UIHierarchyItems.Count <= 0) { return; } UIHierarchyItem rootNode = solutionExplorer.UIHierarchyItems.Item(1); Collapse(rootNode, ref solutionExplorer); rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect); rootNode.DTE.SuppressUI = false; }
private void CollapseProject(UIHierarchyItem project) { if (project.UIHierarchyItems.Expanded) { if (helper.IsDirectProjectNode(project)) { project.UIHierarchyItems.Expanded = false; } else if (helper.IsProjectNodeInSolutionFolder(project)) { project.Select(vsUISelectionType.vsUISelectionTypeSelect); helper.SolutionExplorerNode.DoDefaultAction(); } } }
/// <summary> /// Enumerating children recursive would work, but it may be slow on large solution. /// This tries to be smarter and faster /// </summary> /// <param name="items">The items.</param> /// <param name="item">The item.</param> private static UIHierarchyItem FindHierarchyItem( UIHierarchyItems items, object item) { //// This creates the full hierarchy for the given item Stack 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 UIHierarchyItem 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 object itemOrParent = itemHierarchy.Pop(); last = null; foreach (UIHierarchyItem child in items) { if (child.Object == itemOrParent) { last = child; items = child.UIHierarchyItems; break; } } } return(last); }
public static void CollapseAllFolders(this Solution solution) { var DTE = CodeRush.ApplicationObject; var UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object as UIHierarchy; if (UIHSolutionExplorer == null || UIHSolutionExplorer.UIHierarchyItems.Count == 0) { return; } UIHierarchyItem rootItem = UIHSolutionExplorer.UIHierarchyItems.Item(1); rootItem.DTE.SuppressUI = true; Collapse(rootItem); rootItem.Select(vsUISelectionType.vsUISelectionTypeSelect); rootItem.DTE.SuppressUI = false; }
/// <summary> /// The CollapseSolutionAsync. /// </summary> /// <returns>The <see cref="Task"/>.</returns> public async Task CollapseSolutionAsync() { await VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); UIHierarchy solutionExplorer = _dte.ToolWindows.SolutionExplorer; if (solutionExplorer.UIHierarchyItems.Count <= 0) { return; } UIHierarchyItem rootNode = solutionExplorer.UIHierarchyItems.Item(1); Collapse(rootNode, ref solutionExplorer); rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect); rootNode.DTE.SuppressUI = false; }
public static void CollapseAllFolders(this Solution solution) { var dte = DteExtensions.DTE; var uihSolutionExplorer = dte.Windows.Item(Constants.vsext_wk_SProjectWindow).Object as UIHierarchy; if (uihSolutionExplorer == null || uihSolutionExplorer.UIHierarchyItems.Count == 0) { return; } UIHierarchyItem hierarchyItem = uihSolutionExplorer.UIHierarchyItems.Item(1); hierarchyItem.DTE.SuppressUI = true; hierarchyItem.Expand(false); hierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect); hierarchyItem.DTE.SuppressUI = false; }
private void MenuItemCallback(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); try { DTE2 dte = (DTE2)Package.GetGlobalService(typeof(DTE)); if (dte.Solution.SolutionBuild.BuildState == vsBuildState.vsBuildStateInProgress) { return; } string project_name = ""; foreach (String s in (Array)dte.Solution.SolutionBuild.StartupProjects) { project_name += s; } Project startupProject = null; foreach (Project p in Utils.GetAllProjectsInSolution()) { if (p.UniqueName == project_name) { startupProject = p; break; } } UIHierarchyItem item = Utils.FindUIHierarchyItem(startupProject); item.Select(vsUISelectionType.vsUISelectionTypeSelect); dte.ToolWindows.SolutionExplorer.Parent.Activate(); dte.ExecuteCommand("Build.BuildSelection"); foreach (EnvDTE.Window window in dte.Windows) { if (window.Caption.StartsWith("Changes")) { window.Activate(); break; } } dte.ActiveDocument.Activate(); } catch (Exception ex) { Utils.PrintMessage("Exception", ex.Message + "\n" + ex.StackTrace, true); } }
private void ExpandItems(UIHierarchyItems items) { 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(); } }
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); }
public static void CollapseAllFolders(UIHierarchy solExplorer, string projectName) { if (solExplorer.UIHierarchyItems.Count > 0) { UIHierarchyItem rootNode = solExplorer.UIHierarchyItems.Item(1); rootNode.DTE.SuppressUI = true; foreach (UIHierarchyItem uihitem in rootNode.UIHierarchyItems) { if (uihitem.UIHierarchyItems.Expanded) { collapse(uihitem, projectName); } } rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect); rootNode.DTE.SuppressUI = false; } }
private void RaiseCommandForSelectedProject(int commandId) { var dte2 = (EnvDTE80.DTE2)SolutionItem.StorageSolution.DTE; UIHierarchy solutionExplorer = dte2.ToolWindows.SolutionExplorer; UIHierarchyItem item = solutionExplorer.FindHierarchyItem(SelectedProjectItem.StorageProject); if (item == null) { throw new Exception(string.Format("Project '{0}' not found in SolutionExplorer.", SelectedProjectItem.StorageProject.UniqueName)); } solutionExplorer.Parent.Activate(); item.Select(vsUISelectionType.vsUISelectionTypeSelect); object customIn = null; object customOut = null; dte2.Commands.Raise(VSConstants.GUID_VSStandardCommandSet97.ToString(), commandId, ref customIn, ref customOut); }
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); }
public static void SelectProjectItem(Solution solution, string projectName, string projectItemName) { //Guard.ArgumentNotNull(solution, "solution"); //Guard.ArgumentNotNullOrEmptyString(projectName, "projectName"); //Guard.ArgumentNotNullOrEmptyString(projectItemName, "projectItemName"); UIHierarchyItem projectH = FindItemByName <Project>( ((DTE2)solution.DTE).ToolWindows.SolutionExplorer.UIHierarchyItems, projectName); if (projectH != null) { UIHierarchyItem projectItemH = FindItemByName <ProjectItem>( projectH.UIHierarchyItems, projectItemName); if (projectItemH != null) { projectItemH.Select(vsUISelectionType.vsUISelectionTypeSelect); } } }
/// <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); } }
public static void CollapseAll(this DTE2 dte, IEnumerable <string> exlusionList = null) { UIHierarchy solutionExplorer = dte.ToolWindows.SolutionExplorer; // Check if there is any open solution if (solutionExplorer.UIHierarchyItems.Count == 0) { return; } // Get the top node (the name of the solution) UIHierarchyItem rootNode = solutionExplorer.UIHierarchyItems.Item(1); rootNode.DTE.SuppressUI = true; // Collapse each project node Collapse(rootNode, solutionExplorer, exlusionList); rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect); rootNode.DTE.SuppressUI = false; }
private void ExpandCollapseNodes(UIHierarchyItem node, bool expanded, UIHierarchy tree) { foreach(UIHierarchyItem subNode in node.UIHierarchyItems) { ExpandCollapseNodes(subNode, expanded, tree); } if (node.Object is SolutionFolder) { node.UIHierarchyItems.Expanded = true; } else if (node.Object is Solution) { node.UIHierarchyItems.Expanded = true; } else if (node.Object is Project) { if (((Project)node.Object).Object is SolutionFolder) { //are there projects in the solution folder SolutionFolder f = ((Project)node.Object).Object as SolutionFolder; bool expandit = false; foreach (ProjectItem pi in f.Parent.ProjectItems) { if (pi.Object is Project) { //solutionfolder contains a child project, dont close expandit = true; } } node.UIHierarchyItems.Expanded = expandit; } else { node.UIHierarchyItems.Expanded = false; } } else { node.UIHierarchyItems.Expanded = false; //bug in VS //if still expanded if (node.UIHierarchyItems.Expanded == true) { node.Select(vsUISelectionType.vsUISelectionTypeSelect); tree.DoDefaultAction(); } } }
/// <summary> /// Toggles all solution folders open temporarily to workaround searches not working inside /// solution folders that have never been expanded. /// </summary> /// <param name="parentItem">The parent item to inspect.</param> private void ToggleSolutionFoldersOpenTemporarily(UIHierarchyItem parentItem) { if (parentItem == null) { throw new ArgumentNullException("parentItem"); } const string solutionFolderGuid = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"; var project = parentItem.Object as Project; bool isCollapsedSolutionFolder = project != null && project.Kind == solutionFolderGuid && !parentItem.UIHierarchyItems.Expanded; // Expand the solution folder temporarily. if (isCollapsedSolutionFolder) { OutputWindowHelper.DiagnosticWriteLine( string.Format("FindInSolutionExplorerCommand.ToggleSolutionFoldersOpenTemporarily expanding '{0}'", parentItem.Name)); parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); Package.IDE.ToolWindows.SolutionExplorer.DoDefaultAction(); } // Run recursively to children as well for nested solution folders. foreach (UIHierarchyItem childItem in parentItem.UIHierarchyItems) { ToggleSolutionFoldersOpenTemporarily(childItem); } // Collapse the solution folder. if (isCollapsedSolutionFolder) { OutputWindowHelper.DiagnosticWriteLine( string.Format("FindInSolutionExplorerCommand.ToggleSolutionFoldersOpenTemporarily collapsing '{0}'", parentItem.Name)); parentItem.Select(vsUISelectionType.vsUISelectionTypeSelect); Package.IDE.ToolWindows.SolutionExplorer.DoDefaultAction(); } }
public static void CollapseFilter(UIHierarchyItem item, UIHierarchy hierarchy) { UIHierarchyItems subItems = item.UIHierarchyItems; if (subItems != null) { foreach (UIHierarchyItem innerItem in subItems) { if (innerItem.UIHierarchyItems.Count > 0) { CollapseFilter(innerItem, hierarchy); if (innerItem.UIHierarchyItems.Expanded) { innerItem.UIHierarchyItems.Expanded = false; if (innerItem.UIHierarchyItems.Expanded == true) { innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect); hierarchy.DoDefaultAction(); } } } } } if (item.UIHierarchyItems.Expanded) { item.UIHierarchyItems.Expanded = false; if (item.UIHierarchyItems.Expanded == true) { item.Select(vsUISelectionType.vsUISelectionTypeSelect); hierarchy.DoDefaultAction(); } } }
private static void GetAndSelectSolutionExplorerHierarchy(_DTE vs, out UIHierarchy hier, out UIHierarchyItem sol) { if(vs == null) { throw new ArgumentNullException("vs"); } // Select the parent folder to add the project to it. Window win = vs.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer); if(win == null) { // Can't select as there's no solution explorer open. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } win.Activate(); win.SetFocus(); hier = win.Object as UIHierarchy; sol = hier.UIHierarchyItems.Item(1); if(sol == null) { // No solution is opened. throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer); } sol.Select(vsUISelectionType.vsUISelectionTypeSelect); }