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;

  final equalityMethod = getEqualityMethod(classDecl);
  final hashCodeMethod = getHashCodeMethod(classDecl);

  if (equalityMethod == null || hashCodeMethod == null) return;

  if (getMissingFieldsInEquality(classDecl, equalityMethod).isEmpty &&
      getMissingFieldsInHashCode(classDecl, hashCodeMethod).isEmpty) {
    return;
  }

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

  await builder.addDartFileEdit(file, (builder) {
    final equalityRange = equalityMethod.offset;
    final equalityLength = equalityMethod.length;

    final equalitySnippet = buildEqualitySnippet(
      className,
      classFields,
      builder: builder,
    );

    builder.addSimpleReplacement(
      SourceRange(equalityRange, equalityLength),
      equalitySnippet,
    );

    final hashCodeRange = hashCodeMethod.offset;
    final hashCodeLength = hashCodeMethod.length;

    final hashCodeSnippet = buildHashCodeSnippet(
      classFields,
      builder: builder,
    );

    builder.addSimpleReplacement(
      SourceRange(hashCodeRange, hashCodeLength),
      hashCodeSnippet,
    );
    builder.format(SourceRange(0, unitResult.content.length));
  });
}