Skip to content

Commit 607161d

Browse files
committed
Add namespaceIdentifier to CompilationUnitWidget. Minor changes.
1 parent 52fc479 commit 607161d

File tree

5 files changed

+45
-42
lines changed

5 files changed

+45
-42
lines changed

src/UnityUxmlGenerator/UxmlGenerator.Attributes.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ private static SourceText GenerateAttributeClass(string attributeClassIdentifier
2626
MemberDeclarationSyntax[]? members = null)
2727
{
2828
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)),
29+
namespaceIdentifier: AssemblyName.Name,
30+
members: ClassWidget(
31+
identifier: attributeClassIdentifier,
32+
modifiers: new[] { SyntaxKind.InternalKeyword, SyntaxKind.SealedKeyword },
33+
baseType: SimpleBaseType(IdentifierName(AttributeBaseType)),
34+
members: members,
35+
addGeneratedCodeAttributes: true),
3736
normalizeWhitespace: true)
3837
.GetText(Encoding.UTF8);
3938
}
@@ -49,7 +48,7 @@ private static MemberDeclarationSyntax[] GetUxmlAttributeMembers()
4948
identifier: "defaultValue",
5049
type: NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))),
5150
addDefaultKeyword: true),
52-
body: ExpressionStatement(AssignmentWidget(
51+
bodyStatements: ExpressionStatement(AssignmentWidget(
5352
left: IdentifierName("DefaultValue"),
5453
right: IdentifierName("defaultValue"))),
5554
addGeneratedCodeAttributes: true
@@ -58,7 +57,7 @@ private static MemberDeclarationSyntax[] GetUxmlAttributeMembers()
5857
identifier: "DefaultValue",
5958
type: NullableType(PredefinedType(Token(SyntaxKind.ObjectKeyword))),
6059
modifier: SyntaxKind.PublicKeyword,
61-
accessor: SyntaxKind.GetAccessorDeclaration,
60+
accessors: SyntaxKind.GetAccessorDeclaration,
6261
addGeneratedCodeAttributes: true
6362
)
6463
};

src/UnityUxmlGenerator/UxmlGenerator.Factory.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ internal sealed partial class UxmlGenerator
1212
private static SourceText GenerateUxmlFactory(UxmlFactoryCapture capture)
1313
{
1414
return CompilationUnitWidget(
15-
members: NamespaceWidget(
16-
identifier: capture.ClassNamespace,
15+
namespaceIdentifier: capture.ClassNamespace,
16+
members: ClassWidget(
17+
identifier: capture.ClassName,
18+
modifier: SyntaxKind.PartialKeyword,
1719
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-
))),
20+
identifier: "UxmlFactory",
21+
modifiers: new[] { SyntaxKind.PublicKeyword, SyntaxKind.NewKeyword },
22+
baseType: SimpleBaseType(IdentifierName(string.Format(FactoryBaseTypeIdentifier, capture.ClassName))),
23+
addGeneratedCodeAttributes: true
24+
)),
2625
normalizeWhitespace: true)
2726
.GetText(Encoding.UTF8);
2827
}

src/UnityUxmlGenerator/UxmlGenerator.Traits.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ internal sealed partial class UxmlGenerator
1818
private static SourceText GenerateUxmlTraits(GeneratorExecutionContext context, UxmlTraitsCapture capture)
1919
{
2020
return CompilationUnitWidget(
21-
members: NamespaceWidget(
22-
identifier: capture.ClassNamespace,
21+
namespaceIdentifier: capture.ClassNamespace,
22+
members: ClassWidget(
23+
identifier: capture.ClassName,
24+
modifier: SyntaxKind.PartialKeyword,
2325
member: ClassWidget(
24-
identifier: capture.ClassName,
25-
modifier: SyntaxKind.PartialKeyword,
26-
member: ClassWidget(
27-
identifier: "UxmlTraits",
28-
modifiers: new[] { SyntaxKind.PublicKeyword, SyntaxKind.NewKeyword },
29-
baseType: SimpleBaseType(IdentifierName($"{GetBaseClassName(context, capture)}.UxmlTraits")),
30-
members: GetTraitsClassMembers(context, capture),
31-
addGeneratedCodeAttributes: true
32-
))),
26+
identifier: "UxmlTraits",
27+
modifiers: new[] { SyntaxKind.PublicKeyword, SyntaxKind.NewKeyword },
28+
baseType: SimpleBaseType(IdentifierName($"{GetBaseClassName(context, capture)}.UxmlTraits")),
29+
members: GetTraitsClassMembers(context, capture),
30+
addGeneratedCodeAttributes: true
31+
)),
3332
normalizeWhitespace: true)
3433
.GetText(Encoding.UTF8);
3534
}
@@ -100,7 +99,7 @@ private static MemberDeclarationSyntax[] GetTraitsClassMembers(GeneratorExecutio
10099
identifier: "context",
101100
type: IdentifierName(string.Format(UnityUiElementsFullName, "CreationContext"))),
102101
},
103-
body: initMethodBody.ToArray(),
102+
bodyStatements: initMethodBody.ToArray(),
104103
addGeneratedCodeAttributes: true
105104
);
106105

src/UnityUxmlGenerator/UxmlGenerator.Widgets.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ namespace UnityUxmlGenerator;
77
internal sealed partial class UxmlGenerator
88
{
99
private static CompilationUnitSyntax CompilationUnitWidget(
10+
string? namespaceIdentifier = null,
1011
bool addGeneratedCodeLeadingTrivia = true,
1112
bool normalizeWhitespace = true,
1213
params MemberDeclarationSyntax[]? members)
1314
{
1415
var compilationUnit = CompilationUnit();
1516

16-
if (members is not null)
17+
if (string.IsNullOrWhiteSpace(namespaceIdentifier) == false)
18+
{
19+
compilationUnit =
20+
compilationUnit.AddMembers(NamespaceWidget(identifier: namespaceIdentifier!, members: members));
21+
}
22+
else if (members is not null)
1723
{
1824
compilationUnit = compilationUnit.AddMembers(members);
1925
}
@@ -90,7 +96,7 @@ private static ConstructorDeclarationSyntax ConstructorWidget(
9096
ParameterSyntax? parameter = null,
9197
ParameterSyntax[]? parameters = null,
9298
bool addGeneratedCodeAttributes = false,
93-
params StatementSyntax[]? body)
99+
params StatementSyntax[]? bodyStatements)
94100
{
95101
var constructorDeclaration = ConstructorDeclaration(Identifier(identifier));
96102

@@ -104,9 +110,9 @@ private static ConstructorDeclarationSyntax ConstructorWidget(
104110
constructorDeclaration = constructorDeclaration.AddParameterListParameters(parameters);
105111
}
106112

107-
if (body is not null)
113+
if (bodyStatements is not null)
108114
{
109-
constructorDeclaration = constructorDeclaration.WithBody(Block(body));
115+
constructorDeclaration = constructorDeclaration.WithBody(Block(bodyStatements));
110116
}
111117

112118
return BaseWidgetDecoration(constructorDeclaration, modifier, modifiers, addGeneratedCodeAttributes);
@@ -137,14 +143,14 @@ private static PropertyDeclarationSyntax PropertyWidget(
137143
SyntaxKind? modifier = null,
138144
SyntaxKind[]? modifiers = null,
139145
bool addGeneratedCodeAttributes = false,
140-
params SyntaxKind[]? accessor)
146+
params SyntaxKind[]? accessors)
141147
{
142148
var propertyDeclaration = PropertyDeclaration(type, Identifier(identifier));
143149

144-
if (accessor is not null)
150+
if (accessors is not null)
145151
{
146152
propertyDeclaration =
147-
propertyDeclaration.AddAccessorListAccessors(accessor.Select(AccessorWidget).ToArray());
153+
propertyDeclaration.AddAccessorListAccessors(accessors.Select(AccessorWidget).ToArray());
148154
}
149155

150156
return BaseWidgetDecoration(propertyDeclaration, modifier, modifiers, addGeneratedCodeAttributes);
@@ -158,7 +164,7 @@ private static MethodDeclarationSyntax MethodWidget(
158164
ParameterSyntax? parameter = null,
159165
ParameterSyntax[]? parameters = null,
160166
bool addGeneratedCodeAttributes = false,
161-
params StatementSyntax[]? body)
167+
params StatementSyntax[]? bodyStatements)
162168
{
163169
var methodDeclaration = MethodDeclaration(type, Identifier(identifier));
164170

@@ -172,9 +178,9 @@ private static MethodDeclarationSyntax MethodWidget(
172178
methodDeclaration = methodDeclaration.AddParameterListParameters(parameters);
173179
}
174180

175-
if (body is not null)
181+
if (bodyStatements is not null)
176182
{
177-
methodDeclaration = methodDeclaration.WithBody(Block(body));
183+
methodDeclaration = methodDeclaration.WithBody(Block(bodyStatements));
178184
}
179185

180186
return BaseWidgetDecoration(methodDeclaration, modifier, modifiers, addGeneratedCodeAttributes);

0 commit comments

Comments
 (0)