void (@NotNull Editor editor, boolean preferVerticalScroll) { editor.getSelectionModel().removeSelection(); Document document = editor.getDocument(); int lastLine = Math.max(0, document.getLineCount() - 1); boolean caretWasAtLastLine = editor.getCaretModel().getLogicalPosition().line == lastLine; editor.getCaretModel().moveToOffset(document.getTextLength()); ScrollingModel scrollingModel = editor.getScrollingModel(); if (preferVerticalScroll && document.getLineStartOffset(lastLine) == document.getLineEndOffset(lastLine)) { // don't move 'focus' to empty last line int scrollOffset; if (editor instanceof EditorEx) { JScrollBar verticalScrollBar = ((EditorEx)editor).getScrollPane().getVerticalScrollBar(); scrollOffset = verticalScrollBar.getMaximum() - verticalScrollBar.getModel().getExtent(); } else { scrollOffset = editor.getContentComponent().getHeight() - scrollingModel.getVisibleArea().height; } scrollingModel.scrollVertically(scrollOffset); } else if (!caretWasAtLastLine) { // don't scroll to the end of the last line (IDEA-124688)... scrollingModel.scrollTo(new LogicalPosition(lastLine, 0), ScrollType.RELATIVE); } else { // ...unless the caret was already on the last line - then scroll to the end of it. scrollingModel.scrollToCaret(ScrollType.RELATIVE); } }