void (@NotNull final Project project, @NotNull final Editor editor, final char braceChar) { final int offset = editor.getCaretModel().getOffset() - 1; final Document document = editor.getDocument(); CharSequence chars = document.getCharsSequence(); if (offset < 0 || chars.charAt(offset) != braceChar) return; int spaceStart = CharArrayUtil.shiftBackward(chars, offset - 1, " \t"); if (spaceStart < 0 || chars.charAt(spaceStart) == '\n' || chars.charAt(spaceStart) == '\r'){ final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document); if (file == null || !file.isWritable()) { return; } EditorHighlighter highlighter = editor.getHighlighter(); HighlighterIterator iterator = highlighter.createIterator(offset); final FileType fileType = file.getFileType(); BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator); boolean rBraceToken = braceMatcher.isRBraceToken(iterator, chars, fileType); final boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || rBraceToken; int lBraceOffset = -1; if (CodeInsightSettings.getInstance().REFORMAT_BLOCK_ON_RBRACE && rBraceToken && braceMatcher.isStructuralBrace(iterator, chars, fileType) && offset > 0) { lBraceOffset = BraceMatchingUtil.findLeftLParen( highlighter.createIterator(offset - 1), braceMatcher.getOppositeBraceTokenType(iterator.getTokenType()), editor.getDocument().getCharsSequence(), fileType ); } if (isBrace) { DefaultRawTypedHandler handler = ((TypedActionImpl)TypedAction.getInstance()).getDefaultRawTypedHandler(); handler.beginUndoablePostProcessing(); final int finalLBraceOffset = lBraceOffset; ApplicationManager.getApplication().runWriteAction(() -> { final TypingActionsExtension extension = TypingActionsExtension.findForContext(project, editor); try{ RangeMarker marker = document.createRangeMarker(offset, offset + 1); if (finalLBraceOffset != -1) { extension.format(project, editor, CodeInsightSettings.REFORMAT_BLOCK, finalLBraceOffset, offset, 0, false); } else { extension.format(project, editor, CodeInsightSettings.INDENT_EACH_LINE, offset, offset, 0, false); } editor.getCaretModel().moveToOffset(marker.getStartOffset() + 1); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().removeSelection(); marker.dispose(); } catch(IncorrectOperationException e){ LOG.error(e); } }); } } }