@@ -291,13 +291,22 @@ import FlutterMacOS
291291 if (isCustomCodec) {
292292 _writeCodec (indent, api, root);
293293 }
294+
294295 const List <String > generatedComments = < String > [
295- ' Generated class from Pigeon that represents Flutter messages that can be called from Swift.'
296+ ' Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.'
296297 ];
297298 addDocumentationComments (indent, api.documentationComments, _docCommentSpec,
298299 generatorComments: generatedComments);
299300
300- indent.write ('class ${api .name } ' );
301+ indent.addScoped ('protocol ${api .name }Protocol {' , '}' , () {
302+ for (final Method func in api.methods) {
303+ addDocumentationComments (
304+ indent, func.documentationComments, _docCommentSpec);
305+ indent.writeln (_getMethodSignature (func));
306+ }
307+ });
308+
309+ indent.write ('class ${api .name }: ${api .name }Protocol ' );
301310 indent.addScoped ('{' , '}' , () {
302311 indent.writeln ('private let binaryMessenger: FlutterBinaryMessenger' );
303312 indent.write ('init(binaryMessenger: FlutterBinaryMessenger)' );
@@ -314,47 +323,19 @@ import FlutterMacOS
314323 });
315324 }
316325 for (final Method func in api.methods) {
317- final _SwiftFunctionComponents components =
318- _SwiftFunctionComponents .fromMethod (func);
319-
320326 final String channelName = makeChannelName (api, func, dartPackageName);
321- final String returnType = func.returnType.isVoid
322- ? 'Void'
323- : _nullsafeSwiftTypeForDartType (func.returnType);
324- String sendArgument;
327+
325328 addDocumentationComments (
326329 indent, func.documentationComments, _docCommentSpec);
327-
328- if (func.arguments.isEmpty) {
329- indent.write (
330- 'func ${func .name }(completion: @escaping (Result<$returnType , FlutterError>) -> Void) ' );
331- sendArgument = 'nil' ;
332- } else {
333- final Iterable <String > argTypes = func.arguments
334- .map ((NamedType e) => _nullsafeSwiftTypeForDartType (e.type));
335- final Iterable <String > argLabels = indexMap (components.arguments,
336- (int index, _SwiftFunctionArgument argument) {
337- return argument.label ??
338- _getArgumentName (index, argument.namedType);
339- });
340- final Iterable <String > argNames =
341- indexMap (func.arguments, _getSafeArgumentName);
330+ indent.writeScoped ('${_getMethodSignature (func )} {' , '}' , () {
342331 final Iterable <String > enumSafeArgNames = func.arguments
343332 .asMap ()
344333 .entries
345334 .map ((MapEntry <int , NamedType > e) =>
346335 getEnumSafeArgumentExpression (root, e.key, e.value));
347- sendArgument = '[${enumSafeArgNames .join (', ' )}] as [Any?]' ;
348- final String argsSignature = map3 (
349- argTypes,
350- argLabels,
351- argNames,
352- (String type, String label, String name) =>
353- '$label $name : $type ' ).join (', ' );
354- indent.write (
355- 'func ${components .name }($argsSignature , completion: @escaping (Result<$returnType , FlutterError>) -> Void) ' );
356- }
357- indent.addScoped ('{' , '}' , () {
336+ final String sendArgument = func.arguments.isEmpty
337+ ? 'nil'
338+ : '[${enumSafeArgNames .join (', ' )}] as [Any?]' ;
358339 const String channel = 'channel' ;
359340 indent.writeln (
360341 'let $channel = FlutterBasicMessageChannel(name: "$channelName ", binaryMessenger: binaryMessenger$codecArgumentString )' );
@@ -893,6 +874,31 @@ String _nullsafeSwiftTypeForDartType(TypeDeclaration type) {
893874 return '${_swiftTypeForDartType (type )}$nullSafe ' ;
894875}
895876
877+ String _getMethodSignature (Method func) {
878+ final _SwiftFunctionComponents components =
879+ _SwiftFunctionComponents .fromMethod (func);
880+ final String returnType = func.returnType.isVoid
881+ ? 'Void'
882+ : _nullsafeSwiftTypeForDartType (func.returnType);
883+
884+ if (func.arguments.isEmpty) {
885+ return 'func ${func .name }(completion: @escaping (Result<$returnType , FlutterError>) -> Void) ' ;
886+ } else {
887+ final Iterable <String > argTypes = func.arguments
888+ .map ((NamedType e) => _nullsafeSwiftTypeForDartType (e.type));
889+ final Iterable <String > argLabels = indexMap (components.arguments,
890+ (int index, _SwiftFunctionArgument argument) {
891+ return argument.label ?? _getArgumentName (index, argument.namedType);
892+ });
893+ final Iterable <String > argNames =
894+ indexMap (func.arguments, _getSafeArgumentName);
895+ final String argsSignature = map3 (argTypes, argLabels, argNames,
896+ (String type, String label, String name) => '$label $name : $type ' )
897+ .join (', ' );
898+ return 'func ${components .name }($argsSignature , completion: @escaping (Result<$returnType , FlutterError>) -> Void) ' ;
899+ }
900+ }
901+
896902/// A class that represents a Swift function argument.
897903///
898904/// The [name] is the name of the argument.
0 commit comments