@@ -18,54 +18,54 @@ Action<string, SourceText> addSource
1818
1919 var attributeText = $$ """
2020
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+ """ ;
4040 // add the attribute text
4141 addSource ( "PreserveAttribute.g.cs" , SourceText . From ( attributeText , Encoding . UTF8 ) ) ;
4242
4343 var generatedClassText = $$ """
4444
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+ """ ;
6969 addSource ( "Generated.g.cs" , SourceText . From ( generatedClassText , Encoding . UTF8 ) ) ;
7070 }
7171
@@ -76,39 +76,53 @@ public static SourceText EmitInterface(InterfaceModel model)
7676 // if nullability is supported emit the nullable directive
7777 if ( model . Nullability != Nullability . None )
7878 {
79- source . WriteLine ( "#nullable " + ( model . Nullability == Nullability . Enabled ? "enable" : "disable" ) ) ;
79+ source . WriteLine (
80+ "#nullable " + ( model . Nullability == Nullability . Enabled ? "enable" : "disable" )
81+ ) ;
8082 }
8183
8284 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+ """
110123 ) ;
111124
125+ source . Indentation ++ ;
112126 var uniqueNames = new UniqueNameBuilder ( ) ;
113127 uniqueNames . Reserve ( model . MemberNames ) ;
114128
@@ -135,13 +149,15 @@ partial class {model.Ns}{model.ClassDeclaration}
135149 WriteDisposableMethod ( source ) ;
136150 }
137151
152+ source . Indentation -= 2 ;
138153 source . WriteLine (
139- @ "
140- }
141- }
142- }
154+ "" "
155+ }
156+ }
157+ }
143158
144- #pragma warning restore"
159+ #pragma warning restore
160+ """
145161 ) ;
146162 return source . ToSourceText ( ) ;
147163 }
@@ -172,12 +188,11 @@ UniqueNameBuilder uniqueNames
172188 ReturnTypeInfo . AsyncVoid => ( true , "await (" , ").ConfigureAwait(false)" ) ,
173189 ReturnTypeInfo . AsyncResult => ( true , "return await (" , ").ConfigureAwait(false)" ) ,
174190 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+ ) ,
181196 } ;
182197
183198 WriteMethodOpening ( source , methodModel , ! isTopLevel , isAsync ) ;
@@ -204,13 +219,13 @@ UniqueNameBuilder uniqueNames
204219 ? $ ", new global::System.Type[] {{ { string . Join ( ", " , genericArray ) } }}"
205220 : string . Empty ;
206221
207- source . Append (
208- @$ "
222+ source . WriteLine (
223+ $ "" "
209224 var ______arguments = { argumentsArrayString } ;
210- var ______func = requestBuilder.BuildRestResultFuncForMethod("" { methodModel . Name } " ", { parameterTypesExpression } { genericString } );
225+ var ______func = requestBuilder.BuildRestResultFuncForMethod("{ methodModel . Name } ", { parameterTypesExpression } { genericString } );
211226
212227 { @return } ({ returnType } )______func(this.Client, ______arguments){ configureAwait } ;
213- "
228+ "" "
214229 ) ;
215230
216231 WriteMethodClosing ( source ) ;
@@ -221,29 +236,26 @@ private static void WriteNonRefitMethod(SourceWriter source, MethodModel methodM
221236 WriteMethodOpening ( source , methodModel , true ) ;
222237
223238 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+ ) ;
226241
227- source . Indentation += 1 ;
228242 WriteMethodClosing ( source ) ;
229- source . Indentation -= 1 ;
230243 }
231244
232245 // TODO: This assumes that the Dispose method is a void that takes no parameters.
233246 // The previous version did not.
234247 // Does the bool overload cause an issue here.
235248 private static void WriteDisposableMethod ( SourceWriter source )
236249 {
237- source . Append (
250+ source . WriteLine (
238251 """
239252
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+ """
247259 ) ;
248260 }
249261
@@ -267,12 +279,12 @@ UniqueNameBuilder uniqueNames
267279 // find a name and generate field declaration.
268280 var typeParameterFieldName = uniqueNames . New ( TypeParameterVariableName ) ;
269281 var types = string . Join ( ", " , methodModel . Parameters . Select ( x => $ "typeof({ x . Type } )") ) ;
270- source . Append (
271- $$ """
272282
283+ source . WriteLine (
284+ $$ """
273285
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+ """
276288 ) ;
277289
278290 return typeParameterFieldName ;
@@ -288,18 +300,17 @@ private static void WriteMethodOpening(
288300 var visibility = ! isExplicitInterface ? "public " : string . Empty ;
289301 var async = isAsync ? "async " : "" ;
290302
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 } "
296307 ) ;
297308
298309 if ( isExplicitInterface )
299310 {
300- source . Append ( @$ "{ methodModel . ContainingType } .") ;
311+ builder . Append ( @$ "{ methodModel . ContainingType } .") ;
301312 }
302- source . Append ( @$ "{ methodModel . DeclaredMethod } (") ;
313+ builder . Append ( @$ "{ methodModel . DeclaredMethod } (") ;
303314
304315 if ( methodModel . Parameters . Count > 0 )
305316 {
@@ -311,38 +322,45 @@ private static void WriteMethodOpening(
311322 list. Add( $@"{param.Type}{(annotation ? '?' : string.Empty)} @{param.MetadataName}" ) ;
312323 }
313324
314- source . Append( string . Join ( ", " , list ) ) ;
325+ builder . Append( string . Join ( ", " , list ) ) ;
315326 }
316327
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 ++ ;
321337 }
322338
323- private static void WriteMethodClosing( SourceWriter source ) => source. Append ( @" }" ) ;
339+ private static void WriteMethodClosing( SourceWriter source )
340+ {
341+ source. Indentation -- ;
342+ source. WriteLine ( "}" ) ;
343+ }
324344
325- private static string GenerateConstraints(
345+ private static void GenerateConstraints(
346+ SourceWriter writer ,
326347 ImmutableEquatableArray < TypeConstraint > typeParameters ,
327348 bool isOverrideOrExplicitImplementation
328349 )
329350 {
330- var source = new StringBuilder( ) ;
331351 // Need to loop over the constraints and create them
332352 foreach ( var typeParameter in typeParameters)
333353 {
334354 WriteConstraintsForTypeParameter(
335- source ,
355+ writer ,
336356 typeParameter ,
337357 isOverrideOrExplicitImplementation
338358 ) ;
339359 }
340-
341- return source. ToString ( ) ;
342360 }
343361
344362 private static void WriteConstraintsForTypeParameter(
345- StringBuilder source ,
363+ SourceWriter source ,
346364 TypeConstraint typeParameter ,
347365 bool isOverrideOrExplicitImplementation
348366 )
@@ -388,10 +406,7 @@ bool isOverrideOrExplicitImplementation
388406
389407 if ( parameters . Count > 0 )
390408 {
391- source. Append (
392- @$ "
393- where { typeParameter . TypeName } : { string . Join ( ", " , parameters ) } "
394- ) ;
409+ source. WriteLine ( $ "where { typeParameter . TypeName } : { string . Join ( ", " , parameters ) } ") ;
395410 }
396411 }
397412}
0 commit comments