public static QualifiedTypeName QualifiedName(this BaseTypeDeclarationSyntax dec) { var current = dec.Parent as BaseTypeDeclarationSyntax; var typeNames = new Stack <string>(); while (current != null) { typeNames.Push(current.Name()); current = current.Parent as BaseTypeDeclarationSyntax; } return(new QualifiedTypeName(dec.Name(), typeNames)); }
static ParameterListSyntax ToWithParameters(string fullClassName, BaseTypeDeclarationSyntax classDeclarationSyntax, BaseParameterListSyntax constructorParameterList) { var thisTypeName = fullClassName; var thisParameters = SyntaxFactory.Parameter(SyntaxFactory.Identifier(classDeclarationSyntax.Name().FirstToLower())) .WithType(SyntaxFactory.ParseTypeName(thisTypeName)) .AddThis(); var furtherParameters = constructorParameterList.Parameters .Select(p => p .WithType(SyntaxFactory.ParseTypeName($"Option<{p.Type.Name()}>")) .WithDefault(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("null"))) ); return(SyntaxFactory.ParameterList() .AddParameters( new[] { thisParameters }.Concat(furtherParameters).ToArray())); }
static string Call(string fullClassName, BaseTypeDeclarationSyntax classDeclaration, BaseParameterListSyntax constructorParameters) { var sb = new StringBuilder($"new {fullClassName}("); var thisParameterName = classDeclaration.Name().ToParameterName(); var lines = constructorParameters.Parameters .Select(p => { var parameterName = p.Identifier.Text; var propertyName = parameterName.FirstToUpper(); return $"{parameterName}: {parameterName} != null ? {parameterName}.Match(x => x, () => {thisParameterName}.{propertyName}) : {thisParameterName}.{propertyName}"; }); sb.AppendLine(lines.ToDelimitedString("," + Environment.NewLine)); sb.AppendLine(")"); return sb.ToString(); }