Locates nodes in an AST.
Inheritance: Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker
Example #1
0
        /// <summary>
        /// Gets the first occurrance of a node type in the AST.
        /// </summary>
        /// <param name="nodeType">The node type to look for.</param>
        /// <returns>A <see cref="SyntaxNode"/>.</returns>
        public SyntaxNode LocateFirst(Type nodeType)
        {
            ValidateInputType(nodeType);

            List<SyntaxNode> nodes = new List<SyntaxNode>();
            var astExecutor = new ASTWalkerNodeTypeOperationExecutor(this.Root, nodeType, (node) => nodes.Add(node));
            astExecutor.Start();

            return nodes.Count > 0 ? nodes[0] : null;
        }
Example #2
0
        /// <summary>
        /// Gets all occurrances of a node type in the AST.
        /// </summary>
        /// <param name="nodeType">The node type to look for.</param>
        /// <returns>A <see cref="SyntaxNode"/>.</returns>
        public IEnumerable<SyntaxNode> LocateAll(Type nodeType)
        {
            ValidateInputType(nodeType);

            List<SyntaxNode> nodes = new List<SyntaxNode>();
            var astExecutor = new ASTWalkerNodeTypeOperationExecutor(this.Root, nodeType, (node) => nodes.Add(node));
            astExecutor.Start();

            return nodes.ToArray();
        }