AbstractGoToDefinitionService.cs 4.5 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.LanguageServices;
9
using Microsoft.CodeAnalysis.Navigation;
10 11 12
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

13
namespace Microsoft.CodeAnalysis.Editor.GoToDefinition
14
{
I
isc30 已提交
15
    // GoToDefinition
16 17
    internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService
    {
18
        private readonly IStreamingFindUsagesPresenter _streamingPresenter;
19

20
        protected AbstractGoToDefinitionService(IStreamingFindUsagesPresenter streamingPresenter)
21
        {
22
            _streamingPresenter = streamingPresenter;
23 24
        }

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

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

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

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

C
CyrusNajmabadi 已提交
51 52 53 54
            var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);

            return GoToDefinitionHelpers.TryGoToDefinition(symbol,
                document.Project,
55
                _streamingPresenter,
C
CyrusNajmabadi 已提交
56 57 58
                thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
                throwOnHiddenDefinition: true,
                cancellationToken: cancellationToken);
59 60
        }

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

            if (containingTypeDeclaration != null)
            {
                var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken);
70 71 72 73

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

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

81
                    if (Equals(containingTypeSymbol, candidateTypeSymbol))
82 83 84 85 86 87 88
                    {
                        // 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;
                    }
                }
89 90
            }

91
            return true;
92 93 94
        }
    }
}