void (PsiElement element) { final List<PsiStatement> emptyElseBranches = new SmartList<>(); element.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitIfStatement(@NotNull PsiIfStatement statement) { super.visitIfStatement(statement); final PsiStatement elseBranch = statement.getElseBranch(); if (ControlFlowUtils.isEmpty(elseBranch, false, true)) { emptyElseBranches.add(elseBranch); } } }); for (PsiStatement statement : emptyElseBranches) { final List<PsiComment> comments = new ArrayList<>(PsiTreeUtil.collectElementsOfType(statement, PsiComment.class)); final PsiParserFacade parserFacade = PsiParserFacade.getInstance(statement.getProject()); for (int i = comments.size() - 1; i >= 0; i--) { final PsiElement parent = statement.getParent(); final PsiComment comment = comments.get(i); parent.addAfter(comment, statement); // newline followed by space convinces formatter to indent line parent.addAfter(parserFacade.createWhiteSpaceFromText(isAtStartOfLine(comment) ? "\n" : "\n "), statement); } statement.delete(); } }