public static Result <IVsCodeWindow> GetCodeWindow(this IVsWindowFrame vsWindowFrame)
        {
            var iid = typeof(IVsCodeWindow).GUID;
            var ptr = IntPtr.Zero;

            try
            {
                var hr = vsWindowFrame.QueryViewInterface(ref iid, out ptr);
                if (ErrorHandler.Failed(hr))
                {
                    return(Result.CreateError(hr));
                }

                return(Result.CreateSuccess((IVsCodeWindow)Marshal.GetObjectForIUnknown(ptr)));
            }
            catch (Exception e)
            {
                // Venus will throw when querying for the code window
                return(Result.CreateError(e));
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.Release(ptr);
                }
            }
        }
        public int NavigateTo()
        {
            int hr      = VSConstants.S_OK;
            var openDoc = _serviceProvider.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

            if (openDoc == null)
            {
                Debug.Fail("Failed to get SVsUIShellOpenDocument service.");
                return(VSConstants.E_UNEXPECTED);
            }

            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = null;
            IVsUIHierarchy hierarchy = null;
            uint           itemID    = 0;
            IVsWindowFrame frame     = null;
            Guid           viewGuid  = VSConstants.LOGVIEWID_TextView;

            hr = openDoc.OpenDocumentViaProject(_file, ref viewGuid, out sp, out hierarchy, out itemID, out frame);
            Debug.Assert(hr == VSConstants.S_OK, "OpenDocumentViaProject did not return S_OK.");

            hr = frame.Show();
            Debug.Assert(hr == VSConstants.S_OK, "Show did not return S_OK.");

            IntPtr viewPtr       = IntPtr.Zero;
            Guid   textLinesGuid = typeof(IVsTextLines).GUID;

            hr = frame.QueryViewInterface(ref textLinesGuid, out viewPtr);
            Debug.Assert(hr == VSConstants.S_OK, "QueryViewInterface did not return S_OK.");

            IVsTextLines textLines = Marshal.GetUniqueObjectForIUnknown(viewPtr) as IVsTextLines;

            var textMgr = _serviceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;

            if (textMgr == null)
            {
                Debug.Fail("Failed to get SVsTextManager service.");
                return(VSConstants.E_UNEXPECTED);
            }

            IVsTextView textView = null;

            hr = textMgr.GetActiveView(0, textLines, out textView);
            Debug.Assert(hr == VSConstants.S_OK, "QueryViewInterface did not return S_OK.");

            if (textView != null)
            {
                if (_line >= 0)
                {
                    textView.SetCaretPos(_line, Math.Max(_column, 0));
                }
            }

            return(VSConstants.S_OK);
        }
Beispiel #3
0
        void LoadXamlEditor()
        {
            //Guid editorGuid = new Guid("FA3CD31E-987B-443A-9B81-186104E8DAC1"); // XML Editor
            //Guid editorGuid = new Guid("DEE6CEF9-3BCA-449A-82A6-FC757D6956FB"); // XSD Editor
            //Guid editorGuid = new Guid("f11acc28-31d1-4c80-a34b-f4e09d3d753c"); // XAML UI Designer (TabbedViewEditorFactory)
            //Guid editorGuid = new Guid("a4f9ff65-a78c-4650-866d-5069cc4127cf"); // XAML Text Editor (XamlTabEditorFactory)
            Guid editorGuid = VSConstants.VsEditorFactoryGuid.TextEditor_guid;

            IVsWindowFrame frame;
            IVsProject3    vsProject   = (IVsProject3)_hierarchy;
            Guid           _emptyGuid  = Guid.Empty;
            uint           editorFlags = (uint)__VSSPECIFICEDITORFLAGS.VSSPECIFICEDITOR_UseEditor | (uint)__VSSPECIFICEDITORFLAGS.VSSPECIFICEDITOR_DoOpen;

            ErrorHandler.ThrowOnFailure(
                vsProject.OpenItemWithSpecific(
                    _itemid,
                    editorFlags,
                    ref editorGuid,
                    _pszPhysicalView,
                    ref _emptyGuid,
                    _punkDocData,
                    out frame));

            IVsWindowFrame windowFrameOrig = GetFrame();

            Debug.Assert(windowFrameOrig != null);
            ErrorHandler.ThrowOnFailure(frame.SetProperty((int)__VSFPROPID2.VSFPROPID_ParentFrame, windowFrameOrig));
            ErrorHandler.ThrowOnFailure(frame.SetProperty((int)__VSFPROPID2.VSFPROPID_ParentHwnd, GetChildContainerHandle()));
            //ErrorHandler.ThrowOnFailure(frame.SetProperty((int)__VSFPROPID.VSFPROPID_pszPhysicalView, "MainFrame"));
            _subFrame = frame;
            var    IID_IVsCodeWindow = typeof(IVsCodeWindow).GUID;
            IntPtr intPtr;

            _subFrame.QueryViewInterface(ref IID_IVsCodeWindow, out intPtr);
            _subCodeWindow = Marshal.GetObjectForIUnknown(intPtr) as IVsCodeWindow;
            //ErrorHandler.ThrowOnFailure(((IVsWindowFrame2)frame).ActivateOwnerDockedWindow());

            /*
             * IVsTextView vsTextView;
             * ((IVsCodeWindow)_subCodeWindow).GetPrimaryView(out vsTextView);
             *
             * // Enable the "Quick Find" feature of Visual Studio:
             * EnableAutonomousFind(windowFrameOrig, vsTextView);
             * EnableAutonomousFind(_subFrame, vsTextView);
             *
             * foreach (Type theInterface in _subCodeWindow.GetType().GetInterfaces())
             * {
             *  string name = theInterface.Name;
             *  string toString = theInterface.ToString();
             * }
             */
        }
Beispiel #4
0
        public int NavigateTo(string fileName, int lineNumber, int columnNumber = 1)
        {
            lineNumber--;
            columnNumber--;
            int hr      = VSConstants.S_OK;
            var openDoc = ServiceProvider.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

            if (openDoc == null)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = null;
            IVsUIHierarchy hierarchy = null;
            uint           itemID    = 0;
            IVsWindowFrame frame     = null;
            Guid           viewGuid  = VSConstants.LOGVIEWID_TextView;

            hr = openDoc.OpenDocumentViaProject(fileName, ref viewGuid, out sp, out hierarchy, out itemID, out frame);

            hr = frame.Show();

            IntPtr viewPtr       = IntPtr.Zero;
            Guid   textLinesGuid = typeof(IVsTextLines).GUID;

            hr = frame.QueryViewInterface(ref textLinesGuid, out viewPtr);

            IVsTextLines textLines = Marshal.GetUniqueObjectForIUnknown(viewPtr) as IVsTextLines;

            var textMgr = ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;

            if (textMgr == null)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            IVsTextView textView = null;

            hr = textMgr.GetActiveView(0, textLines, out textView);

            if (textView != null)
            {
                if (lineNumber >= 0)
                {
                    textView.SetCaretPos(lineNumber, Math.Max(columnNumber, 0));
                }
            }

            return(VSConstants.S_OK);
        }
Beispiel #5
0
        private static IVsTextView GetActiveVsTextView(IVsWindowFrame windowFrame)
        {
            Requires.NotNull(windowFrame, nameof(windowFrame));

            // We want to query for IVsCodeWindow here in order to get the correct text view in diff mode.
            // Using windowFrame.GetProperty() method gets us the difference viewer that's always tied to the right view, which won't work for inline diff.
            IntPtr ppCodeWindow;

            ErrorHandler.ThrowOnFailure(windowFrame.QueryViewInterface(typeof(IVsCodeWindow).GUID, out ppCodeWindow));

            var vsCodeWindow = Marshal.GetObjectForIUnknown(ppCodeWindow) as IVsCodeWindow;

            Marshal.Release(ppCodeWindow);

            if (vsCodeWindow != null)
            {
                // We want to call GetLastActiveView() to make sure we get the right text view to support inline diff.
                // Otherwise we might be stuck with the right view only for side by side diff.
                ErrorHandler.ThrowOnFailure(vsCodeWindow.GetLastActiveView(out IVsTextView textView));
                return(textView);
            }

            return(null);
        }
        public static IVsCodeWindow GetCodeWindow(this IVsTextView text_view)
        {
            Contract.Requires <ArgumentNullException>(text_view != null, "textView");

            IObjectWithSite object_with_site = text_view as IObjectWithSite;

            if (object_with_site == null)
            {
                return(null);
            }

            Guid   riid = typeof(IOleServiceProvider).GUID;
            IntPtr ppvSite;

            object_with_site.GetSite(ref riid, out ppvSite);
            if (ppvSite == IntPtr.Zero)
            {
                return(null);
            }

            IOleServiceProvider ole_service_provider = null;

            try
            {
                ole_service_provider = Marshal.GetObjectForIUnknown(ppvSite) as IOleServiceProvider;
            }
            finally
            {
                Marshal.Release(ppvSite);
            }

            if (ole_service_provider == null)
            {
                return(null);
            }

            Guid guid_service = typeof(SVsWindowFrame).GUID;

            riid = typeof(IVsWindowFrame).GUID;
            IntPtr ppvObject;

            if (ErrorHandler.Failed(ole_service_provider.QueryService(ref guid_service, ref riid, out ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            IVsWindowFrame frame = null;

            try
            {
                frame = Marshal.GetObjectForIUnknown(ppvObject) as IVsWindowFrame;
            }
            finally
            {
                Marshal.Release(ppvObject);
            }

            riid = typeof(IVsCodeWindow).GUID;
            if (ErrorHandler.Failed(frame.QueryViewInterface(ref riid, out ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            IVsCodeWindow code_window = null;

            try
            {
                code_window = Marshal.GetObjectForIUnknown(ppvObject) as IVsCodeWindow;
                return(code_window);
            }
            finally
            {
                Marshal.Release(ppvObject);
            }
        }
Beispiel #7
0
        public int NavigateTo(string file, int line, int column)
        {
            int hr      = VSConstants.S_OK;
            var openDoc = _provider.globalServiceProvider.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

            if (openDoc == null)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = null;
            IVsUIHierarchy hierarchy = null;
            uint           itemID    = 0;
            IVsWindowFrame frame     = null;
            Guid           viewGuid  = VSConstants.LOGVIEWID_TextView;

            hr = openDoc.OpenDocumentViaProject(file, ref viewGuid, out sp, out hierarchy, out itemID, out frame);
            if (hr != VSConstants.S_OK)
            {
                return(hr);
            }
            hr = frame.Show();
            if (hr != VSConstants.S_OK)
            {
                return(hr);
            }

            IntPtr viewPtr       = IntPtr.Zero;
            Guid   textLinesGuid = typeof(IVsTextLines).GUID;

            hr = frame.QueryViewInterface(ref textLinesGuid, out viewPtr);
            if (hr != VSConstants.S_OK)
            {
                return(hr);
            }

            IVsTextLines textLines = Marshal.GetUniqueObjectForIUnknown(viewPtr) as IVsTextLines;

            var textMgr = _provider.globalServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;

            if (textMgr == null)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            IVsTextView textView = null;

            hr = textMgr.GetActiveView(0, textLines, out textView);
            if (hr != VSConstants.S_OK)
            {
                return(hr);
            }

            if (textView != null && line > 0)
            {
                int col = Math.Max(column - 1, 0);
                textView.SetCaretPos(line - 1, col);
                var span = new TextSpan {
                    iStartLine = line - 1, iStartIndex = col, iEndLine = line - 1, iEndIndex = col + 1
                };
                textView.EnsureSpanVisible(span);
                textView.CenterLines(line - 1, 1);
            }

            return(VSConstants.S_OK);
        }
Beispiel #8
0
        public static IVsCodeWindow GetCodeWindow([NotNull] this IVsTextView textView)
        {
            Requires.NotNull(textView, nameof(textView));

            IObjectWithSite objectWithSite = textView as IObjectWithSite;

            if (objectWithSite == null)
            {
                return(null);
            }

            Guid   riid = typeof(IOleServiceProvider).GUID;
            IntPtr ppvSite;

            objectWithSite.GetSite(ref riid, out ppvSite);
            if (ppvSite == IntPtr.Zero)
            {
                return(null);
            }

            IOleServiceProvider oleServiceProvider = null;

            try
            {
                oleServiceProvider = Marshal.GetObjectForIUnknown(ppvSite) as IOleServiceProvider;
            }
            finally
            {
                Marshal.Release(ppvSite);
            }

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

            Guid guidService = typeof(SVsWindowFrame).GUID;

            riid = typeof(IVsWindowFrame).GUID;
            IntPtr ppvObject;

            if (ErrorHandler.Failed(oleServiceProvider.QueryService(ref guidService, ref riid, out ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            IVsWindowFrame frame = null;

            try
            {
                frame = Marshal.GetObjectForIUnknown(ppvObject) as IVsWindowFrame;
            }
            finally
            {
                Marshal.Release(ppvObject);
            }

            riid = typeof(IVsCodeWindow).GUID;
            if (ErrorHandler.Failed(frame.QueryViewInterface(ref riid, out ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            IVsCodeWindow codeWindow = null;

            try
            {
                codeWindow = Marshal.GetObjectForIUnknown(ppvObject) as IVsCodeWindow;
                return(codeWindow);
            }
            finally
            {
                Marshal.Release(ppvObject);
            }
        }
        private static IVsCodeWindow TryGetCodeWindow(IVsTextView textView)
        {
            if (textView == null)
            {
                throw new ArgumentNullException(nameof(textView));
            }

            if (!(textView is IObjectWithSite objectWithSite))
            {
                return(null);
            }

            var riid = typeof(IOleServiceProvider).GUID;

            objectWithSite.GetSite(ref riid, out var ppvSite);
            if (ppvSite == IntPtr.Zero)
            {
                return(null);
            }

            IOleServiceProvider oleServiceProvider = null;

            try
            {
                oleServiceProvider = Marshal.GetObjectForIUnknown(ppvSite) as IOleServiceProvider;
            }
            finally
            {
                Marshal.Release(ppvSite);
            }

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

            var guidService = typeof(SVsWindowFrame).GUID;

            riid = typeof(IVsWindowFrame).GUID;
            if (ErrorHandler.Failed(oleServiceProvider.QueryService(ref guidService, ref riid, out var ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            IVsWindowFrame frame = null;

            try
            {
                frame = Marshal.GetObjectForIUnknown(ppvObject) as IVsWindowFrame;
            }
            finally
            {
                Marshal.Release(ppvObject);
            }

            riid = typeof(IVsCodeWindow).GUID;
            if (ErrorHandler.Failed(frame.QueryViewInterface(ref riid, out ppvObject)) || ppvObject == IntPtr.Zero)
            {
                return(null);
            }

            try
            {
                return(Marshal.GetObjectForIUnknown(ppvObject) as IVsCodeWindow);
            }
            finally
            {
                Marshal.Release(ppvObject);
            }
        }
Beispiel #10
0
 // --------------------------------------------------------------------------------------------
 /// <summary>
 /// Provides IVsWindowFrame with a view helper (VSFPROPID_ViewHelper) inserted into its list
 /// of event notifications.
 /// </summary>
 /// <param name="riid">Identifier of the window frame being requested.</param>
 /// <param name="ppv">
 /// Address of pointer variable that receives the window frame pointer requested in riid.
 /// </param>
 /// <returns>
 /// If the method succeeds, it returns S_OK. If it fails, it returns an error code.
 /// </returns>
 // --------------------------------------------------------------------------------------------
 int IVsWindowFrame.QueryViewInterface(ref Guid riid, out IntPtr ppv)
 {
     return(_Frame.QueryViewInterface(ref riid, out ppv));
 }
        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            IntPtr pCodeWindow;
            Guid riid = typeof(IVsCodeWindow).GUID;

            pFrame.QueryViewInterface(ref riid, out pCodeWindow);
            if (pCodeWindow.ToInt32() == 0)
            {
                //the window focus is set on is not codeWindow,so do not handle textView changes 
                HandleTextViewChange(null);
            }
            else
            {
                activeCodeFile = PathOfCookie(docCookie);
                if (NeedsNotify(activeCodeFile))
                {
                    try
                    {
                        IVsCodeWindow codeWindow = (IVsCodeWindow)Marshal.GetObjectForIUnknown(pCodeWindow);
                        IVsTextView textView = null;
                        codeWindow.GetPrimaryView(out textView);
                        HandleTextViewChange(textView);
                    }
                    finally
                    {
                        Marshal.Release(pCodeWindow);
                    }

                }
                else
                {
                    activeCodeFile = null;
                }
            }
            

            return VSConstants.S_OK;
        }