PsiElement (@NotNull PsiElement element, @NotNull TextRange range, boolean canChangeWhiteSpaceOnly) { PsiFile file = element.getContainingFile(); FormattingContext formattingContext = FormattingContext.create(file, range, CodeStyle.getSettings(file), FormattingMode.REFORMAT); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject()); Document document = documentManager.getDocument(file); if (document == null) { document = file.getUserData(DOCUMENT_KEY); } if (document != null) { int offset = element.getTextOffset(); formatDocument(document, Collections.singletonList(range), formattingContext, canChangeWhiteSpaceOnly, false); documentManager.commitDocument(document); PsiElement resultingElement = file.findElementAt(offset); if (resultingElement != null) { return resultingElement; } } return element; }