Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.CodeDom.Compiler;

namespace Appegy.Union.Generator;

public class UnionMatchPart : GeneratorPart<UnionAttributePartInput>
{
public override void Generate(IndentedTextWriter codeWriter, UnionAttributePartInput input)
{
var types = input.Types;

codeWriter.WriteLine("public void Match(");

using (codeWriter.IndentScope())
{
for (var i = 0; i < types.Count; i++)
{
var type = types[i];
codeWriter.Write("global::System.Action<");
codeWriter.Write(type.FullName);
codeWriter.Write('>');
codeWriter.Write(' ');
codeWriter.Write(type.ParamName);
codeWriter.WriteLine(i != types.Count - 1 ? "," : ")");
}
}

codeWriter.WriteLine("{");
using (codeWriter.IndentScope())
{
codeWriter.WriteLine("switch (Type)");
codeWriter.WriteLine("{");
using (codeWriter.IndentScope())
{
foreach (var type in types)
{
codeWriter.Write("case Kind.");
codeWriter.Write(type.Name);
codeWriter.Write(": ");
codeWriter.Write(type.ParamName);
codeWriter.Write("(");
codeWriter.Write(type.FieldName);
codeWriter.Write("); break;");
codeWriter.WriteLine();
}
codeWriter.WriteLine("default: throw new global::System.InvalidOperationException($\"Unknown type of union: {_type}\");");
}
codeWriter.WriteLine("}");
}
codeWriter.WriteLine("}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class UnionAttributeGenerator : IIncrementalGenerator
new UnionFieldsPart(),
new UnionPropertiesPart(),
new UnionConstructorsPart(),
new UnionMatchPart(),
new UnionToStringPart(),
new UnionGetHashCodePart(),
new UnionEqualsPart(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public readonly struct UnionTypeInfo(INamedTypeSymbol symbol)
public readonly INamedTypeSymbol Symbol = symbol;
public readonly string FullName = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
public readonly string FieldName = symbol.GetFieldName();
public readonly string ParamName = symbol.GetParamName();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CodeDom.Compiler;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -23,6 +24,11 @@ public static void AppendParts<T>(this IndentedTextWriter codeWriter, IReadOnlyL
}
}

public static IDisposable IndentScope(this IndentedTextWriter codeWriter)
{
return new DisposableIndent(codeWriter);
}

public static void WriteFieldName(this IndentedTextWriter codeWriter, ISymbol symbol)
{
var name = symbol.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.CodeDom.Compiler;

namespace Appegy.Union.Generator;

public struct DisposableIndent : IDisposable
{
private readonly IndentedTextWriter _codeWriter;
private bool _isDisposed;

public DisposableIndent(IndentedTextWriter codeWriter)
{
_isDisposed = false;
_codeWriter = codeWriter;
_codeWriter.Indent++;
}

public void Dispose()
{
if (_isDisposed) return;
_codeWriter.Indent--;
_isDisposed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ public static ImmutableList<INamedTypeSymbol> GetTypesFromConstructor(this Attri
}

public static string GetFieldName(this INamedTypeSymbol symbol)
{
var name = symbol.Name;
var param = symbol.GetParamName();

if (string.IsNullOrEmpty(param)) return string.Empty;

return "_" + param;
}

public static string GetParamName(this INamedTypeSymbol symbol)
{
var name = symbol.Name;

if (string.IsNullOrEmpty(name)) return string.Empty;

return name.Length > 1
? "_" + char.ToLower(name[0]) + name.Substring(1)
: "_" + char.ToLower(name[0]);
? char.ToLower(name[0]) + name.Substring(1)
: name.ToLower();
}
}
4 changes: 2 additions & 2 deletions Runtime/Appegy.Union.Generator.dll
Git LFS file not shown
Binary file modified Runtime/Appegy.Union.Generator.pdb
Binary file not shown.