public function getHunks()

in src/StringDiff.php [40:84]


  public function getHunks(int $context): vec<vec<DiffOp<string>>> {
    $hunks = vec[];

    $remaining = $this->getDiff();
    $last = C\lastx($remaining);
    // diff -u ignores trailing newlines
    if ($last->isKeepOp() && $last->getContent() === '') {
      $remaining = Vec\slice($remaining, 0, C\count($remaining) - 1);
    }

    while (!C\is_empty($remaining)) {
      $not_keep = C\find_key($remaining, $row ==> !$row->isKeepOp());
      if ($not_keep === null) {
        break;
      }
      $start = ($not_keep > $context) ? ($not_keep - $context) : 0;

      $remaining = Vec\drop($remaining, $start);
      $count = C\count($remaining);

      $end = $count;
      $run_start = null;
      for ($i = $context; $i < $count; ++$i) {
        if ($remaining[$i]->isKeepOp()) {
          $run_start ??= $i;
          continue;
        }

        if ($run_start === null) {
          continue;
        }

        if ($i >= $run_start + (2 * $context)) {
          $end = $run_start + $context;
          break;
        }
      }
      if ($run_start !== null) {
        $end = $run_start + $context;
      }
      $hunks[] = Vec\take($remaining, $end);
      $remaining = Vec\drop($remaining, $end);
    }
    return $hunks;
  }