1
+ using System . Text ;
2
+ using Microsoft . CodeAnalysis ;
3
+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
4
+
5
+ namespace UnityUxmlGenerator . Extensions ;
6
+
7
+ public static class StringBuilderExtensions
8
+ {
9
+ public static void AppendGenericString ( this StringBuilder stringBuilder , GeneratorExecutionContext context ,
10
+ IEnumerable < TypeSyntax > genericTypeArguments )
11
+ {
12
+ var isFirstArgument = true ;
13
+
14
+ stringBuilder . Append ( '<' ) ;
15
+
16
+ foreach ( var genericClassTypeSyntax in genericTypeArguments )
17
+ {
18
+ if ( isFirstArgument == false )
19
+ {
20
+ stringBuilder . Append ( ", " ) ;
21
+ }
22
+
23
+ isFirstArgument = false ;
24
+
25
+ switch ( genericClassTypeSyntax )
26
+ {
27
+ case PredefinedTypeSyntax predefinedTypeSyntax :
28
+ AppendPredefinedTypeSyntax ( stringBuilder , predefinedTypeSyntax ) ;
29
+ break ;
30
+
31
+ case IdentifierNameSyntax identifierNameSyntax :
32
+ AppendIdentifierNameSyntax ( stringBuilder , context , identifierNameSyntax ) ;
33
+ break ;
34
+
35
+ case GenericNameSyntax genericTypeSyntax :
36
+ AppendGenericTypeSyntax ( stringBuilder , context , genericTypeSyntax ) ;
37
+ break ;
38
+ }
39
+ }
40
+
41
+ stringBuilder . Append ( '>' ) ;
42
+ }
43
+
44
+ private static void AppendPredefinedTypeSyntax ( StringBuilder stringBuilder ,
45
+ PredefinedTypeSyntax predefinedTypeSyntax )
46
+ {
47
+ stringBuilder . Append ( predefinedTypeSyntax . Keyword . Text ) ;
48
+ }
49
+
50
+ private static void AppendIdentifierNameSyntax ( StringBuilder stringBuilder , GeneratorExecutionContext context ,
51
+ IdentifierNameSyntax identifierNameSyntax )
52
+ {
53
+ var genericClassName = identifierNameSyntax . Identifier . Text ;
54
+ var genericClassNamespace = identifierNameSyntax . GetTypeNamespace ( context ) ;
55
+
56
+ stringBuilder . Append ( "global::" ) ;
57
+ stringBuilder . Append ( genericClassNamespace ) ;
58
+ stringBuilder . Append ( '.' ) ;
59
+ stringBuilder . Append ( genericClassName ) ;
60
+ }
61
+
62
+ private static void AppendGenericTypeSyntax ( StringBuilder stringBuilder , GeneratorExecutionContext context ,
63
+ GenericNameSyntax genericTypeSyntax )
64
+ {
65
+ var genericClassName = genericTypeSyntax . Identifier . Text ;
66
+ var genericClassNamespace = genericTypeSyntax . GetTypeNamespace ( context ) ;
67
+
68
+ if ( string . IsNullOrWhiteSpace ( genericClassNamespace ) == false )
69
+ {
70
+ stringBuilder . Append ( "global::" ) ;
71
+ stringBuilder . Append ( genericClassNamespace ) ;
72
+ stringBuilder . Append ( '.' ) ;
73
+ }
74
+
75
+ stringBuilder . Append ( genericClassName ) ;
76
+
77
+ AppendGenericString ( stringBuilder , context , genericTypeSyntax . TypeArgumentList . Arguments ) ;
78
+ }
79
+ }
0 commit comments