Search in sources :

Example 6 with FoldingDescriptor

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

the class PythonFoldingBuilder method foldSequentialComments.

private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
    //do not start folded comments from custom region
    if (isCustomRegionElement(node.getPsi())) {
        return;
    }
    //need to skip previous comments in sequence
    ASTNode curNode = node.getTreePrev();
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            return;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
    }
    //fold sequence comments in one block
    curNode = node.getTreeNext();
    ASTNode lastCommentNode = node;
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            //do not end folded comments with custom region
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            lastCommentNode = curNode;
            curNode = curNode.getTreeNext();
            continue;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreeNext() : null;
    }
    if (lastCommentNode != node) {
        descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 7 with FoldingDescriptor

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

the class PropertyFoldingBuilder method checkLiteral.

private static void checkLiteral(PsiLiteralExpression expression, List<FoldingDescriptor> result) {
    if (isI18nProperty(expression)) {
        final IProperty property = getI18nProperty(expression);
        final HashSet<Object> set = new HashSet<>();
        set.add(property != null ? property : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
        final String msg = formatI18nProperty(expression, property);
        final PsiElement parent = expression.getParent();
        if (!msg.equals(expression.getText()) && parent instanceof PsiExpressionList && ((PsiExpressionList) parent).getExpressions()[0] == expression) {
            final PsiExpressionList expressions = (PsiExpressionList) parent;
            final int count = JavaI18nUtil.getPropertyValueParamsMaxCount(expression);
            final PsiExpression[] args = expressions.getExpressions();
            if (args.length == 1 + count && parent.getParent() instanceof PsiMethodCallExpression) {
                boolean ok = true;
                for (int i = 1; i < count + 1; i++) {
                    Object value = JavaConstantExpressionEvaluator.computeConstantExpression(args[i], false);
                    if (value == null) {
                        if (!(args[i] instanceof PsiReferenceExpression)) {
                            ok = false;
                            break;
                        }
                    }
                }
                if (ok) {
                    result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(parent.getParent().getNode()), parent.getParent().getTextRange(), null, set));
                    return;
                }
            }
        }
        result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(expression.getNode()), expression.getTextRange(), null, set));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) IProperty(com.intellij.lang.properties.IProperty)

Example 8 with FoldingDescriptor

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

the class FoldingUpdate method getUpdateResult.

private static CachedValueProvider.Result<Runnable> getUpdateResult(PsiFile file, @NotNull Document document, boolean quick, final Project project, final Editor editor, final boolean applyDefaultState) {
    final FoldingMap elementsToFoldMap = getFoldingsFor(file, document, quick);
    final UpdateFoldRegionsOperation operation = new UpdateFoldRegionsOperation(project, editor, file, elementsToFoldMap, applyDefaultState ? EXCEPT_CARET_REGION : NO, !applyDefaultState, false);
    Runnable runnable = () -> editor.getFoldingModel().runBatchFoldingOperationDoNotCollapseCaret(operation);
    Set<Object> dependencies = new HashSet<>();
    dependencies.add(document);
    dependencies.add(editor.getFoldingModel());
    for (FoldingDescriptor descriptor : elementsToFoldMap.values()) {
        dependencies.addAll(descriptor.getDependencies());
    }
    return CachedValueProvider.Result.create(runnable, ArrayUtil.toObjectArray(dependencies));
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor)

Example 9 with FoldingDescriptor

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

the class FoldingUpdate method getFoldingsFor.

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)) {
                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;
                }
                elementsToFoldMap.putValue(psiElement, descriptor);
            }
        }
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingBuilder(com.intellij.lang.folding.FoldingBuilder) Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange)

Example 10 with FoldingDescriptor

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

the class UpdateFoldRegionsOperation method removeInvalidRegions.

private void removeInvalidRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap) {
    List<FoldRegion> toRemove = new ArrayList<>();
    InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(myProject);
    for (FoldRegion region : foldingModel.getAllFoldRegions()) {
        if (myKeepCollapsedRegions && !region.isExpanded() && !regionOrGroupCanBeRemovedWhenCollapsed(region))
            continue;
        PsiElement element = info.getPsiElement(region);
        if (element != null) {
            PsiFile containingFile = element.getContainingFile();
            boolean isInjected = injectedManager.isInjectedFragment(containingFile);
            if (isInjected != myForInjected)
                continue;
        }
        final Collection<FoldingDescriptor> descriptors;
        if (element != null && !(descriptors = elementsToFold.get(element)).isEmpty()) {
            boolean matchingDescriptorFound = false;
            FoldingDescriptor[] array = descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
            for (FoldingDescriptor descriptor : array) {
                TextRange range = descriptor.getRange();
                if (TextRange.areSegmentsEqual(region, range)) {
                    matchingDescriptorFound = true;
                    if (!region.isValid() || region.getGroup() != null || descriptor.getGroup() != null || !region.getPlaceholderText().equals(descriptor.getPlaceholderText()) || range.getLength() < 2) {
                        rangeToExpandStatusMap.put(range, region.isExpanded());
                        toRemove.add(region);
                        break;
                    } else {
                        elementsToFold.remove(element, descriptor);
                    }
                }
            }
            if (!matchingDescriptorFound) {
                if (Registry.is("editor.durable.folding.state")) {
                    for (FoldingDescriptor descriptor : descriptors) {
                        rangeToExpandStatusMap.put(descriptor.getRange(), region.isExpanded());
                    }
                }
                toRemove.add(region);
            }
        } else if (region.isValid() && info.isLightRegion(region)) {
            boolean isExpanded = region.isExpanded();
            rangeToExpandStatusMap.put(TextRange.create(region), isExpanded);
        } else {
            toRemove.add(region);
        }
    }
    for (final FoldRegion region : toRemove) {
        foldingModel.removeFoldRegion(region);
        info.removeRegion(region);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

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