@@ -171,7 +171,7 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, string symbolN
171171 /// <param name="bindingContext">The current binding context.</param>
172172 /// <param name="diagnostics">Out parameter with the error diagnostics.</param>
173173 /// <returns>True if the binding was generated, false otherwise.</returns>
174- bool TryEmitSmartEnum ( in BindingTypeData < SmartEnum > _ , in BindingContext bindingContext , [ NotNullWhen ( false ) ] out ImmutableArray < Diagnostic > ? diagnostics )
174+ bool TryEmitSmartEnum ( in BindingContext bindingContext , [ NotNullWhen ( false ) ] out ImmutableArray < Diagnostic > ? diagnostics )
175175 {
176176 diagnostics = null ;
177177 if ( bindingContext . Changes . BindingType != BindingType . SmartEnum ) {
@@ -184,34 +184,36 @@ bool TryEmitSmartEnum (in BindingTypeData<SmartEnum> _, in BindingContext bindin
184184 }
185185 // in the old generator we had to copy over the enum, in this new approach, the only code
186186 // we need to create is the extension class for the enum that is backed by fields
187- bindingContext . Builder . WriteLine ( ) ;
188- bindingContext . Builder . WriteLine ( $ "namespace { string . Join ( "." , bindingContext . Changes . Namespace ) } ;") ;
189- bindingContext . Builder . WriteLine ( ) ;
190-
191- var symbolName = GetSymbolName ( bindingContext . Changes ) ;
192- var extensionClassDeclaration =
193- bindingContext . Changes . ToSmartEnumExtensionDeclaration ( symbolName ) ;
194-
195- bindingContext . Builder . WriteDocumentation ( Documentation . SmartEnum . ClassDocumentation ( symbolName ) ) ;
196- bindingContext . Builder . AppendMemberAvailability ( bindingContext . Changes . SymbolAvailability ) ;
197- bindingContext . Builder . AppendGeneratedCodeAttribute ( ) ;
198- using ( var classBlock = bindingContext . Builder . CreateBlock ( extensionClassDeclaration . ToString ( ) , true ) ) {
199- classBlock . WriteLine ( ) ;
200- classBlock . WriteLine ( $ "static IntPtr[] values = new IntPtr [{ bindingContext . Changes . EnumMembers . Length } ];") ;
201- // foreach member in the enum we need to create a field that holds the value, the property emitter
202- // will take care of generating the property. Do not order it by name to keep the order of the enum
203- if ( ! TryEmit ( classBlock , bindingContext . Changes ) ) {
204- diagnostics = [ ] ; // empty diagnostics since it was a user error
205- return false ;
187+ this . EmitNamespace ( bindingContext ) ;
188+
189+ using ( var _ = this . EmitOuterClasses ( bindingContext , out var builder ) ) {
190+ var symbolName = GetSymbolName ( bindingContext . Changes ) ;
191+ var extensionClassDeclaration =
192+ bindingContext . Changes . ToSmartEnumExtensionDeclaration ( symbolName ) ;
193+
194+ builder . WriteDocumentation ( Documentation . SmartEnum . ClassDocumentation ( symbolName ) ) ;
195+ builder . AppendMemberAvailability ( bindingContext . Changes . SymbolAvailability ) ;
196+ builder . AppendGeneratedCodeAttribute ( ) ;
197+ using ( var classBlock = builder . CreateBlock ( extensionClassDeclaration . ToString ( ) , true ) ) {
198+ classBlock . WriteLine ( ) ;
199+ classBlock . WriteLine (
200+ $ "static IntPtr[] values = new IntPtr [{ bindingContext . Changes . EnumMembers . Length } ];") ;
201+ // foreach member in the enum we need to create a field that holds the value, the property emitter
202+ // will take care of generating the property. Do not order it by name to keep the order of the enum
203+ if ( ! TryEmit ( classBlock , bindingContext . Changes ) ) {
204+ diagnostics = [ ] ; // empty diagnostics since it was a user error
205+ return false ;
206+ }
207+
208+ classBlock . WriteLine ( ) ;
209+
210+ // emit the extension methods that will be used to get the values from the enum
211+ EmitExtensionMethods ( classBlock , symbolName , bindingContext . Changes ) ;
212+ classBlock . WriteLine ( ) ;
206213 }
207- classBlock . WriteLine ( ) ;
208214
209- // emit the extension methods that will be used to get the values from the enum
210- EmitExtensionMethods ( classBlock , symbolName , bindingContext . Changes ) ;
211- classBlock . WriteLine ( ) ;
215+ return true ;
212216 }
213-
214- return true ;
215217 }
216218
217219 bool TryEmitErrorCode ( in BindingTypeData < SmartEnum > bindingTypeData , in BindingContext bindingContext , [ NotNullWhen ( false ) ] out ImmutableArray < Diagnostic > ? diagnostics )
@@ -252,35 +254,36 @@ bool TryEmitErrorCode (in BindingTypeData<SmartEnum> bindingTypeData, in Binding
252254
253255 var library = libraryPath ?? libraryName ;
254256
255- bindingContext . Builder . WriteLine ( ) ;
256- bindingContext . Builder . WriteLine ( $ "namespace { string . Join ( "." , bindingContext . Changes . Namespace ) } ;" ) ;
257- bindingContext . Builder . WriteLine ( ) ;
258-
259- bindingContext . Builder . AppendMemberAvailability ( bindingContext . Changes . SymbolAvailability ) ;
260- bindingContext . Builder . AppendGeneratedCodeAttribute ( ) ;
261- var extensionClassDeclaration =
262- bindingContext . Changes . ToSmartEnumExtensionDeclaration ( GetSymbolName ( bindingContext . Changes ) ) ;
263-
264- using ( var classBlock = bindingContext . Builder . CreateBlock ( extensionClassDeclaration . ToString ( ) , true ) ) {
265- classBlock . WriteLine ( ) ;
266- // emit the field that holds the error domain
267- classBlock . WriteLine ( $ "[Field ( \" { bindingTypeData . ErrorDomain } \" , \" { library } \" )]" ) ;
268- classBlock . WriteLine ( StaticVariable ( backingFieldName , NSString , true ) . ToString ( ) ) ;
269- classBlock . WriteLine ( ) ;
270-
271- // emit the extension method to return the error domain
272- classBlock . WriteDocumentation ( Documentation . SmartEnum . GetDomain ( bindingContext . Changes . FullyQualifiedSymbol ) ) ;
273- classBlock . WriteRaw (
274- $@ "public static { NSString } ? GetDomain (this { bindingContext . Changes . Name . GetIdentifierName ( bindingContext . Changes . Namespace ) } self)
257+ this . EmitNamespace ( bindingContext ) ;
258+
259+ using ( var _ = this . EmitOuterClasses ( bindingContext , out var builder ) ) {
260+ builder . AppendMemberAvailability ( bindingContext . Changes . SymbolAvailability ) ;
261+ builder . AppendGeneratedCodeAttribute ( ) ;
262+ var extensionClassDeclaration =
263+ bindingContext . Changes . ToSmartEnumExtensionDeclaration ( GetSymbolName ( bindingContext . Changes ) ) ;
264+
265+ using ( var classBlock = builder . CreateBlock ( extensionClassDeclaration . ToString ( ) , true ) ) {
266+ classBlock . WriteLine ( ) ;
267+ // emit the field that holds the error domain
268+ classBlock . WriteLine ( $ "[Field ( \" { bindingTypeData . ErrorDomain } \" , \" { library } \" )]" ) ;
269+ classBlock . WriteLine ( StaticVariable ( backingFieldName , NSString , true ) . ToString ( ) ) ;
270+ classBlock . WriteLine ( ) ;
271+
272+ // emit the extension method to return the error domain
273+ classBlock . WriteDocumentation (
274+ Documentation . SmartEnum . GetDomain ( bindingContext . Changes . FullyQualifiedSymbol ) ) ;
275+ classBlock . WriteRaw (
276+ $@ "public static { NSString } ? GetDomain (this { bindingContext . Changes . Name . GetIdentifierName ( bindingContext . Changes . Namespace ) } self)
275277{{
276278 if ({ backingFieldName } is null)
277279 { backingFieldName } = { Dlfcn } .GetStringConstant ({ Libraries } .{ libraryName } .Handle, ""{ bindingTypeData . ErrorDomain } "");
278280 return { backingFieldName } ;
279281}}
280282" ) ;
281- }
283+ }
282284
283- return true ;
285+ return true ;
286+ }
284287 }
285288
286289 public bool TryEmit ( in BindingContext bindingContext , [ NotNullWhen ( false ) ] out ImmutableArray < Diagnostic > ? diagnostics )
@@ -292,6 +295,6 @@ public bool TryEmit (in BindingContext bindingContext, [NotNullWhen (false)] out
292295
293296 return bindingData . Flags . HasFlag ( SmartEnum . ErrorCode )
294297 ? TryEmitErrorCode ( bindingData , bindingContext , out diagnostics )
295- : TryEmitSmartEnum ( bindingData , bindingContext , out diagnostics ) ;
298+ : TryEmitSmartEnum ( bindingContext , out diagnostics ) ;
296299 }
297300}
0 commit comments