1- using System . Reflection ;
2- using System . Text ;
1+ using System . Text ;
32using Microsoft . CodeAnalysis . CSharp ;
43using Microsoft . CodeAnalysis . CSharp . Syntax ;
54using Microsoft . CodeAnalysis . Text ;
@@ -8,56 +7,60 @@ namespace UnityUxmlGenerator;
87
98internal 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}
0 commit comments