public CompilationUnitSyntax GeneratePartialDeclaration(INamedTypeSymbol container, StructDeclarationSyntax structDecl) { var recordDecl = (StructDeclarationSyntax)structDecl.ChildNodes().First(x => x is StructDeclarationSyntax); var def = new RecordDefinition(structDecl, recordDecl); var generatedNodes = GetGeneratedNodes(def).ToArray(); var newStructDecl = container.GetContainingTypesAndThis() .Select((type, i) => i == 0 ? StructDeclaration(type.Name).GetPartialTypeDelaration().AddMembers(generatedNodes) : StructDeclaration(type.Name).GetPartialTypeDelaration()) .Aggregate((a, b) => b.AddMembers(a)); var ns = container.ContainingNamespace.FullName().NullIfEmpty(); MemberDeclarationSyntax topDecl = ns is not null ? NamespaceDeclaration(IdentifierName(ns)).AddMembers(newStructDecl) : newStructDecl; var root = (CompilationUnitSyntax)structDecl.SyntaxTree.GetRoot(); return(CompilationUnit() .AddUsings(WithComponentModel(root.Usings)) .AddMembers(topDecl) .WithLeadingTrivia(CreateNullableTrivia()) .WithTrailingTrivia(CarriageReturnLineFeed) .NormalizeWhitespace()); }
private IEnumerable <MemberDeclarationSyntax> GetGeneratedNodes(RecordDefinition def) { yield return(CSharpSyntaxTree.ParseText( $@" private readonly {Consts.SourceValuesStruct} _values; ") .GetRoot().ChildNodes() .OfType <MemberDeclarationSyntax>() .First() .WithTrailingTrivia(CarriageReturnLineFeed, CarriageReturnLineFeed)); // Properties foreach (var p in def.Properties) { foreach (var s in WithTrivia(GetGeneratedMember(p), p.LeadingTrivia, p.TrailingTrivia)) { yield return(s); } } if (!def.IsConstructorDeclared) { foreach (var s in GetGeneratedCtor(def)) { yield return(s); } } foreach (var s in GetGeneratedInterfaces(def)) { yield return(s); } foreach (var s in GetGeneratedRoixMethods(def)) { yield return(s); } }
private IEnumerable <MemberDeclarationSyntax> GetGeneratedCtor(RecordDefinition def) {