edit method

  1. @override
Future<RepoEdit> edit(
  1. String path,
  2. String oldString,
  3. String newString, {
  4. bool replaceAll = false,
  5. int? atLine,
})
override

Replaces oldString with newString in path. When replaceAll is false exactly one occurrence must match. When atLine is given, the match must occur on that 1-based line or the edit is rejected.

Implementation

@override
Future<RepoEdit> edit(
  String path,
  String oldString,
  String newString, {
  bool replaceAll = false,
  int? atLine,
}) async {
  final file = File(_resolve(path));
  if (!file.existsSync()) throw RepoException('File not found: $path');
  final content = file.readAsStringSync();
  final result = applyEdit(
    content,
    oldString,
    newString,
    replaceAll: replaceAll,
    atLine: atLine,
  );
  file.writeAsStringSync(result.content);
  return RepoEdit(
    path: normalizeRepoPath(path),
    replacements: result.replacements,
    appliedAtLine: result.appliedAtLine,
  );
}