void (final @NotNull Document document) { TrailingSpacesOptions options = getOptions(document); if (options == null) return; if (options.isStripTrailingSpaces()) { boolean success = strip(document, options.isChangedLinesOnly(), options.isKeepTrailingSpacesOnCaretLine()); if (!success) { myDocumentsToStripLater.add(document); } } if (options.isRemoveTrailingBlankLines()) { removeTrailngBlankLines(document, options.isEnsureNewLineAtEOF()); } final int lines = document.getLineCount(); if (options.isEnsureNewLineAtEOF() && lines > 0) { final int start = document.getLineStartOffset(lines - 1); final int end = document.getLineEndOffset(lines - 1); if (start != end) { final CharSequence content = document.getCharsSequence(); performUndoableWrite(new DocumentRunnable(document, null) { @Override public void run() { if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && options.isStripTrailingSpaces() && !(options.isKeepTrailingSpacesOnCaretLine() && hasCaretIn(start, end))) { document.deleteString(start, end); } else { document.insertString(end, "\n"); } } private boolean hasCaretIn(int start, int end) { for (Editor activeEditor : getActiveEditors(document)) { for (Caret caret : activeEditor.getCaretModel().getAllCarets()) { int offset = caret.getOffset(); if (offset >= start && offset <= end) return true; } } return false; } }); } } }