Ejemplo n.º 1
0
        private async Task <ImmutableArray <DocumentHighlights> > GetTagsForReferencedSymbolAsync(
            SymbolAndProjectId symbolAndProjectId,
            Document document,
            IImmutableSet <Document> documentsToSearch,
            CancellationToken cancellationToken)
        {
            var symbol = symbolAndProjectId.Symbol;

            Contract.ThrowIfNull(symbol);
            if (ShouldConsiderSymbol(symbol))
            {
                var progress = new StreamingProgressCollector(
                    StreamingFindReferencesProgress.Instance);

                var options = FindReferencesSearchOptions.GetFeatureOptionsForStartingSymbol(symbol);
                await SymbolFinder.FindReferencesAsync(
                    symbolAndProjectId, document.Project.Solution, progress,
                    documentsToSearch, options, cancellationToken).ConfigureAwait(false);

                return(await FilterAndCreateSpansAsync(
                           progress.GetReferencedSymbols(), document, documentsToSearch,
                           symbol, options, cancellationToken).ConfigureAwait(false));
            }

            return(ImmutableArray <DocumentHighlights> .Empty);
        }
        /// <summary>
        /// Locates all the call sites of the method that introduced the parameter
        /// </summary>
        protected static async Task <Dictionary <Document, List <SyntaxNode> > > FindCallSitesAsync(
            Document document, IMethodSymbol methodSymbol, CancellationToken cancellationToken)
        {
            var methodCallSites = new Dictionary <Document, List <SyntaxNode> >();
            var progress        = new StreamingProgressCollector();
            await SymbolFinder.FindReferencesAsync(
                methodSymbol, document.Project.Solution, progress,
                documents : null, FindReferencesSearchOptions.Default, cancellationToken).ConfigureAwait(false);

            var referencedSymbols = progress.GetReferencedSymbols();

            // Ordering by descending to sort invocations by starting span to account for nested invocations
            var referencedLocations = referencedSymbols.SelectMany(referencedSymbol => referencedSymbol.Locations)
                                      .Distinct().Where(reference => !reference.IsImplicit)
                                      .OrderByDescending(reference => reference.Location.SourceSpan.Start);

            // Adding the original document to ensure that it will be seen again when processing the call sites
            // in order to update the original expression and containing method.
            methodCallSites.Add(document, new List <SyntaxNode>());
            var syntaxFacts = document.GetRequiredLanguageService <ISyntaxFactsService>();

            foreach (var refLocation in referencedLocations)
            {
                // Does not support cross-language references currently
                if (refLocation.Document.Project.Language == document.Project.Language)
                {
                    var reference = refLocation.Location.FindNode(cancellationToken).GetRequiredParent();
                    if (reference is not(TObjectCreationExpressionSyntax or TInvocationExpressionSyntax))
                    {
                        reference = reference.GetRequiredParent();
                    }

                    // Only adding items that are of type InvocationExpressionSyntax or TObjectCreationExpressionSyntax
                    var invocationOrCreation = reference as TObjectCreationExpressionSyntax ?? (SyntaxNode?)(reference as TInvocationExpressionSyntax);
                    if (invocationOrCreation is null)
                    {
                        continue;
                    }

                    if (!methodCallSites.TryGetValue(refLocation.Document, out var list))
                    {
                        list = new List <SyntaxNode>();
                        methodCallSites.Add(refLocation.Document, list);
                    }

                    list.Add(invocationOrCreation);
                }
            }

            return(methodCallSites);
        }
Ejemplo n.º 3
0
        private static async Task <ImmutableArray <IMethodSymbol> > FindMethodDeclarationReferencesAsync(
            Document invocationDocument, IMethodSymbol method, CancellationToken cancellationToken)
        {
            var progress = new StreamingProgressCollector();

            await SymbolFinder.FindReferencesAsync(
                method, invocationDocument.Project.Solution, progress : progress,
                documents : null, FindReferencesSearchOptions.Default, cancellationToken).ConfigureAwait(false);

            var referencedSymbols = progress.GetReferencedSymbols();

            return(referencedSymbols.Select(referencedSymbol => referencedSymbol.Definition)
                   .OfType <IMethodSymbol>()
                   .Distinct()
                   .ToImmutableArray());
        }
            private static async Task <ImmutableArray <ReferenceLocation> > FindReferenceLocationsForSymbol(
                Document document, ISymbol symbol, CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var progress = new StreamingProgressCollector(StreamingFindReferencesProgress.Instance);

                await SymbolFinder.FindReferencesAsync(
                    symbolAndProjectId : SymbolAndProjectId.Create(symbol, document.Project.Id),
                    solution : document.Project.Solution,
                    documents : null,
                    progress : progress,
                    options : FindReferencesSearchOptions.Default,
                    cancellationToken : cancellationToken).ConfigureAwait(false);

                var referencedSymbols = progress.GetReferencedSymbols();

                return(referencedSymbols.Where(refSymbol => refSymbol.Definition.Equals(symbol))
                       .SelectMany(refSymbol => refSymbol.Locations).ToImmutableArray());
            }
        private static async Task <ImmutableArray <ReferencedSymbol> > FindChangeSignatureReferencesAsync(
            ISymbol symbol,
            Solution solution,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.FindReference_ChangeSignature, cancellationToken))
            {
                var streamingProgress = new StreamingProgressCollector();

                var engine = new FindReferencesSearchEngine(
                    solution,
                    documents: null,
                    ReferenceFinders.DefaultReferenceFinders.Add(DelegateInvokeMethodReferenceFinder.DelegateInvokeMethod),
                    streamingProgress,
                    FindReferencesSearchOptions.Default,
                    cancellationToken);

                await engine.FindReferencesAsync(symbol).ConfigureAwait(false);

                return(streamingProgress.GetReferencedSymbols());
            }
        }