Exemple #1
0
        private async Task AugumentPeekSessionAsync(
            IList <IPeekableItem> peekableItems, IUIThreadOperationContext context, SnapshotPoint triggerPoint, Document document)
        {
            var cancellationToken = context.UserCancellationToken;
            var services          = document.Project.Solution.Workspace.Services;

            if (!document.SupportsSemanticModel)
            {
                // For documents without semantic models, just try to use the goto-def service
                // as a reasonable place to peek at.
                var goToDefinitionService = document.GetLanguageService <IGoToDefinitionService>();
                if (goToDefinitionService == null)
                {
                    return;
                }

                var navigableItems = await goToDefinitionService.FindDefinitionsAsync(document, triggerPoint.Position, cancellationToken).ConfigureAwait(false);

                await foreach (var item in GetPeekableItemsForNavigableItemsAsync(
                                   navigableItems, document.Project, _peekResultFactory, cancellationToken).ConfigureAwait(false))
                {
                    peekableItems.Add(item);
                }
            }
            else
            {
                var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                var semanticInfo = await SymbolFinder.GetSemanticInfoAtPositionAsync(
                    semanticModel,
                    triggerPoint.Position,
                    services,
                    cancellationToken).ConfigureAwait(false);

                var symbol = semanticInfo.GetAnySymbol(includeType: true);
                if (symbol == null)
                {
                    return;
                }

                symbol = symbol.GetOriginalUnreducedDefinition();

                // Get the symbol back from the originating workspace
                var symbolMappingService = services.GetRequiredService <ISymbolMappingService>();

                var mappingResult = await symbolMappingService.MapSymbolAsync(document, symbol, cancellationToken).ConfigureAwait(false);

                mappingResult ??= new SymbolMappingResult(document.Project, symbol);

                peekableItems.AddRange(await _peekableItemFactory.GetPeekableItemsAsync(
                                           mappingResult.Symbol, mappingResult.Project, _peekResultFactory, cancellationToken).ConfigureAwait(false));
            }
        }
        public void AugmentPeekSession(IPeekSession session, IList <IPeekableItem> peekableItems)
        {
            if (!string.Equals(session.RelationshipName, PredefinedPeekRelationships.Definitions.Name, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var triggerPoint = session.GetTriggerPoint(_textBuffer.CurrentSnapshot);

            if (!triggerPoint.HasValue)
            {
                return;
            }

            var document = triggerPoint.Value.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                return;
            }

            _waitIndicator.Wait(EditorFeaturesResources.Peek, EditorFeaturesResources.Loading_Peek_information, allowCancel: true, action: context =>
            {
                var cancellationToken = context.CancellationToken;

                IEnumerable <IPeekableItem> results;

                if (!document.SupportsSemanticModel)
                {
                    // For documents without semantic models, just try to use the goto-def service
                    // as a reasonable place to peek at.
                    var goToDefinitionService = document.GetLanguageService <IGoToDefinitionService>();
                    var navigableItems        = goToDefinitionService.FindDefinitionsAsync(document, triggerPoint.Value.Position, cancellationToken)
                                                .WaitAndGetResult(cancellationToken);

                    results = GetPeekableItemsForNavigableItems(navigableItems, document.Project, _peekResultFactory, cancellationToken);
                }
                else
                {
                    var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken);
                    var symbol        = SymbolFinder.FindSymbolAtPositionAsync(semanticModel,
                                                                               triggerPoint.Value.Position,
                                                                               document.Project.Solution.Workspace,
                                                                               bindLiteralsToUnderlyingType: true,
                                                                               cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);

                    if (symbol == null)
                    {
                        return;
                    }

                    symbol = symbol.GetOriginalUnreducedDefinition();

                    // Get the symbol back from the originating workspace
                    var symbolMappingService = document.Project.Solution.Workspace.Services.GetService <ISymbolMappingService>();
                    var mappingResult        = symbolMappingService.MapSymbolAsync(document, symbol, cancellationToken)
                                               .WaitAndGetResult(cancellationToken);

                    mappingResult = mappingResult ?? new SymbolMappingResult(document.Project, symbol);

                    results = _peekableItemFactory.GetPeekableItemsAsync(mappingResult.Symbol, mappingResult.Project, _peekResultFactory, cancellationToken)
                              .WaitAndGetResult(cancellationToken);
                }

                peekableItems.AddRange(results);
            });
        }