Exemplo n.º 1
0
        internal SolutionEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsSolution svc = VS.GetRequiredService <SVsSolution, IVsSolution>();

            svc.AdviseSolutionEvents(this, out _);
        }
Exemplo n.º 2
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);
        }
        internal BuildEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsSolutionBuildManager svc = VS.GetRequiredService <SVsSolutionBuildManager, IVsSolutionBuildManager>();

            svc !.AdviseUpdateSolutionEvents(this, out _);
        }
Exemplo n.º 4
0
        internal DebuggerEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsDebugger svc = VS.GetRequiredService <IVsDebugger, IVsDebugger>();

            svc.AdviseDebuggerEvents(this, out _);
        }
Exemplo n.º 5
0
        internal WindowEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsUIShell7 svc = VS.GetRequiredService <SVsUIShell, IVsUIShell7>();

            svc.AdviseWindowFrameEvents(this);
        }
Exemplo n.º 6
0
        internal SelectionEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsMonitorSelection monitor = VS.GetRequiredService <SVsShellMonitorSelection, IVsMonitorSelection>();

            monitor.AdviseSelectionEvents(this, out _);
        }
        internal ProjectItemsEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsTrackProjectDocuments2 tpd = VS.GetRequiredService <SVsTrackProjectDocuments, IVsTrackProjectDocuments2>();

            tpd.AdviseTrackProjectDocumentsEvents(this, out _);
        }
        /// <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 an instance of <see cref="TextDocument"/> from the currently active document.</summary>
        public async Task <TextDocument?> GetActiveTextDocumentAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            EnvDTE80.DTE2 dte = await VS.GetDTEAsync();

            return(dte.ActiveDocument?.Object("TextDocument") as TextDocument);
        }
Exemplo n.º 10
0
        internal ShellEvents()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsShell svc = VS.GetRequiredService <SVsShell, IVsShell>();

            svc.AdviseShellPropertyChanges(this, out _);
            svc.AdviseBroadcastMessages(this, out _);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Builds the solution asynchronously
        /// </summary>
        /// <returns>Returns 'true' if successfull</returns>
        public async Task <bool> BuildAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            DTE2 dte = await VS.GetDTEAsync();

            return(await dte.Solution.BuildAsync());
        }
        /// <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);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the current solution.
        /// </summary>
        public Solution?GetCurrentSolution()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsHierarchy     solution = VS.GetRequiredService <SVsSolution, IVsHierarchy>();
            IVsHierarchyItem?hierItem = solution.ToHierarchyItem(VSConstants.VSITEMID_ROOT);

            return(SolutionItem.FromHierarchyItem(hierItem) as Solution);
        }
        /// <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));
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
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();
                }
            }
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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);
        }
        /// <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);
        }
        /// <summary>
        /// Creates the code view.
        /// </summary>
        protected virtual IVsCodeWindow CreateCodeView(string documentMoniker, IVsTextLines?textLines, bool createdDocData, ref string editorCaption, ref Guid cmdUI)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (_serviceProvider == null)
            {
                throw new Exception("ServiceProvider can't be null");
            }

            IVsEditorAdaptersFactoryService adapterService = VS.GetMefService <IVsEditorAdaptersFactoryService>();

            IVsCodeWindow window = adapterService.CreateVsCodeWindowAdapter((IOleServiceProvider)_serviceProvider.GetService(typeof(IOleServiceProvider)));

            ErrorHandler.ThrowOnFailure(window.SetBuffer(textLines));
            ErrorHandler.ThrowOnFailure(window.SetBaseEditorCaption(null));
            ErrorHandler.ThrowOnFailure(window.GetEditorCaption(READONLYSTATUS.ROSTATUS_Unknown, out editorCaption));

            if (textLines is IVsUserData userData)
            {
                if (PromptEncodingOnLoad)
                {
                    Guid guid = VSConstants.VsTextBufferUserDataGuid.VsBufferEncodingPromptOnLoad_guid;
                    userData.SetData(ref guid, (uint)1);
                }
            }

            cmdUI = VSConstants.GUID_TextEditorFactory;

            if (!createdDocData && textLines != null)
            {
                // we have a pre-created buffer, go ahead and initialize now as the buffer already
                // exists and is initialized
                TextBufferEventListener?bufferEventListener = new(textLines, _languageServiceId);
                bufferEventListener.OnLoadCompleted(0);
            }

            return(window);
        }
 /// <summary>Manages a Tools Options dialog box. The environment implements this interface.</summary>
 public Task <IVsToolsOptions> GetToolsOptionsAsync() => VS.GetRequiredServiceAsync <SVsToolsOptions, IVsToolsOptions>();
 /// <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>();
Exemplo n.º 24
0
 /// <summary>Controls the caching of font and color settings.</summary>
 public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetRequiredServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();
Exemplo n.º 25
0
 /// <summary>Provides top-level manipulation or maintenance of the solution.</summary>
 public Task <IVsSolution> GetSolutionAsync() => VS.GetServiceAsync <SVsSolution, IVsSolution>();
Exemplo n.º 26
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>();
Exemplo n.º 27
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>();
Exemplo n.º 28
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>();
Exemplo n.º 29
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>();
Exemplo n.º 30
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>();