replaceNode method

SyntaxTree replaceNode(
  1. SyntaxNode pos,
  2. GreenNode newNode
)

Replace node at pos with newNode

Implementation

SyntaxTree replaceNode(SyntaxNode pos, GreenNode newNode) {
  if (identical(pos.value, newNode)) {
    return this;
  }
  if (identical(pos, root)) {
    return SyntaxTree(greenRoot: newNode.wrapWithEquationRow());
  }
  final posParent = pos.parent;
  if (posParent == null) {
    throw ArgumentError(
        'The replaced node is not the root of this tree but has no parent');
  }
  return replaceNode(
      posParent,
      posParent.value.updateChildren(posParent.children
          .map((child) => identical(child, pos) ? newNode : child?.value)
          .toList(growable: false)));
}