-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUxmlGenerator.cs
More file actions
97 lines (79 loc) · 3.38 KB
/
UxmlGenerator.cs
File metadata and controls
97 lines (79 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using UnityUxmlGenerator.Captures;
using UnityUxmlGenerator.Diagnostics;
using UnityUxmlGenerator.Extensions;
using UnityUxmlGenerator.SyntaxReceivers;
namespace UnityUxmlGenerator;
[Generator]
internal sealed partial class UxmlGenerator : ISourceGenerator
{
private const string VisualElementFullName = "UnityEngine.UIElements.VisualElement";
private static readonly AssemblyName AssemblyName = typeof(UxmlGenerator).Assembly.GetName();
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new VisualElementReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
context.AddSource($"{nameof(UxmlElementClassName)}.g.cs", GenerateUxmlElementAttribute());
context.AddSource($"{nameof(UxmlAttributeClassName)}.g.cs", GenerateUxmlAttributeAttribute());
if (context.SyntaxReceiver is not VisualElementReceiver receiver ||
context.CancellationToken.IsCancellationRequested)
{
return;
}
foreach (var uxmlElement in receiver.UxmlFactoryReceiver.Captures)
{
context.CancellationToken.ThrowIfCancellationRequested();
if (IsValidClass(context, uxmlElement.Class) &&
IsValidAttribute(context, uxmlElement.Attribute))
{
AddSource(context, uxmlElement, GenerateUxmlFactory(uxmlElement));
}
}
foreach (var capture in receiver.UxmlTraitsReceiver.Captures)
{
context.CancellationToken.ThrowIfCancellationRequested();
var uxmlTraits = capture.Value;
if (IsValidClass(context, uxmlTraits.Class))
{
AddSource(context, capture.Value, GenerateUxmlTraits(context, capture.Value));
}
}
ReportDiagnostics(context, receiver.UxmlTraitsReceiver.Diagnostics);
ReportDiagnostics(context, receiver.UxmlFactoryReceiver.Diagnostics);
}
private static bool IsValidClass(GeneratorExecutionContext context, BaseTypeDeclarationSyntax @class)
{
if (@class.InheritsFromFullyQualifiedName(context, VisualElementFullName))
{
return true;
}
ReportClassDoesNotInheritFromVisualElementError(context, @class);
return false;
}
private static bool IsValidAttribute(GeneratorExecutionContext context, AttributeSyntax attribute)
{
return attribute.GetTypeNamespace(context) == AssemblyName.Name;
}
private static void AddSource(GeneratorExecutionContext context, BaseCapture capture, SourceText sourceText)
{
context.AddSource($"{capture.ClassName}.{capture.ClassTag}.g.cs", sourceText);
}
private static void ReportDiagnostics(GeneratorExecutionContext context, IEnumerable<Diagnostic> diagnostics)
{
foreach (var diagnostic in diagnostics)
{
context.ReportDiagnostic(diagnostic);
}
}
private static void ReportClassDoesNotInheritFromVisualElementError(GeneratorExecutionContext context,
BaseTypeDeclarationSyntax @class)
{
context.ReportDiagnostic(
ClassDoesNotInheritFromVisualElementError.CreateDiagnostic(@class.GetLocation(), @class.Identifier.Text));
}
}