コード例 #1
0
        /// <summary>
        /// Returns an IVsTextView for the given file path, if the given file is open in Visual Studio.
        /// </summary>
        /// <param name="serviceProvider">The package Service Provider.</param>
        /// <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 async Task <IWpfTextView> GetWpfTextViewByFilePathAsync(this Shell.IAsyncServiceProvider serviceProvider, string filePath)
        {
            if (filePath.IsNullOrWhiteSpace() || serviceProvider == null)
            {
                return(null);
            }

            if (!Shell.ThreadHelper.CheckAccess())
            {
                await Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
            }

            DTE2 dte2 = await serviceProvider.GetServiceAsync <Shell.Interop.SDTE, DTE2>();

            var oleServiceProvider = dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

            if (dte2 == null || oleServiceProvider == null)
            {
                return(null);
            }

            Shell.ServiceProvider shellServiceProvider = new Shell.ServiceProvider(oleServiceProvider);

            if (Shell.VsShellUtilities.IsDocumentOpen(shellServiceProvider, filePath, Guid.Empty, out var uiHierarchy, out var itemID, out var windowFrame))
            {
                IVsTextView textView = Shell.VsShellUtilities.GetTextView(windowFrame);                   // Get the IVsTextView from the windowFrame
                return(await serviceProvider.GetWpfTextViewFromTextViewAsync(textView));
            }

            return(null);
        }
コード例 #2
0
        internal static async Task <IWpfTextView> GetWpfTextViewAsync(this Shell.IAsyncServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                return(null);
            }

            IVsTextManager textManager = await serviceProvider.GetServiceAsync <SVsTextManager, IVsTextManager>();

            if (textManager == null || textManager.GetActiveView(1, null, out IVsTextView textView) != VSConstants.S_OK)
            {
                return(null);
            }

            return(await serviceProvider.GetWpfTextViewFromTextViewAsync(textView));
        }