writeProxyApi method

  1. @override
void writeProxyApi(
  1. DartOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstProxyApi api, {
  5. required String dartPackageName,
})
override

Writes a single Proxy Api to indent.

Implementation

@override
void writeProxyApi(
  DartOptions generatorOptions,
  Root root,
  Indent indent,
  AstProxyApi api, {
  required String dartPackageName,
}) {
  const String codecName = '_${classNamePrefix}ProxyApiBaseCodec';

  // Each API has a private codec instance used by every host method,
  // constructor, or non-static field.
  final String codecInstanceName = '${varNamePrefix}codec${api.name}';

  // AST class used by code_builder to generate the code.
  final cb.Class proxyApi = cb.Class(
    (cb.ClassBuilder builder) => builder
      ..name = api.name
      ..extend = api.superClass != null
          ? cb.refer(api.superClass!.baseName)
          : cb.refer(proxyApiBaseClassName)
      ..implements.addAll(
        api.interfaces.map(
          (TypeDeclaration type) => cb.refer(type.baseName),
        ),
      )
      ..docs.addAll(
        asDocumentationComments(api.documentationComments, _docCommentSpec),
      )
      ..constructors.addAll(_proxyApiConstructors(
        api.constructors,
        apiName: api.name,
        dartPackageName: dartPackageName,
        codecName: codecName,
        codecInstanceName: codecInstanceName,
        superClassApi: api.superClass?.associatedProxyApi,
        unattachedFields: api.unattachedFields,
        flutterMethodsFromSuperClasses: api.flutterMethodsFromSuperClasses(),
        flutterMethodsFromInterfaces: api.flutterMethodsFromInterfaces(),
        declaredFlutterMethods: api.flutterMethods,
      ))
      ..constructors.add(
        _proxyApiDetachedConstructor(
          apiName: api.name,
          superClassApi: api.superClass?.associatedProxyApi,
          unattachedFields: api.unattachedFields,
          flutterMethodsFromSuperClasses:
              api.flutterMethodsFromSuperClasses(),
          flutterMethodsFromInterfaces: api.flutterMethodsFromInterfaces(),
          declaredFlutterMethods: api.flutterMethods,
        ),
      )
      ..fields.addAll(<cb.Field>[
        if (api.constructors.isNotEmpty ||
            api.attachedFields.any((ApiField field) => !field.isStatic) ||
            api.hostMethods.isNotEmpty)
          _proxyApiCodecInstanceField(
            codecInstanceName: codecInstanceName,
            codecName: codecName,
          ),
      ])
      ..fields.addAll(_proxyApiUnattachedFields(api.unattachedFields))
      ..fields.addAll(_proxyApiFlutterMethodFields(
        api.flutterMethods,
        apiName: api.name,
      ))
      ..fields.addAll(_proxyApiInterfaceApiFields(api.apisOfInterfaces()))
      ..fields.addAll(_proxyApiAttachedFields(api.attachedFields))
      ..methods.add(
        _proxyApiSetUpMessageHandlerMethod(
          flutterMethods: api.flutterMethods,
          apiName: api.name,
          dartPackageName: dartPackageName,
          codecName: codecName,
          unattachedFields: api.unattachedFields,
          hasCallbackConstructor: api.hasCallbackConstructor(),
        ),
      )
      ..methods.addAll(
        _proxyApiAttachedFieldMethods(
          api.attachedFields,
          apiName: api.name,
          dartPackageName: dartPackageName,
          codecInstanceName: codecInstanceName,
          codecName: codecName,
        ),
      )
      ..methods.addAll(_proxyApiHostMethods(
        api.hostMethods,
        apiName: api.name,
        dartPackageName: dartPackageName,
        codecInstanceName: codecInstanceName,
        codecName: codecName,
      ))
      ..methods.add(
        _proxyApiCopyMethod(
          apiName: api.name,
          unattachedFields: api.unattachedFields,
          declaredAndInheritedFlutterMethods: api
              .flutterMethodsFromSuperClasses()
              .followedBy(api.flutterMethodsFromInterfaces())
              .followedBy(api.flutterMethods),
        ),
      ),
  );

  final cb.DartEmitter emitter = cb.DartEmitter(useNullSafetySyntax: true);
  indent.format(DartFormatter().format('${proxyApi.accept(emitter)}'));
}