RhoMicro.CodeAnalysis.Lyra
1.2.0
Prefix Reserved
dotnet add package RhoMicro.CodeAnalysis.Lyra --version 1.2.0
NuGet\Install-Package RhoMicro.CodeAnalysis.Lyra -Version 1.2.0
<PackageReference Include="RhoMicro.CodeAnalysis.Lyra" Version="1.2.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="RhoMicro.CodeAnalysis.Lyra" Version="1.2.0" />
<PackageReference Include="RhoMicro.CodeAnalysis.Lyra"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add RhoMicro.CodeAnalysis.Lyra --version 1.2.0
#r "nuget: RhoMicro.CodeAnalysis.Lyra, 1.2.0"
#:package RhoMicro.CodeAnalysis.Lyra@1.2.0
#addin nuget:?package=RhoMicro.CodeAnalysis.Lyra&version=1.2.0
#tool nuget:?package=RhoMicro.CodeAnalysis.Lyra&version=1.2.0
Lyra
This is a source generator generating library classes for writing beautiful C# source code; e.g. in source generators.
Licensing
This project is licensed under the MPL-2.0 license.
Features
- indentation and newline aware string building
- reusable component system
- advanced interpolated strings for efficient source building
Installation
.NET CLI:
dotnet add package RhoMicro.CodeAnalysis.Lyra --version 1.1.0
PackageReference:
<PackageReference Include="RhoMicro.CodeAnalysis.Lyra" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
How To Use
Simple Example
Instantiate a new CSharpSourceBuilder:
using var sb = new CSharpSourceBuilder();
Append text:
sb.Append("namespace ").AppendLine("Foo").AppendLine('{');
Indent text:
sb.Indent();
Use builtin components:
sb.Append(
ComponentFactory.Type(
"enum",
"Bar",
ComponentFactory.Create((b, _) => b.AppendLine("Baz = 0"))));
Detent text:
sb.Detent();
Generate output:
sb.AppendLine('}');
Console.WriteLine(sb);
The generated output will look like this:
// <auto-generated>
// This file was generated using the global::RhoMicro.CodeAnalysis.Lyra.CSharpSourceBuilder.
// </auto-generated>
namespace Foo
{
enum Bar
{
Baz = 0
}
}
The CSharpSourceBuilder can be configured using the CSharpSourceBuilderOptions passed to its constructor:
using var sb1 = new CSharpSourceBuilder(
new CSharpSourceBuilderOptions()
{
Prelude = (b,_)=>b.AppendLine("// <auto-generated/>"),
DefaultIndentation = " ".AsMemory()
});
The output changes to:
// <auto-generated/>
namespace Foo
{
enum Bar
{
Baz = 0
}
}
Interpolated Strings
Interpolated strings are compiled to Append invocations, with indentation being automatically detected:
var placeholder = "\nbar\nbaz";
sb.Clear().AppendLine(
$"""
foo{placeholder}
""");
Console.WriteLine(sb);
The output changes as follows:
// <auto-generated/>
foo
bar
baz
Note how the initial indentation in front of foo is carried along.
The detection of these indentations can be configured using the
CSharpSourceBuilderOptions.InitialInterpolationIndentationDetector property.
Components
Some useful components are provided out of the box:
var body = ComponentFactory.Create(
myModel,
static (myModel, b, ct) =>
{
ct.ThrowIfCancellationRequested();
b.Append(myModel.Value);
});
var type = ComponentFactory.Type(
"partial struct",
myModel.Name,
body,
baseTypeList:
[
TypeName<IComparable>()
]);
var @namespace = ComponentFactory.Namespace(
myModel.Namespace,
type);
builder.AppendLine(@namespace);
The output will look similar to this:
namespace Namespace
{
partial struct Name : global::System.IComparable
{
// myModel.Value
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Bcl.HashCode (>= 6.0.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.