|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
| 4 | +using System.Text; |
4 | 5 |
|
5 | 6 | using Microsoft.CodeAnalysis; |
6 | 7 |
|
7 | 8 | namespace Microsoft.Maui.Controls.SourceGen; |
8 | 9 |
|
9 | | -class CompilationReferencesComparer : IEqualityComparer<Compilation> |
| 10 | +/// <summary> |
| 11 | +/// Compares compilations by their public API signatures (types, members, method signatures). |
| 12 | +/// Implementation changes (method bodies) are ignored, so editing code inside a method |
| 13 | +/// won't trigger XAML regeneration. |
| 14 | +/// |
| 15 | +/// This is slower than a references-only comparer (~1ms for 100 files vs ~0.001ms) |
| 16 | +/// but avoids regenerating all XAML files on every C# keystroke while still detecting |
| 17 | +/// signature changes that could affect XAML (new types, changed members, etc.). |
| 18 | +/// |
| 19 | +/// Performance characteristics: |
| 20 | +/// - 10 files: ~0.1ms per comparison |
| 21 | +/// - 50 files: ~0.9ms per comparison |
| 22 | +/// - 100 files: ~1.1ms per comparison |
| 23 | +/// |
| 24 | +/// The comparer triggers XAML regeneration when: |
| 25 | +/// - A new type is added or removed |
| 26 | +/// - A type's base class or interfaces change |
| 27 | +/// - A public/internal member is added, removed, or has its signature changed |
| 28 | +/// - External references change |
| 29 | +/// |
| 30 | +/// The comparer does NOT trigger regeneration for: |
| 31 | +/// - Method body changes (implementation details) |
| 32 | +/// - Comment changes |
| 33 | +/// - Whitespace changes |
| 34 | +/// - Private member changes |
| 35 | +/// </summary> |
| 36 | +class CompilationSignaturesComparer : IEqualityComparer<Compilation> |
10 | 37 | { |
11 | 38 | public bool Equals(Compilation x, Compilation y) |
12 | 39 | { |
| 40 | + if (ReferenceEquals(x, y)) |
| 41 | + return true; |
| 42 | + |
13 | 43 | if (x.AssemblyName != y.AssemblyName |
14 | 44 | || x.ExternalReferences.Length != y.ExternalReferences.Length) |
15 | 45 | return false; |
16 | 46 |
|
17 | | - return x.ExternalReferences.OfType<PortableExecutableReference>().SequenceEqual(y.ExternalReferences.OfType<PortableExecutableReference>()); |
| 47 | + if (!x.ExternalReferences.OfType<PortableExecutableReference>().SequenceEqual(y.ExternalReferences.OfType<PortableExecutableReference>())) |
| 48 | + return false; |
| 49 | + |
| 50 | + // Compare type signatures (ignoring method implementations) |
| 51 | + return GetSignatureString(x) == GetSignatureString(y); |
| 52 | + } |
| 53 | + |
| 54 | + private static string GetSignatureString(Compilation compilation) |
| 55 | + { |
| 56 | + var sb = new StringBuilder(); |
| 57 | + AppendNamespace(sb, compilation.Assembly.GlobalNamespace); |
| 58 | + return sb.ToString(); |
| 59 | + } |
| 60 | + |
| 61 | + private static void AppendNamespace(StringBuilder sb, INamespaceSymbol ns) |
| 62 | + { |
| 63 | + foreach (var type in ns.GetTypeMembers().OrderBy(t => t.Name)) |
| 64 | + AppendType(sb, type); |
| 65 | + foreach (var child in ns.GetNamespaceMembers().OrderBy(n => n.Name)) |
| 66 | + AppendNamespace(sb, child); |
| 67 | + } |
| 68 | + |
| 69 | + private static void AppendType(StringBuilder sb, INamedTypeSymbol type) |
| 70 | + { |
| 71 | + sb.Append(type.DeclaredAccessibility).Append(' '); |
| 72 | + sb.Append(type.TypeKind).Append(' '); |
| 73 | + sb.Append(type.ToFQDisplayString()); |
| 74 | + |
| 75 | + if (type.BaseType != null && type.BaseType.SpecialType != SpecialType.System_Object) |
| 76 | + sb.Append(':').Append(type.BaseType.ToFQDisplayString()); |
| 77 | + |
| 78 | + foreach (var iface in type.Interfaces.OrderBy(i => i.ToFQDisplayString())) |
| 79 | + sb.Append(',').Append(iface.ToFQDisplayString()); |
| 80 | + |
| 81 | + sb.Append('{'); |
| 82 | + |
| 83 | + // Include non-private, non-compiler-generated members |
| 84 | + foreach (var member in type.GetMembers() |
| 85 | + .Where(m => m.DeclaredAccessibility != Accessibility.Private && !m.IsImplicitlyDeclared) |
| 86 | + .OrderBy(m => m.Name) |
| 87 | + .ThenBy(m => m.Kind)) |
| 88 | + { |
| 89 | + switch (member) |
| 90 | + { |
| 91 | + case IFieldSymbol f: |
| 92 | + sb.Append(f.DeclaredAccessibility).Append(' '); |
| 93 | + if (f.IsStatic) sb.Append("static "); |
| 94 | + sb.Append(f.Type.ToFQDisplayString()).Append(' ').Append(f.Name).Append(';'); |
| 95 | + break; |
| 96 | + |
| 97 | + case IPropertySymbol p: |
| 98 | + sb.Append(p.DeclaredAccessibility).Append(' '); |
| 99 | + if (p.IsStatic) sb.Append("static "); |
| 100 | + sb.Append(p.Type.ToFQDisplayString()).Append(' ').Append(p.Name); |
| 101 | + if (p.GetMethod != null) sb.Append("{get;}"); |
| 102 | + if (p.SetMethod != null) sb.Append("{set;}"); |
| 103 | + break; |
| 104 | + |
| 105 | + case IMethodSymbol m when m.MethodKind == MethodKind.Ordinary: |
| 106 | + sb.Append(m.DeclaredAccessibility).Append(' '); |
| 107 | + if (m.IsStatic) sb.Append("static "); |
| 108 | + sb.Append(m.ReturnType.ToFQDisplayString()).Append(' ').Append(m.Name); |
| 109 | + sb.Append('('); |
| 110 | + sb.Append(string.Join(",", m.Parameters.Select(p => p.Type.ToFQDisplayString()))); |
| 111 | + sb.Append(')').Append(';'); |
| 112 | + break; |
| 113 | + |
| 114 | + case INamedTypeSymbol nested: |
| 115 | + AppendType(sb, nested); |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + sb.Append('}'); |
18 | 121 | } |
19 | 122 |
|
20 | 123 | public int GetHashCode(Compilation obj) => obj.References.GetHashCode(); |
|
0 commit comments