Skip to content

Commit

Permalink
Support multiple records with same name (#38)
Browse files Browse the repository at this point in the history
* Remove unused union record generator code.
* Support multiple unions with the same name.
* Clarify union attribute constant names.
* Union attribute source naming improvements.
  • Loading branch information
domn1995 authored Jul 7, 2022
1 parent 4c87009 commit c0e0430
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/GenerateUnionAttribute/UnionAttributeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterPostInitializationOutput(
ctx =>
ctx.AddSource(
"UnionAttribute.g.cs",
SourceText.From(UnionAttributeSource.Attribute, Encoding.UTF8)
$"{UnionAttributeSource.Name}.g.cs",
SourceText.From(UnionAttributeSource.SourceCode, Encoding.UTF8)
)
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/GenerateUnionAttribute/UnionAttributeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

internal class UnionAttributeSource
{
public const string AttributeNamespace = "Dunet";
public const string AttributeName = "UnionAttribute";
public const string FullAttributeName = $"{AttributeNamespace}.{AttributeName}";
public const string Namespace = "Dunet";
public const string Name = "UnionAttribute";
public const string FullyQualifiedName = $"{Namespace}.{Name}";

public const string Attribute =
public const string SourceCode =
@"using System;
namespace Dunet;
Expand Down
20 changes: 8 additions & 12 deletions src/GenerateUnionRecord/UnionRecordGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class UnionRecordGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var recordDeclarations = context.SyntaxProvider
var targets = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: static (node, _) => node.IsDecoratedRecord(),
transform: static (ctx, _) => GetGenerationTarget(ctx)
)
.Where(static m => m is not null);

var compilation = context.CompilationProvider.Combine(recordDeclarations.Collect());
var compilation = context.CompilationProvider.Combine(targets.Collect());

context.RegisterSourceOutput(
compilation,
Expand All @@ -47,7 +47,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

var fullAttributeName = attributeSymbol.ToDisplayString();

if (fullAttributeName is UnionAttributeSource.FullAttributeName)
if (fullAttributeName is UnionAttributeSource.FullyQualifiedName)
{
return recordDeclaration;
}
Expand All @@ -68,23 +68,19 @@ SourceProductionContext context
return;
}

var distinctRecords = recordDeclarations.Distinct();

var unionRecords = GetCodeToGenerate(
compilation,
distinctRecords,
recordDeclarations,
context.CancellationToken
);

if (unionRecords.Count <= 0)
{
return;
}

foreach (var unionRecord in unionRecords)
{
var result = UnionRecordSource.GenerateRecord(unionRecord);
context.AddSource($"{unionRecord.Name}.g.cs", SourceText.From(result, Encoding.UTF8));
context.AddSource(
$"{unionRecord.Namespace}.{unionRecord.Name}.g.cs",
SourceText.From(result, Encoding.UTF8)
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/IsExternalInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ namespace System.Runtime.CompilerServices;
/// Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
sealed class IsExternalInit { }
internal sealed class IsExternalInit { }

0 comments on commit c0e0430

Please sign in to comment.