public static void insertNewLineAtCaret(Editor editor) {
    EditorUIUtil.hideCursorInEditor(editor);
    Document document = editor.getDocument();
    if(!editor.isInsertMode()) {
      int caretLine = editor.getCaretModel().getLogicalPosition().line;
      int lineCount = document.getLineCount();
      if(caretLine < lineCount) {
        if (caretLine == lineCount - 1) {
          document.insertString(document.getTextLength(), "\n");
        }
        LogicalPosition pos = new LogicalPosition(caretLine + 1, 0);
        editor.getCaretModel().moveToLogicalPosition(pos);
        editor.getSelectionModel().removeSelection();
        EditorModificationUtil.scrollToCaret(editor);
      }
      return;
    }
    EditorModificationUtil.deleteSelectedText(editor);
    // Smart indenting here:
    CharSequence text = document.getCharsSequence();
    int caretOffset = editor.getCaretModel().getOffset();
    int lineStartOffset = DocumentUtil.getLineStartOffset(caretOffset, document);
    int lineStartWsEndOffset = CharArrayUtil.shiftForward(text, lineStartOffset, " \t");
    String s = "\n" + text.subSequence(lineStartOffset, Math.min(caretOffset, lineStartWsEndOffset));
    document.insertString(caretOffset, s);
    editor.getCaretModel().moveToOffset(caretOffset + s.length());
    EditorModificationUtil.scrollToCaret(editor);
    editor.getSelectionModel().removeSelection();
  }
public static void insertNewLineAtCaret(Editor editor)