diff --git a/_SUMMARY.md b/_SUMMARY.md index aa9a48c2e..bd2cdd8a0 100644 --- a/_SUMMARY.md +++ b/_SUMMARY.md @@ -52,7 +52,7 @@ * [Completion Contributor](completion_contributor.html) * [Reference Contributor](reference_contributor.html) * [Find Usages Provider](find_usages_provider.html) - * [Folding Builder](TODO) + * [Folding Builder](folding_builder.html) * [Go To Symbol Contributor](TODO) * [Structure View Factory](TODO) * [Formatter](TODO) diff --git a/folding_builder.md b/folding_builder.md new file mode 100644 index 000000000..b9c450f4b --- /dev/null +++ b/folding_builder.md @@ -0,0 +1,94 @@ +--- +title: Folding Builder +--- + + + +A folding builder helps you to fold the code regions and replace it with specific text. + +### 1. Define a folding builder + +Let's replace usages of properties with its values by default. + +```java +package com.simpleplugin; + +import com.intellij.lang.ASTNode; +import com.intellij.lang.folding.FoldingBuilderEx; +import com.intellij.lang.folding.FoldingDescriptor; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.FoldingGroup; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiLiteralExpression; +import com.intellij.psi.util.PsiTreeUtil; +import com.simpleplugin.psi.SimpleProperty; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class SimpleFoldingBuilder extends FoldingBuilderEx { + @NotNull + @Override + public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) { + FoldingGroup group = FoldingGroup.newGroup("simple"); + + List descriptors = new ArrayList(); + Collection literalExpressions = PsiTreeUtil.findChildrenOfType(root, PsiLiteralExpression.class); + for (final PsiLiteralExpression literalExpression : literalExpressions) { + String value = (String) literalExpression.getValue(); + if (value != null && value.startsWith("simple:")) { + Project project = literalExpression.getProject(); + final List properties = SimpleUtil.findProperties(project, value.substring(7)); + 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() { + return properties.get(0).getValue(); + } + }); + } + } + } + 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; + } +} +``` + +### 2. Register the folding builder + +```xml + +``` + +### 3. Run the project + +Now when we open a Java file, it shows the property's value instead of the key. + +![Folding](img/cls_tutorial/folding.png) + +[Previous](find_usages_provider.html) +[Top](cls_tutorial.html) +[Next](go_to_symbol_contributor.html) + + diff --git a/img/cls_tutorial/folding.png b/img/cls_tutorial/folding.png new file mode 100644 index 000000000..7cae4ba0f Binary files /dev/null and b/img/cls_tutorial/folding.png differ