public void onKeyPressed()

in cell/src/main/java/jetbrains/jetpad/cell/text/TextNavigationTrait.java [80:146]


  public void onKeyPressed(Cell cell, KeyEvent event) {
    TextCell editor = (TextCell) cell;
    String currentText = TextEditing.nonNullText(editor);
    int caret = editor.caretPosition().get();
    int minCaret = getMinPos(editor);
    int textLen = currentText.length();
    int maxCaret = isLastAllowed(editor) ? textLen : textLen - 1;

    boolean selectionAvailable = isSelectionAvailable(editor);

    boolean selection = event.has(ModifierKey.SHIFT);
    try {
      if (isCaretKey(selectionAvailable, event, Key.LEFT) && caret > minCaret) {
        editor.caretPosition().set(caret - 1);
        editor.scrollToCaret();
        event.consume();
        return;
      }

      if (isCaretKey(selectionAvailable, event, Key.RIGHT) && caret < maxCaret) {
        editor.caretPosition().set(caret + 1);
        editor.scrollToCaret();
        event.consume();
        return;
      }

      if ((caret > 0 && isCellHome(selectionAvailable, event)) ||
          (selectionAvailable && event.is(KeyStrokeSpecs.SELECT_HOME)) ||
          event.is(KeyStrokeSpecs.PREV_WORD_CONTROL.with(ModifierKey.SHIFT))) {
        editor.caretPosition().set(0);
        editor.scrollToCaret();
        event.consume();
        return;
      }

      if ((caret < maxCaret && isCellEnd(selectionAvailable, event)) ||
          (selectionAvailable && event.is(KeyStrokeSpecs.SELECT_END)) ||
          event.is(KeyStrokeSpecs.NEXT_WORD_CONTROL.with(ModifierKey.SHIFT))) {
        editor.caretPosition().set(maxCaret);
        editor.scrollToCaret();
        event.consume();
        return;
      }

    } finally {
      if (event.isConsumed()) {
        if (editor.selectionVisible().get() && !selection) {
          editor.selectionVisible().set(false);
        }
        if (!editor.selectionVisible().get() && selection) {
          editor.selectionStart().set(caret);
          editor.selectionVisible().set(true);
        }
      }
    }

    if (event.is(KeyStrokeSpecs.SELECT_ALL)) {
      editor.selectionStart().set(0);
      editor.caretPosition().set(maxCaret);
      editor.selectionVisible().set(true);
      editor.scrollToCaret();
      event.consume();
      return;
    }

    super.onKeyPressed(cell, event);
  }