Search in sources :

Example 1 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.

the class GroovyFoldingBuilder method addFoldingForStrings.

private static void addFoldingForStrings(List<FoldingDescriptor> descriptors, ASTNode node) {
    if (!isMultiLineStringLiteral(node))
        return;
    if (!node.getElementType().equals(GroovyElementTypes.GSTRING) && !node.getElementType().equals(GroovyElementTypes.REGEX)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
        return;
    }
    final GrString grString = (GrString) node.getPsi();
    if (grString == null)
        return;
    final GrStringInjection[] injections = grString.getInjections();
    if (injections.length == 0) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
        return;
    }
    final String start_quote = GrStringUtil.getStartQuote(node.getText());
    final String end_quote = GrStringUtil.getEndQuote(node.getText());
    final FoldingGroup group = FoldingGroup.newGroup("GString");
    final TextRange nodeRange = node.getTextRange();
    int startOffset = nodeRange.getStartOffset();
    GrStringInjection injection = injections[0];
    TextRange injectionRange = injection.getTextRange();
    if (startOffset + 1 < injectionRange.getStartOffset()) {
        descriptors.add(new NamedFoldingDescriptor(node, startOffset, injectionRange.getStartOffset(), group, start_quote));
    }
    final String placeholder = " ";
    startOffset = injectionRange.getEndOffset();
    for (int i = 1; i < injections.length; i++) {
        injection = injections[i];
        injectionRange = injection.getTextRange();
        final int endOffset = injectionRange.getStartOffset();
        if (endOffset - startOffset >= 2) {
            descriptors.add(new NamedFoldingDescriptor(injection.getNode().getTreePrev(), startOffset, endOffset, group, placeholder));
        }
        startOffset = injectionRange.getEndOffset();
    }
    if (startOffset + 1 < nodeRange.getEndOffset()) {
        descriptors.add(new NamedFoldingDescriptor(node.getLastChildNode(), startOffset, nodeRange.getEndOffset(), group, end_quote));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) TextRange(com.intellij.openapi.util.TextRange) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 2 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.

the class GroovyFoldingBuilder method collapseBlock.

private static void collapseBlock(List<FoldingDescriptor> descriptors, PsiElement psi) {
    if (psi instanceof GrCodeBlock) {
        final int lineFeedCount = StringUtil.countChars(psi.getText(), '\n');
        if (lineFeedCount <= 2) {
            final PsiElement lbrace = ((GrCodeBlock) psi).getLBrace();
            final PsiElement rbrace = ((GrCodeBlock) psi).getRBrace();
            if (lbrace != null && rbrace != null) {
                final PsiElement next = lbrace.getNextSibling();
                final PsiElement prev = rbrace.getPrevSibling();
                if (next != null && PsiImplUtil.isWhiteSpaceOrNls(next) && prev != null && PsiImplUtil.isWhiteSpaceOrNls(prev)) {
                    final FoldingGroup group = FoldingGroup.newGroup("block_group");
                    descriptors.add(new NamedFoldingDescriptor(psi, lbrace.getTextRange().getStartOffset(), next.getTextRange().getEndOffset(), group, "{"));
                    descriptors.add(new NamedFoldingDescriptor(psi, prev.getTextRange().getStartOffset(), rbrace.getTextRange().getEndOffset(), group, "}"));
                    return;
                }
            }
        }
    }
    descriptors.add(new FoldingDescriptor(psi, psi.getTextRange()));
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 3 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.

the class UpdateFoldRegionsOperation method applyExpandStatus.

private static void applyExpandStatus(@NotNull List<FoldRegion> newRegions, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    for (final FoldRegion region : newRegions) {
        final FoldingGroup group = region.getGroup();
        final Boolean expanded = group == null ? shouldExpand.get(region) : groupExpand.get(group);
        if (expanded != null) {
            region.setExpanded(expanded.booleanValue());
        }
    }
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 4 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup in project idea-php-typo3-plugin by cedricziel.

the class RouteFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Route");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        for (PsiReference reference : literalExpression.getReferences()) {
            if (reference instanceof RouteReference) {
                String value = literalExpression.getContents();
                FoldingDescriptor descriptor = foldRouteReferenceString(reference, value, group);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                }
            }
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup in project idea-php-typo3-plugin by cedricziel.

the class TranslationFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Translation");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        String value = literalExpression.getContents();
        if (value.startsWith("LLL:")) {
            Project project = literalExpression.getProject();
            final List<StubTranslation> properties = TranslationIndex.findById(project, value);
            StubTranslation defaultTranslation = findDefaultTranslationFromVariants(properties);
            if (defaultTranslation != null) {
                TextRange foldingRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
                descriptors.add(new FoldingDescriptor(literalExpression.getNode(), foldingRange, group) {

                    @Nullable
                    @Override
                    public String getPlaceholderText() {
                        PsiElement[] definitionElements = TranslationUtil.findDefinitionElements(project, value);
                        for (PsiElement definitionElement : definitionElements) {
                            if (definitionElement instanceof XmlTag) {
                                if (((XmlTag) definitionElement).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                            if (definitionElement instanceof XmlAttributeValue) {
                                if (((XmlTag) definitionElement.getParent().getParent()).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement.getParent().getParent()).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
            }
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Project(com.intellij.openapi.project.Project) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FoldingGroup (com.intellij.openapi.editor.FoldingGroup)12 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)6 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)5 FoldRegion (com.intellij.openapi.editor.FoldRegion)5 TextRange (com.intellij.openapi.util.TextRange)5 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 ArrayList (java.util.ArrayList)3 PsiReference (com.intellij.psi.PsiReference)2 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 SmartPointerManager (com.intellij.psi.SmartPointerManager)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1