Esempio n. 1
0
        /// <summary>
        /// Returns the active <see cref="ProjectItem"/>.
        /// </summary>
        public async Task <ProjectItem?> GetActiveProjectItemAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsMonitorSelection?monitorSelection = await VS.GetRequiredServiceAsync <SVsShellMonitorSelection, IVsMonitorSelection>();

            IntPtr hierarchyPointer          = IntPtr.Zero;
            IntPtr selectionContainerPointer = IntPtr.Zero;
            object?selectedObject            = null;

            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 as ProjectItem);
        }
        /// <summary>Gets the native text view from the currently active document.</summary>
        public async Task <IVsTextView> GetCurrentNativeTextViewAsync()
        {
            IVsTextManager textManager = await VS.GetRequiredServiceAsync <SVsTextManager, IVsTextManager>();

            ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out IVsTextView activeView));

            return(activeView);
        }
        /// <summary>Gets the native text view from the currently active document.</summary>
        private async Task <IVsTextView?> GetActiveNativeTextViewAsync()
        {
            IVsTextManager textManager = await VS.GetRequiredServiceAsync <SVsTextManager, IVsTextManager>();

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

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

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

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

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

            List <SelectedItem> list = new();

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

            return(list);
        }
Esempio n. 6
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.GetRequiredServiceAsync <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();
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Displays the InfoBar in the tool window or document previously specified.
        /// </summary>
        /// <returns><c>true</c> if the InfoBar was shown; otherwise <c>false</c>.</returns>
        public async Task <bool> TryShowInfoBarUIAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsInfoBarUIFactory infoBarUIFactory = (IVsInfoBarUIFactory)await VS.GetRequiredServiceAsync <SVsInfoBarUIFactory, object>();

            _uiElement = infoBarUIFactory.CreateInfoBar(_model);
            _uiElement.Advise(this, out _);

            if (_host != null)
            {
                _host.AddInfoBar(_uiElement);
                IsVisible = true;
            }

            return(IsVisible);
        }
        /// <summary>
        /// Opens the file via the project instead of as a misc file.
        /// </summary>
        public async Task <DocumentView?> OpenViaProjectAsync(string file)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsUIShellOpenDocument openDoc = await VS.GetRequiredServiceAsync <SVsUIShellOpenDocument, IVsUIShellOpenDocument>();

            Guid viewGuid = VSConstants.LOGVIEWID_TextView;

            if (ErrorHandler.Succeeded(openDoc.OpenDocumentViaProject(file, ref viewGuid, out _, out _, out _, out IVsWindowFrame frame)))
            {
                IVsTextView?nativeView = VsShellUtilities.GetTextView(frame);

                if (nativeView != null)
                {
                    return(await nativeView.ToDocumentViewAsync());
                }
            }

            return(null);
        }
Esempio n. 9
0
        /// <summary>
        /// Gets the active project.
        /// </summary>
        public async Task <Project?> GetActiveProjectAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            DTE2?dte = await VS.GetRequiredServiceAsync <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);
        }
Esempio n. 10
0
 /// <summary>Registers well-known images (such as icons) for Visual Studio.</summary>
 /// <returns>Cast return object to <see cref="IVsImageService2"/></returns>
 public Task <object> GetImageServiceAsync() => VS.GetRequiredServiceAsync <SVsImageService, object>();
Esempio n. 11
0
 /// <summary>Controls the caching of font and color settings.</summary>
 public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetRequiredServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();
Esempio n. 12
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.GetRequiredServiceAsync <SVsUIShell, IVsUIShell>();
Esempio n. 13
0
 /// <summary>This interface is used by a package to read command-line switches entered by the user.</summary>
 public Task <IVsAppCommandLine> GetAppCommandLineAsync() => VS.GetRequiredServiceAsync <SVsAppCommandLine, IVsAppCommandLine>();
Esempio n. 14
0
 /// <summary>Provides access to the environment's status bar.</summary>
 public Task <IVsStatusbar> GetStatusBarAsync() => VS.GetRequiredServiceAsync <SVsStatusbar, IVsStatusbar>();
 /// <summary>Manages and controls functions specific to the Output tool window that has multiple panes.</summary>
 public Task <IVsOutputWindow> GetOutputWindowAsync() => VS.GetRequiredServiceAsync <SVsOutputWindow, IVsOutputWindow>();
Esempio n. 16
0
 /// <summary>Used to retrieved services defined in the MEF catalog, such as the editor specific services like <see cref="IVsEditorAdaptersFactoryService"/>.</summary>
 public Task <IComponentModel2> GetComponentModelAsync() => VS.GetRequiredServiceAsync <SComponentModel, IComponentModel2>();
 /// <summary>Implemented by the environment. Used by VsPackages that want to manipulate Object Browser.</summary>
 public Task <IVsObjBrowser> GetObjectBrowserAsync() => VS.GetRequiredServiceAsync <SVsObjBrowser, IVsObjBrowser>();
 /// <summary>Provides access to the settings manager.</summary>
 /// /// <returns>Cast return object to <see cref="IVsSettingsManager"/></returns>
 public Task <object> GetSettingsManagerAsync() => VS.GetRequiredServiceAsync <SVsSettingsManager, object>();
Esempio n. 19
0
 /// <summary>Allows a VSPackage to retrieve or save font and color data to the registry.</summary>
 public Task <IVsFontAndColorStorage> GetFontAndColorStorageAsync() => VS.GetRequiredServiceAsync <SVsFontAndColorStorage, IVsFontAndColorStorage>();
 /// <summary>
 /// Opens a Solution or Project using the standard open dialog boxes.
 /// </summary>
 public Task <IVsOpenProjectOrSolutionDlg> GetOpenProjectOrSolutionDlgAsync() => VS.GetRequiredServiceAsync <SVsOpenProjectOrSolutionDlg, IVsOpenProjectOrSolutionDlg>();
 /// <summary>
 /// Provides top-level manipulation or maintenance of the solution.
 /// </summary>
 public Task <IVsSolution> GetSolutionAsync() => VS.GetRequiredServiceAsync <SVsSolution, IVsSolution>();
 /// <summary>
 /// Provides access to the selection API.
 /// </summary>
 public Task <IVsMonitorSelection> GetMonitorSelectionAsync() => VS.GetRequiredServiceAsync <SVsShellMonitorSelection, IVsMonitorSelection>();
 /// <summary>Used to manage the Toolbox.</summary>
 public Task <IVsToolbox2> GetToolboxAsync() => VS.GetRequiredServiceAsync <SVsToolbox, IVsToolbox2>();
 /// <summary>Manages lists of task items supplied by task providers.</summary>
 public Task <IVsTaskList> GetTaskListAsync() => VS.GetRequiredServiceAsync <SVsTaskList, IVsTaskList>();
Esempio n. 25
0
 /// <summary>Controls the most recently used (MRU) items collection.</summary>
 /// <returns>Cast return object to <see cref="IVsMRUItemsStore"/></returns>
 public Task <object> GetMRUItemsStoreAsync() => VS.GetRequiredServiceAsync <SVsMRUItemsStore, object>();
 /// <summary>Allows navigation to an object in Class View.</summary>
 public Task <IVsClassView> GetClassViewAsync() => VS.GetRequiredServiceAsync <SVsClassView, IVsClassView>();
 public Task <object> GetInfoBarUIFactoryAsync() => VS.GetRequiredServiceAsync <SVsInfoBarUIFactory, object>();
 /// <summary>Enables the package to use the Command Window.</summary>
 public Task <IVsCommandWindow> GetCommandWindowAsync() => VS.GetRequiredServiceAsync <SVsCommandWindow, IVsCommandWindow>();
 /// <summary>Manages a Tools Options dialog box. The environment implements this interface.</summary>
 public Task <IVsToolsOptions> GetToolsOptionsAsync() => VS.GetRequiredServiceAsync <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>
 /// <remarks>This is only available for Visual Studio 2019 (16.0).</remarks>
 public Task <IVsTaskStatusCenterService> GetTaskStatusCenterAsync() => VS.GetRequiredServiceAsync <SVsTaskStatusCenterService, IVsTaskStatusCenterService>();