goBack method

void goBack(
  1. {bool deleteMode = false}
)

Navigate to the previous node.

Implementation

void goBack({bool deleteMode = false}) {
  final state =
      deleteMode ? currentNode.remove() : currentNode.shiftCursorLeft();
  switch (state) {
    // CASE 1: Courser was moved 1 position to the left in the current node.
    case NavigationState.success:
      notifyListeners();
      return;
    // CASE 2: The upcoming tex is a function.
    // We want to step in this function rather than skipping/deleting it.
    case NavigationState.func:
      final pos = currentNode.courserPosition;
      currentNode = (currentNode.children[pos] as TeXFunction).argNodes.last;
      currentNode.courserPosition = currentNode.children.length;
      currentNode.setCursor();
      notifyListeners();
      return;
    // CASE 3: The courser is already at the beginning of this node.
    case NavigationState.end:
      // If the current node is the root, we can't navigate further.
      if (currentNode.parent == null) {
        return;
      }
      // Otherwise, the current node must be a function argument.
      currentNode.removeCursor();
      final parent = currentNode.parent!;
      final nextArg = parent.argNodes.indexOf(currentNode) - 1;
      // If the parent function has another argument before this one,
      // we jump into that, otherwise we position the courser right
      // before the function.
      if (nextArg < 0) {
        currentNode = parent.parent;
        currentNode.courserPosition = currentNode.children.indexOf(parent);
        if (deleteMode) {
          currentNode.children.remove(parent);
        }
        currentNode.setCursor();
      } else {
        currentNode = currentNode.parent!.argNodes[nextArg];
        currentNode.courserPosition = currentNode.children.length;
        currentNode.setCursor();
      }
      notifyListeners();
  }
}