partition static method

ReusePartition partition(
  1. List<LatexNode> oldChildren,
  2. TextEdit edit
)

Partitions the top-level child nodes of the old AST

oldChildren The top-level child nodes of the old document edit The edit operation description Returns ReusePartition: A three-segment partition of prefix, dirty region, and suffix

Implementation

static ReusePartition partition(List<LatexNode> oldChildren, TextEdit edit) {
  if (oldChildren.isEmpty || (edit.oldLength == 0 && edit.delta == 0)) {
    return ReusePartition(prefix: oldChildren, dirty: [], suffix: []);
  }

  final prefixNodes = <LatexNode>[];
  final dirtyNodes = <LatexNode>[];
  final suffixNodes = <LatexNode>[];

  for (final node in oldChildren) {
    final range = node.sourceRange;
    if (range == null) {
      // Nodes without source location information are considered dirty nodes
      dirtyNodes.add(node);
      continue;
    }

    if (range.end <= edit.startOffset) {
      // Node is completely before the edit start point -> prefix
      if (dirtyNodes.isEmpty) {
        prefixNodes.add(node);
      } else {
        // If there are already dirty nodes, subsequent nodes cannot be classified as prefix
        dirtyNodes.add(node);
      }
    } else if (range.start >= edit.oldEndOffset) {
      // Node is completely after the edit end point -> suffix (needs shifting)
      suffixNodes.add(node);
    } else {
      // Overlaps with the edit region -> dirty node
      dirtyNodes.add(node);
    }
  }

  return ReusePartition(
    prefix: prefixNodes,
    dirty: dirtyNodes,
    suffix: suffixNodes,
  );
}