Exemplo n.º 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;
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
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)));
        }