@@ -18,54 +18,54 @@ Action<string, SourceText> addSource
18
18
19
19
var attributeText = $$ """
20
20
21
- #pragma warning disable
22
- namespace {{ model . RefitInternalNamespace }}
23
- {
24
- [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
25
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
26
- [global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
27
- sealed class PreserveAttribute : global::System.Attribute
28
- {
29
- //
30
- // Fields
31
- //
32
- public bool AllMembers;
33
-
34
- public bool Conditional;
35
- }
36
- }
37
- #pragma warning restore
38
-
39
- """ ;
21
+ #pragma warning disable
22
+ namespace {{ model . RefitInternalNamespace }}
23
+ {
24
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
25
+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
26
+ [global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
27
+ sealed class PreserveAttribute : global::System.Attribute
28
+ {
29
+ //
30
+ // Fields
31
+ //
32
+ public bool AllMembers;
33
+
34
+ public bool Conditional;
35
+ }
36
+ }
37
+ #pragma warning restore
38
+
39
+ """ ;
40
40
// add the attribute text
41
41
addSource ( "PreserveAttribute.g.cs" , SourceText . From ( attributeText , Encoding . UTF8 ) ) ;
42
42
43
43
var generatedClassText = $$ """
44
44
45
- #pragma warning disable
46
- namespace Refit.Implementation
47
- {
48
-
49
- /// <inheritdoc />
50
- [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
51
- [global::System.Diagnostics.DebuggerNonUserCode]
52
- [{{ model . PreserveAttributeDisplayName }} ]
53
- [global::System.Reflection.Obfuscation(Exclude=true)]
54
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
55
- internal static partial class Generated
56
- {
57
- #if NET5_0_OR_GREATER
58
- [System.Runtime.CompilerServices.ModuleInitializer]
59
- [System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::Refit.Implementation.Generated))]
60
- public static void Initialize()
61
- {
62
- }
63
- #endif
64
- }
65
- }
66
- #pragma warning restore
67
-
68
- """ ;
45
+ #pragma warning disable
46
+ namespace Refit.Implementation
47
+ {
48
+
49
+ /// <inheritdoc />
50
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
51
+ [global::System.Diagnostics.DebuggerNonUserCode]
52
+ [{{ model . PreserveAttributeDisplayName }} ]
53
+ [global::System.Reflection.Obfuscation(Exclude=true)]
54
+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
55
+ internal static partial class Generated
56
+ {
57
+ #if NET5_0_OR_GREATER
58
+ [System.Runtime.CompilerServices.ModuleInitializer]
59
+ [System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::Refit.Implementation.Generated))]
60
+ public static void Initialize()
61
+ {
62
+ }
63
+ #endif
64
+ }
65
+ }
66
+ #pragma warning restore
67
+
68
+ """ ;
69
69
addSource ( "Generated.g.cs" , SourceText . From ( generatedClassText , Encoding . UTF8 ) ) ;
70
70
}
71
71
@@ -76,39 +76,53 @@ public static SourceText EmitInterface(InterfaceModel model)
76
76
// if nullability is supported emit the nullable directive
77
77
if ( model . Nullability != Nullability . None )
78
78
{
79
- source . WriteLine ( "#nullable " + ( model . Nullability == Nullability . Enabled ? "enable" : "disable" ) ) ;
79
+ source . WriteLine (
80
+ "#nullable " + ( model . Nullability == Nullability . Enabled ? "enable" : "disable" )
81
+ ) ;
80
82
}
81
83
82
84
source . WriteLine (
83
- $@ "#pragma warning disable
84
- namespace Refit.Implementation
85
- {{
86
-
87
- partial class Generated
88
- {{
89
-
90
- /// <inheritdoc />
91
- [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
92
- [global::System.Diagnostics.DebuggerNonUserCode]
93
- [{ model . PreserveAttributeDisplayName } ]
94
- [global::System.Reflection.Obfuscation(Exclude=true)]
95
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
96
- partial class { model . Ns } { model . ClassDeclaration }
97
- : { model . InterfaceDisplayName } { GenerateConstraints ( model . Constraints , false ) }
98
-
99
- {{
100
- /// <inheritdoc />
101
- public global::System.Net.Http.HttpClient Client {{ get; }}
102
- readonly global::Refit.IRequestBuilder requestBuilder;
103
-
104
- /// <inheritdoc />
105
- public { model . Ns } { model . ClassSuffix } (global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
106
- {{
107
- Client = client;
108
- this.requestBuilder = requestBuilder;
109
- }}"
85
+ $$ """
86
+ #pragma warning disable
87
+ namespace Refit.Implementation
88
+ {
89
+
90
+ partial class Generated
91
+ {
92
+
93
+ /// <inheritdoc />
94
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
95
+ [global::System.Diagnostics.DebuggerNonUserCode]
96
+ [{{ model . PreserveAttributeDisplayName }} ]
97
+ [global::System.Reflection.Obfuscation(Exclude=true)]
98
+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
99
+ partial class {{ model . Ns }} {{ model . ClassDeclaration }}
100
+ : {{ model . InterfaceDisplayName }}
101
+ """
102
+ ) ;
103
+
104
+ source . Indentation += 2 ;
105
+ GenerateConstraints ( source , model . Constraints , false ) ;
106
+ source . Indentation -- ;
107
+
108
+ source . WriteLine (
109
+ $$ """
110
+ {
111
+ /// <inheritdoc />
112
+ public global::System.Net.Http.HttpClient Client { get; }
113
+ readonly global::Refit.IRequestBuilder requestBuilder;
114
+
115
+ /// <inheritdoc />
116
+ public {{ model . Ns }} {{ model . ClassSuffix }} (global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
117
+ {
118
+ Client = client;
119
+ this.requestBuilder = requestBuilder;
120
+ }
121
+
122
+ """
110
123
) ;
111
124
125
+ source . Indentation ++ ;
112
126
var uniqueNames = new UniqueNameBuilder ( ) ;
113
127
uniqueNames . Reserve ( model . MemberNames ) ;
114
128
@@ -135,13 +149,15 @@ partial class {model.Ns}{model.ClassDeclaration}
135
149
WriteDisposableMethod ( source ) ;
136
150
}
137
151
152
+ source . Indentation -= 2 ;
138
153
source . WriteLine (
139
- @ "
140
- }
141
- }
142
- }
154
+ "" "
155
+ }
156
+ }
157
+ }
143
158
144
- #pragma warning restore"
159
+ #pragma warning restore
160
+ """
145
161
) ;
146
162
return source . ToSourceText ( ) ;
147
163
}
@@ -172,12 +188,11 @@ UniqueNameBuilder uniqueNames
172
188
ReturnTypeInfo . AsyncVoid => ( true , "await (" , ").ConfigureAwait(false)" ) ,
173
189
ReturnTypeInfo . AsyncResult => ( true , "return await (" , ").ConfigureAwait(false)" ) ,
174
190
ReturnTypeInfo . Return => ( false , "return " , "" ) ,
175
- _
176
- => throw new ArgumentOutOfRangeException (
177
- nameof ( methodModel . ReturnTypeMetadata ) ,
178
- methodModel . ReturnTypeMetadata ,
179
- "Unsupported value."
180
- )
191
+ _ => throw new ArgumentOutOfRangeException (
192
+ nameof ( methodModel . ReturnTypeMetadata ) ,
193
+ methodModel . ReturnTypeMetadata ,
194
+ "Unsupported value."
195
+ ) ,
181
196
} ;
182
197
183
198
WriteMethodOpening ( source , methodModel , ! isTopLevel , isAsync ) ;
@@ -204,13 +219,13 @@ UniqueNameBuilder uniqueNames
204
219
? $ ", new global::System.Type[] {{ { string . Join ( ", " , genericArray ) } }}"
205
220
: string . Empty ;
206
221
207
- source . Append (
208
- @$ "
222
+ source . WriteLine (
223
+ $ "" "
209
224
var ______arguments = { argumentsArrayString } ;
210
- var ______func = requestBuilder.BuildRestResultFuncForMethod("" { methodModel . Name } " ", { parameterTypesExpression } { genericString } );
225
+ var ______func = requestBuilder.BuildRestResultFuncForMethod("{ methodModel . Name } ", { parameterTypesExpression } { genericString } );
211
226
212
227
{ @return } ({ returnType } )______func(this.Client, ______arguments){ configureAwait } ;
213
- "
228
+ "" "
214
229
) ;
215
230
216
231
WriteMethodClosing ( source ) ;
@@ -221,29 +236,26 @@ private static void WriteNonRefitMethod(SourceWriter source, MethodModel methodM
221
236
WriteMethodOpening ( source , methodModel , true ) ;
222
237
223
238
source . WriteLine (
224
- @"
225
- throw new global::System.NotImplementedException(""Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument."");" ) ;
239
+ @"throw new global::System.NotImplementedException(""Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument."");"
240
+ ) ;
226
241
227
- source . Indentation += 1 ;
228
242
WriteMethodClosing ( source ) ;
229
- source . Indentation -= 1 ;
230
243
}
231
244
232
245
// TODO: This assumes that the Dispose method is a void that takes no parameters.
233
246
// The previous version did not.
234
247
// Does the bool overload cause an issue here.
235
248
private static void WriteDisposableMethod ( SourceWriter source )
236
249
{
237
- source . Append (
250
+ source . WriteLine (
238
251
"""
239
252
240
-
241
- /// <inheritdoc />
242
- void global::System.IDisposable.Dispose()
243
- {
244
- Client?.Dispose();
245
- }
246
- """
253
+ /// <inheritdoc />
254
+ void global::System.IDisposable.Dispose()
255
+ {
256
+ Client?.Dispose();
257
+ }
258
+ """
247
259
) ;
248
260
}
249
261
@@ -267,12 +279,12 @@ UniqueNameBuilder uniqueNames
267
279
// find a name and generate field declaration.
268
280
var typeParameterFieldName = uniqueNames . New ( TypeParameterVariableName ) ;
269
281
var types = string . Join ( ", " , methodModel . Parameters . Select ( x => $ "typeof({ x . Type } )") ) ;
270
- source . Append (
271
- $$ """
272
282
283
+ source . WriteLine (
284
+ $$ """
273
285
274
- private static readonly global::System.Type[] {{ typeParameterFieldName }} = new global::System.Type[] {{{types}} };
275
- """
286
+ private static readonly global::System.Type[] {{ typeParameterFieldName }} = new global::System.Type[] {{{types}} };
287
+ """
276
288
) ;
277
289
278
290
return typeParameterFieldName ;
@@ -288,18 +300,17 @@ private static void WriteMethodOpening(
288
300
var visibility = ! isExplicitInterface ? "public " : string . Empty ;
289
301
var async = isAsync ? "async " : "" ;
290
302
291
- source . Append (
292
- @$ "
293
-
294
- /// <inheritdoc />
295
- { visibility } { async} { methodModel . ReturnType } "
303
+ var builder = new StringBuilder ( ) ;
304
+ builder . Append (
305
+ @$ "/// <inheritdoc />
306
+ { visibility } { async} { methodModel . ReturnType } "
296
307
) ;
297
308
298
309
if ( isExplicitInterface )
299
310
{
300
- source . Append ( @$ "{ methodModel . ContainingType } .") ;
311
+ builder . Append ( @$ "{ methodModel . ContainingType } .") ;
301
312
}
302
- source . Append ( @$ "{ methodModel . DeclaredMethod } (") ;
313
+ builder . Append ( @$ "{ methodModel . DeclaredMethod } (") ;
303
314
304
315
if ( methodModel . Parameters . Count > 0 )
305
316
{
@@ -311,38 +322,45 @@ private static void WriteMethodOpening(
311
322
list. Add( $@"{param.Type}{(annotation ? '?' : string.Empty)} @{param.MetadataName}" ) ;
312
323
}
313
324
314
- source . Append( string . Join ( ", " , list ) ) ;
325
+ builder . Append( string . Join ( ", " , list ) ) ;
315
326
}
316
327
317
- source. Append (
318
- @$ "){ GenerateConstraints ( methodModel . Constraints , isExplicitInterface ) }
319
- {{"
320
- ) ;
328
+ builder. Append ( ")" ) ;
329
+
330
+ source. WriteLine ( ) ;
331
+ source. WriteLine ( builder . ToString ( ) ) ;
332
+ source. Indentation ++ ;
333
+ GenerateConstraints( source , methodModel . Constraints , isExplicitInterface ) ;
334
+ source. Indentation -- ;
335
+ source. WriteLine ( "{" ) ;
336
+ source. Indentation ++ ;
321
337
}
322
338
323
- private static void WriteMethodClosing( SourceWriter source ) => source. Append ( @" }" ) ;
339
+ private static void WriteMethodClosing( SourceWriter source )
340
+ {
341
+ source. Indentation -- ;
342
+ source. WriteLine ( "}" ) ;
343
+ }
324
344
325
- private static string GenerateConstraints(
345
+ private static void GenerateConstraints(
346
+ SourceWriter writer ,
326
347
ImmutableEquatableArray < TypeConstraint > typeParameters ,
327
348
bool isOverrideOrExplicitImplementation
328
349
)
329
350
{
330
- var source = new StringBuilder( ) ;
331
351
// Need to loop over the constraints and create them
332
352
foreach ( var typeParameter in typeParameters)
333
353
{
334
354
WriteConstraintsForTypeParameter(
335
- source ,
355
+ writer ,
336
356
typeParameter ,
337
357
isOverrideOrExplicitImplementation
338
358
) ;
339
359
}
340
-
341
- return source. ToString ( ) ;
342
360
}
343
361
344
362
private static void WriteConstraintsForTypeParameter(
345
- StringBuilder source ,
363
+ SourceWriter source ,
346
364
TypeConstraint typeParameter ,
347
365
bool isOverrideOrExplicitImplementation
348
366
)
@@ -388,10 +406,7 @@ bool isOverrideOrExplicitImplementation
388
406
389
407
if ( parameters . Count > 0 )
390
408
{
391
- source. Append (
392
- @$ "
393
- where { typeParameter . TypeName } : { string . Join ( ", " , parameters ) } "
394
- ) ;
409
+ source. WriteLine ( $ "where { typeParameter . TypeName } : { string . Join ( ", " , parameters ) } ") ;
395
410
}
396
411
}
397
412
}
0 commit comments