splitForSelection method

void splitForSelection(
  1. int selectionStart,
  2. int selectionEnd
)

Implementation

void splitForSelection(int selectionStart, int selectionEnd) {
  if (!hasPlaintextSelectionMatch(selectionStart, selectionEnd)) {
    throw Exception(
        "Selection ($selectionStart, $selectionEnd) has no match");
  }

  if (parent == null) {
    throw Exception("Cannot split root node");
  }

  List<int> splitPoints = [0];

  if (selectionStart > scopeStart) {
    splitPoints.add(selectionStart - scopeStart);
  }

  if (selectionEnd < scopeEnd) {
    splitPoints.add(selectionEnd - scopeStart);
  }

  splitPoints.add(scopeEnd - scopeStart);

  int myIdx = parentIndex;
  List<NodeV3> split = [];

  for (int i = 0; i < splitPoints.length - 1; i++) {
    int pStart = splitPoints[i];
    int pEnd = splitPoints[i + 1];

    String splitContent = content!.substring(pStart, pEnd);

    split.add(
      NodeV3(
        pStart + scopeStart,
        pEnd + scopeStart,
        content: splitContent,
        parent: parent,
      ),
    );
  }

  deleteFromTree();
  parent!.addChildren(split, myIdx);
}