https://github.com/mayuki/qulaly
A library that queries Roslyn's C# syntax tree with CSS selector-like syntax.
https://github.com/mayuki/qulaly
csharp dotnet-core roslyn
Last synced: over 1 year ago
JSON representation
A library that queries Roslyn's C# syntax tree with CSS selector-like syntax.
- Host: GitHub
- URL: https://github.com/mayuki/qulaly
- Owner: mayuki
- License: mit
- Created: 2020-08-16T09:09:14.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-24T11:18:37.000Z (almost 6 years ago)
- Last Synced: 2025-02-28T16:23:12.107Z (over 1 year ago)
- Topics: csharp, dotnet-core, roslyn
- Language: C#
- Homepage:
- Size: 30.8 MB
- Stars: 30
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Qulaly
**Qu**ery **la**nguage for Ros**ly**n. Qulaly is a library that queries Roslyn's C# syntax tree with CSS selector-like syntax. Inspired by [esquery](https://github.com/estools/esquery) in ECMAScript ecosystem.
[](https://www.nuget.org/packages/Qulaly)
[](https://github.com/mayuki/Qulaly/actions?query=workflow%3ABuild-Development)
⚡Live Demo: https://mayuki.github.io/Qulaly/
## Example
The following code shows how to query the `async` method.
```csharp
using Qulaly;
var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Class1
{
public static async ValueTask FooAsync(int a, string b, T c) => throw new NotImplementedException();
public async Task BarAsync() => 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
```bash
$ dotnet add package Qulaly
```
```powershell
PS> Install-Package Qulaly
```
## Supported Selectors
Qulaly supports a subset of [CSS selector level 4](https://www.w3.org/TR/selectors-4/). The selector engine also supports Qulaly-specific extensions to the selector.
- SyntaxNode Type: `MethodDeclaration`, `ClassDeclaration` ...
- See also [SyntaxKind enum](https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.csharp.syntaxkind?view=roslyn-dotnet)
- SyntaxNode Univarsal: `*`
- SyntaxNode pseudo-classes (for short-hand)
- `:method`
- `:class`
- `:interface`
- `:lambda`
- Combinators
- [Descendant](https://www.w3.org/TR/selectors-4/#descendant-combinators): `node descendant`
- [Child](https://www.w3.org/TR/selectors-4/#child-combinators): `node > child`
- [Next-sibling](https://www.w3.org/TR/selectors-4/#adjacent-sibling-combinators): `node + next`
- [Subsequent-sibling](https://www.w3.org/TR/selectors-4/#general-sibling-combinators): `node ~ sibling`
- Pseudo-class
- [Negation](https://www.w3.org/TR/selectors-4/#negation): `:not(...)`
- [Matches-any](https://www.w3.org/TR/selectors-4/#matches): `:is(...)`
- [Relational](https://www.w3.org/TR/selectors-4/#relational): `:has(...)`
- [`:first-child`](https://www.w3.org/TR/selectors-4/#the-first-child-pseudo)
- [`:last-child`](https://www.w3.org/TR/selectors-4/#the-last-child-pseudo)
- 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
```