void (@NotNull PsiFile file, final FormatTextRanges ranges, boolean doPostponedFormatting) { final Project project = file.getProject(); Document document = file.getViewProvider().getDocument(); final List<FormatTextRange> textRanges = ranges.getRanges(); if (document instanceof DocumentWindow documentWindow && shouldDelegateToTopLevel(file)) { file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); for (FormatTextRange range : textRanges) { range.setTextRange(documentWindow.injectedToHost(range.getTextRange())); } document = documentWindow.getDelegate(); } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); if (builder != null) { if (file.getTextLength() > 0) { LOG.assertTrue(document != null); if (ranges.isExtendToContext()) { ranges.setExtendedRanges(new ContextFormattingRangesExtender(document, file).getExtendedRanges(ranges.getTextRanges())); } CodeFormattingData formattingData = CodeFormattingData.getOrCreate(file); try { ASTNode containingNode = findContainingNode(file, ranges.getBoundRange()); if (containingNode != null) { for (FormatTextRange range : ranges.getRanges()) { TextRange rangeToUse = preprocess(formattingData, containingNode, range.getTextRange()); range.setTextRange(rangeToUse); } } if (doPostponedFormatting) { invokePostponedFormatting(file, document, textRanges); } if (FORMATTING_CANCELLED_FLAG.get()) { return; } TextRange formattingModelRange = ObjectUtils.notNull(ranges.getBoundRange(), file.getTextRange()); final FormattingModel originalModel = CoreFormatterUtil.buildModel(builder, file, formattingModelRange, mySettings, FormattingMode.REFORMAT); final FormattingModel model = new DocumentBasedFormattingModel(originalModel, document, project, mySettings, file.getFileType(), file); FormatterEx formatter = FormatterEx.getInstanceEx(); if (CodeStyleManager.getInstance(project).isSequentialProcessingAllowed()) { FormattingProgressCallback progressCallback = FormattingProgressCallbackFactory.getInstance().createProgressCallback(project, file, document); if (progressCallback != null) { formatter.setProgressTask(progressCallback); } } CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions(mySettings, project, file, document, textRanges.size() == 1 ? textRanges.get(0).getTextRange() : null); setDisabledRanges(file, ranges); formatter.format(model, mySettings, indentOptions, ranges); } catch (IncorrectOperationException e) { LOG.error(e); } finally { formattingData.dispose(); } } } }