@NotNull private static Map<PsiElement, FoldingDescriptor> buildRanges(@NotNull Editor editor, @NotNull PsiFile psiFile) { final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(psiFile.getLanguage()); final ASTNode node = psiFile.getNode(); if (node == null) return Collections.emptyMap(); final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true); Map<PsiElement, FoldingDescriptor> ranges = new HashMap<PsiElement, FoldingDescriptor>(); for (FoldingDescriptor descriptor : descriptors) { final ASTNode ast = descriptor.getElement(); final PsiElement psi = ast.getPsi(); if (psi != null) { ranges.put(psi, descriptor); } } return ranges; }
private static void getFoldingsFor(@NotNull PsiFile file, @NotNull Document document, @NotNull FoldingMap elementsToFoldMap, boolean quick) { final FileViewProvider viewProvider = file.getViewProvider(); TextRange docRange = TextRange.from(0, document.getTextLength()); for (final Language language : viewProvider.getLanguages()) { final PsiFile psi = viewProvider.getPsi(language); final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if (psi != null && foldingBuilder != null) { for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) { TextRange range = descriptor.getRange(); if (!docRange.contains(range)) { LOG.error("Folding descriptor " + descriptor + " made by " + foldingBuilder + " for " +language + " and called on file " + psi + " is outside document range: " + docRange); } elementsToFoldMap.putValue(descriptor.getElement().getPsi(), descriptor); } } } }
@Nonnull private static Map<PsiElement, FoldingDescriptor> buildRanges(@Nonnull Editor editor, @Nonnull PsiFile psiFile) { final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(psiFile.getLanguage()); final ASTNode node = psiFile.getNode(); if (node == null) return Collections.emptyMap(); final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true); Map<PsiElement, FoldingDescriptor> ranges = new HashMap<>(); for (FoldingDescriptor descriptor : descriptors) { final ASTNode ast = descriptor.getElement(); final PsiElement psi = ast.getPsi(); if (psi != null) { ranges.put(psi, descriptor); } } return ranges; }
private static void getFoldingsFor(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull List<RegionInfo> elementsToFold, boolean quick) { final FileViewProvider viewProvider = file.getViewProvider(); TextRange docRange = TextRange.from(0, document.getTextLength()); for (final Language language : viewProvider.getLanguages()) { final PsiFile psi = viewProvider.getPsi(language); final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if (psi != null && foldingBuilder != null) { 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; } if (!docRange.contains(descriptor.getRange())) { diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement); continue; } RegionInfo regionInfo = new RegionInfo(descriptor, psiElement); elementsToFold.add(regionInfo); } } } }
/** * Checks the ability to initialize folding in the Dumb Mode for file. * * @param file the file to test * @return true if folding initialization available in the Dumb Mode */ public static boolean supportsDumbModeFolding(@NotNull PsiFile file) { final FileViewProvider viewProvider = file.getViewProvider(); for (final Language language : viewProvider.getLanguages()) { final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if(foldingBuilder != null && !DumbService.isDumbAware(foldingBuilder)) return false; } return true; }
private static void getFoldingsFor(@NotNull PsiFile file, @NotNull Document document, @NotNull FoldingMap elementsToFoldMap, boolean quick) { final FileViewProvider viewProvider = file.getViewProvider(); TextRange docRange = TextRange.from(0, document.getTextLength()); for (final Language language : viewProvider.getLanguages()) { final PsiFile psi = viewProvider.getPsi(language); final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if (psi != null && foldingBuilder != null) { for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) { TextRange range = descriptor.getRange(); ASTNode element = descriptor.getElement(); PsiElement psiElement = element.getPsi(); if (!docRange.contains(range)) { PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(file.getProject()); LOG.error("Folding descriptor " + descriptor + " made by " + foldingBuilder + " for " + language + " and called on file " + psi + " is outside document range: " + docRange + ", document committed: " + psiDocumentManager.isCommitted(document) + ", element range: " + element.getTextRange() + ", PSI element: " + psiElement + ", PSI element range: " + (psiElement == null ? null : psiElement.getTextRange()) + ", PSI element is valid: " + (psiElement != null && psiElement.isValid()) + ", PSI file is valid: " + file.isValid() + ", PSI file range: " + file.getTextRange() + ", PSI file corresponds to document: " + (file == psiDocumentManager.getCachedPsiFile(document)), ApplicationManager.getApplication().isInternal() ? new Attachment[] {AttachmentFactory.createAttachment(document), new Attachment("psiTree.txt", DebugUtil.psiToString(psi, false, true))} : new Attachment[0]); } elementsToFoldMap.putValue(psiElement, descriptor); } } } }
protected void doAddForChildren(final XmlElement tag, final List<FoldingDescriptor> foldings, final Document document) { final PsiElement[] children = tag.getChildren(); for (PsiElement child : children) { ProgressManager.checkCanceled(); if (child instanceof XmlTag || child instanceof XmlConditionalSection) { addElementsToFold(foldings, (XmlElement)child, document); } else if (child instanceof XmlComment) { addToFold(foldings, child, document); } else if (child instanceof XmlText || child instanceof XmlProlog) { final PsiElement[] grandChildren = child.getChildren(); for (PsiElement grandChild : grandChildren) { ProgressManager.checkCanceled(); if (grandChild instanceof XmlComment) { addToFold(foldings, grandChild, document); } } } else if(child instanceof XmlAttribute && isAttributeShouldBeFolded((XmlAttribute)child)) { addToFold(foldings, child, document); } else { final Language language = child.getLanguage(); if (!(language instanceof XMLLanguage) && language != Language.ANY) { final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if (foldingBuilder != null) { final FoldingDescriptor[] foldingDescriptors = foldingBuilder.buildFoldRegions(child.getNode(), document); ContainerUtil.addAll(foldings, foldingDescriptors); } } } } }
/** * Checks the ability to initialize folding in the Dumb Mode for file. * * @param file the file to test * @return true if folding initialization available in the Dumb Mode */ static boolean supportsDumbModeFolding(@Nonnull PsiFile file) { final FileViewProvider viewProvider = file.getViewProvider(); for (final Language language : viewProvider.getLanguages()) { final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language); if(foldingBuilder != null && !DumbService.isDumbAware(foldingBuilder)) return false; } return true; }
private static void diagnoseIncorrectRange(@Nonnull PsiFile file, @Nonnull Document document, Language language, FoldingBuilder foldingBuilder, FoldingDescriptor descriptor, PsiElement psiElement) { String message = "Folding descriptor " + descriptor + " made by " + foldingBuilder + " for " + language + " is outside document range" + ", PSI element: " + psiElement + ", PSI element range: " + psiElement.getTextRange() + "; " + DebugUtil.diagnosePsiDocumentInconsistency(psiElement, document); LOG.error(message, ApplicationManager.getApplication().isInternal() ? new Attachment[]{AttachmentFactory.createAttachment(document), new Attachment("psiTree.txt", DebugUtil.psiToString(file, false, true))} : Attachment.EMPTY_ARRAY); }
public static boolean isCollapseByDefault(PsiElement element) { final Language lang = element.getLanguage(); final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(lang); return foldingBuilder != null && foldingBuilder.isCollapsedByDefault(element.getNode()); }