TextTokenCell getPair()

in hybrid/src/main/java/jetbrains/jetpad/hybrid/BaseHybridSynchronizer.java [698:750]


  TextTokenCell getPair(TextTokenCell cell) {
    PairSpec pairSpec = mySpec.get().getPairSpec();
    List<Token> tokens = myTokenListEditor.tokens;
    Token token = cell.getToken();

    if (pairSpec.isLeft(cell.getToken())) {
      int index = myTargetList.indexOf(cell);
      if (index == -1) return null;

      Stack<Token> pairStack = new Stack<>();
      for (int i = index + 1; i < myTargetList.size(); i++) {
        Cell targetCell = myTargetList.get(i);
        if (!(targetCell instanceof TextTokenCell)) continue;

        TextTokenCell tc = (TextTokenCell) targetCell;
        Token t = tc.getToken();
        if (pairStack.isEmpty() && pairSpec.isPair(token, t)) {
          return tc;
        }

        if (pairSpec.isLeft(t)) {
          pairStack.push(t);
        } else if (pairSpec.isRight(t) && !pairStack.isEmpty() && pairSpec.isPair(pairStack.peek(), t)) {
          pairStack.pop();
        }
      }
      return null;
    } else if (pairSpec.isRight(cell.getToken())) {
      int index = myTargetList.indexOf(cell);
      if (index == -1) return null;
      Stack<Token> pairStack = new Stack<>();

      for (int i = index - 1; i >=0; i--) {
        Cell targetCell = myTargetList.get(i);
        if (!(targetCell instanceof TextTokenCell)) continue;

        TextTokenCell tc = (TextTokenCell) targetCell;
        Token t = tokens.get(i);
        if (pairStack.isEmpty() && pairSpec.isPair(t, token)) {
          return tc;
        }

        if (pairSpec.isRight(t)) {
          pairStack.push(t);
        } else if (pairSpec.isLeft(t) && !pairStack.isEmpty() && pairSpec.isPair(t, pairStack.peek())) {
          pairStack.pop();
        }
      }
      return null;
    } else {
      return null;
    }
  }