| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Text; |
| | | 9 | | |
| | | 10 | | using NexusLabs.Needlr.Avalonia.Diagnostics; |
| | | 11 | | |
| | | 12 | | namespace NexusLabs.Needlr.Avalonia; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Source generator that emits design-time parameterless constructors for Avalonia |
| | | 16 | | /// controls decorated with <c>[GenerateAvaloniaDesignTimeConstructor]</c>. |
| | | 17 | | /// </summary> |
| | | 18 | | [Generator(LanguageNames.CSharp)] |
| | | 19 | | public sealed class AvaloniaDesignTimeConstructorGenerator : IIncrementalGenerator |
| | | 20 | | { |
| | | 21 | | private const string AttributeFullName = |
| | | 22 | | "NexusLabs.Needlr.Avalonia.GenerateAvaloniaDesignTimeConstructorAttribute"; |
| | | 23 | | |
| | | 24 | | public void Initialize(IncrementalGeneratorInitializationContext context) |
| | | 25 | | { |
| | 11 | 26 | | var candidates = context.SyntaxProvider.ForAttributeWithMetadataName( |
| | 11 | 27 | | AttributeFullName, |
| | 11 | 28 | | predicate: static (node, _) => node is ClassDeclarationSyntax, |
| | 22 | 29 | | transform: static (ctx, _) => GetClassInfo(ctx)); |
| | | 30 | | |
| | 11 | 31 | | context.RegisterSourceOutput(candidates, static (spc, info) => |
| | 11 | 32 | | { |
| | 11 | 33 | | if (info == null) |
| | 0 | 34 | | return; |
| | 11 | 35 | | |
| | 11 | 36 | | var classInfo = info.Value; |
| | 11 | 37 | | |
| | 11 | 38 | | // Validate: must be partial |
| | 11 | 39 | | if (!classInfo.IsPartial) |
| | 11 | 40 | | { |
| | 2 | 41 | | spc.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 42 | | AvaloniaDiagnosticDescriptors.ClassMustBePartial, |
| | 2 | 43 | | classInfo.Location, |
| | 2 | 44 | | classInfo.ClassName)); |
| | 2 | 45 | | return; |
| | 11 | 46 | | } |
| | 11 | 47 | | |
| | 11 | 48 | | // Validate: must not use a primary constructor |
| | 9 | 49 | | if (classInfo.HasPrimaryConstructor) |
| | 11 | 50 | | { |
| | 2 | 51 | | spc.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 52 | | AvaloniaDiagnosticDescriptors.PrimaryConstructorNotSupported, |
| | 2 | 53 | | classInfo.Location, |
| | 2 | 54 | | classInfo.ClassName)); |
| | 2 | 55 | | return; |
| | 11 | 56 | | } |
| | 11 | 57 | | |
| | 11 | 58 | | // Validate: must not already have a parameterless constructor |
| | 7 | 59 | | if (classInfo.HasParameterlessCtor) |
| | 11 | 60 | | { |
| | 1 | 61 | | spc.ReportDiagnostic(Diagnostic.Create( |
| | 1 | 62 | | AvaloniaDiagnosticDescriptors.AlreadyHasParameterlessCtor, |
| | 1 | 63 | | classInfo.Location, |
| | 1 | 64 | | classInfo.ClassName)); |
| | 1 | 65 | | return; |
| | 11 | 66 | | } |
| | 11 | 67 | | |
| | 11 | 68 | | // Warn: should have at least one constructor with parameters |
| | 6 | 69 | | if (!classInfo.HasParameterizedCtor) |
| | 11 | 70 | | { |
| | 1 | 71 | | spc.ReportDiagnostic(Diagnostic.Create( |
| | 1 | 72 | | AvaloniaDiagnosticDescriptors.NoParameterizedCtor, |
| | 1 | 73 | | classInfo.Location, |
| | 1 | 74 | | classInfo.ClassName)); |
| | 1 | 75 | | return; |
| | 11 | 76 | | } |
| | 11 | 77 | | |
| | 11 | 78 | | // Emit the design-time constructor |
| | 5 | 79 | | var source = GenerateSource(classInfo); |
| | 5 | 80 | | spc.AddSource( |
| | 5 | 81 | | $"{classInfo.ClassName}.DesignTimeCtor.g.cs", |
| | 5 | 82 | | SourceText.From(source, Encoding.UTF8)); |
| | 16 | 83 | | }); |
| | 11 | 84 | | } |
| | | 85 | | |
| | | 86 | | private static DesignTimeCtorClassInfo? GetClassInfo( |
| | | 87 | | GeneratorAttributeSyntaxContext ctx) |
| | | 88 | | { |
| | 11 | 89 | | if (ctx.TargetSymbol is not INamedTypeSymbol typeSymbol) |
| | 0 | 90 | | return null; |
| | | 91 | | |
| | 11 | 92 | | var classDecl = ctx.TargetNode as ClassDeclarationSyntax; |
| | 11 | 93 | | var isPartial = classDecl?.Modifiers.Any(SyntaxKind.PartialKeyword) ?? false; |
| | | 94 | | |
| | 11 | 95 | | var hasPrimaryConstructor = classDecl?.ParameterList != null; |
| | | 96 | | |
| | 11 | 97 | | var hasParameterlessCtor = typeSymbol.InstanceConstructors |
| | 22 | 98 | | .Any(c => !c.IsStatic && |
| | 22 | 99 | | c.DeclaredAccessibility == Accessibility.Public && |
| | 22 | 100 | | c.Parameters.Length == 0 && |
| | 22 | 101 | | !c.IsImplicitlyDeclared); |
| | | 102 | | |
| | 11 | 103 | | var hasParameterizedCtor = typeSymbol.InstanceConstructors |
| | 23 | 104 | | .Any(c => !c.IsStatic && |
| | 23 | 105 | | c.DeclaredAccessibility == Accessibility.Public && |
| | 23 | 106 | | c.Parameters.Length > 0); |
| | | 107 | | |
| | 11 | 108 | | var namespaceName = typeSymbol.ContainingNamespace?.IsGlobalNamespace == true |
| | 11 | 109 | | ? null |
| | 11 | 110 | | : typeSymbol.ContainingNamespace?.ToDisplayString(); |
| | | 111 | | |
| | | 112 | | // Build the class declaration modifiers (sealed, internal, etc.) |
| | 11 | 113 | | var modifiers = ""; |
| | 11 | 114 | | if (classDecl != null) |
| | | 115 | | { |
| | 11 | 116 | | var mods = classDecl.Modifiers |
| | 20 | 117 | | .Where(m => m.IsKind(SyntaxKind.SealedKeyword) || |
| | 20 | 118 | | m.IsKind(SyntaxKind.InternalKeyword) || |
| | 20 | 119 | | m.IsKind(SyntaxKind.PublicKeyword)) |
| | 22 | 120 | | .Select(m => m.Text); |
| | 11 | 121 | | modifiers = string.Join(" ", mods); |
| | | 122 | | } |
| | | 123 | | |
| | 11 | 124 | | return new DesignTimeCtorClassInfo( |
| | 11 | 125 | | className: typeSymbol.Name, |
| | 11 | 126 | | namespaceName: namespaceName, |
| | 11 | 127 | | modifiers: modifiers, |
| | 11 | 128 | | isPartial: isPartial, |
| | 11 | 129 | | hasPrimaryConstructor: hasPrimaryConstructor, |
| | 11 | 130 | | hasParameterlessCtor: hasParameterlessCtor, |
| | 11 | 131 | | hasParameterizedCtor: hasParameterizedCtor, |
| | 11 | 132 | | location: ctx.TargetNode.GetLocation()); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private static string GenerateSource(DesignTimeCtorClassInfo info) |
| | | 136 | | { |
| | 5 | 137 | | var sb = new StringBuilder(); |
| | 5 | 138 | | sb.AppendLine("// <auto-generated/>"); |
| | 5 | 139 | | sb.AppendLine("#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting c |
| | 5 | 140 | | sb.AppendLine(); |
| | | 141 | | |
| | 5 | 142 | | if (info.NamespaceName != null) |
| | | 143 | | { |
| | 5 | 144 | | sb.AppendLine($"namespace {info.NamespaceName};"); |
| | 5 | 145 | | sb.AppendLine(); |
| | | 146 | | } |
| | | 147 | | |
| | 5 | 148 | | sb.AppendLine($"partial class {info.ClassName}"); |
| | 5 | 149 | | sb.AppendLine("{"); |
| | 5 | 150 | | sb.AppendLine(" /// <summary>"); |
| | 5 | 151 | | sb.AppendLine(" /// Design-time constructor for Avalonia's XAML previewer."); |
| | 5 | 152 | | sb.AppendLine(" /// At runtime, the DI container uses the richer constructor."); |
| | 5 | 153 | | sb.AppendLine(" /// </summary>"); |
| | 5 | 154 | | sb.AppendLine($" public {info.ClassName}()"); |
| | 5 | 155 | | sb.AppendLine(" {"); |
| | 5 | 156 | | sb.AppendLine(" if (!global::Avalonia.Controls.Design.IsDesignMode)"); |
| | 5 | 157 | | sb.AppendLine(" {"); |
| | 5 | 158 | | sb.AppendLine($" throw new global::System.InvalidOperationException("); |
| | 5 | 159 | | sb.AppendLine($" \"The parameterless constructor on '{info.ClassName}' exists only for \" +"); |
| | 5 | 160 | | sb.AppendLine($" \"Avalonia design-time support. At runtime, resolve this type from \" +"); |
| | 5 | 161 | | sb.AppendLine($" \"the DI container to use the constructor with dependencies.\");"); |
| | 5 | 162 | | sb.AppendLine(" }"); |
| | 5 | 163 | | sb.AppendLine(); |
| | 5 | 164 | | sb.AppendLine(" InitializeComponent();"); |
| | 5 | 165 | | sb.AppendLine(" }"); |
| | 5 | 166 | | sb.AppendLine("}"); |
| | 5 | 167 | | sb.AppendLine(); |
| | 5 | 168 | | sb.AppendLine("#pragma warning restore CS8618"); |
| | | 169 | | |
| | 5 | 170 | | return sb.ToString(); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private readonly struct DesignTimeCtorClassInfo |
| | | 174 | | { |
| | | 175 | | public DesignTimeCtorClassInfo( |
| | | 176 | | string className, |
| | | 177 | | string? namespaceName, |
| | | 178 | | string modifiers, |
| | | 179 | | bool isPartial, |
| | | 180 | | bool hasPrimaryConstructor, |
| | | 181 | | bool hasParameterlessCtor, |
| | | 182 | | bool hasParameterizedCtor, |
| | | 183 | | Location location) |
| | | 184 | | { |
| | 11 | 185 | | ClassName = className; |
| | 11 | 186 | | NamespaceName = namespaceName; |
| | 11 | 187 | | Modifiers = modifiers; |
| | 11 | 188 | | IsPartial = isPartial; |
| | 11 | 189 | | HasPrimaryConstructor = hasPrimaryConstructor; |
| | 11 | 190 | | HasParameterlessCtor = hasParameterlessCtor; |
| | 11 | 191 | | HasParameterizedCtor = hasParameterizedCtor; |
| | 11 | 192 | | Location = location; |
| | 11 | 193 | | } |
| | | 194 | | |
| | 26 | 195 | | public string ClassName { get; } |
| | 10 | 196 | | public string? NamespaceName { get; } |
| | 0 | 197 | | public string Modifiers { get; } |
| | 11 | 198 | | public bool IsPartial { get; } |
| | 9 | 199 | | public bool HasPrimaryConstructor { get; } |
| | 7 | 200 | | public bool HasParameterlessCtor { get; } |
| | 6 | 201 | | public bool HasParameterizedCtor { get; } |
| | 6 | 202 | | public Location Location { get; } |
| | | 203 | | } |
| | | 204 | | } |