private static ConstructorInitializerSyntax GenerateConstructorInitializer( IMethodSymbol constructor) { var arguments = CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) ?? CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor); var kind = CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) != null ? SyntaxKind.ThisConstructorInitializer : SyntaxKind.BaseConstructorInitializer; return(arguments == null ? null : SyntaxFactory.ConstructorInitializer(kind).WithArgumentList(GenerateArgumentList(arguments))); }
private static RecordDeclarationSyntax GenerateRecordMembers( ICodeGenerationService service, CSharpCodeGenerationContextInfo info, RecordDeclarationSyntax recordDeclaration, ImmutableArray <ISymbol> members, CancellationToken cancellationToken) { if (!info.Context.GenerateMembers) { members = ImmutableArray <ISymbol> .Empty; } // For a record, add record parameters if we have a primary constructor. var primaryConstructor = members.OfType <IMethodSymbol>().FirstOrDefault(m => CodeGenerationConstructorInfo.GetIsPrimaryConstructor(m)); if (primaryConstructor != null) { var parameterList = ParameterGenerator.GenerateParameterList(primaryConstructor.Parameters, isExplicit: false, info); recordDeclaration = recordDeclaration.WithParameterList(parameterList); // remove the primary constructor from the list of members to generate. members = members.Remove(primaryConstructor); // remove any fields/properties that were created by the primary constructor members = members.WhereAsArray(m => m is not IPropertySymbol and not IFieldSymbol || !primaryConstructor.Parameters.Any(static (p, m) => p.Name == m.Name, m));
private static RecordDeclarationSyntax GenerateRecordMembers( ICodeGenerationService service, CodeGenerationOptions options, RecordDeclarationSyntax recordDeclaration, ImmutableArray <ISymbol> members, CancellationToken cancellationToken) { if (!options.GenerateMembers) { members = ImmutableArray <ISymbol> .Empty; } // For a record, add record parameters if we have a primary constructor. var primaryConstructor = members.OfType <IMethodSymbol>().FirstOrDefault(m => CodeGenerationConstructorInfo.GetIsPrimaryConstructor(m)); if (primaryConstructor != null) { var parameterList = ParameterGenerator.GenerateParameterList(primaryConstructor.Parameters, isExplicit: false, options); recordDeclaration = recordDeclaration.WithParameterList(parameterList); // remove the primary constructor from the list of members to generate. members = members.Remove(primaryConstructor); // remove any fields/properties that were created by the primary constructor members = members.WhereAsArray(m => m is not IPropertySymbol and not IFieldSymbol || !primaryConstructor.Parameters.Any(p => p.Name == m.Name)); } // remove any implicit overrides to generate. members = members.WhereAsArray(m => !m.IsImplicitlyDeclared); // If there are no members, just make a simple record with no body if (members.Length == 0) { return(recordDeclaration.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))); } // Otherwise, give the record a body and add the members to it. recordDeclaration = recordDeclaration.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken)) .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken)) .WithSemicolonToken(default);