Esempio n. 1
0
        private bool TryGoToImplementations(IEnumerable <ISymbol> candidateImplementations, SymbolMappingResult mapping, CancellationToken cancellationToken, out string message)
        {
            var implementations = candidateImplementations
                                  .Where(s => !s.IsAbstract && s.Locations.Any(l => l.IsInSource))
                                  .ToList();

            if (implementations.Count == 0)
            {
                message = EditorFeaturesResources.The_symbol_has_no_implementations;
                return(false);
            }
            else if (implementations.Count == 1)
            {
                GoToDefinition.GoToDefinitionHelpers.TryGoToDefinition(implementations.Single(), mapping.Project, _externalDefinitionProviders, _navigableItemPresenters, cancellationToken);
                message = null;
                return(true);
            }
            else
            {
                // We have multiple symbols, so we'll build a list of all preferred locations for all the symbols
                var navigableItems = implementations.SelectMany(
                    implementation => CreateItemsForImplementation(implementation, mapping.Solution));

                var presenter = _navigableItemPresenters.First();

                var taggedParts = NavigableItemFactory.GetSymbolDisplayTaggedParts(mapping.Project, mapping.Symbol);

                presenter.Value.DisplayResult(taggedParts.JoinText(), navigableItems);
                message = null;
                return(true);
            }
        }
Esempio n. 2
0
        private bool TryPresentInNavigableItemsPresenter(
            SymbolMappingResult mapping, List <ISymbol> implementations, out string message)
        {
            // We have multiple symbols, so we'll build a list of all preferred locations for all the symbols
            var navigableItems = implementations.SelectMany(
                implementation => CreateItemsForImplementation(implementation, mapping.Solution));

            var presenter = _navigableItemPresenters.First();

            var taggedParts = NavigableItemFactory.GetSymbolDisplayTaggedParts(mapping.Project, mapping.Symbol);

            presenter.Value.DisplayResult(taggedParts.JoinText(), navigableItems);
            message = null;
            return(true);
        }
        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));
            }
        }