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();

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

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

            return(editorAdapter.GetWpfTextView(viewAdapter));
        }
Exemple #4
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 #5
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 #6
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 #7
0
 /// <summary>Controls the most recently used (MRU) items collection.</summary>
 public Task <IVsMRUItemsStore> GetMRUItemsStoreAsync() => VS.GetServiceAsync <SVsMRUItemsStore, IVsMRUItemsStore>();
Exemple #8
0
 /// <summary>Used to manage the Toolbox.</summary>
 public Task <IVsToolbox2> GetToolboxAsync() => VS.GetServiceAsync <SVsToolbox, IVsToolbox2>();
Exemple #9
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 #10
0
 /// <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>();
Exemple #12
0
 /// <summary>Manipulates the Call Browser for debugging.</summary>
 public Task <IVsCallBrowser> GetCallBrowserAsync() => VS.GetServiceAsync <SVsCodeWindow, IVsCallBrowser>();
Exemple #13
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>();
Exemple #14
0
 /// <summary>Allows navigation to an object in Class View.</summary>
 public Task <IVsClassView> GetClassViewAsync() => VS.GetServiceAsync <SVsClassView, IVsClassView>();
Exemple #15
0
 /// <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>();
Exemple #16
0
 /// <summary>Manages lists of task items supplied by task providers.</summary>
 public Task <IVsTaskList> GetTaskListAsync() => VS.GetServiceAsync <SVsTaskList, IVsTaskList>();
Exemple #17
0
 /// <summary>Manages and controls functions specific to the Output tool window that has multiple panes.</summary>
 public Task <IVsOutputWindow> GetOutputWindowAsync() => VS.GetServiceAsync <SVsOutputWindow, IVsOutputWindow>();
Exemple #18
0
 /// <summary>Implemented by the environment. Used by VsPackages that want to manipulate Object Browser.</summary>
 public Task <IVsObjBrowser> GetObjectBrowserAsync() => VS.GetServiceAsync <SVsObjBrowser, IVsObjBrowser>();
Exemple #19
0
 /// <summary>Enables the package to use the Command Window.</summary>
 public Task <IVsCommandWindow> GetCommandWindowAsync() => VS.GetServiceAsync <SVsCommandWindow, IVsCommandWindow>();
Exemple #20
0
 /// <summary>Used to register and unregister a command target as a high priority command handler.</summary>
 public Task <IVsRegisterPriorityCommandTarget> GetPriorityCommandTargetAsync() => VS.GetServiceAsync <SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>();
Exemple #21
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>();
Exemple #22
0
 /// <summary>Used to write log messaged to the ActivityLog.xml file.</summary>
 public Task <IVsActivityLog> GetActivityLogAsync() => VS.GetServiceAsync <SVsActivityLog, IVsActivityLog>();
Exemple #23
0
 /// <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>();
Exemple #24
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 #25
0
 /// <summary>Allows clients to add to the debuggable protocol list.`</summary>
 public Task <IVsDebuggableProtocol> GetDebuggableProtocolAsync() => VS.GetServiceAsync <SVsDebuggableProtocol, IVsDebuggableProtocol>();
Exemple #26
0
 /// <summary>Used to launch the debugger.</summary>
 public Task <IVsDebugLaunch> GetDebugLaunchAsync() => VS.GetServiceAsync <SVsDebugLaunch, IVsDebugLaunch>();
Exemple #27
0
 /// <summary>Registers well-known images (such as icons) for Visual Studio.</summary>
 public Task <IVsImageService2> GetImageServiceAsync() => VS.GetServiceAsync <SVsImageService, IVsImageService2>();
Exemple #28
0
 private Task <IVsStatusbar> GetServiceAsync()
 {
     return(VS.GetServiceAsync <SVsStatusbar, IVsStatusbar>());
 }
Exemple #29
0
 /// <summary>Controls the caching of font and color settings.</summary>
 public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();