private static void removeTrailingBlankLines(PsiFile file) {
    Document document = file.getFileDocument();
    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());
    }
  }
private static void removeTrailingBlankLines(PsiFile file)