RhoMicro.CodeAnalysis.UtilityGenerators
17.0.2
dotnet add package RhoMicro.CodeAnalysis.UtilityGenerators --version 17.0.2
NuGet\Install-Package RhoMicro.CodeAnalysis.UtilityGenerators -Version 17.0.2
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="17.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add RhoMicro.CodeAnalysis.UtilityGenerators --version 17.0.2
#r "nuget: RhoMicro.CodeAnalysis.UtilityGenerators, 17.0.2"
// Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Addin #addin nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=17.0.2 // Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Tool #tool nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=17.0.2
What is this?
This project contains generators and analyzers that help writing generators and analyzers:
Installation
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Templating
A string templating engine is provided via the TemplatingGenerator
.
Templating is done via attributes on template types. A generator analyzes these and generates an implementation for rendering the template, similar to the ASP.Net razor engine.
Notes:
- holes:
- Value (singleline, multiple):
�(foo)
forbuilder.Append(foo);
- builtin conversions to
ReadOnlySpan<Char>
for common types ⇒TemplateHelpers.GetCharSpan(foo)
- detect template type and use
builder.Render
instead- Code (multiline, multiple):
�{foreach(var bar in foo){�(bar)}}
forforeach(var bar in foo){builder.Append(bar);}
- → unlike razor, we require explicit hole type when used in code blocks
- escape with
\
- generated templates must always be generated using escaped source newlines, so we may efficiently use spans on a oneliner template constant
Sample Usage
namespace RhoMicro.CodeAnalysis.Foo;
[Template(
"""
// \�(escaped hole)
// \\�(Name) (unescaped hole)
public �(Accessibility) class �(Name)
{�{
foreach(var member in Members)
{
$(member)
}
}}
""")]
internal sealed partial class FooTemplate
{
public FooTemplate(String accessibility, String name)
{
Accessibility = accessibility;
Name = name;
}
public String Accessibility { get; }
public String Name { get; }
public List<MemberTemplate> Members { get; } = [];
}
should generate something like:
namespace RhoMicro.CodeAnalysis.Foo;
partial class FooTemplate : global::RhoMicro.CodeAnalysis.Library.Text.Templating.Template
{
public override void Render(global::RhoMicro.CodeAnalysis.Library.Text.Templating.BufferedStringBuilder builder)
{
// this should be a one-liner with source newlines
const string __template =
"""
// \�(escaped hole)
// \\�(Name) (unescaped hole)
public �(Accessibility) class �(Name)
{�{
foreach(var member in Members)
{
$(member)
}
}}
""";
builder.Append(__template.AsSpan()[..57]);
builder.Append(TemplateUtilities.GetCharSpan(Accessibility));
// append span of text part
builder.Append(TemplateUtilities.GetCharSpan(Name));
// append span of new line, open brace after Name
// insert newline, tab before each following line in code block
foreach(var member in Members)
{
$(member)
}
// omit trailing whitespace in codeblock up to closing brace
// append closing brace from template
}
}
Grammar
Non-Terminals
Template
template = *(text / hole)
Text
text = *(%x00-%x5B %x5D-%xA6 / %xA8-%xFF / escaped)
Escaped
escaped = ESC (MRK / ESC)
Hole
hole = value-hole / code-hole
Value Hole
value-hole = MRK OPA identifier CPA
Code Hole
code-hole = MRK OBR *(text / value-hole) CBR
Identifier
identifier = ALPHA *(ALPHA / DIGIT / "_")
Terminals
Escape
ESC = %x5C
%5C
=\
Marker
MRK = %xA7
%A7
=�
Opening Curly Brace
OBR = %x7B
%7B
={
Closing Curly Brace
CBR = %x7D
%7D
=}
Opening Parenthesis
OPA = %x28
%28
=(
Closing Parenthesis
CPA = %x29
%29
=)
Building The Project
This project is running the generators contained against itself. To do this, run the bootstrap.ps1
script.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.12.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
17.0.2 | 44 | 12/24/2024 |
16.1.2 | 69 | 12/22/2024 |
16.1.1 | 72 | 12/21/2024 |
16.1.0 | 73 | 12/20/2024 |
16.0.4 | 91 | 12/18/2024 |
16.0.3 | 77 | 12/18/2024 |
16.0.0 | 88 | 12/18/2024 |
15.3.4 | 170 | 12/7/2024 |
15.3.3 | 80 | 12/7/2024 |
15.3.2 | 84 | 12/7/2024 |
15.3.1 | 88 | 12/7/2024 |
15.3.0 | 93 | 12/6/2024 |
15.2.4 | 100 | 12/6/2024 |
15.2.3 | 102 | 12/6/2024 |
15.1.7 | 228 | 7/15/2024 |
15.1.6 | 176 | 6/11/2024 |
15.1.5 | 187 | 3/26/2024 |
15.1.4 | 126 | 3/26/2024 |
15.1.3 | 213 | 3/11/2024 |
15.1.2 | 147 | 3/8/2024 |
15.1.1 | 172 | 3/4/2024 |
15.1.0 | 153 | 2/28/2024 |
15.0.1 | 124 | 2/22/2024 |
15.0.0 | 169 | 2/20/2024 |
14.0.5 | 148 | 2/19/2024 |
14.0.4 | 97 | 2/19/2024 |
14.0.3 | 106 | 2/19/2024 |
14.0.2 | 130 | 2/15/2024 |
14.0.0 | 129 | 2/15/2024 |
14.0.0-alpha.30 | 60 | 2/15/2024 |
14.0.0-alpha.29 | 53 | 2/15/2024 |
14.0.0-alpha.28 | 55 | 2/15/2024 |
14.0.0-alpha.27 | 58 | 2/15/2024 |
14.0.0-alpha.26 | 62 | 2/15/2024 |
14.0.0-alpha.25 | 59 | 2/15/2024 |
14.0.0-alpha.24 | 63 | 2/15/2024 |
14.0.0-alpha.22 | 58 | 2/15/2024 |
14.0.0-alpha.20 | 62 | 2/15/2024 |
13.0.0 | 226 | 1/8/2024 |
12.1.2 | 163 | 1/6/2024 |
12.1.1 | 146 | 1/6/2024 |
12.1.0 | 134 | 1/6/2024 |
12.0.9 | 178 | 1/5/2024 |
12.0.8 | 160 | 1/5/2024 |
11.0.0 | 160 | 12/16/2023 |
8.0.3 | 139 | 12/11/2023 |
8.0.2 | 157 | 12/11/2023 |
8.0.0 | 186 | 12/11/2023 |