/// <summary> /// Creates a plugin instance when a new text editor is opened /// </summary> public void VsTextViewCreated(IVsTextView adapter) { IWpfTextView view = adapterFactory.GetWpfTextView(adapter); if (view == null) return; ITextDocument document; if (!docFactory.TryGetTextDocument(view.TextDataModel.DocumentBuffer, out document)) return; IObjectWithSite iows = adapter as IObjectWithSite; if (iows == null) return; System.IntPtr p; iows.GetSite(typeof(IServiceProvider).GUID, out p); if (p == System.IntPtr.Zero) return; IServiceProvider isp = Marshal.GetObjectForIUnknown(p) as IServiceProvider; if (isp == null) return; ServiceProvider sp = new ServiceProvider(isp); DTE dte = sp.GetService(typeof(DTE)) as DTE; sp.Dispose(); new Plugin(view, document, dte); }
/// <summary> /// Returns an IVsTextView for the given file path, if the given file is open in Visual Studio. /// </summary> /// <param name="filePath">Full Path of the file you are looking for.</param> /// <returns>The IVsTextView for this file, if it is open, null otherwise.</returns> internal static IVsTextView GetIVsTextView(string filePath, bool setfocus = false) { var dte2 = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)); var sp = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte2; var serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(sp); try { IVsUIHierarchy uiHierarchy; uint itemID; IVsWindowFrame windowFrame; if (!VsShellUtilities.IsDocumentOpen(serviceProvider, filePath, Guid.Empty, out uiHierarchy, out itemID, out windowFrame)) { VsShellUtilities.OpenDocument(serviceProvider, filePath); } if (VsShellUtilities.IsDocumentOpen(serviceProvider, filePath, Guid.Empty, out uiHierarchy, out itemID, out windowFrame)) { // Get the IVsTextView from the windowFrame. IVsTextView view = VsShellUtilities.GetTextView(windowFrame); if (setfocus) { windowFrame.Show(); } return(view); } return(null); } finally { serviceProvider.Dispose(); } }
private static void CleanupFields() { if (container != null) { container.Dispose(); container = null; } if (serviceProvider != null) { serviceProvider.Dispose(); serviceProvider = null; } if (composition != null) { composition.Dispose(); composition = null; } if (globalCatalog != null) { globalCatalog.Dispose(); globalCatalog = null; } componentHost = null; catalogProvider = null; localComponentModel = null; }
public virtual int Close() { if (this.site != null) { if (this.editorFactory != null) { Guid editorGuid = this.editorFactory.GetType().GUID; IVsRegisterEditors vre = (IVsRegisterEditors)site.GetService(typeof(SVsRegisterEditors)); vre.UnregisterEditor(this.editorFactoryCookie); this.editorFactory.Close(); this.editorFactory = null; } if (this.projectFactory != null) { IVsRegisterProjectTypes rpt = (IVsRegisterProjectTypes)this.site.GetService(typeof(IVsRegisterProjectTypes)); if (rpt != null) { rpt.UnregisterProjectType(this.projectFactoryCookie); } this.projectFactoryCookie = 0; this.projectFactory.Close(); this.projectFactory = null; } } foreach (ILanguageService svc in this.languageServices.Values) { svc.Done(); } this.languageServices.Clear(); if (this.componentID != 0) { this.componentManager.FRevokeComponent(this.componentID); this.componentID = 0; } this.componentManager = null; if (site != null) { site.Dispose(); } this.site = null; GC.Collect(); return(0); }
/// <include file='doc\WindowPane.uex' path='docs/doc[@for="WindowPane.IVsWindowPane.SetSite"]/*' /> /// <internalonly/> /// <devdoc> /// IVsWindowPane implementation. /// </devdoc> int IVsWindowPane.SetSite(IOleServiceProvider p) { // The siting mechanism works as follows: If the // parent provider provides ServiceProviderHierarchy // as a service we will insert our service provider in // the WindowPaneSite slot of the hierarchy. // If, however, it does not provide // this service, we will create a new // ServiceProvider that will be used to resolve // services through this site. // if (_provider != null) { _provider.Dispose(); _provider = null; } IObjectWithSite ows = GetService(typeof(IObjectWithSite)) as IObjectWithSite; ServiceProviderHierarchy serviceHierarchy = GetService(typeof(ServiceProviderHierarchy)) as ServiceProviderHierarchy; if (serviceHierarchy != null) { ServiceProvider sp = (p == null ? null : new ServiceProvider(p)); serviceHierarchy[ServiceProviderHierarchyOrder.WindowPaneSite] = sp; } else if (ows != null) { ows.SetSite(p); } else { if (p != null) { _provider = new ServiceProvider(p); } } if (p != null) { Initialize(); } return(NativeMethods.S_OK); }