A high-performance source text writer for .NET incremental source generators using string interpolations
$ dotnet add package Raiqub.Generators.InterpolationCodeWriter.CSharpProvides Roslyn-specific utilities for .NET incremental source generators, including CodeWriterDispatcher and extensions for integrating SourceTextWriter with SourceProductionContext.
dotnet add package Raiqub.Generators.InterpolationCodeWriter.CSharp
Implement ICodeWriter<T> to define a code writer and use CodeWriterDispatcher<T> to dispatch source generation:
public class MyWriter : ICodeWriter<MyModel>
{
public bool CanGenerateFor(MyModel model) => model.Properties.Count > 0;
public string GetFileName(MyModel model) => $"{model.Namespace ?? "_"}.{model.Name}Extensions.g.cs";
public void Write(SourceTextWriter writer, MyModel model)
{
writer.WriteLine($"namespace {model.Namespace};");
writer.WriteLine();
writer.WriteLine($"public static partial class {model.Name}Extensions");
writer.WriteLine("{");
writer.PushIndent();
// Write members using string interpolation...
writer.PopIndent();
writer.WriteLine("}");
}
}
Then dispatch generation using CodeWriterDispatcher<T>:
private static readonly CodeWriterDispatcher<MyModel> s_dispatcher =
new([new MyWriter()]);
private static void Emit(
SourceProductionContext context,
ImmutableArray<MyModel> types)
{
s_dispatcher.GenerateSources(types, context);
}
Targets .NET Standard 2.0.
For the full documentation and guides, see the project repository.
See the LICENSE file for details.