void (@NotNull PsiFile file, @NotNull Document document, @NotNull Editor editor, int caretOffset, int rBracesInsertOffset, String generatedRBraces) { Project project = file.getProject(); long stamp = document.getModificationStamp(); boolean closingBraceIndentAdjusted; try { PsiDocumentManager.getInstance(project).commitDocument(document); CodeStyleManager.getInstance(project).adjustLineIndent(file, new TextRange(caretOffset, rBracesInsertOffset + 2)); } catch (IncorrectOperationException e) { LOG.error(e); } finally { closingBraceIndentAdjusted = stamp != document.getModificationStamp(); // do you remember that we insert the '\n'? here we take it back! document.deleteString(caretOffset, caretOffset + 1); } // There is a possible case that formatter was unable to adjust line indent for the closing brace (that is the case for plain text // document for example). Hence, we're trying to do the manually. if (!closingBraceIndentAdjusted) { int line = document.getLineNumber(rBracesInsertOffset); StringBuilder buffer = new StringBuilder(); int start = document.getLineStartOffset(line); int end = document.getLineEndOffset(line); final CharSequence text = document.getCharsSequence(); for (int i = start; i < end; i++) { char c = text.charAt(i); if (c != ' ' && c != '\t') { break; } else { buffer.append(c); } } if (buffer.length() > 0) { document.insertString(rBracesInsertOffset + 1, buffer); } } }