Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        /// <summary>Gets the WPF text view from the currently active document.</summary>
        public async Task <IWpfTextView?> GetCurrentWpfTextViewAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IComponentModel2 compService = await VS.GetServiceAsync <SComponentModel, IComponentModel2>();

            IVsEditorAdaptersFactoryService?editorAdapter = compService.GetService <IVsEditorAdaptersFactoryService>();
            IVsTextView viewAdapter = await GetCurrentNativeTextViewAsync();

            return(editorAdapter.GetWpfTextView(viewAdapter));
        }
        /// <summary>
        /// Gets a list of the selected items.
        /// </summary>
        public async Task <IEnumerable <SelectedItem> > GetSelectedItemsAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            DTE2 dte = await VS.GetServiceAsync <SDTE, DTE2>();

            List <SelectedItem> list = new();

            foreach (SelectedItem item in dte.SelectedItems)
            {
                list.Add(item);
            }

            return(list);
        }
Exemple #5
0
        /// <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();
                }
            }
        }
Exemple #6
0
        ///<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);
        }
Exemple #7
0
        /// <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);
        }
Exemple #8
0
 /// <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 manage the Toolbox.</summary>
 public Task <IVsToolbox2> GetToolboxAsync() => VS.GetServiceAsync <SVsToolbox, IVsToolbox2>();
 /// <summary>Used for background tasks that needs to block the UI if they take longer than the specified seconds.</summary>
 /// <returns>Cast return object to <see cref="IVsThreadedWaitDialogFactory"/></returns>
 public Task <object> GetThreadedWaitDialogAsync() => VS.GetServiceAsync <SVsThreadedWaitDialogFactory, object>();
 /// <summary>Used to write log messaged to the ActivityLog.xml file.</summary>
 public Task <IVsActivityLog> GetActivityLogAsync() => VS.GetServiceAsync <SVsActivityLog, IVsActivityLog>();
Exemple #12
0
 /// <summary>Provides top-level manipulation or maintenance of the solution.</summary>
 public Task <IVsSolution> GetSolutionAsync() => VS.GetServiceAsync <SVsSolution, IVsSolution>();
Exemple #13
0
 /// <summary>Allows a VSPackage to retrieve or save font and color data to the registry.</summary>
 public Task <IVsFontAndColorStorage> GetFontAndColorStorageAsync() => VS.GetServiceAsync <SVsFontAndColorStorage, IVsFontAndColorStorage>();
Exemple #14
0
 /// <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>();
Exemple #15
0
 private static Task <IVsStatusbar> GetServiceAsync()
 {
     return(VS.GetServiceAsync <SVsStatusbar, IVsStatusbar>());
 }
 /// <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>Used to launch the debugger.</summary>
 public Task <IVsDebugLaunch> GetDebugLaunchAsync() => VS.GetServiceAsync <SVsDebugLaunch, IVsDebugLaunch>();
Exemple #18
0
 /// <summary>Registers well-known images (such as icons) for Visual Studio.</summary>
 public Task <IVsImageService2> GetImageServiceAsync() => VS.GetServiceAsync <SVsImageService, IVsImageService2>();
Exemple #19
0
 /// <summary>Controls the most recently used (MRU) items collection.</summary>
 public Task <IVsMRUItemsStore> GetMRUItemsStoreAsync() => VS.GetServiceAsync <SVsMRUItemsStore, IVsMRUItemsStore>();
Exemple #20
0
 /// <summary>Manages a Tools Options dialog box. The environment implements this interface.</summary>
 public Task <IVsToolsOptions> GetToolsOptionsAsync() => VS.GetServiceAsync <SVsToolsOptions, IVsToolsOptions>();
 /// <summary>
 /// Opens a Solution or Project using the standard open dialog boxes.
 /// </summary>
 public Task <IVsOpenProjectOrSolutionDlg> GetOpenProjectOrSolutionDlgAsync() => VS.GetServiceAsync <SVsOpenProjectOrSolutionDlg, IVsOpenProjectOrSolutionDlg>();
 /// <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>Manages lists of task items supplied by task providers.</summary>
 public Task <IVsTaskList> GetTaskListAsync() => VS.GetServiceAsync <SVsTaskList, IVsTaskList>();
 /// <summary>The Task Status Center is used to run background tasks and is located in the left-most side of the Status bar.</summary>
 /// <remarks>This is only available for Visual Studio 2019 (16.0).</remarks>
 public Task <IVsTaskStatusCenterService> GetTaskStatusCenterAsync() => VS.GetServiceAsync <SVsTaskStatusCenterService, IVsTaskStatusCenterService>();
Exemple #25
0
 /// <summary>Provides access to the environment's status bar.</summary>
 public Task <IVsStatusbar> GetStatusbarAsync() => VS.GetServiceAsync <SVsStatusbar, IVsStatusbar>();
 /// <summary>The Infobar is often referred to as the 'yellow' or 'gold' bar.</summary>
 /// <returns>Cast return object to <see cref="IVsInfoBarUIFactory"/></returns>
 public Task <object> GetInfoBarUIFactoryAsync() => VS.GetServiceAsync <SVsInfoBarUIFactory, object>();
 /// <summary>Allows clients to add to the debuggable protocol list.`</summary>
 public Task <IVsDebuggableProtocol> GetDebuggableProtocolAsync() => VS.GetServiceAsync <SVsDebuggableProtocol, IVsDebuggableProtocol>();
Exemple #28
0
 /// <summary>Controls the caching of font and color settings.</summary>
 public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();
Exemple #29
0
 /// <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>Implemented by the environment. Used by VsPackages that want to manipulate Object Browser.</summary>
 public Task <IVsObjBrowser> GetObjectBrowserAsync() => VS.GetServiceAsync <SVsObjBrowser, IVsObjBrowser>();