Exemple #1
0
        /// <summary>
        /// Get all usages of <paramref name="localOrParameter"/>.
        /// </summary>
        /// <param name="localOrParameter">The <see cref="LocalOrParameter"/>.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that cancels the operation.</param>
        /// <returns>A <see cref="UsagesWalker"/> for <paramref name="localOrParameter"/>.</returns>
        public static UsagesWalker Borrow(LocalOrParameter localOrParameter, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (localOrParameter.TryGetScope(cancellationToken, out var scope))
            {
                return(Borrow(localOrParameter.Symbol, scope, semanticModel, cancellationToken));
            }

            return(Borrow(() => new UsagesWalker()));
        }
Exemple #2
0
        /// <summary>
        /// Get a walker that has all uses of <paramref name="localOrParameter"/>.
        /// </summary>
        /// <param name="localOrParameter">The <see cref="LocalOrParameter"/>.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <returns>A walker that has all uses of <paramref name="localOrParameter"/>.</returns>
        public static IdentifierNameWalker For(LocalOrParameter localOrParameter, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (localOrParameter.TryGetScope(cancellationToken, out var scope))
            {
                var walker = BorrowAndVisit(scope, () => new IdentifierNameWalker());
                walker.RemoveAll(x => !IsMatch(x));
                return(walker);
            }

            return(Borrow(() => new IdentifierNameWalker()));

            bool IsMatch(IdentifierNameSyntax candidate)
            {
                return(candidate.IsSymbol(localOrParameter.Symbol, semanticModel, cancellationToken));
            }
        }
Exemple #3
0
        /// <summary>
        /// Get a walker with all mutations for <paramref name="localOrParameter"/>.
        /// </summary>
        /// <param name="localOrParameter">The <see cref="LocalOrParameter"/>.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <returns>A walker with all mutations for <paramref name="localOrParameter"/>.</returns>
        public static MutationWalker For(LocalOrParameter localOrParameter, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (localOrParameter.TryGetScope(cancellationToken, out var node))
            {
                var walker = Borrow(node, SearchScope.Member, semanticModel, cancellationToken);
                walker.assignments.RemoveAll(x => !IsMatch(x.Left));
                walker.prefixUnaries.RemoveAll(x => !IsMatch(x.Operand));
                walker.postfixUnaries.RemoveAll(x => !IsMatch(x.Operand));
                walker.refOrOutArguments.RemoveAll(x => !IsMatch(x.Expression));
                return(walker);
            }

            return(Borrow(() => new MutationWalker()));

            bool IsMatch(ExpressionSyntax expression)
            {
                return(semanticModel.TryGetSymbol(expression, cancellationToken, out var symbol) &&
                       SymbolComparer.Equal(symbol, localOrParameter.Symbol));
            }
        }