AbstractGoToDefinitionService.cs 5.0 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 22 23 24 25
        /// <summary>
        /// Used to present go to definition results in <see cref="TryGoToDefinition(Document, int, CancellationToken)"/>
        /// This is lazily created as the LSP server only calls <see cref="FindDefinitionsAsync(Document, int, CancellationToken)"/>
        /// and therefore never needs to construct the presenter.
        /// </summary>
26
        private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenter;
27

28
        protected AbstractGoToDefinitionService(Lazy<IStreamingFindUsagesPresenter> streamingPresenter)
29
        {
30
            _streamingPresenter = streamingPresenter;
31 32
        }

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

39
            // Try to compute source definitions from symbol.
40
            var items = symbol != null
41
                ? NavigableItemFactory.GetItemsFromPreferredSourceLocations(document.Project.Solution, symbol, displayTaggedParts: null, cancellationToken: cancellationToken)
42
                : null;
43

44 45
            // realize the list here so that the consumer await'ing the result doesn't lazily cause
            // them to be created on an inappropriate thread.
46
            return items?.ToList();
47 48 49 50
        }

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

C
CyrusNajmabadi 已提交
59 60 61 62
            var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);

            return GoToDefinitionHelpers.TryGoToDefinition(symbol,
                document.Project,
63
                _streamingPresenter.Value,
C
CyrusNajmabadi 已提交
64 65 66
                thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
                throwOnHiddenDefinition: true,
                cancellationToken: cancellationToken);
67 68
        }

69
        private static bool IsThirdPartyNavigationAllowed(ISymbol symbolToNavigateTo, int caretPosition, Document document, CancellationToken cancellationToken)
70
        {
C
CyrusNajmabadi 已提交
71
            var syntaxRoot = document.GetSyntaxRootSynchronously(cancellationToken);
72 73 74 75 76 77
            var syntaxFactsService = document.GetLanguageService<ISyntaxFactsService>();
            var containingTypeDeclaration = syntaxFactsService.GetContainingTypeDeclaration(syntaxRoot, caretPosition);

            if (containingTypeDeclaration != null)
            {
                var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken);
78 79 80 81

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

C
CyrusNajmabadi 已提交
82
                if (semanticModel.GetDeclaredSymbol(containingTypeDeclaration, cancellationToken) is ITypeSymbol containingTypeSymbol &&
83 84 85 86 87 88
                    (symbolToNavigateTo is ITypeSymbol || symbolToNavigateTo.IsConstructor()))
                {
                    var candidateTypeSymbol = symbolToNavigateTo is ITypeSymbol
                        ? symbolToNavigateTo
                        : symbolToNavigateTo.ContainingType;

89
                    if (Equals(containingTypeSymbol, candidateTypeSymbol))
90 91 92 93 94 95 96
                    {
                        // 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;
                    }
                }
97 98
            }

99
            return true;
100 101 102
        }
    }
}