Пример #1
0
        public async Task GetSymbolsAsync(GoToSymbolContext context)
        {
            var document          = context.Document;
            var position          = context.Position;
            var cancellationToken = context.CancellationToken;
            var service           = document.GetLanguageService <IGoToDefinitionSymbolService>();

            // [includeType: false]
            // Enable Ctrl+Click on tokens with aliased, referenced or declared symbol.
            // If the token has none of those but does have a type (mostly literals), we're not interested
            var(symbol, span) = await service.GetSymbolAndBoundSpanAsync(document, position, includeType : false, cancellationToken).ConfigureAwait(false);

            if (symbol == null)
            {
                return;
            }

            // We want ctrl-click GTD to be as close to regular GTD as possible.
            // This means we have to query for "third party navigation", from
            // XAML, etc. That call has to be done on the UI thread.
            await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield : true, cancellationToken);

            var solution    = document.Project.Solution;
            var definitions = GoToDefinitionHelpers.GetDefinitions(symbol, solution, thirdPartyNavigationAllowed: true, cancellationToken)
                              .WhereAsArray(d => d.CanNavigateTo(solution.Workspace));

            await TaskScheduler.Default;

            foreach (var definition in definitions)
            {
                context.AddItem(WellKnownSymbolTypes.Definition, definition);
            }

            context.Span = span;
        }
Пример #2
0
        public async Task GetSymbolsAsync(GoToSymbolContext context)
        {
            var document          = context.Document;
            var position          = context.Position;
            var cancellationToken = context.CancellationToken;
            var service           = document.GetRequiredLanguageService <IGoToDefinitionSymbolService>();

            // [includeType: false]
            // Enable Ctrl+Click on tokens with aliased, referenced or declared symbol.
            // If the token has none of those but does have a type (mostly literals), we're not interested
            var(symbol, span) = await service.GetSymbolAndBoundSpanAsync(document, position, includeType : false, cancellationToken).ConfigureAwait(false);

            if (symbol == null)
            {
                return;
            }

            var solution    = document.Project.Solution;
            var definitions = await GoToDefinitionHelpers.GetDefinitionsAsync(symbol, solution, thirdPartyNavigationAllowed : true, cancellationToken).ConfigureAwait(false);

            foreach (var definition in definitions)
            {
                if (await definition.CanNavigateToAsync(solution.Workspace, cancellationToken).ConfigureAwait(false))
                {
                    context.AddItem(WellKnownSymbolTypes.Definition, definition);
                }
            }

            context.Span = span;
        }
Пример #3
0
        public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken)
        {
            // Try to compute the referenced symbol and attempt to go to definition for the symbol.
            var symbolService = document.GetLanguageService <IGoToDefinitionSymbolService>();

            var(symbol, _) = symbolService.GetSymbolAndBoundSpanAsync(document, position, includeType: true, cancellationToken).WaitAndGetResult(cancellationToken);
            if (symbol is null)
            {
                return(false);
            }

            // if the symbol only has a single source location, and we're already on it,
            // try to see if there's a better symbol we could navigate to.
            var remapped = TryGoToAlternativeLocationIfAlreadyOnDefinition(document, position, symbol, cancellationToken);

            if (remapped)
            {
                return(true);
            }

            var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);

            return(GoToDefinitionHelpers.TryGoToDefinition(symbol,
                                                           document.Project,
                                                           _streamingPresenter.Value,
                                                           thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
                                                           cancellationToken: cancellationToken));
        }
Пример #4
0
        public async Task GetSymbolsAsync(GoToSymbolContext context)
        {
            var document          = context.Document;
            var position          = context.Position;
            var cancellationToken = context.CancellationToken;

            var service = document.GetLanguageService <IGoToDefinitionSymbolService>();

            var(symbol, span) = await service.GetSymbolAndBoundSpanAsync(document, position, cancellationToken).ConfigureAwait(false);

            if (symbol == null)
            {
                return;
            }

            // We want ctrl-click GTD to be as close to regular GTD as possible.
            // This means we have to query for "third party navigation", from
            // XAML, etc. That call has to be done on the UI thread.
            var definitions = await Task.Factory.StartNew(() =>
                                                          GoToDefinitionHelpers.GetDefinitions(symbol, document.Project, thirdPartyNavigationAllowed : true, cancellationToken : cancellationToken)
                                                          .WhereAsArray(d => d.CanNavigateTo(document.Project.Solution.Workspace)),
                                                          cancellationToken,
                                                          TaskCreationOptions.None,
                                                          ForegroundTaskScheduler).ConfigureAwait(false);

            foreach (var definition in definitions)
            {
                context.AddItem(WellKnownSymbolTypes.Definition, definition);
            }

            context.Span = span;
        }
Пример #5
0
        private bool TryGoToAlternativeLocationIfAlreadyOnDefinition(
            Document document, int position,
            ISymbol symbol, CancellationToken cancellationToken)
        {
            var project  = document.Project;
            var solution = project.Solution;

            var sourceLocations = symbol.Locations.WhereAsArray(loc => loc.IsInSource);

            if (sourceLocations.Length != 1)
            {
                return(false);
            }

            var definitionLocation = sourceLocations[0];

            if (!definitionLocation.SourceSpan.IntersectsWith(position))
            {
                return(false);
            }

            var definitionTree     = definitionLocation.SourceTree;
            var definitionDocument = solution.GetDocument(definitionTree);

            if (definitionDocument != document)
            {
                return(false);
            }

            // Ok, we were already on the definition. Look for better symbols we could show results
            // for instead. For now, just see if we're on an interface member impl. If so, we can
            // instead navigate to the actual interface member.
            //
            // In the future we can expand this with other mappings if appropriate.
            var interfaceImpls = symbol.ExplicitOrImplicitInterfaceImplementations();

            if (interfaceImpls.Length == 0)
            {
                return(false);
            }

            var definitions = interfaceImpls.SelectMany(
                i => GoToDefinitionHelpers.GetDefinitions(
                    i, solution, thirdPartyNavigationAllowed: false, cancellationToken)).ToImmutableArray();

            var title = string.Format(EditorFeaturesResources._0_implemented_members,
                                      FindUsagesHelpers.GetDisplayName(symbol));

            return(_threadingContext.JoinableTaskFactory.Run(() =>
                                                             _streamingPresenter.TryNavigateToOrPresentItemsAsync(
                                                                 _threadingContext, solution.Workspace, title, definitions, cancellationToken)));
        }
Пример #6
0
        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 symbol = FindSymbolAsync(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));
        }
Пример #7
0
        public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken)
        {
            // Try to compute the referenced symbol and attempt to go to definition for the symbol.
            var symbolService = document.GetLanguageService <IGoToDefinitionSymbolService>();

            var(symbol, _) = symbolService.GetSymbolAndBoundSpanAsync(document, position, includeType: true, cancellationToken).WaitAndGetResult(cancellationToken);
            if (symbol is null)
            {
                return(false);
            }

            var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);

            return(GoToDefinitionHelpers.TryGoToDefinition(symbol,
                                                           document.Project,
                                                           _streamingPresenter.Value,
                                                           thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
                                                           throwOnHiddenDefinition: true,
                                                           cancellationToken: cancellationToken));
        }
Пример #8
0
        private bool TryGoToAlternativeLocationIfAlreadyOnDefinition(
            Document document, int position,
            ISymbol symbol, CancellationToken cancellationToken)
        {
            var project  = document.Project;
            var solution = project.Solution;

            var sourceLocations = symbol.Locations.WhereAsArray(loc => loc.IsInSource);

            if (sourceLocations.Length != 1)
            {
                return(false);
            }

            var definitionLocation = sourceLocations[0];

            if (!definitionLocation.SourceSpan.IntersectsWith(position))
            {
                return(false);
            }

            var definitionTree     = definitionLocation.SourceTree;
            var definitionDocument = solution.GetDocument(definitionTree);

            if (definitionDocument != document)
            {
                return(false);
            }

            // Ok, we were already on the definition. Look for better symbols we could show results
            // for instead. For now, just see if we're on an interface member impl. If so, we can
            // instead navigate to the actual interface member.
            //
            // In the future we can expand this with other mappings if appropriate.
            var interfaceImpls = symbol.ExplicitOrImplicitInterfaceImplementations();

            if (interfaceImpls.Length == 0)
            {
                return(false);
            }

            var title = string.Format(EditorFeaturesResources._0_implemented_members,
                                      FindUsagesHelpers.GetDisplayName(symbol));

            return(_threadingContext.JoinableTaskFactory.Run(async() =>
            {
                using var _ = ArrayBuilder <DefinitionItem> .GetInstance(out var definitions);
                foreach (var impl in interfaceImpls)
                {
                    // Use ConfigureAwait(true) here.  Not for a correctness requirements, but because we're
                    // already blocking the UI thread by being in a JTF.Run call.  So we might as well try to
                    // continue to use the blocking UI thread to do as much work as possible instead of making
                    // it wait for threadpool threads to be available to process the work.
                    definitions.AddRange(await GoToDefinitionHelpers.GetDefinitionsAsync(
                                             impl, solution, thirdPartyNavigationAllowed: false, cancellationToken).ConfigureAwait(true));
                }

                return await _streamingPresenter.TryNavigateToOrPresentItemsAsync(
                    _threadingContext, solution.Workspace, title, definitions.ToImmutable(), cancellationToken).ConfigureAwait(true);
            }));
        }