applyRemotePlanToBuffer function

Set<int> applyRemotePlanToBuffer(
  1. RemotePlan plan,
  2. CellBuffer mirror
)

Applies a decoded plan to a CellBuffer mirror, reproducing the server's frame. The client rebuilds the dirty DOM rows from the mirror afterward. Returns the row indices the plan touched.

Implementation

Set<int> applyRemotePlanToBuffer(RemotePlan plan, CellBuffer mirror) {
  final touched = <int>{};
  final scroll = plan.scrollUpRows;
  if (scroll != null && scroll > 0) {
    // Shift the mirror up to match the server's retained region before the
    // residual patches land on top.
    mirror.scrollUp(scroll);
  }
  for (final patch in plan.patches) {
    if (patch.row < 0 || patch.row >= mirror.size.rows) continue;
    touched.add(patch.row);
    var col = patch.startCol;
    for (final run in patch.runs) {
      if (run.styleIndex < 0 || run.styleIndex >= plan.styleTable.length) {
        continue;
      }
      final style = plan.styleTable[run.styleIndex];
      final advanced = mirror.writeText(
        CellOffset(col, patch.row),
        run.text,
        style: style,
      );
      col += advanced == 0 ? run.text.length : advanced;
    }
  }
  return touched;
}