void (@NotNull PsiFile file, @NotNull List<? super RegionInfo> elementsToFold, boolean quick) { FileViewProvider viewProvider = file.getViewProvider(); Document document = viewProvider.getDocument(); if (document == null) { LOG.error("No document for " + viewProvider); return; } LOG.assertTrue(PsiDocumentManager.getInstance(file.getProject()).isCommitted(document)); int textLength = document.getTextLength(); List<RangeMarker> hardRefToRangeMarkers = new ArrayList<>(); Comparator<PsiFile> preferBaseLanguage = Comparator.comparing((PsiFile f) -> f.getLanguage() != viewProvider.getBaseLanguage()).thenComparing(f->f.getLanguage().getID()); List<PsiFile> allFiles = ContainerUtil.sorted(viewProvider.getAllFiles(), preferBaseLanguage); DocumentEx copyDoc = allFiles.size() > 1 ? new DocumentImpl(document.getImmutableCharSequence(), document instanceof DocumentImpl && ((DocumentImpl)document).acceptsSlashR(), true) : null; for (PsiFile psi : allFiles) { Language language = psi.getLanguage(); FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if (foldingBuilder != null) { if (psi.getTextLength() != textLength) { LOG.error(DebugUtil.diagnosePsiDocumentInconsistency(psi, document)); return; } PsiFile containingFile = PsiUtilCore.getTemplateLanguageFile(file); for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) { PsiElement psiElement = descriptor.getElement().getPsi(); if (psiElement == null) { LOG.error("No PSI for folding descriptor " + descriptor); continue; } // in case of CompositeBuilder, the assertion was already checked in CompositeFoldingBuilder.buildFoldRegions if (!(foldingBuilder instanceof CompositeFoldingBuilder)) { CompositeFoldingBuilder.assertSameFile(containingFile, descriptor, psiElement, foldingBuilder); } TextRange range = descriptor.getRange(); if (range.getEndOffset() > textLength) { diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement); continue; } if (copyDoc != null && !addNonConflictingRegion(copyDoc, range, hardRefToRangeMarkers)) { continue; } RegionInfo regionInfo = new RegionInfo(descriptor, psiElement, foldingBuilder); elementsToFold.add(regionInfo); } } } }