Skip to content
Open
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
12 changes: 12 additions & 0 deletions PrimaryConstructor.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ public string GetName()
public class NotRegisteredDependency
{
}

public partial class NestingGrandMother
{
public partial class NestingFather
{
[PrimaryConstructor]
public partial class Nested
{
private readonly int _dummy;
}
}
}
}
59 changes: 51 additions & 8 deletions PrimaryConstructor/PrimaryConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ public void Execute(GeneratorExecutionContext context)
classNames.TryGetValue(classSymbol.Name, out var i);
var name = i == 0 ? classSymbol.Name : $"{classSymbol.Name}{i + 1}";
classNames[classSymbol.Name] = i + 1;
context.AddSource($"{name}.PrimaryConstructor.g.cs",
SourceText.From(CreatePrimaryConstructor(classSymbol), Encoding.UTF8));

var source = CSharpSyntaxTree
.ParseText(SourceText.From(CreatePrimaryConstructor(classSymbol), Encoding.UTF8))
.GetRoot()
.NormalizeWhitespace()
.SyntaxTree
.GetText();

context.AddSource($"{name}.PrimaryConstructor.g.cs", source);
}
}

Expand Down Expand Up @@ -89,12 +96,21 @@ private static string CreatePrimaryConstructor(INamedTypeSymbol classSymbol)
var memberList = GetMembers(classSymbol, false);
var arguments = (baseClassConstructorArgs == null ? memberList : memberList.Concat(baseClassConstructorArgs))
.Select(it => $"{it.Type} {it.ParameterName}");
var fullTypeName = classSymbol.ToDisplayString(TypeFormat);
var i = fullTypeName.IndexOf('<');
var generic = i < 0 ? "" : fullTypeName.Substring(i);
var nestingStack = GetNestingAncestors(classSymbol);
var nestingCount = nestingStack.Count;
var source = new StringBuilder($@"namespace {namespaceName}
{{
partial class {classSymbol.Name}{generic}
{{");
while (nestingStack.Any())
{
var currentNestingAncestor = nestingStack.Pop();

source.Append($@"
partial class {currentNestingAncestor.Name}{GetGenericsNamePart(currentNestingAncestor)}
{{");
}

source.Append($@"
partial class {classSymbol.Name}{GetGenericsNamePart(classSymbol)}
{{
public {classSymbol.Name}({string.Join(", ", arguments)}){baseConstructorInheritance}
{{");
Expand All @@ -104,9 +120,18 @@ partial class {classSymbol.Name}{generic}
source.Append($@"
this.{item.Name} = {item.ParameterName};");
}

source.Append(@"
}
}
}");

for (int i = 0; i < nestingCount; i++)
{
source.Append(@"
}");
}

source.Append(@"
}
");

Expand Down Expand Up @@ -177,5 +202,23 @@ select model.GetDeclaredSymbol(clazz)! into classSymbol
where HasAttribute(classSymbol, nameof(PrimaryConstructorAttribute))
select classSymbol;
}

private static Stack<INamedTypeSymbol> GetNestingAncestors(INamedTypeSymbol classSymbol)
{
var stack = new Stack<INamedTypeSymbol>();
var current = classSymbol.ContainingType;
while (current is not null)
{
stack.Push(current);
current = current.ContainingType;
}

return stack;
}

private static string GetGenericsNamePart(INamedTypeSymbol classSymbol) =>
classSymbol.TypeArguments.Any()
? $"<{string.Join(", ", classSymbol.TypeArguments.Select(s => s.ToDisplayString(TypeFormat)))}>"
: "";
}
}