internal SolutionEvents() { ThreadHelper.ThrowIfNotOnUIThread(); IVsSolution svc = VS.GetRequiredService <SVsSolution, IVsSolution>(); svc.AdviseSolutionEvents(this, out _); }
/// <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 _); }
internal DebuggerEvents() { ThreadHelper.ThrowIfNotOnUIThread(); IVsDebugger svc = VS.GetRequiredService <IVsDebugger, IVsDebugger>(); svc.AdviseDebuggerEvents(this, out _); }
internal WindowEvents() { ThreadHelper.ThrowIfNotOnUIThread(); IVsUIShell7 svc = VS.GetRequiredService <SVsUIShell, IVsUIShell7>(); svc.AdviseWindowFrameEvents(this); }
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); }
internal ShellEvents() { ThreadHelper.ThrowIfNotOnUIThread(); IVsShell svc = VS.GetRequiredService <SVsShell, IVsShell>(); svc.AdviseShellPropertyChanges(this, out _); svc.AdviseBroadcastMessages(this, out _); }
/// <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); }
/// <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)); }
/// <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); }
/// <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(); } } }
/// <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>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> /// 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>();
/// <summary>Controls the caching of font and color settings.</summary> public Task <IVsFontAndColorCacheManager> GetFontAndColorCacheManagerAsync() => VS.GetRequiredServiceAsync <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();
/// <summary>Provides top-level manipulation or maintenance of the solution.</summary> public Task <IVsSolution> GetSolutionAsync() => VS.GetServiceAsync <SVsSolution, IVsSolution>();
/// <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>();
/// <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>Allows a VSPackage to retrieve or save font and color data to the registry.</summary> public Task <IVsFontAndColorStorage> GetFontAndColorStorageAsync() => VS.GetRequiredServiceAsync <SVsFontAndColorStorage, IVsFontAndColorStorage>();
/// <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>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>();