Пример #1
0
        protected sealed override async Task <SignatureHelpItems> GetItemsWorkerAsync(LogicalDocument document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            var sourceLocation = syntaxTree.MapRootFilePosition(position);
            var token          = ((SyntaxNode)syntaxTree.Root).FindTokenOnLeft(sourceLocation);

            var node = token.Parent
                       .AncestorsAndSelf()
                       .OfType <TNode>()
                       .FirstOrDefault(c => c.IsBetweenParentheses(sourceLocation));

            if (node == null)
            {
                return(null);
            }

            var rootSpan = node.GetTextSpanRoot();

            if (rootSpan == null)
            {
                return(null);
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (semanticModel == null)
            {
                return(null);
            }

            return(GetModel((SemanticModel)semanticModel, node, sourceLocation));
        }
        public async Task <ImmutableArray <DocumentHighlights> > GetDocumentHighlightsAsync(LogicalDocument document, int position, IImmutableSet <LogicalDocument> documentsToSearch, CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (semanticModel == null)
            {
                return(ImmutableArray <DocumentHighlights> .Empty);
            }

            var syntaxTree     = semanticModel.SyntaxTree;
            var mappedPosition = syntaxTree.MapRootFilePosition(position);

            var symbolSearchService = document.LanguageServices.GetService <ISymbolSearchService>();

            if (symbolSearchService == null)
            {
                return(ImmutableArray <DocumentHighlights> .Empty);
            }

            var symbolAtPosition = symbolSearchService.FindSymbol(semanticModel, mappedPosition);

            if (symbolAtPosition == null)
            {
                return(ImmutableArray <DocumentHighlights> .Empty);
            }

            return(SpecializedCollections.SingletonList(
                       new DocumentHighlights(
                           document,
                           symbolSearchService.FindUsages(semanticModel, symbolAtPosition.Value.Symbol)
                           .Where(s => s.Span.File.IsRootFile)
                           .Select(s => new HighlightSpan(s.Span.Span, MapSymbolSpanKind(s.Kind)))
                           .ToImmutableArray())
                       ).ToImmutableArray());
        }
Пример #3
0
        private async Task <ValueTuple <SemanticModelBase, ImmutableArray <ISymbol> > > BindTokenAsync(
            LogicalDocument document,
            ISyntaxToken token,
            CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (semanticModel != null)
            {
                var symbols = semanticModel.GetSemanticInfo(token, document.Workspace, cancellationToken)
                              .GetSymbols(includeType: true);

                symbols = symbols.Distinct().ToImmutableArray();

                if (symbols.Any())
                {
                    return(ValueTuple.Create(semanticModel, symbols));
                }
            }

            return(ValueTuple.Create(semanticModel, ImmutableArray <ISymbol> .Empty));
        }
        private async Task <ISymbol> FindSymbolAsync(LogicalDocument document, int position, CancellationToken cancellationToken)
        {
            if (!document.SupportsSemanticModel)
            {
                return(null);
            }

            var workspace = document.Workspace;

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var semanticInfo = await SymbolFinder.GetSemanticInfoAtPositionAsync(semanticModel, position, workspace, cancellationToken).ConfigureAwait(false);

            // prefer references to declarations.  It's more likely that the user is attempting to
            // go to a definition at some other location, rather than the definition they're on.
            // This can happen when a token is at a location that is both a reference and a definition.
            // For example, on an anonymous type member declaration.
            return(semanticInfo.AliasSymbol ??
                   semanticInfo.ReferencedSymbols.FirstOrDefault() ??
                   semanticInfo.DeclaredSymbol ??
                   semanticInfo.Type);
        }
Пример #5
0
        public async Task <ImmutableArray <INavigateToSearchResult> > SearchDocumentAsync(LogicalDocument document, string searchPattern, CancellationToken cancellationToken)
        {
            var result = ArrayBuilder <INavigateToSearchResult> .GetInstance();

            cancellationToken.ThrowIfCancellationRequested();

            var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (semanticModel == null)
            {
                return(ImmutableArray <INavigateToSearchResult> .Empty);
            }

            foreach (var node in syntaxTree.Root.DescendantNodesAndSelf())
            {
                cancellationToken.ThrowIfCancellationRequested();

                var declaredSymbol = semanticModel.GetDeclaredSymbol(node);

                if (declaredSymbol != null &&
                    declaredSymbol.Kind != SymbolKind.Parameter &&
                    (declaredSymbol.Kind != SymbolKind.Variable || declaredSymbol.Parent == null || declaredSymbol.Parent.Kind != SymbolKind.Function) &&
                    Contains(declaredSymbol.Name, searchPattern))
                {
                    var matchKind = declaredSymbol.Name.StartsWith(searchPattern, StringComparison.CurrentCultureIgnoreCase)
                        ? NavigateToMatchKind.Prefix
                        : (declaredSymbol.Name == searchPattern)
                            ? NavigateToMatchKind.Exact
                            : NavigateToMatchKind.Substring;

                    result.AddRange(ConvertResult(declaredSymbol, document, syntaxTree, matchKind));
                }
            }

            return(result.ToImmutableAndFree());
        }