// 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; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editor.Host; using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.Navigation; using Microsoft.CodeAnalysis.Shared.Extensions; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Editor.GoToDefinition { internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService { private readonly IEnumerable> _streamingPresenters; protected AbstractGoToDefinitionService( IEnumerable> streamingPresenters) { _streamingPresenters = streamingPresenters; } public async Task> FindDefinitionsAsync( Document document, int position, CancellationToken cancellationToken) { var symbolService = document.GetLanguageService(); var (symbol, span) = await symbolService.GetSymbolAndBoundSpanAsync(document, position, cancellationToken).ConfigureAwait(false); // Try to compute source definitions from symbol. var items = symbol != null ? NavigableItemFactory.GetItemsFromPreferredSourceLocations(document.Project.Solution, symbol, displayTaggedParts: null, cancellationToken: cancellationToken) : null; // realize the list here so that the consumer await'ing the result doesn't lazily cause // them to be created on an inappropriate thread. return items?.ToList(); } public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken) { // First try to compute the referenced symbol and attempt to go to definition for the symbol. var symbolService = document.GetLanguageService(); var (symbol, span) = symbolService.GetSymbolAndBoundSpanAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken); if (symbol == null) { return false; } var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken); return GoToDefinitionHelpers.TryGoToDefinition(symbol, document.Project, _streamingPresenters, thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed, throwOnHiddenDefinition: true, cancellationToken: cancellationToken); } private static bool IsThirdPartyNavigationAllowed(ISymbol symbolToNavigateTo, int caretPosition, Document document, CancellationToken cancellationToken) { var syntaxRoot = document.GetSyntaxRootSynchronously(cancellationToken); var syntaxFactsService = document.GetLanguageService(); var containingTypeDeclaration = syntaxFactsService.GetContainingTypeDeclaration(syntaxRoot, caretPosition); if (containingTypeDeclaration != null) { var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken); var containingTypeSymbol = semanticModel.GetDeclaredSymbol(containingTypeDeclaration, cancellationToken) as ITypeSymbol; // Allow third parties to navigate to all symbols except types/constructors // if we are navigating from the corresponding type. if (containingTypeSymbol != null && (symbolToNavigateTo is ITypeSymbol || symbolToNavigateTo.IsConstructor())) { var candidateTypeSymbol = symbolToNavigateTo is ITypeSymbol ? symbolToNavigateTo : symbolToNavigateTo.ContainingType; if (containingTypeSymbol == candidateTypeSymbol) { // 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; } } } return true; } } }