1
- using System . Reflection ;
2
- using System . Text ;
1
+ using System . Text ;
3
2
using Microsoft . CodeAnalysis . CSharp ;
4
3
using Microsoft . CodeAnalysis . CSharp . Syntax ;
5
4
using Microsoft . CodeAnalysis . Text ;
@@ -8,56 +7,60 @@ namespace UnityUxmlGenerator;
8
7
9
8
internal sealed partial class UxmlGenerator
10
9
{
10
+ private const string AttributeBaseType = "global::System.Attribute" ;
11
+
11
12
private const string UxmlElementClassName = "UxmlElementAttribute" ;
12
13
private const string UxmlAttributeClassName = "UxmlAttributeAttribute" ;
13
14
14
- private static readonly AssemblyName AssemblyName = typeof ( UxmlGenerator ) . Assembly . GetName ( ) ;
15
-
16
15
private static SourceText GenerateUxmlElementAttribute ( )
17
16
{
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 ) ;
26
18
}
27
19
28
20
private static SourceText GenerateUxmlAttributeAttribute ( )
29
21
{
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
+ }
37
24
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 )
39
38
. GetText ( Encoding . UTF8 ) ;
40
39
}
41
40
42
41
private static MemberDeclarationSyntax [ ] GetUxmlAttributeMembers ( )
43
42
{
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
+ } ;
62
65
}
63
66
}
0 commit comments