Runnable (@NotNull Editor editor, @NotNull PsiFile file, boolean applyDefaultState) { if (file instanceof PsiCompiledElement) return null; boolean codeFoldingForInjectedEnabled = editor.getUserData(INJECTED_CODE_FOLDING_ENABLED) != Boolean.FALSE; ApplicationManager.getApplication().assertReadAccessAllowed(); Project project = file.getProject(); Document document = editor.getDocument(); LOG.assertTrue(!PsiDocumentManager.getInstance(project).isUncommited(document)); FoldingModel foldingModel = editor.getFoldingModel(); long timeStamp = document.getModificationStamp(); Object lastTimeStamp = editor.getUserData(LAST_UPDATE_INJECTED_STAMP_KEY); if (lastTimeStamp instanceof Long && ((Long)lastTimeStamp).longValue() == timeStamp) return null; List<DocumentWindow> injectedDocuments = InjectedLanguageManager.getInstance(project).getCachedInjectedDocumentsInRange(file, file.getTextRange()); if (injectedDocuments.isEmpty()) return null; List<EditorWindow> injectedEditors = new ArrayList<>(); List<PsiFile> injectedFiles = new ArrayList<>(); List<List<RegionInfo>> lists = new ArrayList<>(); for (DocumentWindow injectedDocument : injectedDocuments) { if (!injectedDocument.isValid()) { continue; } InjectedLanguageUtil.enumerate(injectedDocument, file, (injectedFile, places) -> { if (!injectedFile.isValid()) return; Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile); if (!(injectedEditor instanceof EditorWindow)) return; injectedEditors.add((EditorWindow)injectedEditor); injectedFiles.add(injectedFile); List<RegionInfo> map = new ArrayList<>(); lists.add(map); if (codeFoldingForInjectedEnabled) { getFoldingsFor(injectedFile, map, false); } }); } return () -> { ArrayList<Runnable> updateOperations = new ArrayList<>(injectedEditors.size()); for (int i = 0; i < injectedEditors.size(); i++) { EditorWindow injectedEditor = injectedEditors.get(i); PsiFile injectedFile = injectedFiles.get(i); if (!injectedEditor.getDocument().isValid()) continue; List<RegionInfo> list = lists.get(i); updateOperations.add(new UpdateFoldRegionsOperation(project, injectedEditor, injectedFile, list, applyDefaultStateMode(applyDefaultState), !applyDefaultState, true)); } foldingModel.runBatchFoldingOperation(() -> { for (Runnable operation : updateOperations) { operation.run(); } }); EditorFoldingInfo info = EditorFoldingInfo.get(editor); for (FoldRegion region : editor.getFoldingModel().getAllFoldRegions()) { FoldingRegionWindow injectedRegion = FoldingRegionWindow.getInjectedRegion(region); if (injectedRegion != null && !injectedRegion.isValid()) { info.removeRegion(region); } } editor.putUserData(LAST_UPDATE_INJECTED_STAMP_KEY, timeStamp); }; }