Search in sources :

Example 26 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class UpdateFoldRegionsOperation method addNewRegions.

private List<FoldRegion> addNewRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    List<FoldRegion> newRegions = new ArrayList<>();
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    for (PsiElement element : elementsToFold.keySet()) {
        ProgressManager.checkCanceled();
        final Collection<FoldingDescriptor> descriptors = elementsToFold.get(element);
        for (FoldingDescriptor descriptor : descriptors) {
            FoldingGroup group = descriptor.getGroup();
            TextRange range = descriptor.getRange();
            String placeholder = null;
            try {
                placeholder = descriptor.getPlaceholderText();
            } catch (IndexNotReadyException ignore) {
            }
            if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
                LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
                continue;
            }
            FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
            if (region == null)
                continue;
            PsiElement psi = descriptor.getElement().getPsi();
            if (psi == null || !psi.isValid() || !foldingModel.addFoldRegion(region) || !myFile.isValid()) {
                region.dispose();
                continue;
            }
            if (descriptor.canBeRemovedWhenCollapsed())
                region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
            info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
            newRegions.add(region);
            boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(element, range, rangeToExpandStatusMap);
            if (group == null) {
                shouldExpand.put(region, expandStatus);
            } else {
                final Boolean alreadyExpanded = groupExpand.get(group);
                groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
            }
        }
    }
    return newRegions;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) SmartPointerManager(com.intellij.psi.SmartPointerManager) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) PsiElement(com.intellij.psi.PsiElement)

Example 27 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class JavaFoldingBuilderBase method buildLanguageFoldRegions.

@Override
protected void buildLanguageFoldRegions(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, boolean quick) {
    if (!(root instanceof PsiJavaFile)) {
        return;
    }
    PsiJavaFile file = (PsiJavaFile) root;
    PsiImportList importList = file.getImportList();
    if (importList != null) {
        PsiImportStatementBase[] statements = importList.getAllImportStatements();
        if (statements.length > 1) {
            final TextRange rangeToFold = getRangeToFold(importList);
            if (rangeToFold != null && rangeToFold.getLength() > 1) {
                FoldingDescriptor descriptor = new FoldingDescriptor(importList, rangeToFold);
                // imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
                descriptor.setCanBeRemovedWhenCollapsed(true);
                descriptors.add(descriptor);
            }
        }
    }
    PsiClass[] classes = file.getClasses();
    for (PsiClass aClass : classes) {
        ProgressManager.checkCanceled();
        ProgressIndicatorProvider.checkCanceled();
        addElementsToFold(descriptors, aClass, document, true, quick);
    }
    TextRange range = getFileHeader(file);
    if (range != null && range.getLength() > 1 && document.getLineNumber(range.getEndOffset()) > document.getLineNumber(range.getStartOffset())) {
        PsiElement anchorElementToUse = file;
        PsiElement candidate = file.getFirstChild();
        // So, our point is to preserve fold descriptor referencing javadoc PSI element.
        if (candidate != null && candidate.getTextRange().equals(range)) {
            ASTNode node = candidate.getNode();
            if (node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) {
                anchorElementToUse = candidate;
            }
        }
        descriptors.add(new FoldingDescriptor(anchorElementToUse, range));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 28 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class JavaFoldingBuilderBase method addFoldRegion.

private static boolean addFoldRegion(@NotNull List<FoldingDescriptor> list, @NotNull PsiElement elementToFold, @NotNull Document document, boolean allowOneLiners, @NotNull TextRange range) {
    final TextRange fileRange = elementToFold.getContainingFile().getTextRange();
    if (range.equals(fileRange))
        return false;
    LOG.assertTrue(range.getStartOffset() >= 0 && range.getEndOffset() <= fileRange.getEndOffset());
    // PSI element text ranges may be invalid because of reparse exception (see, for example, IDEA-10617)
    if (range.getStartOffset() < 0 || range.getEndOffset() > fileRange.getEndOffset()) {
        return false;
    }
    if (!allowOneLiners) {
        int startLine = document.getLineNumber(range.getStartOffset());
        int endLine = document.getLineNumber(range.getEndOffset() - 1);
        if (startLine < endLine && range.getLength() > 1) {
            list.add(new FoldingDescriptor(elementToFold, range));
            return true;
        }
        return false;
    } else {
        if (range.getLength() > getPlaceholderText(elementToFold).length()) {
            list.add(new FoldingDescriptor(elementToFold, range));
            return true;
        }
        return false;
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 29 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class DocumentFoldingInfo method buildRanges.

@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<>();
    for (FoldingDescriptor descriptor : descriptors) {
        final ASTNode ast = descriptor.getElement();
        final PsiElement psi = ast.getPsi();
        if (psi != null) {
            ranges.put(psi, descriptor);
        }
    }
    return ranges;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingBuilder(com.intellij.lang.folding.FoldingBuilder) ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class XmlCodeFoldingBuilder method addToFold.

protected boolean addToFold(List<FoldingDescriptor> foldings, PsiElement elementToFold, Document document) {
    PsiUtilCore.ensureValid(elementToFold);
    TextRange range = getRangeToFold(elementToFold);
    if (range == null)
        return false;
    if (range.getStartOffset() >= 0 && range.getEndOffset() <= elementToFold.getContainingFile().getTextRange().getEndOffset() && // psi and document maybe not in sync after error
    range.getEndOffset() <= document.getTextLength()) {
        int startLine = document.getLineNumber(range.getStartOffset());
        int endLine = document.getLineNumber(range.getEndOffset() - 1);
        final boolean entity = isEntity(elementToFold);
        if (startLine < endLine || elementToFold instanceof XmlAttribute || entity) {
            if (range.getStartOffset() + MIN_TEXT_RANGE_LENGTH < range.getEndOffset() || entity) {
                foldings.add(new FoldingDescriptor(elementToFold.getNode(), range));
                return true;
            }
        }
    }
    return false;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Aggregations

FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)39 TextRange (com.intellij.openapi.util.TextRange)24 ASTNode (com.intellij.lang.ASTNode)17 PsiElement (com.intellij.psi.PsiElement)12 IElementType (com.intellij.psi.tree.IElementType)9 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)8 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)6 PsiComment (com.intellij.psi.PsiComment)5 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)5 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)4 NotNull (org.jetbrains.annotations.NotNull)4 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 LinkedList (java.util.LinkedList)3 HbBlockWrapper (com.dmarcotte.handlebars.psi.HbBlockWrapper)2 FoldingBuilder (com.intellij.lang.folding.FoldingBuilder)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)1