|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.Text; |
| 6 | + |
| 7 | +namespace VL.Avalonia.CodeGen.Forwards |
| 8 | +{ |
| 9 | + public static class RoutedEventArgsGenerator |
| 10 | + { |
| 11 | + const string TARGET_FILENAME = "RoutedEventArgsForwards.g.cs"; |
| 12 | + const string TARGET_TYPE_NAME = "Avalonia.Interactivity.RoutedEventArgs"; |
| 13 | + |
| 14 | + public static void Generate(SourceProductionContext spc, Compilation compilation) |
| 15 | + { |
| 16 | + var routedEventArgsType = compilation.GetTypeByMetadataName(TARGET_TYPE_NAME); |
| 17 | + if (routedEventArgsType == null) |
| 18 | + return; |
| 19 | + |
| 20 | + var foundTypes = new List<INamedTypeSymbol>(); |
| 21 | + |
| 22 | + foreach (var reference in compilation.SourceModule.ReferencedAssemblySymbols) |
| 23 | + { |
| 24 | + if (!reference.Name.StartsWith("Avalonia")) |
| 25 | + continue; |
| 26 | + |
| 27 | + // Helper function to visit all types in the assembly |
| 28 | + VisitNamespace(reference.GlobalNamespace, routedEventArgsType, foundTypes); |
| 29 | + } |
| 30 | + |
| 31 | + if (foundTypes.Count == 0) |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + foundTypes.Add(routedEventArgsType); |
| 37 | + |
| 38 | + var namespaces = foundTypes |
| 39 | + .Select(t => t.ContainingNamespace.ToDisplayString()) |
| 40 | + .Distinct() |
| 41 | + .OrderBy(n => n); |
| 42 | + |
| 43 | + var sb = new StringBuilder(); |
| 44 | + |
| 45 | + sb.AppendLine("using System;"); |
| 46 | + sb.AppendLine("using VL.Core.Import;"); |
| 47 | + |
| 48 | + foreach (var ns in namespaces) |
| 49 | + { |
| 50 | + sb.AppendLine($"using {ns};"); |
| 51 | + } |
| 52 | + sb.AppendLine(); |
| 53 | + |
| 54 | + var typeFormat = SymbolDisplayFormat.MinimallyQualifiedFormat; |
| 55 | + |
| 56 | + foreach (var type in foundTypes) |
| 57 | + { |
| 58 | + sb.AppendLine( |
| 59 | + // csharpier-ignore |
| 60 | + $@"[assembly: ImportType(typeof({type.ToDisplayString(typeFormat)}), Category = ""{type.ContainingNamespace.ToDisplayString()}"")]" |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + spc.AddSource(TARGET_FILENAME, SourceText.From(sb.ToString(), Encoding.UTF8)); |
| 65 | + } |
| 66 | + |
| 67 | + private static void VisitNamespace( |
| 68 | + INamespaceSymbol namespaceSymbol, |
| 69 | + INamedTypeSymbol baseType, |
| 70 | + List<INamedTypeSymbol> foundTypes |
| 71 | + ) |
| 72 | + { |
| 73 | + foreach (var member in namespaceSymbol.GetMembers()) |
| 74 | + { |
| 75 | + if (member is INamespaceSymbol nestedNs) |
| 76 | + { |
| 77 | + VisitNamespace(nestedNs, baseType, foundTypes); |
| 78 | + } |
| 79 | + else if (member is INamedTypeSymbol typeSymbol) |
| 80 | + { |
| 81 | + if ( |
| 82 | + typeSymbol.TypeKind == TypeKind.Class |
| 83 | + && !typeSymbol.IsAbstract |
| 84 | + && InheritsFrom(typeSymbol, baseType) |
| 85 | + ) |
| 86 | + { |
| 87 | + foundTypes.Add(typeSymbol); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static bool InheritsFrom(INamedTypeSymbol type, INamedTypeSymbol baseType) |
| 94 | + { |
| 95 | + var current = type.BaseType; |
| 96 | + while (current != null) |
| 97 | + { |
| 98 | + if (SymbolEqualityComparer.Default.Equals(current, baseType)) |
| 99 | + return true; |
| 100 | + current = current.BaseType; |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments