Qulaly icon indicating copy to clipboard operation
Qulaly copied to clipboard

A library that queries Roslyn's C# syntax tree with CSS selector-like syntax.

Qulaly

Query language for Roslyn. Qulaly is a library that queries Roslyn's C# syntax tree with CSS selector-like syntax. Inspired by esquery in ECMAScript ecosystem.

NuGet version Build-Development

⚡Live Demo: https://mayuki.github.io/Qulaly/

Example

The following code shows how to query the async method.

using Qulaly;

var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class Class1
{
    public static async ValueTask<T> FooAsync<T>(int a, string b, T c) => throw new NotImplementedException();
    public async Task BarAsync<T>() => throw new NotImplementedException();
    public object MethodA(int arg1) => throw new NotImplementedException();
    public object MethodB(int arg1, string arg2) => throw new NotImplementedException();
}
");

// Enumerate SyntaxNodes by calling `QuerySelectorAll` extension method for SyntaxNode/SyntaxTree.
foreach (var methodNode in syntaxTree.QuerySelectorAll(":method[Modifiers ~= 'async']"))
{
    Console.WriteLine(((MethodDeclarationSyntax)methodNode).Identifier.ToFullString());
}

Output

FooAsync
BarAsync

Methods

  • QuerySelectorAll
  • QuerySelector

Install

Install NuGet package from NuGet.org

$ dotnet add package Qulaly
PS> Install-Package Qulaly

Supported Selectors

Qulaly supports a subset of CSS selector level 4. The selector engine also supports Qulaly-specific extensions to the selector.

  • SyntaxNode Type: MethodDeclaration, ClassDeclaration ...
  • SyntaxNode Univarsal: *
  • SyntaxNode pseudo-classes (for short-hand)
    • :method
    • :class
    • :interface
    • :lambda
  • Combinators
  • Pseudo-class
  • Attributes (Properties)
    • [PropName] (existance)
    • [PropName = 'Exact']
    • [PropName ^= 'StartsWith']
    • [PropName $= 'EndsWith']
    • [PropName *= 'Contains']
    • [PropName ~= 'Item'] (ex. [Modifiers ~= 'async'])
  • Qulaly Extensions
    • [Name = 'MethodName']: Name special property
      • Name is a special property for convenience that can be used in MethodDeclaration, ClassDeclaration ... etc
    • [TypeParameters.Count > 0]: Conditions
      • Parameters.Count
      • TypeParameters.Count

License

MIT License

Copyright © 2020-present Mayuki Sawatari <[email protected]>