readEditContext function

Future<EditContext?> readEditContext(
  1. String path,
  2. String needle, {
  3. int contextLines = 3,
})

Read the edit context around a needle in a file. Returns null on ENOENT. Returns truncated context if needle not found.

Implementation

Future<EditContext?> readEditContext(
  String path,
  String needle, {
  int contextLines = 3,
}) async {
  RandomAccessFile handle;
  try {
    handle = await File(path).open(mode: FileMode.read);
  } on PathNotFoundException {
    return null;
  }

  try {
    return await _scanForContext(handle, needle, contextLines);
  } finally {
    await handle.close();
  }
}