@@ -7,10 +7,10 @@ namespace SpocR.Extensions
7
7
{
8
8
internal static class CompilationUnitSyntaxExtensions
9
9
{
10
- internal static CompilationUnitSyntax ReplaceUsings ( this CompilationUnitSyntax root , Func < string , string > replacer )
10
+ internal static CompilationUnitSyntax ReplaceUsings ( this CompilationUnitSyntax root , Func < string , string > replacer )
11
11
{
12
12
var newUsings = new SyntaxList < UsingDirectiveSyntax > ( ) ;
13
- foreach ( var u in root . Usings )
13
+ foreach ( var u in root . Usings )
14
14
{
15
15
var uValue = replacer . Invoke ( u . Name . ToString ( ) ) ;
16
16
var usingName = SyntaxFactory . ParseName ( uValue ) ;
@@ -19,35 +19,97 @@ internal static CompilationUnitSyntax ReplaceUsings(this CompilationUnitSyntax r
19
19
return root . WithUsings ( newUsings ) ;
20
20
}
21
21
22
- internal static CompilationUnitSyntax ReplaceNamespace ( this CompilationUnitSyntax root , Func < string , string > replacer )
22
+ internal static CompilationUnitSyntax ReplaceNamespace ( this CompilationUnitSyntax root , Func < string , string > replacer )
23
23
{
24
24
var nsNode = ( NamespaceDeclarationSyntax ) root . Members [ 0 ] ;
25
25
var nsValue = replacer . Invoke ( nsNode . Name . ToString ( ) ) ;
26
26
var fullSchemaName = SyntaxFactory . ParseName ( $ "{ nsValue } { Environment . NewLine } ") ;
27
27
return root . ReplaceNode ( nsNode , nsNode . WithName ( fullSchemaName ) ) ;
28
28
}
29
29
30
- internal static CompilationUnitSyntax ReplaceClassName ( this CompilationUnitSyntax root , Func < string , string > replacer , Func < NamespaceDeclarationSyntax , ClassDeclarationSyntax > selector = null )
30
+ internal static CompilationUnitSyntax ReplaceClassName ( this CompilationUnitSyntax root , Func < string , string > replacer , Func < NamespaceDeclarationSyntax , ClassDeclarationSyntax > selector = null )
31
31
{
32
32
var nsNode = ( NamespaceDeclarationSyntax ) root . Members [ 0 ] ;
33
33
var classNode = selector != null
34
34
? selector . Invoke ( nsNode )
35
35
: ( ClassDeclarationSyntax ) nsNode . Members [ 0 ] ;
36
- var cnValue = replacer . Invoke ( classNode . Identifier . ValueText ) ;
36
+ var cnValue = replacer . Invoke ( classNode . Identifier . ValueText ) ;
37
37
var classIdentifier = SyntaxFactory . ParseToken ( $ "{ cnValue } { Environment . NewLine } ") ;
38
38
return root . ReplaceNode ( classNode , classNode . WithIdentifier ( classIdentifier ) ) ;
39
39
}
40
40
41
- internal static CompilationUnitSyntax AddProperty ( this CompilationUnitSyntax root , ClassDeclarationSyntax classDeclaration , PropertyDeclarationSyntax propertyDeclaration )
41
+ internal static CompilationUnitSyntax AddProperty ( this CompilationUnitSyntax root , ref ClassDeclarationSyntax classDeclaration , PropertyDeclarationSyntax propertyDeclaration )
42
42
{
43
43
var newClass = classDeclaration . AddMembers ( propertyDeclaration ) ;
44
- return root . ReplaceNode ( classDeclaration , newClass ) ;
44
+ root = root . ReplaceNode ( classDeclaration , newClass ) ;
45
+ classDeclaration = newClass ;
46
+ return root ;
45
47
}
46
48
47
- internal static CompilationUnitSyntax AddMethod ( this CompilationUnitSyntax root , ClassDeclarationSyntax classDeclaration , MethodDeclarationSyntax methodDeclaration )
49
+ internal static CompilationUnitSyntax AddMethod ( this CompilationUnitSyntax root , ref ClassDeclarationSyntax classDeclaration , MethodDeclarationSyntax methodDeclaration )
48
50
{
49
51
var newClass = classDeclaration . AddMembers ( methodDeclaration ) ;
50
- return root . ReplaceNode ( classDeclaration , newClass ) ;
52
+ root = root . ReplaceNode ( classDeclaration , newClass ) ;
53
+ classDeclaration = newClass ;
54
+ return root ;
55
+ }
56
+
57
+ internal static CompilationUnitSyntax AddConstructor ( this CompilationUnitSyntax root , ref ClassDeclarationSyntax classDeclaration , ConstructorDeclarationSyntax constructorDeclaration )
58
+ {
59
+ var newClass = classDeclaration . AddMembers ( constructorDeclaration ) ;
60
+ root = root . ReplaceNode ( classDeclaration , newClass ) ;
61
+ classDeclaration = newClass ;
62
+ return root ;
63
+ }
64
+
65
+ internal static CompilationUnitSyntax AddCustomAttribute ( this CompilationUnitSyntax root , ref MethodDeclarationSyntax methodDeclaration , string name , AttributeArgumentListSyntax arguments = default )
66
+ {
67
+ var attributes = methodDeclaration . AttributeLists . Add (
68
+ SyntaxFactory . AttributeList ( SyntaxFactory . SingletonSeparatedList < AttributeSyntax > (
69
+ SyntaxFactory . Attribute ( SyntaxFactory . IdentifierName ( name ) )
70
+ . WithArgumentList ( arguments )
71
+ ) ) . NormalizeWhitespace ( ) ) ;
72
+
73
+ var newMethodDeclaration = methodDeclaration . WithAttributeLists ( attributes ) ;
74
+ root = root . ReplaceNode (
75
+ methodDeclaration ,
76
+ newMethodDeclaration
77
+ ) ;
78
+
79
+ methodDeclaration = newMethodDeclaration ;
80
+
81
+ return root ;
82
+ }
83
+
84
+ internal static CompilationUnitSyntax AddCustomAttribute ( this CompilationUnitSyntax root , ref ConstructorDeclarationSyntax constructorDeclaration , string name , AttributeArgumentListSyntax arguments = default )
85
+ {
86
+ var attributes = constructorDeclaration . AttributeLists . Add (
87
+ SyntaxFactory . AttributeList ( SyntaxFactory . SingletonSeparatedList < AttributeSyntax > (
88
+ SyntaxFactory . Attribute ( SyntaxFactory . IdentifierName ( name ) )
89
+ . WithArgumentList ( arguments )
90
+ ) ) . NormalizeWhitespace ( ) ) ;
91
+
92
+ var newConstructorDeclaration = constructorDeclaration . WithAttributeLists ( attributes ) ;
93
+ root = root . ReplaceNode (
94
+ constructorDeclaration ,
95
+ newConstructorDeclaration
96
+ ) ;
97
+
98
+ constructorDeclaration = newConstructorDeclaration ;
99
+
100
+ return root ;
101
+ }
102
+
103
+ internal static CompilationUnitSyntax AddObsoleteAttribute ( this CompilationUnitSyntax root , ref MethodDeclarationSyntax methodDeclaration , string message = null )
104
+ {
105
+ var arguments = ! string . IsNullOrWhiteSpace ( message ) ? SyntaxFactory . ParseAttributeArgumentList ( $ "(\" { message } \" )") : default ;
106
+ return root . AddCustomAttribute ( ref methodDeclaration , "Obsolete" , arguments ) ;
107
+ }
108
+
109
+ internal static CompilationUnitSyntax AddObsoleteAttribute ( this CompilationUnitSyntax root , ref ConstructorDeclarationSyntax constructorDeclaration , string message = null )
110
+ {
111
+ var arguments = ! string . IsNullOrWhiteSpace ( message ) ? SyntaxFactory . ParseAttributeArgumentList ( $ "(\" { message } \" )") : default ;
112
+ return root . AddCustomAttribute ( ref constructorDeclaration , "Obsolete" , arguments ) ;
51
113
}
52
114
}
53
115
}
0 commit comments