private static Document OpenDocument(Workspace workspace, DocumentId documentId, OptionSet options)
        {
            options = options ?? workspace.Options;

            // Always open the document again, even if the document is already open in the
            // workspace. If a document is already open in a preview tab and it is opened again
            // in a permanent tab, this allows the document to transition to the new state.
            if (workspace.CanOpenDocuments)
            {
                if (options.GetOption(NavigationOptions.PreferProvisionalTab))
                {
                    using (NewDocumentStateScope ndss = new NewDocumentStateScope(__VSNEWDOCUMENTSTATE.NDS_Provisional, VSConstants.NewDocumentStateReason.Navigation))
                    {
                        workspace.OpenDocument(documentId);
                    }
                }
                else
                {
                    workspace.OpenDocument(documentId);
                }
            }

            if (!workspace.IsDocumentOpen(documentId))
            {
                return(null);
            }

            return(workspace.CurrentSolution.GetDocument(documentId));
        }
示例#2
0
        private static Document OpenDocument(Workspace workspace, DocumentId documentId, OptionSet options)
        {
            options = options ?? workspace.Options;

            // Always open the document again, even if the document is already open in the
            // workspace. If a document is already open in a preview tab and it is opened again
            // in a permanent tab, this allows the document to transition to the new state.
            if (workspace.CanOpenDocuments)
            {
                if (options.GetOption(NavigationOptions.PreferProvisionalTab))
                {
                    // If we're just opening the provisional tab, then do not "activate" the document
                    // (i.e. don't give it focus).  This way if a user is just arrowing through a set
                    // of FindAllReferences results, they don't have their cursor placed into the document.
                    var state = __VSNEWDOCUMENTSTATE.NDS_Provisional | __VSNEWDOCUMENTSTATE.NDS_NoActivate;
                    using (var scope = new NewDocumentStateScope(state, VSConstants.NewDocumentStateReason.Navigation))
                    {
                        workspace.OpenDocument(documentId);
                    }
                }
                else
                {
                    workspace.OpenDocument(documentId);
                }
            }

            if (!workspace.IsDocumentOpen(documentId))
            {
                return(null);
            }

            return(workspace.CurrentSolution.GetDocument(documentId));
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == _vSStd97CmdIDGuid && nCmdID == (uint)VSConstants.VSStd97CmdID.GotoDefn)
            {
                TextView.TextBuffer.Properties.TryGetProperty <ITextDocument>(typeof(ITextDocument), out ITextDocument textDoc);

                if (PackageCompletionSource.IsInRangeForPackageCompletion(TextView.TextSnapshot, TextView.Caret.Position.BufferPosition.Position, out Span targetSpan, out string packageName, out string packageVersion, out string completionType))
                {
                    if (PackageExistsOnNuGet(packageName, packageVersion, out string url))
                    {
                        System.Diagnostics.Process.Start(url);
                        return(VSConstants.S_OK);
                    }
                }

                IWorkspace        workspace   = _workspaceManager.GetWorkspace(textDoc.FilePath);
                List <Definition> definitions = workspace.ResolveDefinition(textDoc.FilePath, TextView.TextSnapshot.GetText(), TextView.Caret.Position.BufferPosition.Position);

                if (definitions.Count == 1)
                {
                    DTE dte = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE;
                    dte.MainWindow.Activate();

                    using (var state = new NewDocumentStateScope(Microsoft.VisualStudio.Shell.Interop.__VSNEWDOCUMENTSTATE.NDS_Provisional, Guid.Parse(ProjectFileToolsPackage.PackageGuidString)))
                    {
                        EnvDTE.Window w = dte.ItemOperations.OpenFile(definitions[0].File, EnvDTE.Constants.vsViewKindTextView);

                        if (definitions[0].Line.HasValue)
                        {
                            ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).GotoLine(definitions[0].Line.Value, true);
                        }
                    }

                    return(VSConstants.S_OK);
                }

                else if (definitions.Count > 1)
                {
                    IFindAllReferencesService farService = (IFindAllReferencesService)Package.GetGlobalService(typeof(SVsFindAllReferences));
                    FarDataSource             dataSource = new FarDataSource(1);
                    dataSource.Snapshots[0] = new FarDataSnapshot(definitions);

                    IFindAllReferencesWindow farWindow   = farService.StartSearch(definitions[0].Type);
                    ITableManager            _farManager = farWindow.Manager;
                    _farManager.AddSource(dataSource);

                    dataSource.Sink.IsStable = false;
                    dataSource.Sink.AddSnapshot(dataSource.Snapshots[0]);
                    dataSource.Sink.IsStable = true;

                    return(VSConstants.S_OK);
                }
            }

            return(Next?.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut) ?? (int)Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
        }
示例#4
0
        private int?HandleGoToDefinition()
        {
            TextView.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDoc);

            XmlInfo info = XmlTools.GetXmlInfo(TextView.TextSnapshot, TextView.Caret.Position.BufferPosition.Position);

            if (info != null)
            {
                int?attributeCompletionResult = HandleAttributeCompletionResult(textDoc, info);

                if (attributeCompletionResult.HasValue)
                {
                    return(attributeCompletionResult.Value);
                }
            }

            IWorkspace        workspace   = _workspaceManager.GetWorkspace(textDoc.FilePath);
            List <Definition> definitions = workspace.ResolveDefinition(textDoc.FilePath, TextView.TextSnapshot.GetText(), TextView.Caret.Position.BufferPosition.Position);

            if (definitions.Count == 1)
            {
                DTE dte = ServiceUtil.DTE;
                dte.MainWindow.Activate();

                using (var state = new NewDocumentStateScope(__VSNEWDOCUMENTSTATE.NDS_Provisional, Guid.Parse(ProjectFileToolsPackage.PackageGuidString)))
                {
                    Window w = dte.ItemOperations.OpenFile(definitions[0].File, EnvDTE.Constants.vsViewKindTextView);

                    if (definitions[0].Line.HasValue)
                    {
                        ((TextSelection)dte.ActiveDocument.Selection).GotoLine(definitions[0].Line.Value, true);
                    }
                }

                return(VSConstants.S_OK);
            }
            else if (definitions.Count > 1)
            {
                return(ShowInFar("Symbol Definition", definitions));
            }

            return(null);
        }
        private static Document OpenDocument(Workspace workspace, DocumentId documentId, OptionSet options)
        {
            options = options ?? workspace.Options;

            // Always open the document again, even if the document is already open in the 
            // workspace. If a document is already open in a preview tab and it is opened again 
            // in a permanent tab, this allows the document to transition to the new state.
            if (workspace.CanOpenDocuments)
            {
                if (options.GetOption(NavigationOptions.PreferProvisionalTab))
                {
                    using (NewDocumentStateScope ndss = new NewDocumentStateScope(__VSNEWDOCUMENTSTATE.NDS_Provisional, VSConstants.NewDocumentStateReason.Navigation))
                    {
                        workspace.OpenDocument(documentId);
                    }
                }
                else
                {
                    workspace.OpenDocument(documentId);
                }
            }

            if (!workspace.IsDocumentOpen(documentId))
            {
                return null;
            }

            return workspace.CurrentSolution.GetDocument(documentId);
        }