/// <summary> /// Returns either a Project or ProjectItem. Returns null if Solution is Selected. /// </summary> public async Task <object?> GetSelectedItemAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); object?selectedObject = null; IVsMonitorSelection?monitorSelection = await VS.GetServiceAsync <SVsShellMonitorSelection, IVsMonitorSelection>(); IntPtr hierarchyPointer = IntPtr.Zero; IntPtr selectionContainerPointer = IntPtr.Zero; try { monitorSelection.GetCurrentSelection(out hierarchyPointer, out var itemId, out IVsMultiItemSelect multiItemSelect, out selectionContainerPointer); if (Marshal.GetTypedObjectForIUnknown(hierarchyPointer, typeof(IVsHierarchy)) is IVsHierarchy selectedHierarchy) { ErrorHandler.ThrowOnFailure(selectedHierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out selectedObject)); } } catch (Exception ex) { await ex.LogAsync(); } finally { Marshal.Release(hierarchyPointer); Marshal.Release(selectionContainerPointer); } return(selectedObject); }
/// <summary>Gets the native text view from the currently active document.</summary> public async Task <IVsTextView> GetCurrentNativeTextViewAsync() { IVsTextManager textManager = await VS.GetServiceAsync <SVsTextManager, IVsTextManager>(); ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out IVsTextView activeView)); return(activeView); }
/// <summary>Gets the WPF text view from the currently active document.</summary> public async Task <IWpfTextView?> GetCurrentWpfTextViewAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IComponentModel compService = await VS.GetServiceAsync <SComponentModel, IComponentModel>(); IVsEditorAdaptersFactoryService?editorAdapter = compService.GetService <IVsEditorAdaptersFactoryService>(); IVsTextView viewAdapter = await GetCurrentNativeTextViewAsync(); return(editorAdapter.GetWpfTextView(viewAdapter)); }
/// <summary> /// Opens the file via the project instead of as a misc file. /// </summary> public async Task OpenDocumentViaProjectAsync(string fileName) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IVsUIShellOpenDocument openDoc = await VS.GetServiceAsync <SVsUIShellOpenDocument, IVsUIShellOpenDocument>(); System.Guid viewGuid = VSConstants.LOGVIEWID_TextView; if (ErrorHandler.Succeeded(openDoc.OpenDocumentViaProject(fileName, ref viewGuid, out _, out _, out _, out IVsWindowFrame frame))) { if (frame != null) { frame.Show(); } } }
///<summary>Gets the full paths to the currently selected item(s) in the Solution Explorer.</summary> public async Task <IEnumerable <string>?> GetSelectedItemFilePathsAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); DTE2?dte = await VS.GetServiceAsync <SDTE, DTE2>(); var items = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems; List <string> list = new(); foreach (UIHierarchyItem selItem in items) { if (selItem.Object is ProjectItem item && item.Properties != null) { list.Add(item.Properties.Item("FullPath").Value.ToString()); } } return(list); }
/// <summary>Gets the active project.</summary> public async Task <Project?> GetActiveProjectAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); DTE2?dte = await VS.GetServiceAsync <SDTE, DTE2>(); try { if (dte.ActiveSolutionProjects is Array projects && projects.Length > 0) { return(projects.GetValue(0) as Project); } } catch (Exception ex) { await ex.LogAsync(); } return(null); }
/// <summary>Controls the most recently used (MRU) items collection.</summary> public Task <IVsMRUItemsStore> GetMRUItemsStoreAsync() => VS.GetServiceAsync <SVsMRUItemsStore, IVsMRUItemsStore>();
/// <summary>Used to manage the Toolbox.</summary> public Task <IVsToolbox2> GetToolboxAsync() => VS.GetServiceAsync <SVsToolbox, IVsToolbox2>();
/// <summary>Allows a VSPackage to retrieve or save font and color data to the registry.</summary> public Task <IVsFontAndColorStorage> GetFontAndColorStorageAsync() => VS.GetServiceAsync <SVsFontAndColorStorage, IVsFontAndColorStorage>();
/// <summary>Manages a Tools Options dialog box. The environment implements this interface.</summary> public Task <IVsToolsOptions> GetToolsOptionsAsync() => VS.GetServiceAsync <SVsToolsOptions, IVsToolsOptions>();
/// <summary>The Task Status Center is used to run background tasks and is located in the left-most side of the Status bar.</summary> public Task <IVsTaskStatusCenterService> GetTaskStatusCenterAsync() => VS.GetServiceAsync <SVsTaskStatusCenterService, IVsTaskStatusCenterService>();
/// <summary>Manipulates the Call Browser for debugging.</summary> public Task <IVsCallBrowser> GetCallBrowserAsync() => VS.GetServiceAsync <SVsCodeWindow, IVsCallBrowser>();
/// <summary>Used for background tasks that needs to block the UI if they take longer than the specified seconds.</summary> public Task <IVsThreadedWaitDialogFactory> GetThreadedWaitDialogAsync() => VS.GetServiceAsync <SVsThreadedWaitDialogFactory, IVsThreadedWaitDialogFactory>();
/// <summary>Allows navigation to an object in Class View.</summary> public Task <IVsClassView> GetClassViewAsync() => VS.GetServiceAsync <SVsClassView, IVsClassView>();
/// <summary>Provides methods to manage the global designer verbs and menu commands available in design mode, and to show some types of shortcut menus.</summary> public Task <IMenuCommandService> GetCommandServiceAsync() => VS.GetServiceAsync <IMenuCommandService, IMenuCommandService>();
/// <summary>Manages lists of task items supplied by task providers.</summary> public Task <IVsTaskList> GetTaskListAsync() => VS.GetServiceAsync <SVsTaskList, IVsTaskList>();
/// <summary>Manages and controls functions specific to the Output tool window that has multiple panes.</summary> public Task <IVsOutputWindow> GetOutputWindowAsync() => VS.GetServiceAsync <SVsOutputWindow, IVsOutputWindow>();
/// <summary>Implemented by the environment. Used by VsPackages that want to manipulate Object Browser.</summary> public Task <IVsObjBrowser> GetObjectBrowserAsync() => VS.GetServiceAsync <SVsObjBrowser, IVsObjBrowser>();
/// <summary>Enables the package to use the Command Window.</summary> public Task <IVsCommandWindow> GetCommandWindowAsync() => VS.GetServiceAsync <SVsCommandWindow, IVsCommandWindow>();
/// <summary>Used to register and unregister a command target as a high priority command handler.</summary> public Task <IVsRegisterPriorityCommandTarget> GetPriorityCommandTargetAsync() => VS.GetServiceAsync <SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>();
/// <summary>This interface provides access to basic windowing functionality, including access to and creation of tool windows and document windows.</summary> public Task <IVsUIShell> GetUIShellAsync() => VS.GetServiceAsync <SVsUIShell, IVsUIShell>();
/// <summary>Used to write log messaged to the ActivityLog.xml file.</summary> public Task <IVsActivityLog> GetActivityLogAsync() => VS.GetServiceAsync <SVsActivityLog, IVsActivityLog>();
/// <summary>Provides access to the current debugger so that the package can listen for debugger events.</summary> public Task <IVsDebugger> GetDebuggerAsync() => VS.GetServiceAsync <SVsShell, IVsDebugger>();
/// <summary>This interface is used by a package to read command-line switches entered by the user.</summary> public Task <IVsAppCommandLine> GetAppCommandLineAsync() => VS.GetServiceAsync <SVsAppCommandLine, IVsAppCommandLine>();
/// <summary>Allows clients to add to the debuggable protocol list.`</summary> public Task <IVsDebuggableProtocol> GetDebuggableProtocolAsync() => VS.GetServiceAsync <SVsDebuggableProtocol, IVsDebuggableProtocol>();
/// <summary>Used to launch the debugger.</summary> public Task <IVsDebugLaunch> GetDebugLaunchAsync() => VS.GetServiceAsync <SVsDebugLaunch, IVsDebugLaunch>();
/// <summary>Registers well-known images (such as icons) for Visual Studio.</summary> public Task <IVsImageService2> GetImageServiceAsync() => VS.GetServiceAsync <SVsImageService, IVsImageService2>();
private Task <IVsStatusbar> GetServiceAsync() { return(VS.GetServiceAsync <SVsStatusbar, IVsStatusbar>()); }
/// <summary>Controls the caching of font and color settings.</summary> public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();