Skip to content

Commit 52fc479

Browse files
committed
Add syntax widgets.
1 parent 3fab0a1 commit 52fc479

File tree

7 files changed

+467
-232
lines changed

7 files changed

+467
-232
lines changed
Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Reflection;
2-
using System.Text;
1+
using System.Text;
32
using Microsoft.CodeAnalysis.CSharp;
43
using Microsoft.CodeAnalysis.CSharp.Syntax;
54
using Microsoft.CodeAnalysis.Text;
@@ -8,56 +7,60 @@ namespace UnityUxmlGenerator;
87

98
internal sealed partial class UxmlGenerator
109
{
10+
private const string AttributeBaseType = "global::System.Attribute";
11+
1112
private const string UxmlElementClassName = "UxmlElementAttribute";
1213
private const string UxmlAttributeClassName = "UxmlAttributeAttribute";
1314

14-
private static readonly AssemblyName AssemblyName = typeof(UxmlGenerator).Assembly.GetName();
15-
1615
private static SourceText GenerateUxmlElementAttribute()
1716
{
18-
var baseList = SimpleBaseType(IdentifierName("global::System.Attribute"));
19-
20-
var @class = ClassDeclaration(UxmlElementClassName)
21-
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)))
22-
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(baseList)));
23-
24-
return GetCompilationUnit((TypeDeclarationSyntax) ProcessMemberDeclaration(@class), AssemblyName.Name)
25-
.GetText(Encoding.UTF8);
17+
return GenerateAttributeClass(UxmlElementClassName);
2618
}
2719

2820
private static SourceText GenerateUxmlAttributeAttribute()
2921
{
30-
var baseList = SimpleBaseType(IdentifierName("global::System.Attribute"));
31-
32-
var @class = ClassDeclaration(UxmlAttributeClassName)
33-
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)))
34-
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(baseList)));
35-
36-
var members = GetUxmlAttributeMembers();
22+
return GenerateAttributeClass(UxmlAttributeClassName, GetUxmlAttributeMembers());
23+
}
3724

38-
return GetCompilationUnit((TypeDeclarationSyntax) ProcessMemberDeclaration(@class), AssemblyName.Name, members)
25+
private static SourceText GenerateAttributeClass(string attributeClassIdentifier,
26+
MemberDeclarationSyntax[]? members = null)
27+
{
28+
return CompilationUnitWidget(
29+
members: NamespaceWidget(
30+
identifier: AssemblyName.Name,
31+
member: ClassWidget(
32+
identifier: attributeClassIdentifier,
33+
modifiers: new[] { SyntaxKind.InternalKeyword, SyntaxKind.SealedKeyword },
34+
baseType: SimpleBaseType(IdentifierName(AttributeBaseType)),
35+
members: members,
36+
addGeneratedCodeAttributes: true)),
37+
normalizeWhitespace: true)
3938
.GetText(Encoding.UTF8);
4039
}
4140

4241
private static MemberDeclarationSyntax[] GetUxmlAttributeMembers()
4342
{
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 };
43+
return new MemberDeclarationSyntax[]
44+
{
45+
ConstructorWidget(
46+
identifier: UxmlAttributeClassName,
47+
modifier: SyntaxKind.PublicKeyword,
48+
parameter: ParameterWidget(
49+
identifier: "defaultValue",
50+
type: NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))),
51+
addDefaultKeyword: true),
52+
body: ExpressionStatement(AssignmentWidget(
53+
left: IdentifierName("DefaultValue"),
54+
right: IdentifierName("defaultValue"))),
55+
addGeneratedCodeAttributes: true
56+
),
57+
PropertyWidget(
58+
identifier: "DefaultValue",
59+
type: NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))),
60+
modifier: SyntaxKind.PublicKeyword,
61+
accessor: SyntaxKind.GetAccessorDeclaration,
62+
addGeneratedCodeAttributes: true
63+
)
64+
};
6265
}
6366
}

src/UnityUxmlGenerator/UxmlGenerator.Execute.cs

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
using System.Text;
22
using Microsoft.CodeAnalysis.CSharp;
3-
using Microsoft.CodeAnalysis.CSharp.Syntax;
43
using Microsoft.CodeAnalysis.Text;
54
using UnityUxmlGenerator.Captures;
65

76
namespace UnityUxmlGenerator;
87

98
internal sealed partial class UxmlGenerator
109
{
11-
private static SourceText GenerateUxmlFactory(UxmlFactoryCapture capture)
12-
{
13-
var @class = ClassDeclaration(capture.ClassName).AddModifiers(Token(SyntaxKind.PartialKeyword));
10+
private const string FactoryBaseTypeIdentifier = "global::UnityEngine.UIElements.UxmlFactory<{0}, UxmlTraits>";
1411

15-
var factoryClass = GetFactoryClass(capture);
16-
17-
return GetCompilationUnit(@class, capture.ClassNamespace, factoryClass).GetText(Encoding.UTF8);
18-
}
19-
20-
private static MemberDeclarationSyntax GetFactoryClass(UxmlFactoryCapture capture)
12+
private static SourceText GenerateUxmlFactory(UxmlFactoryCapture capture)
2113
{
22-
var uxmlFactoryBaseList =
23-
SimpleBaseType(
24-
IdentifierName($"global::UnityEngine.UIElements.UxmlFactory<{capture.ClassName}, UxmlTraits>"));
25-
26-
return
27-
ClassDeclaration("UxmlFactory")
28-
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.NewKeyword)))
29-
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(uxmlFactoryBaseList)));
14+
return CompilationUnitWidget(
15+
members: NamespaceWidget(
16+
identifier: capture.ClassNamespace,
17+
member: ClassWidget(
18+
identifier: capture.ClassName,
19+
modifier: SyntaxKind.PartialKeyword,
20+
member: ClassWidget(
21+
identifier: "UxmlFactory",
22+
modifiers: new[] { SyntaxKind.PublicKeyword, SyntaxKind.NewKeyword },
23+
baseType: SimpleBaseType(IdentifierName(string.Format(FactoryBaseTypeIdentifier, capture.ClassName))),
24+
addGeneratedCodeAttributes: true
25+
))),
26+
normalizeWhitespace: true)
27+
.GetText(Encoding.UTF8);
3028
}
3129
}

0 commit comments

Comments
 (0)