T (PsiFile file, int offset, @Nullable TextRange range, T defaultValue) { if (file instanceof PsiCompiledFile) { file = ((PsiCompiledFile)file).getDecompiledPsiFile(); } PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject()); Document document = documentManager.getDocument(file); if (document instanceof DocumentWindow documentWindow && CodeFormatterFacade.shouldDelegateToTopLevel(file)) { final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); if (!file.equals(topLevelFile)) { if (range != null) { range = documentWindow.injectedToHost(range); } if (offset != -1) { offset = documentWindow.injectedToHost(offset); } // see `InjectedLanguageBlockBuilder` to configure injected blocks formatting in the host formatter return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow); } } final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file); if (templateFile != null) { file = templateFile; document = documentManager.getDocument(templateFile); } PsiElement element = null; if (offset != -1) { element = CoreCodeStyleUtil.findElementInTreeWithFormatterEnabled(file, offset); if (element == null && offset != file.getTextLength()) { return defaultValue; } if (isInsidePlainComment(offset, element)) { return computeValueInsidePlainComment(file, offset, defaultValue); } } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder; if (builder != null && elementBuilder != null) { mySettings = CodeStyle.getSettings(file); mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null; myIndentOptions = CodeFormatterFacade.getIndentOptions(mySettings, myCodeStyleManager.getProject(), file, document, mySignificantRange); FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode(); myCodeStyleManager.setCurrentFormattingMode(myMode); try { TextRange modelRange = range != null ? range : offset != -1 ? TextRange.create(offset, offset) : file.getTextRange(); myModel = buildModel(builder, file, modelRange, document); T result = doPerform(offset, range); if (result != null) { return result; } } finally { myCodeStyleManager.setCurrentFormattingMode(currentMode); } } return defaultValue; }