IntelliJ Platform SDK DevGuide

Edit page

12. Folding Builder

A folding builder helps you to fold the code regions and replace it with specific text.

12.1. Define a folding builder

Let’s replace usages of properties with its values by default.

package com.simpleplugin; import com.intellij.lang.ASTNode; import com.intellij.lang.folding.*; import com.intellij.openapi.editor.*; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.simpleplugin.psi.SimpleProperty; import org.jetbrains.annotations.*; import java.util.*; public class SimpleFoldingBuilder extends FoldingBuilderEx { @NotNull @Override public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) { FoldingGroup group = FoldingGroup.newGroup("simple"); List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>(); Collection<PsiLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, PsiLiteralExpression.class); for (final PsiLiteralExpression literalExpression : literalExpressions) { String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null; if (value != null && value.startsWith("simple:")) { Project project = literalExpression.getProject(); String key = value.substring(7); final List<SimpleProperty> properties = SimpleUtil.findProperties(project, key); if (properties.size() == 1) { descriptors.add(new FoldingDescriptor(literalExpression.getNode(), new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1), group) { @Nullable @Override public String getPlaceholderText() { // IMPORTANT: keys can come with no values, so a test for null is needed // IMPORTANT: Convert embedded \n to backslash n, so that the string will look like it has LF embedded // in it and embedded " to escaped " String valueOf = properties.get(0).getValue(); return valueOf == null ? "" : valueOf.replaceAll("\n", "\\n").replaceAll("\"", "\\\\\""); } }); } } } return descriptors.toArray(new FoldingDescriptor[descriptors.size()]); } @Nullable @Override public String getPlaceholderText(@NotNull ASTNode node) { return "..."; } @Override public boolean isCollapsedByDefault(@NotNull ASTNode node) { return true; } }

12.2. Register the folding builder

<lang.foldingBuilder language="JAVA" implementationClass="com.simpleplugin.SimpleFoldingBuilder"/>

12.3. Run the project

Now when we open a Java file, it shows the property’s value instead of the key.

Folding

Last modified: 6 October 2017