compute method

  1. @override
Future<void> compute(
  1. ChangeBuilder builder
)
override

Computes the changes for this producer using builder.

This method should not modify fixKind.

Implementation

@override
Future<void> compute(ChangeBuilder builder) async {
  final classDecl = node.thisOrAncestorOfType<ClassDeclaration>();
  if (classDecl == null) return;

  // Determine which snippets are needed
  final needEquality =
      (getEqualityMethod(classDecl) == null ||
      getHashCodeMethod(classDecl) == null);
  final needCopyWith = getCopyWithMethod(classDecl) == null;

  if (!needEquality && !needCopyWith) return;

  final className = getClassName(classDecl);
  final classFields = getClassFields(classDecl);

  await builder.addDartFileEdit(file, (fileBuilder) {
    final insertOffset = classDecl.rightBracket.offset;
    fileBuilder.addInsertion(insertOffset, (builder) {
      // equality + hashCode
      if (needEquality) {
        builder.write(
          buildEqualityAndHashCodeSnippet(
            className,
            classFields,
            builder: fileBuilder,
          ),
        );
      }

      // copyWith
      if (needCopyWith) {
        final snippet = buildCopyWithSnippet(classDecl);
        if (snippet != null) {
          if (needEquality) builder.writeln();
          builder.write(snippet);
        }
      }
    });

    fileBuilder.format(SourceRange(0, unitResult.content.length));
  });
}