public static IEnumerable <SyntaxNode> GetEnumerable(SyntaxNode node, QulalySelector selector, SemanticModel?semanticModel) { if (selector.Matcher(new SelectorMatcherContext(node, semanticModel))) { yield return(node); } foreach (var child in node.ChildNodes()) { foreach (var next in GetEnumerable(child, selector, semanticModel)) { yield return(next); } } }
public override SelectorMatcher GetMatcher() { SelectorMatcher matcher = (in SelectorMatcherContext _) => false; foreach (var selector in _relativeSelectors) { matcher = SelectorCompilerHelper.ComposeOr(matcher, selector.GetMatcher()); } var query = new QulalySelector(matcher, this); return((in SelectorMatcherContext ctx) => { return ctx.Node.QuerySelector(query) != null; });
public void Method() { var selector = QulalySelector.Parse(":method"); var syntaxTree = CSharpSyntaxTree.ParseText(@" using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApp22 { public class Program { static void Main(string[] args) { } public static async ValueTask<T> Test<T>(int a, string b, T c) { } object Bar(int key) { } object Foo(int key) { } } public class Class1 { object Bar(int key) => throw new NotImplementedException(); object Foo(int key) => throw new NotImplementedException(); } } "); var compilation = CSharpCompilation.Create("Test") .AddSyntaxTrees(syntaxTree); var root = syntaxTree.GetCompilationUnitRoot(); var matches = root.QuerySelectorAll(selector, compilation).ToArray(); matches.Should().HaveCount(6); matches.OfType <MethodDeclarationSyntax>().Select(x => x.Identifier.ToString()).Should().ContainInOrder("Main", "Test", "Bar", "Foo", "Bar", "Foo"); }
public static IEnumerable <SyntaxNode[]> GetCapturesEnumerable(SyntaxNode node, QulalySelector selector, SemanticModel?semanticModel) { var ctx = new SelectorMatcherContext(node, semanticModel); if (selector.Matcher(ctx)) { ctx.CapturedNodes.Reverse(); yield return(ctx.CapturedNodes.ToArray()); } foreach (var child in node.ChildNodes()) { foreach (var next in GetCapturesEnumerable(child, selector, semanticModel)) { yield return(next); } } }
public void Complex_1() { var selector = QulalySelector.Parse(":class SwitchSection ObjectCreationExpression > :is(PredefinedType, GenericName, IdentifierName):not([Name^='List']) "); var syntaxTree = CSharpSyntaxTree.ParseText(@" using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApp22 { public class Program { static void Main(string[] args) { Console.WriteLine(""Hello World!""); } public static async ValueTask<T> Test<T>(int a, string b, T c) { return default; } object Bar(int key) { switch (key) { case 0: return new object(); case 1: return new List<int>(); case 2: return new Program(); } return null; } object Foo(int key) { switch (key) { case 0: return new int(); case 1: return new List<string>(); case 2: return new Exception(); } return null; } } public readonly struct AStruct {} [MyNantoka] public class BClass<T> where T: class {} public class MyNantokaAttribute : Attribute { } } "); var compilation = CSharpCompilation.Create("Test") .AddSyntaxTrees(syntaxTree); var root = syntaxTree.GetCompilationUnitRoot(); var matches = root.QuerySelectorAll(selector, compilation).ToArray(); matches.Should().HaveCount(4); // object, Program, int, Exception matches.Select(x => x.ToString()).Should().ContainInOrder("object", "Program", "int", "Exception"); }