コード例 #1
0
        private void OnBeforeDocumentWindowShow(IVsWindowFrame frame)
        {
            var vsTextView = VsShellUtilities.GetTextView(frame);

            if (vsTextView == null)
            {
                return;
            }

            var textView = _adapterFactory.GetWpfTextView(vsTextView);

            if (textView != null && textView != _textView)
            {
                _textView = textView;

                if (_documentTracker != null)
                {
                    _documentTracker.ContextChanged -= DocumentTracker_ContextChanged;
                }

                var textBuffer = textView.BufferGraph.GetRazorBuffers().FirstOrDefault();

                if (!_editorFactoryService.TryGetDocumentTracker(textBuffer, out _documentTracker))
                {
                    return;
                }

                _documentTracker.ContextChanged += DocumentTracker_ContextChanged;

                ((FrameworkElement)Content).DataContext = new RazorDocumentInfoViewModel(_documentTracker);
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns an IVsTextView for the given file path, if the given file is open in Visual Studio.
        /// </summary>
        /// <param name="packageServiceProvider">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 IWpfTextView GetWpfTextViewByFilePath(this IServiceProvider packageServiceProvider, string filePath)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (filePath.IsNullOrWhiteSpace())
            {
                return(null);
            }

            DTE2 dte2 = packageServiceProvider?.GetService <SDTE, DTE2>();
            var  oleServiceProvider = dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

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

            ServiceProvider shellServiceProvider = new ServiceProvider(oleServiceProvider);

            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(shellServiceProvider, filePath, Guid.Empty, out uiHierarchy, out itemID, out windowFrame))
            {
                IVsTextView textView = VsShellUtilities.GetTextView(windowFrame);                   // Get the IVsTextView from the windowFrame
                return(packageServiceProvider.GetWpfTextViewFromTextView(textView));
            }

            return(null);
        }
コード例 #3
0
        // ----------------------------------------------------------------------------------
        /// <summary>
        /// Initialize the editor
        /// </summary>
        // ----------------------------------------------------------------------------------
        private void InitializeEditor(string scriptName)
        {
            Guid guid_microsoft_csharp_editor = new Guid("{A6C744A8-0E4A-4FC6-886A-064283054674}");
            Guid guid_microsoft_csharp_editor_with_encoding = new Guid("{08467b34-b90f-4d91-bdca-eb8c8cf3033a}");
            Guid editorType      = guid_microsoft_csharp_editor;// VSConstants.VsEditorFactoryGuid.TextEditor_guid;
            Guid logicalViewGuid = Microsoft.VisualStudio.VSConstants.LOGVIEWID.Primary_guid;

            IVsWindowFrame ppWindowFrame = null;

            try
            {
                var psp      = (System.IServiceProvider)ReplEditorPackage.CurrentPackage;
                var fileName = System.IO.Path.Combine(VSTools.ScriptsDirectory, scriptName);
                ppWindowFrame = Microsoft.VisualStudio.Shell.VsShellUtilities.OpenDocumentWithSpecificEditor(psp, fileName, editorType, logicalViewGuid);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("REPLException : " + ex.ToString());
                //System.Windows.MessageBox.Show(ex.ToString());
            }
            if (ppWindowFrame != null)
            {
                _ViewAdapter          = VsShellUtilities.GetTextView(ppWindowFrame);
                _childWindowFrame     = ppWindowFrame;
                _TextViewHost         = _EditorAdapterFactory.GetWpfTextViewHost(_ViewAdapter);
                _docData              = VSTools.GetPersistDocData(_TextViewHost);
                this.leftSide.Content = _TextViewHost;
            }
        }
コード例 #4
0
        public static void SelectSpanInCodeWindow(FileLinePositionSpan span)
        {
            // If the path is not available we cannot jump to it
            if (string.IsNullOrEmpty(span.Path))
            {
                return;
            }

            // Check if the document is opened, if not open it.
            IVsUIHierarchy hierarchy;
            uint           itemId;
            IVsWindowFrame windowFrame;

            if (!VsShellUtilities.IsDocumentOpen(ServiceProvider.GlobalProvider, span.Path, VSConstants.LOGVIEWID_Any, out hierarchy, out itemId, out windowFrame))
            {
                VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider, span.Path, VSConstants.LOGVIEWID_Primary, out hierarchy, out itemId, out windowFrame);
            }

            var window = VsShellUtilities.GetWindowObject(windowFrame);

            window.SetFocus();

            var textView = VsShellUtilities.GetTextView(windowFrame);

            textView.SetSelection(span.StartLinePosition.Line, span.StartLinePosition.Character, span.EndLinePosition.Line, span.EndLinePosition.Character);
        }
コード例 #5
0
 public static IVsTextView GetPrimaryTextView(this IVsWindowFrame windowFrame)
 {
     /*
      * object docView;
      * int hresult = windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out docView);
      *
      * if (ErrorHandler.Failed(hresult)) {
      *  return null;
      * }
      *
      * IVsTextView viewAdapter = docView as IVsTextView;
      * if (viewAdapter != null) {
      *  return viewAdapter;
      * }
      *
      * IVsCodeWindow codeWindow = docView as IVsCodeWindow;
      * if (codeWindow != null) {
      *  IVsTextView codeView;
      *  if (ErrorHandler.Succeeded(codeWindow.GetPrimaryView(out codeView)) && codeView != null) {
      *      return codeView;
      *  }
      * }
      *
      * return null;
      */
     return(VsShellUtilities.GetTextView(windowFrame));
 }
コード例 #6
0
        /// <summary>
        /// Gets IVsTextView for a file
        /// </summary>
        /// <param name="file">File path</param>
        /// <param name="forceOpen">Whether the file should be opened, if it's closed</param>
        /// <param name="activate">Whether the window frame should be activated (focused)</param>
        public static IVsTextView GetTextViewForFile(string file, bool forceOpen, bool activate)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException("file");
            }

            IVsWindowFrame frame = GetWindowFrameForFile(file, forceOpen);

            if (frame != null)
            {
                if (forceOpen || activate)
                {
                    frame.Show();
                }
                if (activate)
                {
                    VsShellUtilities.GetWindowObject(frame).Activate();
                }
                return(VsShellUtilities.GetTextView(frame));
            }
            else
            {
                throw new Exception("Cannot get window frame for " + file);
            }
        }
コード例 #7
0
        /// Go to exact location in a given file.
        public static void NavigateTo(this IServiceProvider serviceProvider, string fileName, int startRow, int startCol, int endRow, int endCol)
        {
            IVsUIHierarchy hierarchy;
            UInt32         itemId;
            IVsWindowFrame windowFrame;
            var            isOpened = VsShellUtilities.IsDocumentOpen(
                serviceProvider,
                fileName,
                guidTextView,
                out hierarchy,
                out itemId,
                out windowFrame);

            if (isOpened)
            {
                //var hr = windowFrame.Show();
                //if(hr != S_OK) throw new COMException("windowframe ", hr);

                var vsTextView    = VsShellUtilities.GetTextView(windowFrame);
                var vsTextManager = serviceProvider.GetService <IVsTextManager, SVsTextManager>();

                IVsTextLines vsTextLines;
                var          hr = vsTextView.GetBuffer(out vsTextLines);
                if (hr != VSConstants.S_OK)
                {
                    throw new COMException("get text lines failed", hr);
                }

                hr = vsTextManager.NavigateToLineAndColumn(vsTextLines, ref guidTextView, startRow, startCol, endRow, endCol);
                if (hr != VSConstants.S_OK)
                {
                    throw new COMException("navigate to failed", hr);
                }
            }
        }
コード例 #8
0
        internal static ITextBuffer GetBufferAt(string filePath, IServiceProvider provider)
        {
            //var package = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)node.GetService(typeof(EnvDTE.DTE));
            //var serviceProvider = ;//new Microsoft.VisualStudio.Shell.ServiceProvider(package);
            //ProjectItem pi = (ProjectItem)node.GetAutomationObject();

            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    provider,
                    filePath,
                    Guid.Empty,
                    out uiHierarchy,
                    out itemID,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        var componentModel = (IComponentModel)ProjectPackage.GetGlobalService(typeof(SComponentModel));
                        var editorAdapterFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>();
                        return(editorAdapterFactoryService.GetDataBuffer(buffer));
                    }
                }
            }

            return(null);
        }
コード例 #9
0
        private void OnBeforeDocumentWindowShow(IVsWindowFrame frame)
        {
            var vsTextView = VsShellUtilities.GetTextView(frame);

            if (vsTextView == null)
            {
                return;
            }

            var textView = _adapterFactory.GetWpfTextView(vsTextView);

            if (textView != null && textView != _textView)
            {
                _textView = textView;

                if (_documentTracker != null)
                {
                    _documentTracker.ContextChanged -= DocumentTracker_ContextChanged;
                }

                _documentTracker = _documentTrackerService.GetTracker(textView);
                _documentTracker.ContextChanged += DocumentTracker_ContextChanged;

                ((FrameworkElement)Content).DataContext = new RazorDocumentInfoViewModel(_documentTracker);
            }
        }
コード例 #10
0
        /// <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();
            }
        }
コード例 #11
0
        internal static ITextBuffer GettextBufferAt(TextDocument textDocument, IComponentModel componentModel, IServiceProvider serviceProvider)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    serviceProvider,
                    textDocument.Parent.FullName,
                    Guid.Empty,
                    out var _,
                    out var _,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        var editorAdapterFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>();
                        return(editorAdapterFactoryService.GetDataBuffer(buffer));
                    }
                }
            }

            return(null);
        }
コード例 #12
0
 IVsTextView GetIVsTextView(string filePath)
 {
     return(VsShellUtilities.IsDocumentOpen(_serviceProvider, filePath, Guid.Empty, out var uiHierarchy,
                                            out var itemId, out var windowFrame)
         ? VsShellUtilities.GetTextView(windowFrame)
         : null);
 }
コード例 #13
0
        private static bool TryGetTextBufferAt(string filePath, out ITextBuffer textBuffer)
        {
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    CodeMaidPackage.Instance,
                    filePath,
                    Guid.Empty,
                    out var _,
                    out var _,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        var editorAdapterFactoryService = CodeMaidPackage.Instance.ComponentModel.GetService <IVsEditorAdaptersFactoryService>();
                        textBuffer = editorAdapterFactoryService.GetDataBuffer(buffer);
                        return(true);
                    }
                }
            }

            textBuffer = null;
            return(false);
        }
コード例 #14
0
        ITextBuffer GetBufferAt(string filePath)
        {
            var            editorAdapterFactoryService = Services.ComponentModel.GetService <IVsEditorAdaptersFactoryService>();
            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    Services.GitHubServiceProvider,
                    filePath,
                    Guid.Empty,
                    out uiHierarchy,
                    out itemID,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        return(editorAdapterFactoryService.GetDataBuffer(buffer));
                    }
                }
            }

            return(null);
        }
コード例 #15
0
        public static void ShowContextMenu(ContextMenuStrip contextMenu, DTE dte)
        {
            try
            {
                var serviceProvider = new ServiceProvider(dte as IServiceProvider);

                IVsUIShellOpenDocument sod = (IVsUIShellOpenDocument)serviceProvider.GetService(typeof(SVsUIShellOpenDocument));
                IVsUIHierarchy         targetHier;
                uint[]         targetId = new uint[1];
                IVsWindowFrame targetFrame;
                int            isOpen;
                Guid           viewId = new Guid(LogicalViewID.Primary);
                sod.IsDocumentOpen(null, 0, dte.ActiveWindow.Document.FullName,
                                   ref viewId, 0, out targetHier, targetId,
                                   out targetFrame, out isOpen);

                IVsTextView   textView  = VsShellUtilities.GetTextView(targetFrame);
                TextSelection selection = (TextSelection)dte.ActiveWindow.Document.Selection;
                Microsoft.VisualStudio.OLE.Interop.POINT[] interopPoint = new Microsoft.VisualStudio.OLE.Interop.POINT[1];
                textView.GetPointOfLineColumn(selection.ActivePoint.Line, selection.ActivePoint.LineCharOffset, interopPoint);

                POINT p = new POINT(interopPoint[0].x, interopPoint[0].y);

                ClientToScreen(textView.GetWindowHandle(), p);

                contextMenu.Show(new Point(p.x, p.y));
            }
            catch (Exception)
            {
                contextMenu.Show();
            }
        }
コード例 #16
0
        // TODO: Simplify to one NavigateTo implementation.
        public static void NavigateTo(this IServiceProvider serviceProvider, string fileName, int startLine, int startCol, int endRow, int endCol)
        {
            var logicalTextViewGuid = new Guid(LogicalViewID.TextView);

            IVsUIHierarchy hierarchy;
            uint           itemID;
            IVsWindowFrame frame;
            var            isOpened = VsShellUtilities.IsDocumentOpen(serviceProvider, fileName, logicalTextViewGuid, out hierarchy, out itemID, out frame);

            if (!isOpened)
            {
                try
                {
                    VsShellUtilities.OpenDocument(serviceProvider, fileName, logicalTextViewGuid, out hierarchy, out itemID, out frame);
                }
                catch
                {
                    return;
                }
            }

            ErrorHandler.ThrowOnFailure(frame.Show());

            var vsTextView = VsShellUtilities.GetTextView(frame);

            IVsTextLines vsTextBuffer;

            ErrorHandler.ThrowOnFailure(vsTextView.GetBuffer(out vsTextBuffer));

            var vsTextManager = serviceProvider.GetService <SVsTextManager, IVsTextManager>();

            vsTextManager.NavigateToLineAndColumn(vsTextBuffer, logicalTextViewGuid, startLine, startCol, endRow, endCol);
        }
コード例 #17
0
        private IConnectionPoint GetConnectionPoint(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            IVsUIHierarchy uiHierarchy;
            uint           itemId;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(this.serviceProvider, path, Guid.Empty, out uiHierarchy, out itemId, out windowFrame))
            {
                var view      = VsShellUtilities.GetTextView(windowFrame);
                var container = view as IConnectionPointContainer;
                if (null != container)
                {
                    IConnectionPoint connectionPoint;
                    container.FindConnectionPoint(ref textViewGuid, out connectionPoint);
                    return(connectionPoint);
                }
            }

            return(null);
        }
コード例 #18
0
ファイル: IdeService.cs プロジェクト: teksoi/codestream
        //public EditorState GetActiveEditorState() {
        //	return GetActiveEditorState(out IVsTextView textView);
        //}

        // someday, this can return...
        //public bool QueryExtensions(string author, params string[] names)
        //{
        //    if (_extensionManager == null)
        //    {
        //        Log.Debug($"{nameof(_extensionManager)} is null");
        //        return false;
        //    }

        //    foreach (var extension in _extensionManager.GetInstalledExtensions())
        //    {
        //        IExtensionHeader header = extension.Header;
        //        if (!header.SystemComponent &&
        //            header.Author.EqualsIgnoreCase(author) && names.Any(_ => _.EqualsIgnoreCase(header.Name)))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        /// <summary>
        /// Tries to get an active text view for a file that may have just opened.
        /// Uses a naive exponential backoff algorithm against the IsDocumentOpen VSShell utility
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        /// <remarks>https://stackoverflow.com/a/7373385/208022</remarks>
        internal IWpfTextView TryGetPendingWpfTextView(string filePath)
        {
            var            editorAdapterFactoryService = _componentModel.GetService <IVsEditorAdaptersFactoryService>();
            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame = null;

            if (Retry.WithExponentialBackoff(() => {
                if (VsShellUtilities.IsDocumentOpen(
                        _serviceProvider,
                        filePath,
                        Guid.Empty,
                        out uiHierarchy,
                        out itemID,
                        out windowFrame))
                {
                    return(true);
                }
                return(false);
            }))
            {
                if (windowFrame == null)
                {
                    return(null);
                }

                IVsTextView view = VsShellUtilities.GetTextView(windowFrame);
                Log.Verbose($"{nameof(TryGetPendingWpfTextView)} found for {filePath}");
                return(editorAdapterFactoryService.GetWpfTextView(view));
            }

            return(null);
        }
コード例 #19
0
        public static void NavigateTo(this IServiceProvider serviceProvider, string fileName, int startLine, int startColumn)
        {
            Guid logicalViewGuid = new Guid(LogicalViewID.TextView);

            IVsUIHierarchy hierarchy;
            uint           itemId;
            IVsWindowFrame frame;
            bool           isOpened = VsShellUtilities.IsDocumentOpen(serviceProvider, fileName, logicalViewGuid, out hierarchy, out itemId, out frame);

            if (!isOpened)
            {
                try
                {
                    VsShellUtilities.OpenDocument(serviceProvider, fileName, logicalViewGuid, out hierarchy, out itemId, out frame);
                }
                catch (Exception)
                {
                    // TODO: write error to output
                    return;
                }
            }

            frame.Show();

            IVsTextLines textBuffer;

            VsShellUtilities.GetTextView(frame).GetBuffer(out textBuffer);

            IVsTextManager manager = serviceProvider.GetService <IVsTextManager, SVsTextManager>();

            manager.NavigateToLineAndColumn(textBuffer, logicalViewGuid, startLine, startColumn, startLine, startColumn);
        }
コード例 #20
0
        public virtual void NavigateToPosition(int position)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var serviceProvider = ServiceProvider.GlobalProvider;
            var textManager     = serviceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;
            var adapterService  = serviceProvider.GetMefService <IVsEditorAdaptersFactoryService>();
            var vsTextBuffer    = adapterService.GetBufferAdapter(_textBuffer);

            if (Disposed || vsTextBuffer == null)
            {
                VsShellUtilities.OpenDocument(serviceProvider, Path, Guid.Empty, out _, out _, out var windowFrame);
                var textView = VsShellUtilities.GetTextView(windowFrame);

                if (textView.GetBuffer(out var vsTextLines) != VSConstants.S_OK)
                {
                    return;
                }

                vsTextBuffer = vsTextLines;
            }

            var hr = textManager.NavigateToPosition(vsTextBuffer, VSConstants.LOGVIEWID.TextView_guid, position, 0);

            if (hr != VSConstants.S_OK)
            {
                throw Marshal.GetExceptionForHR(hr);
            }
        }
コード例 #21
0
        internal ITextBuffer GetBufferAt(string filePath)
        {
            var componentModel = (IComponentModel)VSGeneroPackage.GetGlobalService(typeof(SComponentModel));
            var editorAdapterFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>();
            IOleServiceProvider provider    = VSGeneroPackage.GetGlobalService(typeof(IOleServiceProvider)) as IOleServiceProvider;
            var serviceProvider             = new Microsoft.VisualStudio.Shell.ServiceProvider(provider);

            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    serviceProvider,
                    filePath,
                    Guid.Empty,
                    out uiHierarchy,
                    out itemID,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        return(editorAdapterFactoryService.GetDataBuffer(buffer));
                    }
                }
            }

            return(null);
        }
コード例 #22
0
        public void FormatDocument(string filePath)
        {
            IVsUIHierarchy vsUIHierarchy = null;
            uint           num           = 0;
            IVsWindowFrame vsWindowFrame = null;

            if (!VsShellUtilities.IsDocumentOpen(this.VisualStudio.ServiceProvider, filePath, Guid.Empty, out vsUIHierarchy, out num, out vsWindowFrame))
            {
                return;
            }
            IOleCommandTarget textView = (IOleCommandTarget)VsShellUtilities.GetTextView(vsWindowFrame);
            Guid gUID = typeof(VSConstants.VSStd2KCmdID).GUID;

            OLECMD[] oLECMDArray = new OLECMD[1];
            OLECMD   oLECMD      = new OLECMD()
            {
                cmdID = 143
            };

            oLECMDArray[0] = oLECMD;
            OLECMD[] oLECMDArray1 = oLECMDArray;
            int      num1         = textView.QueryStatus(ref gUID, 1, oLECMDArray1, IntPtr.Zero);

            Marshal.ThrowExceptionForHR(num1);
            if (oLECMDArray1[0].cmdf == 3)
            {
                num1 = textView.Exec(ref gUID, 143, 0, IntPtr.Zero, IntPtr.Zero);
                Marshal.ThrowExceptionForHR(num1);
            }
        }
コード例 #23
0
        private static ITextBuffer GetTextBuffer(ServiceProvider serviceProvider, IVsEditorAdaptersFactoryService editorAdapterFactoryService, string fileName)
        {
            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    serviceProvider,
                    fileName,
                    Guid.Empty,
                    out uiHierarchy,
                    out itemID,
                    out windowFrame))
            {
                IVsTextView  view = VsShellUtilities.GetTextView(windowFrame);
                IVsTextLines lines;
                if (view.GetBuffer(out lines) == 0)
                {
                    var buffer = lines as IVsTextBuffer;
                    if (buffer != null)
                    {
                        return(editorAdapterFactoryService.GetDataBuffer(buffer));
                    }
                }
            }

            return(null);
        }
コード例 #24
0
ファイル: EditorIntegration.cs プロジェクト: habbes/odata-lab
        public void FormatDocument(string filePath)
        {
            IVsTextView    vsTextView;
            IVsUIHierarchy uiHierarchy;
            uint           itemID;
            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
                    visualStudio.ServiceProvider,
                    filePath,
                    Guid.Empty,
                    out uiHierarchy,
                    out itemID,
                    out windowFrame))
            {
                vsTextView = VsShellUtilities.GetTextView(windowFrame);
            }
            else
            {
                Contract.Assert(false, "Failed to get the IVsTextView, is the document open in VS?");
                return;
            }

            IOleCommandTarget commandTarget = (IOleCommandTarget)vsTextView;

            Guid guid = typeof(VSConstants.VSStd2KCmdID).GUID;

            OLECMD[] commandStatus = new OLECMD[]
            {
                new OLECMD()
                {
                    cmdID = (uint)VSConstants.VSStd2KCmdID.FORMATDOCUMENT
                }
            };

            int hr = commandTarget.QueryStatus(
                ref guid,
                1,
                commandStatus,
                IntPtr.Zero);

            Marshal.ThrowExceptionForHR(hr);

            if (commandStatus[0].cmdf == (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED))
            {
                hr = commandTarget.Exec(
                    ref guid,
                    (uint)VSConstants.VSStd2KCmdID.FORMATDOCUMENT,
                    0u,
                    IntPtr.Zero,
                    IntPtr.Zero);

                Marshal.ThrowExceptionForHR(hr);
            }
            else
            {
                Contract.Assert(false, "The format command can't be executed right now, we don't expect this to happen.");
            }
        }
コード例 #25
0
        static VsTextView GetIVsTextView(IServiceProvider service, string filePath)
        {
            IVsWindowFrame windowFrame;

            return(VsShellUtilities.IsDocumentOpen(service, filePath, Guid.Empty, out IVsUIHierarchy uiHierarchy, out uint itemID, out windowFrame)
                                ? VsShellUtilities.GetTextView(windowFrame)
                                : null);
        }
コード例 #26
0
        /// <inheritdoc />
        /// <remarks>When an editor window closes, clear the current spell checking target</remarks>
        public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
        {
            if (VsShellUtilities.GetTextView(pFrame) != null)
            {
                ucSpellCheck.CurrentTextView = null;
            }

            return(VSConstants.S_OK);
        }
コード例 #27
0
        /// <summary>
        /// Opens a file in editor window.
        /// </summary>
        public async Task <DocumentView?> OpenAsync(string file)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider, file, Guid.Empty, out _, out _, out IVsWindowFrame? frame);
            IVsTextView?nativeView = VsShellUtilities.GetTextView(frame);

            return(await nativeView.ToDocumentViewAsync());
        }
コード例 #28
0
 public IVsTextView?GetVsTextView(string filePath)
 => VsShellUtilities.IsDocumentOpen(
     serviceProvider,
     filePath,
     Guid.Empty,
     out _,
     out _,
     out var windowFrame)
     ? VsShellUtilities.GetTextView(windowFrame)
     : null;
コード例 #29
0
        /// <inheritdoc />
        /// <remarks>When an editor window gains the focus, set it as the active spell checking target</remarks>
        int IVsSelectionEvents.OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
        {
            IWpfTextView wpfTextView = null;
            object       value;

            if ((Constants)elementid == Constants.SEID_WindowFrame)
            {
                var frame = varValueNew as IVsWindowFrame;

                ucSpellCheck.ParentFocused = false;

                if (frame != null)
                {
                    if (this.Frame == frame)
                    {
                        ucSpellCheck.ParentFocused = true;
                    }
                    else
                    if (frame.GetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, out value) == VSConstants.S_OK)
                    {
                        if ((VSFRAMEMODE)value == VSFRAMEMODE.VSFM_MdiChild ||
                            (VSFRAMEMODE)value == VSFRAMEMODE.VSFM_Float)
                        {
                            var textView = VsShellUtilities.GetTextView(frame);

                            if (textView != null)
                            {
                                var componentModel = Utility.GetServiceFromPackage <IComponentModel, SComponentModel>(true);

                                if (componentModel != null)
                                {
                                    var editorAdapterFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>();

                                    if (editorAdapterFactoryService != null)
                                    {
                                        try
                                        {
                                            wpfTextView = editorAdapterFactoryService.GetWpfTextView(textView);
                                        }
                                        catch (ArgumentException)
                                        {
                                            // Not an IWpfTextView so ignore it
                                        }
                                    }
                                }

                                ucSpellCheck.CurrentTextView = wpfTextView;
                            }
                        }
                    }
                }
            }

            return(VSConstants.S_OK);
        }
コード例 #30
0
        /// <summary>
        /// Opens a file in the Preview Tab (provisional tab) if supported by the editor factory.
        /// </summary>
        public async Task <DocumentView?> OpenInPreviewTabAsync(string file)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            using (new NewDocumentStateScope(__VSNEWDOCUMENTSTATE2.NDS_TryProvisional, VSConstants.NewDocumentStateReason.Navigation))
            {
                VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider, file, Guid.Empty, out _, out _, out IVsWindowFrame? frame);
                IVsTextView?nativeView = VsShellUtilities.GetTextView(frame);
                return(await nativeView.ToDocumentViewAsync());
            }
        }