AbstractGoToDefinitionService.cs 4.6 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4

5
using System;
6 7 8 9 10 11
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.LanguageServices;
12
using Microsoft.CodeAnalysis.Navigation;
13 14 15
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

16
namespace Microsoft.CodeAnalysis.Editor.GoToDefinition
17
{
I
isc30 已提交
18
    // GoToDefinition
19 20
    internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService
    {
21
        private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenter;
22

23
        protected AbstractGoToDefinitionService(Lazy<IStreamingFindUsagesPresenter> streamingPresenter)
24
        {
25
            _streamingPresenter = streamingPresenter;
26 27
        }

28 29
        public async Task<IEnumerable<INavigableItem>> FindDefinitionsAsync(
            Document document, int position, CancellationToken cancellationToken)
30
        {
R
Ravi Chande 已提交
31
            var symbolService = document.GetLanguageService<IGoToDefinitionSymbolService>();
C
Cyrus Najmabadi 已提交
32
            var (symbol, _) = await symbolService.GetSymbolAndBoundSpanAsync(document, position, includeType: true, cancellationToken).ConfigureAwait(false);
33

34
            // Try to compute source definitions from symbol.
35
            var items = symbol != null
36
                ? NavigableItemFactory.GetItemsFromPreferredSourceLocations(document.Project.Solution, symbol, displayTaggedParts: null, cancellationToken: cancellationToken)
37
                : null;
38

39 40
            // realize the list here so that the consumer await'ing the result doesn't lazily cause
            // them to be created on an inappropriate thread.
41
            return items?.ToList();
42 43 44 45
        }

        public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken)
        {
46
            // Try to compute the referenced symbol and attempt to go to definition for the symbol.
R
Ravi Chande 已提交
47
            var symbolService = document.GetLanguageService<IGoToDefinitionSymbolService>();
I
isc30 已提交
48
            var (symbol, _) = symbolService.GetSymbolAndBoundSpanAsync(document, position, includeType: true, cancellationToken).WaitAndGetResult(cancellationToken);
49
            if (symbol is null)
50
            {
C
CyrusNajmabadi 已提交
51
                return false;
52 53
            }

C
CyrusNajmabadi 已提交
54 55 56 57
            var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);

            return GoToDefinitionHelpers.TryGoToDefinition(symbol,
                document.Project,
58
                _streamingPresenter.Value,
C
CyrusNajmabadi 已提交
59 60 61
                thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
                throwOnHiddenDefinition: true,
                cancellationToken: cancellationToken);
62 63
        }

64
        private static bool IsThirdPartyNavigationAllowed(ISymbol symbolToNavigateTo, int caretPosition, Document document, CancellationToken cancellationToken)
65
        {
C
CyrusNajmabadi 已提交
66
            var syntaxRoot = document.GetSyntaxRootSynchronously(cancellationToken);
67 68 69 70 71 72
            var syntaxFactsService = document.GetLanguageService<ISyntaxFactsService>();
            var containingTypeDeclaration = syntaxFactsService.GetContainingTypeDeclaration(syntaxRoot, caretPosition);

            if (containingTypeDeclaration != null)
            {
                var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken);
73 74 75 76

                // Allow third parties to navigate to all symbols except types/constructors
                // if we are navigating from the corresponding type.

C
CyrusNajmabadi 已提交
77
                if (semanticModel.GetDeclaredSymbol(containingTypeDeclaration, cancellationToken) is ITypeSymbol containingTypeSymbol &&
78 79 80 81 82 83
                    (symbolToNavigateTo is ITypeSymbol || symbolToNavigateTo.IsConstructor()))
                {
                    var candidateTypeSymbol = symbolToNavigateTo is ITypeSymbol
                        ? symbolToNavigateTo
                        : symbolToNavigateTo.ContainingType;

84
                    if (Equals(containingTypeSymbol, candidateTypeSymbol))
85 86 87 88 89 90 91
                    {
                        // We are navigating from the same type, so don't allow third parties to perform the navigation.
                        // This ensures that if we navigate to a class from within that class, we'll stay in the same file
                        // rather than navigate to, say, XAML.
                        return false;
                    }
                }
92 93
            }

94
            return true;
95 96 97
        }
    }
}