internal void SetItemForActiveFrame(Uri itemUri)
        {
            object pvarValue;

            NativeMethods.ThrowOnFailure(_sel.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out pvarValue));
            var activeFrame = CreateFrameWrapper(pvarValue as IVsWindowFrame);

            _editingContextMgr.SetCurrentUri(activeFrame, itemUri);
            UpdateToolWindowsAndCmdsForFrame(activeFrame);
        }
        public IVsHierarchy GetStartupProjectHierachy()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            selectionMonitor.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_StartupProject, out object value);
            return(value as IVsHierarchy);
        }
Exemple #3
0
        public static IVsTextView GetIvsTextView(IServiceProvider provider)
        {
            IVsMonitorSelection selection = provider.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
            object frameObj = null;

            ErrorHandler.ThrowOnFailure(selection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out frameObj));

            IVsWindowFrame frame = frameObj as IVsWindowFrame;

            if (frame == null)
            {
            }

            object pvar;

            ErrorHandler.ThrowOnFailure(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView,
                                                          out pvar));

            IVsTextView textView = pvar as IVsTextView;

            if (textView == null)
            {
                IVsCodeWindow codeWin = pvar as IVsCodeWindow;
                if (codeWin != null)
                {
                    ErrorHandler.ThrowOnFailure(codeWin.GetLastActiveView(out textView));
                }
            }
            return(textView);
        }
Exemple #4
0
        public ITextDocument FindActiveDocument()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Get the current doc frame, if there is one
            monitorSelection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out var pvarValue);
            if (!(pvarValue is IVsWindowFrame frame))
            {
                return(null);
            }

            // Now get the doc data, which should also be a text buffer
            frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var docData);
            if (!(docData is IVsTextBuffer vsTextBuffer))
            {
                return(null);
            }

            // Finally, convert from the legacy VS editor interface to the new-style interface
            var textBuffer = editorAdapterService.GetDocumentBuffer(vsTextBuffer);

            ITextDocument newTextDocument = null;

            textBuffer?.Properties?.TryGetProperty(
                typeof(ITextDocument), out newTextDocument);

            return(newTextDocument);
        }
            private object GetCurrentElementValue(VSConstants.VSSELELEMID elementId)
            {
                object value = null;
                if (ErrorHandler.Succeeded(ErrorHandler.CallWithCOMConvention(() => _monitorSelection.GetCurrentElementValue((uint)elementId, out value))))
                    return value;

                return null;
            }
 /// <include file='doc\PropertyBrowserService.uex' path='docs/doc[@for="PropertyBrowserService.PbrsCommandTarget.GetCurrentContextTarget"]/*' />
 /// <devdoc>
 /// Gets the current user context from the shell and asks it for
 /// the current NativeMethods.IOleCommandTarget.  We will delegate to this command
 /// target for undo/redo calls
 /// </devdoc>
 public NativeMethods.IOleCommandTarget GetCurrentContextTarget() {
    if (vsMonitorSelection != null) {
       try {
          Object pUserContext = null;
          vsMonitorSelection.GetCurrentElementValue(__SEID.UndoManager, ref pUserContext);
          return (NativeMethods.IOleCommandTarget)pUserContext;
       }
       catch(Exception) {
       }
    }
    return null;
 }
        public ITextDocument FindActiveDocument()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Get the current doc frame, if there is one
            monitorSelection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out var pvarValue);
            if (!(pvarValue is IVsWindowFrame frame))
            {
                return(null);
            }

            return(textDocumentProvider.GetFromFrame(frame));
        }
Exemple #8
0
        /// <summary>
        /// Find the active text view (if any) in the active document.
        /// </summary>
        /// <returns>The IVsTextView of the active view, or null if there is no active document or the
        /// active view in the active document is not a text view.</returns>
        private IVsTextView GetActiveTextView()
        {
            IVsMonitorSelection selection = GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
            object frameObj = null;

            ErrorHandler.ThrowOnFailure(selection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out frameObj));

            IVsWindowFrame frame = frameObj as IVsWindowFrame;

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

            return(GetActiveView(frame));
        }
Exemple #9
0
        int IVsSelectionEvents.OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
        {
            var id = (VSConstants.VSSELELEMID)elementid;

            if (id == VSConstants.VSSELELEMID.SEID_WindowFrame)
            {
                ITextView getTextView(object obj)
                {
                    var vsWindowFrame = obj as IVsWindowFrame;

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

                    var vsCodeWindow = vsWindowFrame.GetCodeWindow();

                    if (vsCodeWindow.IsError)
                    {
                        return(null);
                    }

                    var lastActiveTextView = vsCodeWindow.Value.GetLastActiveView(_vsAdapter.EditorAdapter);

                    if (lastActiveTextView.IsError)
                    {
                        return(null);
                    }

                    return(lastActiveTextView.Value);
                }

                ITextView oldView = getTextView(varValueOld);
                ITextView newView = null;
                if (ErrorHandler.Succeeded(_vsMonitorSelection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_WindowFrame, out object value)))
                {
                    newView = getTextView(value);
                }

                RaiseActiveTextViewChanged(
                    oldView == null ? FSharpOption <ITextView> .None : FSharpOption.Create <ITextView>(oldView),
                    newView == null ? FSharpOption <ITextView> .None : FSharpOption.Create <ITextView>(newView));
            }

            return(VSConstants.S_OK);
        }
        private static ModelingDocView GetWindowFrameProperty(IServiceProvider provider, __VSFPROPID propertyId)
        {
            IVsMonitorSelection selection = (IVsMonitorSelection)provider.GetService(typeof(IVsMonitorSelection));

            if (selection != null)
            {
                object frameObject;
                selection.GetCurrentElementValue(2, out frameObject);

                IVsWindowFrame windowFrame = frameObject as IVsWindowFrame;
                if (windowFrame != null)
                {
                    object propertyValue;
                    windowFrame.GetProperty((int)propertyId, out propertyValue);
                    return(propertyValue as ModelingDocView);
                }
            }

            return(null);
        }
        private void SetInitialContext()
        {
            IntPtr hierPtr      = IntPtr.Zero;
            IntPtr containerPtr = IntPtr.Zero;

            try {
                _monitorSelection.GetCurrentSelection(out hierPtr, out uint item, out _, out containerPtr);
                var hier = (hierPtr != IntPtr.Zero ? Marshal.GetObjectForIUnknown(hierPtr) : null) as IVsHierarchy;
                if (hier != null)
                {
                    HandleSelection(hier, item);
                }
                else if (ErrorHandler.Succeeded(_monitorSelection.GetCurrentElementValue(VSConstants.DocumentFrame, out object elementVal)))
                {
                    HandleElementValue(elementVal);
                }
                else
                {
                    Reset();
                }
            } finally {
        private WindowType GetActiveWindowType()
        {
            object element;

            _monitorSelection.GetCurrentElementValue((uint)Microsoft.VisualStudio.VSConstants.VSSELELEMID.SEID_WindowFrame, out element);
            if (element == null)
            {
                return(WindowType.Unknown);
            }

            var window = element as IVsWindowFrame;

            // Using VSFPROPID_Type we could quickly identify if it's a Document Frame (1), or a Tool Window (2)
            object windowFrameType;

            window.GetProperty((int)__VSFPROPID.VSFPROPID_Type, out windowFrameType);
            if ((int)windowFrameType == DocumentFrame)
            {
                return(WindowType.DocumentEditor);
            }

            // Well it's not the editor. Now check by guid to see what it is.
            Guid windowTypeGuid;

            window.GetGuidProperty((int)__VSFPROPID.VSFPROPID_GuidPersistenceSlot, out windowTypeGuid);

            if (windowTypeGuid.Equals(SolutionExplorerGuid))
            {
                return(WindowType.SolutionExplorer);
            }
            if (windowTypeGuid.Equals(VSConstants.VsEditorFactoryGuid.TextEditor_guid))
            {
                return(WindowType.DocumentEditor);
            }

            // Log unknown window
            OutputWindow.Log("Current window Guid: " + windowTypeGuid);
            return(WindowType.Unknown);
        }
Exemple #13
0
        public static IVsWindowFrame GetActiveWindowFrame(IServiceProvider serviceProvider)
        {
            Debug.Assert(serviceProvider != null);

            IVsMonitorSelection monitorSelection = (IVsMonitorSelection)serviceProvider.GetService(typeof(SVsShellMonitorSelection));

            if (monitorSelection != null)
            {
                object pvar = null;
                if (ErrorHandler.Succeeded(monitorSelection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out pvar)))
                {
                    IVsWindowFrame activeFrame = pvar as IVsWindowFrame;

                    if (activeFrame != null)
                    {
                        return(activeFrame);
                    }
                }
            }

            return(null);
        }
 public static IVsWindowFrame GetDocumentFrame(IVsMonitorSelection monitorSelection)
 {
     ErrorHandler.Succeeded(monitorSelection.GetCurrentElementValue((uint)VSSELELEMID.SEID_DocumentFrame, out object element));
     return(element as IVsWindowFrame);
 }
 public int GetCurrentElementValue(uint elementid, out object pvarValue)
 {
     return(_selection.GetCurrentElementValue(elementid, out pvarValue));
 }
Exemple #16
0
        private void GetCurrentSource()
        {
            try
            {
                IVsMonitorSelection selection = (IVsMonitorSelection)Package.GetGlobalService(typeof(SVsShellMonitorSelection));
                object pvar = null;
                if (!ErrorHandler.Succeeded(selection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out pvar)))
                {
                    this.currentDocument = null;
                    return;
                }
                IVsWindowFrame frame = pvar as IVsWindowFrame;
                if (frame == null)
                {
                    this.currentDocument = null;
                    return;
                }

                object docData = null;
                if (!ErrorHandler.Succeeded(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out docData)))
                {
                    this.currentDocument = null;
                    return;
                }
                object docViewServiceObject;
                if (!ErrorHandler.Succeeded(frame.GetProperty((int)Microsoft.VisualStudio.Shell.Interop.__VSFPROPID.VSFPROPID_SPFrame, out docViewServiceObject)))
                {
                    this.currentDocument = null;
                    return;
                }

                IVsTextLines buffer = docData as IVsTextLines;
                if (buffer == null)
                {
                    IVsTextBufferProvider tb = docData as IVsTextBufferProvider;
                    if (tb != null)
                    {
                        tb.GetTextBuffer(out buffer);
                    }
                }
                if (buffer == null)
                {
                    this.currentDocument = null;
                    return;
                }

                IOleServiceProvider docViewService = (IOleServiceProvider)docViewServiceObject;
                if (this.currentDocument == null || buffer != this.currentDocument.TextEditorBuffer)
                {
                    this.currentDocument = new VisualStudioDocument(frame, buffer, docViewService);
                    this.changeCount     = this.currentDocument.Source.ChangeCount;
                }
                else
                {
                    if (this.changeCount != this.currentDocument.Source.ChangeCount)
                    {
                        this.currentDocument.Reload();
                        this.changeCount = this.currentDocument.Source.ChangeCount;
                    }
                }
                return;
            }
            catch (Exception e)
            {
                ReportError(e);
            }
        }
Exemple #17
0
        /// <summary>
        /// Create the list of selection elements for the selection view.  This includes the core slots as well as any
        /// registered extension provided selection elenents
        /// </summary>
        void InitializeSelectionItems()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsMonitorSelection selectionMonitor = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
            object elementValue;
            string elementName;

            // Initialize the current selection objects
            IVsHierarchy        hierarchy          = null;
            ISelectionContainer selectionContainer = null;

            selectionMonitor.GetCurrentSelection(out var pHierarchy, out var itemID, out var multiSelect, out var pSelectionContainer);
            if (pHierarchy != IntPtr.Zero)
            {
                hierarchy = Marshal.GetObjectForIUnknown(pHierarchy) as IVsHierarchy;
            }
            if (pSelectionContainer != IntPtr.Zero)
            {
                selectionContainer = Marshal.GetObjectForIUnknown(pSelectionContainer) as ISelectionContainer;
            }

            // Sync descriptions with selection by calling OnSelectionChanged
            SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.Hierarchy, description: string.Empty, owner: string.Empty));
            SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.ItemID, description: string.Empty, owner: string.Empty));
            SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.SelectionContainer, description: string.Empty, owner: string.Empty));
            SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.MultiItemSelect, description: string.Empty, owner: string.Empty));

            OnSelectionChanged(null, 0, null, null, hierarchy, itemID, multiSelect, selectionContainer);

            // Initialize the current selection data with the element values
            VSConstants.SelectionElement[] elements = { VSConstants.SelectionElement.WindowFrame,
                                                        VSConstants.SelectionElement.DocumentFrame,
                                                        VSConstants.SelectionElement.UndoManager,
                                                        VSConstants.SelectionElement.StartupProject,
                                                        VSConstants.SelectionElement.UserContext,
                                                        VSConstants.SelectionElement.PropertyBrowserSID,
                                                        VSConstants.SelectionElement.ResultList,
                                                        VSConstants.SelectionElement.LastWindowFrame };

            foreach (VSConstants.SelectionElement element in elements)
            {
                selectionMonitor.GetCurrentElementValue((uint)element, out elementValue);
                object valueOwner = GetOwnerForSelectedElement(element);
                SelectionItems.Add(new SelectionItemInfo(element, GetSelectionElementDescription(elementValue), GetSelectionElementDescription(valueOwner)));
            }

            // Read the VSIP registered selection elements

            // Skip this one, it is not supported yet and will always be empty
            Guid surfaceSelectionElement = new Guid("{64db9e55-5614-44b3-93c9-e617b95eeb5f}");

            IVsMonitorSelection2 selectionMonitor2 = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection2;

            using RegistryKey rootKey     = GetRegistryRoot();
            using RegistryKey elementsKey = rootKey.OpenSubKey("SelectionElements");
            if (elementsKey != null)
            {
                string[] elementGuids = elementsKey.GetSubKeyNames();
                foreach (string guidString in elementGuids)
                {
                    using RegistryKey elementKey = elementsKey.OpenSubKey(guidString);
                    Guid elementGuid = new Guid(guidString);
                    selectionMonitor2.GetElementID(ref elementGuid, out var selElem);

                    selectionMonitor.GetCurrentElementValue(selElem, out elementValue);
                    elementName = (string)elementKey.GetValue("Name", defaultValue: string.Empty);

                    // Skip this one, it is not supported yet and will always be empty
                    if (elementGuid == surfaceSelectionElement)
                    {
                        continue;
                    }

                    object contextOwner = GetOwnerForSelectedElement((VSConstants.SelectionElement)selElem);

                    SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)selElem, elementName, GetSelectionElementDescription(elementValue), GetSelectionElementDescription(contextOwner)));
                }
            }
        }
Exemple #18
0
        /// <summary>
        /// Move to the next/prior spelling issue within the current document
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void SpellCheckNextPriorIssueExecuteHandler(object sender, EventArgs e)
        {
            var command = sender as OleMenuCommand;

            if (command == null)
            {
                return;
            }

            IWpfTextView wpfTextView = null;
            object       value;

            IVsMonitorSelection ms = (IVsMonitorSelection)GetService(typeof(SVsShellMonitorSelection));

            ms.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out value);

            IVsWindowFrame frame = value as IVsWindowFrame;

            if (frame != null && frame.GetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, out value) == VSConstants.S_OK &&
                ((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);
                                SpellingTagger tagger;

                                if (wpfTextView.Properties.TryGetProperty(typeof(SpellingTagger), out tagger) &&
                                    tagger.CurrentMisspellings.Any())
                                {
                                    var caretPoint = wpfTextView.Caret.Position.BufferPosition;
                                    var allIssues  = tagger.CurrentMisspellings.OrderBy(
                                        i => i.Span.GetSpan(i.Span.TextBuffer.CurrentSnapshot).Start.Position).ToList();
                                    var issue = allIssues.FirstOrDefault(i => i.Span.GetSpan(
                                                                             i.Span.TextBuffer.CurrentSnapshot).Contains(caretPoint));
                                    int offset = (command.CommandID.ID == PkgCmdIDList.SpellCheckNextIssue) ? 1 : -1;

                                    if (issue == null)
                                    {
                                        issue = allIssues.FirstOrDefault(i => i.Span.GetSpan(
                                                                             i.Span.TextBuffer.CurrentSnapshot).Start.Position > caretPoint.Position);

                                        if (issue == null)
                                        {
                                            issue  = tagger.CurrentMisspellings.Last();
                                            offset = (command.CommandID.ID == PkgCmdIDList.SpellCheckNextIssue) ? 1 : 0;
                                        }
                                        else
                                        {
                                            offset = (command.CommandID.ID == PkgCmdIDList.SpellCheckNextIssue) ? 0 : -1;
                                        }
                                    }

                                    if (issue != null)
                                    {
                                        int idx = allIssues.IndexOf(issue) + offset;

                                        if (idx < 0)
                                        {
                                            idx = allIssues.Count - 1;
                                        }
                                        else
                                        if (idx >= allIssues.Count)
                                        {
                                            idx = 0;
                                        }

                                        issue = allIssues[idx];

                                        var span = issue.Span.GetSpan(issue.Span.TextBuffer.CurrentSnapshot);

                                        // If in a collapsed region, expand the region
                                        var outliningManagerService = componentModel.GetService <IOutliningManagerService>();

                                        if (outliningManagerService != null)
                                        {
                                            var outliningManager = outliningManagerService.GetOutliningManager(wpfTextView);

                                            if (outliningManager != null)
                                            {
                                                foreach (var region in outliningManager.GetCollapsedRegions(span, false))
                                                {
                                                    if (region.IsCollapsed)
                                                    {
                                                        outliningManager.Expand(region);
                                                    }
                                                }
                                            }
                                        }

                                        wpfTextView.Caret.MoveTo(span.Start);
                                        wpfTextView.ViewScroller.EnsureSpanVisible(span, EnsureSpanVisibleOptions.AlwaysCenter);
                                        wpfTextView.Selection.Select(span, false);
                                    }
                                }
                            }
                            catch (ArgumentException)
                            {
                                // Not an IWpfTextView so ignore it
                            }
                        }
                    }
                }
            }
        }