private void onChildRemove()

in cell/src/main/java/jetbrains/jetpad/cell/indent/updater/IndentUpdater.java [179:237]


  private void onChildRemove(Cell child) {
    if (!isVisible(child)) return;

    if (!isAttached(child)) {
      throw new IllegalStateException("child " + child + " is already detached");
    }

    Position removeAt = new Position(this, child);
    Position prevNewLinePos = prevNewLine(removeAt.prev());

    if (isCell(child)) {
      TargetT line;
      int index;
      if (prevNewLinePos == null) {
        Position first = new Position(this, firstLeaf(myRoot));
        line = children(myTarget).get(0);
        index = first.deltaTo(removeAt);
      } else {
        line = myNewLineToLine.get(prevNewLinePos.get());
        index = prevNewLinePos.deltaTo(removeAt) - 1 + (indent(prevNewLinePos.get()) > 0 ? 1 : 0);
      }

      TargetT toRemove = children(line).get(index);

      CellWrapper<TargetT> wrapper = myWrappers.get(child);
      if (wrapper.item() != toRemove) {
        throw new IllegalStateException();
      }

      children(line).remove(index);
      myWrappers.remove(child).remove();
    } else if (child instanceof NewLineCell) {
      TargetT lineCell = myNewLineToLine.remove(child);

      if (lineCell == null) {
        throw new IllegalStateException();
      }

      if (indent(child) > 0) {
        children(lineCell).remove(0);
      }

      removeFromParent(lineCell);

      TargetT mergeWith;
      if (prevNewLinePos == null) {
        mergeWith = children(myTarget).get(0);
      } else {
        mergeWith = myNewLineToLine.get(prevNewLinePos.get());
      }

      for (TargetT c : new ArrayList<>(children(lineCell))) {
        removeFromParent(c);
        children(mergeWith).add(c);
      }
    }

    setAttached(child, false);
  }