Skip to content

Commit 750521d

Browse files
committed
Minor changes.
1 parent 817a0ea commit 750521d

File tree

6 files changed

+94
-72
lines changed

6 files changed

+94
-72
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.CodeAnalysis.CSharp.Syntax;
2+
using UnityUxmlGenerator.Extensions;
3+
4+
namespace UnityUxmlGenerator.Captures;
5+
6+
internal abstract class BaseCapture
7+
{
8+
protected BaseCapture(ClassDeclarationSyntax @class)
9+
{
10+
Class = @class;
11+
ClassName = @class.Identifier.Text;
12+
ClassNamespace = @class.GetParent<NamespaceDeclarationSyntax>()!.Name.ToString();
13+
}
14+
15+
public string ClassName { get; }
16+
public string ClassNamespace { get; }
17+
public abstract string ClassTag { get; }
18+
19+
public ClassDeclarationSyntax Class { get; }
20+
}
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
2-
using UnityUxmlGenerator.Extensions;
32

43
namespace UnityUxmlGenerator.Captures;
54

6-
internal sealed class UxmlFactoryCapture
5+
internal sealed class UxmlFactoryCapture : BaseCapture
76
{
8-
public UxmlFactoryCapture(ClassDeclarationSyntax @class)
7+
public UxmlFactoryCapture(ClassDeclarationSyntax @class) : base(@class)
98
{
10-
Class = @class;
11-
ClassName = @class.Identifier.Text;
12-
ClassNamespace = @class.GetParent<NamespaceDeclarationSyntax>()!.Name.ToString();
139
}
1410

15-
public string ClassName { get; }
16-
public string ClassNamespace { get; }
17-
18-
public ClassDeclarationSyntax Class { get; }
11+
public override string ClassTag => "UxmlFactory";
1912
}

src/UnityUxmlGenerator/Captures/UxmlTraitsCapture.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
2-
using UnityUxmlGenerator.Extensions;
32

43
namespace UnityUxmlGenerator.Captures;
54

6-
internal sealed class UxmlTraitsCapture
5+
internal sealed class UxmlTraitsCapture : BaseCapture
76
{
8-
public UxmlTraitsCapture(ClassDeclarationSyntax @class, TypeSyntax baseClassType)
7+
public UxmlTraitsCapture(ClassDeclarationSyntax @class, TypeSyntax baseClassType) : base(@class)
98
{
10-
Class = @class;
11-
ClassName = @class.Identifier.Text;
12-
ClassNamespace = @class.GetParent<NamespaceDeclarationSyntax>()!.Name.ToString();
13-
149
BaseClassType = baseClassType;
1510
Properties = new List<(PropertyDeclarationSyntax property, string? DefaultValue)>();
1611
}
1712

18-
public string ClassName { get; }
19-
public string ClassNamespace { get; }
13+
public override string ClassTag => "UxmlTraits";
2014

2115
public TypeSyntax BaseClassType { get; }
22-
public ClassDeclarationSyntax Class { get; }
23-
2416
public List<(PropertyDeclarationSyntax property, string? DefaultValue)> Properties { get; }
2517

2618
public string GetBaseClassName(out TypeSyntax? genericTypeSyntax)
Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
11
using System.Reflection;
2+
using System.Text;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Microsoft.CodeAnalysis.Text;
26

37
namespace UnityUxmlGenerator;
48

59
internal sealed partial class UxmlGenerator
610
{
11+
private const string UxmlElementClassName = "UxmlElementAttribute";
12+
private const string UxmlAttributeClassName = "UxmlAttributeAttribute";
13+
714
private static readonly AssemblyName AssemblyName = typeof(UxmlGenerator).Assembly.GetName();
815

9-
private static readonly string GeneratedCodeAttribute =
10-
$"""[global::System.CodeDom.Compiler.GeneratedCodeAttribute("{AssemblyName.Name}", "{AssemblyName.Version}")]""";
16+
private static SourceText GenerateUxmlElementAttribute()
17+
{
18+
var baseList = SimpleBaseType(IdentifierName("global::System.Attribute"));
1119

12-
private static readonly string UxmlElementAttribute = $$"""
13-
// <auto-generated/>
14-
#pragma warning disable
20+
var @class = ClassDeclaration(UxmlElementClassName)
21+
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)))
22+
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(baseList)));
1523

16-
#nullable enable
24+
return GetCompilationUnit((TypeDeclarationSyntax) ProcessMemberDeclaration(@class), AssemblyName.Name)
25+
.GetText(Encoding.UTF8);
26+
}
1727

18-
namespace {{AssemblyName.Name}}
19-
{
20-
{{GeneratedCodeAttribute}}
21-
[global::System.AttributeUsageAttribute(global::System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
22-
internal sealed class UxmlElementAttribute : global::System.Attribute
28+
private static SourceText GenerateUxmlAttributeAttribute()
2329
{
24-
}
25-
}
26-
""";
30+
var baseList = SimpleBaseType(IdentifierName("global::System.Attribute"));
2731

28-
private static readonly string UxmlAttributeAttribute = $$"""
29-
// <auto-generated/>
30-
#pragma warning disable
32+
var @class = ClassDeclaration(UxmlAttributeClassName)
33+
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)))
34+
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(baseList)));
3135

32-
#nullable enable
36+
var members = GetUxmlAttributeMembers();
3337

34-
namespace {{AssemblyName.Name}}
35-
{
36-
{{GeneratedCodeAttribute}}
37-
[global::System.AttributeUsageAttribute(global::System.AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
38-
internal sealed class UxmlAttributeAttribute : global::System.Attribute
39-
{
40-
public UxmlAttributeAttribute(object? defaultValue = default)
41-
{
42-
DefaultValue = defaultValue;
43-
}
38+
return GetCompilationUnit((TypeDeclarationSyntax) ProcessMemberDeclaration(@class), AssemblyName.Name, members)
39+
.GetText(Encoding.UTF8);
40+
}
4441

45-
public object? DefaultValue { get; }
42+
private static MemberDeclarationSyntax[] GetUxmlAttributeMembers()
43+
{
44+
var classConstructor =
45+
ConstructorDeclaration(Identifier(UxmlAttributeClassName))
46+
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
47+
.WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("defaultValue"))
48+
.WithType(NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))))
49+
.WithDefault(EqualsValueClause(LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))))))
50+
.WithBody(Block(SingletonList<StatementSyntax>(ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
51+
IdentifierName("DefaultValue"),
52+
IdentifierName("defaultValue"))))));
53+
54+
var defaultProperty =
55+
PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))), Identifier("DefaultValue"))
56+
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
57+
.WithAccessorList(AccessorList(SingletonList(
58+
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
59+
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)))));
60+
61+
return new MemberDeclarationSyntax[] { classConstructor, defaultProperty };
4662
}
47-
}
48-
""";
4963
}

src/UnityUxmlGenerator/UxmlGenerator.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using Microsoft.CodeAnalysis.Text;
34
using UnityUxmlGenerator.Captures;
45
using UnityUxmlGenerator.Diagnostics;
56
using UnityUxmlGenerator.Extensions;
@@ -19,8 +20,8 @@ public void Initialize(GeneratorInitializationContext context)
1920

2021
public void Execute(GeneratorExecutionContext context)
2122
{
22-
context.AddSource($"{nameof(UxmlElementAttribute)}.g.cs", UxmlElementAttribute);
23-
context.AddSource($"{nameof(UxmlAttributeAttribute)}.g.cs", UxmlAttributeAttribute);
23+
context.AddSource($"{nameof(UxmlElementClassName)}.g.cs", GenerateUxmlElementAttribute());
24+
context.AddSource($"{nameof(UxmlAttributeClassName)}.g.cs", GenerateUxmlAttributeAttribute());
2425

2526
if (context.SyntaxReceiver is not VisualElementReceiver receiver)
2627
{
@@ -29,31 +30,33 @@ public void Execute(GeneratorExecutionContext context)
2930

3031
foreach (var uxmlElement in receiver.UxmlFactoryReceiver.Captures)
3132
{
32-
if (uxmlElement.Class.InheritsFromFullyQualifiedName(context, VisualElementFullName))
33-
{
34-
context.AddSource($"{uxmlElement.ClassName}.UxmlFactory.g.cs", GenerateUxmlFactory(uxmlElement));
35-
}
36-
else
37-
{
38-
ReportClassDoesNotInheritFromVisualElementError(context, uxmlElement.Class);
39-
}
33+
AddSource(context, uxmlElement, GenerateUxmlFactory(uxmlElement));
4034
}
4135

4236
foreach (var capture in receiver.UxmlTraitsReceiver.Captures)
4337
{
44-
var traitsCapture = capture.Value;
38+
AddSource(context, capture.Value, GenerateUxmlTraits(context, capture.Value));
39+
}
40+
41+
ReportDiagnostics(context, receiver.UxmlTraitsReceiver.Diagnostics);
42+
ReportDiagnostics(context, receiver.UxmlFactoryReceiver.Diagnostics);
43+
}
4544

46-
if (traitsCapture.Class.InheritsFromFullyQualifiedName(context, VisualElementFullName))
47-
{
48-
context.AddSource($"{traitsCapture.ClassName}.UxmlTraits.g.cs", GenerateUxmlTraits(context, traitsCapture));
49-
}
50-
else
51-
{
52-
ReportClassDoesNotInheritFromVisualElementError(context, traitsCapture.Class);
53-
}
45+
private static void AddSource(GeneratorExecutionContext context, BaseCapture capture, SourceText sourceText)
46+
{
47+
if (capture.Class.InheritsFromFullyQualifiedName(context, VisualElementFullName))
48+
{
49+
context.AddSource($"{capture.ClassName}.{capture.ClassTag}.g.cs", sourceText);
5450
}
51+
else
52+
{
53+
ReportClassDoesNotInheritFromVisualElementError(context, capture.Class);
54+
}
55+
}
5556

56-
foreach (var diagnostic in receiver.UxmlTraitsReceiver.Diagnostics)
57+
private static void ReportDiagnostics(GeneratorExecutionContext context, IEnumerable<Diagnostic> diagnostics)
58+
{
59+
foreach (var diagnostic in diagnostics)
5760
{
5861
context.ReportDiagnostic(diagnostic);
5962
}

0 commit comments

Comments
 (0)