void (PsiFile file) { Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); if (document == null) { return; } int lastBlankLineOffset = -1; for (int i = document.getLineCount() - 1; i >= 0; i--) { int lineStart = document.getLineStartOffset(i); String trimmed = document.getCharsSequence().subSequence(lineStart, document.getLineEndOffset(i)).toString().trim(); if (trimmed.length() > 0) { break; } lastBlankLineOffset = lineStart; } if (lastBlankLineOffset > 0) { document.deleteString(lastBlankLineOffset, document.getTextLength()); } }