Example #1
0
        public static async Task <ImmutableArray <DefinitionItem> > GetDefinitionsAsync(
            ISymbol symbol,
            Solution solution,
            bool thirdPartyNavigationAllowed,
            CancellationToken cancellationToken)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                if (alias.Target is INamespaceSymbol ns && ns.IsGlobalNamespace)
                {
                    return(ImmutableArray.Create <DefinitionItem>());
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            if (alias != null)
            {
                var sourceLocations = NavigableItemFactory.GetPreferredSourceLocations(
                    solution, symbol, cancellationToken);

                if (sourceLocations.All(l => solution.GetDocument(l.SourceTree) == null))
                {
                    symbol = alias.Target;
                }
            }

            var definition = await SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol method)
            {
                symbol = method.PartialImplementationPart ?? symbol;
            }

            using var definitionsDisposer = ArrayBuilder <DefinitionItem> .GetInstance(out var definitions);

            // Going to a symbol may end up actually showing the symbol in the Find-Usages window.
            // This happens when there is more than one location for the symbol (i.e. for partial
            // symbols) and we don't know the best place to take you to.
            //
            // The FindUsages window supports showing the classified text for an item.  It does this
            // in two ways.  Either the item can pass along its classified text (and the window will
            // defer to that), or the item will have no classified text, and the window will compute
            // it in the BG.
            //
            // Passing along the classified information is valuable for OOP scenarios where we want
            // all that expensive computation done on the OOP side and not in the VS side.
            //
            // However, Go To Definition is all in-process, and is also synchronous.  So we do not
            // want to fetch the classifications here.  It slows down the command and leads to a
            // measurable delay in our perf tests.
            //
            // So, if we only have a single location to go to, this does no unnecessary work.  And,
            // if we do have multiple locations to show, it will just be done in the BG, unblocking
            // this command thread so it can return the user faster.
            var definitionItem = symbol.ToNonClassifiedDefinitionItem(solution, includeHiddenLocations: true);

            if (thirdPartyNavigationAllowed)
            {
                var factory = solution.Workspace.Services.GetService <IDefinitionsAndReferencesFactory>();
                if (factory != null)
                {
                    var thirdPartyItem = await factory.GetThirdPartyDefinitionItemAsync(solution, definitionItem, cancellationToken).ConfigureAwait(false);

                    definitions.AddIfNotNull(thirdPartyItem);
                }
            }

            definitions.Add(definitionItem);
            return(definitions.ToImmutable());
        }
        public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            IEnumerable <Lazy <INavigableItemsPresenter> > presenters,
            IEnumerable <Lazy <IStreamingFindUsagesPresenter> > streamingPresenters,
            CancellationToken cancellationToken,
            bool thirdPartyNavigationAllowed = true,
            bool throwOnHiddenDefinition     = false)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (symbol is IAliasSymbol &&
                NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).All(l => project.Solution.GetDocument(l.SourceTree) == null))
            {
                symbol = ((IAliasSymbol)symbol).Target;
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).WaitAndGetResult(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            if (thirdPartyNavigationAllowed && TryThirdPartyNavigation(symbol, solution))
            {
                return(true);
            }

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol)
            {
                symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
            }

            var options = project.Solution.Options;

            var preferredSourceLocations = NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).ToArray();
            var displayParts             = NavigableItemFactory.GetSymbolDisplayTaggedParts(project, symbol);
            var title = displayParts.JoinText();

            if (preferredSourceLocations.Length == 0)
            {
                // If there are no visible source locations, then tell the host about the symbol and
                // allow it to navigate to it.  This will either navigate to any non-visible source
                // locations, or it can appropriately deal with metadata symbols for hosts that can go
                // to a metadata-as-source view.

                var symbolNavigationService = solution.Workspace.Services.GetService <ISymbolNavigationService>();
                return(symbolNavigationService.TryNavigateToSymbol(
                           symbol, project,
                           options: options.WithChangedOption(NavigationOptions.PreferProvisionalTab, true),
                           cancellationToken: cancellationToken));
            }
            else if (preferredSourceLocations.Length == 1)
            {
                var item = NavigableItemFactory.GetItemFromSymbolLocation(
                    solution, symbol, preferredSourceLocations[0],
                    displayTaggedParts: null);
                return(TryGoToSingleLocation(item, options, throwOnHiddenDefinition));
            }
            else
            {
                // We have multiple viable source locations, so ask the host what to do. Most hosts
                // will simply display the results to the user and allow them to choose where to
                // go.

                return(TryPresentInFindUsagesPresenter(solution, symbol, streamingPresenters, cancellationToken) ||
                       TryPresentInNavigableItemsPresenter(solution, symbol, presenters, title, preferredSourceLocations));
            }
        }
        public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            IEnumerable <Lazy <IStreamingFindUsagesPresenter> > streamingPresenters,
            CancellationToken cancellationToken,
            bool thirdPartyNavigationAllowed = true,
            bool throwOnHiddenDefinition     = false)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (alias != null)
            {
                var sourceLocations = NavigableItemFactory.GetPreferredSourceLocations(
                    solution, symbol, cancellationToken);

                if (sourceLocations.All(l => project.Solution.GetDocument(l.SourceTree) == null))
                {
                    symbol = alias.Target;
                }
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).WaitAndGetResult(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            var definitions = ArrayBuilder <DefinitionItem> .GetInstance();

            if (thirdPartyNavigationAllowed)
            {
                var factory        = solution.Workspace.Services.GetService <IDefinitionsAndReferencesFactory>();
                var thirdPartyItem = factory?.GetThirdPartyDefinitionItem(solution, symbol, cancellationToken);
                definitions.AddIfNotNull(thirdPartyItem);
            }

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol method)
            {
                symbol = method.PartialImplementationPart ?? symbol;
            }

            var options = project.Solution.Options;

            definitions.Add(symbol.ToDefinitionItem(solution, includeHiddenLocations: true));

            var presenter = GetFindUsagesPresenter(streamingPresenters);
            var title     = string.Format(EditorFeaturesResources._0_declarations,
                                          FindUsagesHelpers.GetDisplayName(symbol));

            return(presenter.TryNavigateToOrPresentItemsAsync(
                       title, definitions.ToImmutableAndFree()).WaitAndGetResult(cancellationToken));
        }
        public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            IEnumerable <Lazy <INavigableItemsPresenter> > presenters,
            CancellationToken cancellationToken,
            bool thirdPartyNavigationAllowed = true,
            bool throwOnHiddenDefinition     = false)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (symbol is IAliasSymbol &&
                NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).All(l => project.Solution.GetDocument(l.SourceTree) == null))
            {
                symbol = ((IAliasSymbol)symbol).Target;
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).WaitAndGetResult(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            if (thirdPartyNavigationAllowed && TryThirdPartyNavigation(symbol, solution))
            {
                return(true);
            }

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol)
            {
                symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
            }

            var preferredSourceLocations = NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).ToArray();

            if (!preferredSourceLocations.Any())
            {
                // If there are no visible source locations, then tell the host about the symbol and
                // allow it to navigate to it.  THis will either navigate to any non-visible source
                // locations, or it can appropriately deal with metadata symbols for hosts that can go
                // to a metadata-as-source view.

                var symbolNavigationService = solution.Workspace.Services.GetService <ISymbolNavigationService>();
                return(symbolNavigationService.TryNavigateToSymbol(symbol, project, cancellationToken: cancellationToken, usePreviewTab: true));
            }

            // If we have a single location, then just navigate to it.
            if (preferredSourceLocations.Length == 1)
            {
                var firstItem         = preferredSourceLocations[0];
                var workspace         = project.Solution.Workspace;
                var navigationService = workspace.Services.GetService <IDocumentNavigationService>();

                if (navigationService.CanNavigateToSpan(workspace, solution.GetDocument(firstItem.SourceTree).Id, firstItem.SourceSpan))
                {
                    return(navigationService.TryNavigateToSpan(workspace, solution.GetDocument(firstItem.SourceTree).Id, firstItem.SourceSpan, usePreviewTab: true));
                }
                else
                {
                    if (throwOnHiddenDefinition)
                    {
                        const int E_FAIL = -2147467259;
                        throw new COMException(EditorFeaturesResources.TheDefinitionOfTheObjectIsHidden, E_FAIL);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                // We have multiple viable source locations, so ask the host what to do. Most hosts
                // will simply display the results to the user and allow them to choose where to
                // go.

                if (presenters.Any())
                {
                    presenters.First().Value.DisplayResult(NavigableItemFactory.GetSymbolDisplayString(project, symbol),
                                                           preferredSourceLocations.Select(location => NavigableItemFactory.GetItemFromSymbolLocation(solution, symbol, location)).ToList());

                    return(true);
                }

                return(false);
            }
        }
Example #5
0
        public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            IEnumerable <Lazy <IStreamingFindUsagesPresenter> > streamingPresenters,
            CancellationToken cancellationToken,
            bool thirdPartyNavigationAllowed = true,
            bool throwOnHiddenDefinition     = false)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (alias != null)
            {
                var sourceLocations = NavigableItemFactory.GetPreferredSourceLocations(
                    solution, symbol, cancellationToken);

                if (sourceLocations.All(l => project.Solution.GetDocument(l.SourceTree) == null))
                {
                    symbol = alias.Target;
                }
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).WaitAndGetResult(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol method)
            {
                symbol = method.PartialImplementationPart ?? symbol;
            }

            var definitions = ArrayBuilder <DefinitionItem> .GetInstance();

            // Going to a symbol may end up actually showing the symbol in the Find-Usages window.
            // This happens when there is more than one location for the symbol (i.e. for partial
            // symbols) and we don't know the best place to take you to.
            //
            // The FindUsages window supports showing the classified text for an item.  It does this
            // in two ways.  Either the item can pass along its classified text (and the window will
            // defer to that), or the item will have no classified text, and the window will compute
            // it in the BG.
            //
            // Passing along the classified information is valuable for OOP scenarios where we want
            // all that expensive computation done on the OOP side and not in the VS side.
            //
            // However, Go To Definition is all in-process, and is also synchronous.  So we do not
            // want to fetch the classifications here.  It slows down the command and leads to a
            // measurable delay in our perf tests.
            //
            // So, if we only have a single location to go to, this does no unnecessary work.  And,
            // if we do have multiple locations to show, it will just be done in the BG, unblocking
            // this command thread so it can return the user faster.
            var definitionItem = symbol.ToNonClassifiedDefinitionItem(solution, includeHiddenLocations: true);

            if (thirdPartyNavigationAllowed)
            {
                var factory        = solution.Workspace.Services.GetService <IDefinitionsAndReferencesFactory>();
                var thirdPartyItem = factory?.GetThirdPartyDefinitionItem(solution, definitionItem, cancellationToken);
                definitions.AddIfNotNull(thirdPartyItem);
            }

            definitions.Add(definitionItem);

            var presenter = streamingPresenters.FirstOrDefault()?.Value;
            var title     = string.Format(EditorFeaturesResources._0_declarations,
                                          FindUsagesHelpers.GetDisplayName(symbol));

            return(presenter.TryNavigateToOrPresentItemsAsync(
                       project.Solution.Workspace, title, definitions.ToImmutableAndFree()).WaitAndGetResult(cancellationToken));
        }
        public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            IEnumerable <Lazy <INavigableDefinitionProvider> > externalDefinitionProviders,
            IEnumerable <Lazy <INavigableItemsPresenter> > presenters,
            CancellationToken cancellationToken,
            bool thirdPartyNavigationAllowed = true,
            bool throwOnHiddenDefinition     = false)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (symbol is IAliasSymbol &&
                NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).All(l => project.Solution.GetDocument(l.SourceTree) == null))
            {
                symbol = ((IAliasSymbol)symbol).Target;
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).WaitAndGetResult(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            if (thirdPartyNavigationAllowed && TryThirdPartyNavigation(symbol, solution))
            {
                return(true);
            }

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol)
            {
                symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
            }

            var options = project.Solution.Options;

            var preferredSourceLocations = NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).ToArray();
            var title = NavigableItemFactory.GetSymbolDisplayString(project, symbol);

            if (!preferredSourceLocations.Any())
            {
                // Attempt to find source locations from external definition providers.
                if (thirdPartyNavigationAllowed && externalDefinitionProviders.Any())
                {
                    var externalSourceDefinitions = FindExternalDefinitionsAsync(symbol, project, externalDefinitionProviders, cancellationToken).WaitAndGetResult(cancellationToken).ToImmutableArray();
                    if (externalSourceDefinitions.Length > 0)
                    {
                        return(TryGoToDefinition(externalSourceDefinitions, title, options, presenters, throwOnHiddenDefinition));
                    }
                }

                // If there are no visible source locations, then tell the host about the symbol and
                // allow it to navigate to it.  This will either navigate to any non-visible source
                // locations, or it can appropriately deal with metadata symbols for hosts that can go
                // to a metadata-as-source view.

                var symbolNavigationService = solution.Workspace.Services.GetService <ISymbolNavigationService>();
                return(symbolNavigationService.TryNavigateToSymbol(
                           symbol, project,
                           options: options.WithChangedOption(NavigationOptions.PreferProvisionalTab, true),
                           cancellationToken: cancellationToken));
            }

            var navigableItems = preferredSourceLocations.Select(location => NavigableItemFactory.GetItemFromSymbolLocation(solution, symbol, location)).ToImmutableArray();

            return(TryGoToDefinition(navigableItems, title, options, presenters, throwOnHiddenDefinition));
        }