void (@NotNull Editor editor, boolean isWithSelection, boolean ignoreTrailingWhitespace) { Document document = editor.getDocument(); SelectionModel selectionModel = editor.getSelectionModel(); int selectionStart = selectionModel.getLeadSelectionOffset(); CaretModel caretModel = editor.getCaretModel(); LogicalPosition blockSelectionStart = caretModel.getLogicalPosition(); SoftWrapModel softWrapModel = editor.getSoftWrapModel(); int lineNumber = editor.getCaretModel().getLogicalPosition().line; if (lineNumber >= document.getLineCount()) { LogicalPosition pos = new LogicalPosition(lineNumber, 0); editor.getCaretModel().moveToLogicalPosition(pos); setupSelection(editor, isWithSelection, selectionStart, blockSelectionStart); EditorModificationUtilEx.scrollToCaret(editor); return; } VisualPosition currentVisualCaret = editor.getCaretModel().getVisualPosition(); VisualPosition visualEndOfLineWithCaret = new VisualPosition(currentVisualCaret.line, EditorUtil.getLastVisualLineColumnNumber(editor, currentVisualCaret.line), true); // There is a possible case that the caret is already located at the visual end of line and the line is soft wrapped. // We want to move the caret to the end of the logical line then. if (currentVisualCaret.equals(visualEndOfLineWithCaret)) { int offset = editor.visualPositionToOffset(visualEndOfLineWithCaret); if (offset < editor.getDocument().getTextLength()) { int logicalLineEndOffset = EditorUtil.getNotFoldedLineEndOffset(editor, offset); visualEndOfLineWithCaret = editor.offsetToVisualPosition(logicalLineEndOffset, true, false); } } LogicalPosition logLineEnd = editor.visualToLogicalPosition(visualEndOfLineWithCaret); int offset = editor.logicalPositionToOffset(logLineEnd); lineNumber = logLineEnd.line; int newOffset = offset; CharSequence text = document.getCharsSequence(); for (int i = newOffset - 1; i >= document.getLineStartOffset(lineNumber); i--) { if (softWrapModel.getSoftWrap(i) != null) { newOffset = offset; break; } if (text.charAt(i) != ' ' && text.charAt(i) != '\t') { break; } newOffset = i; } // Move to the calculated end of visual line if caret is located on a last non-white space symbols on a line and there are // remaining white space symbols. if (newOffset == offset || newOffset == caretModel.getOffset() || !ignoreTrailingWhitespace) { caretModel.moveToVisualPosition(visualEndOfLineWithCaret); } else { if (editor instanceof EditorImpl) { caretModel.moveToLogicalPosition(editor.offsetToLogicalPosition(newOffset).leanForward(true)); } else { caretModel.moveToOffset(newOffset); } } EditorModificationUtilEx.scrollToCaret(editor); setupSelection(editor, isWithSelection, selectionStart, blockSelectionStart); }