IntelliJ Platform Plugin SDK Help

12. Folding Builder

A folding builder identifies the folding regions in the code. In this step of the tutorial, the folding builder is used to identify folding regions and replace the regions with specific text. Rather than the usual practice of using a folding builder to collapse a class, method, or comments to fewer lines, the folding builder replaces Simple Language keys with their corresponding values.

Define a Folding Builder

The SimpleFoldingBuilder replaces usages of properties with their values by default. Start by subclassing FoldingBuilderEx

Note that SimpleFoldingBuilder also implements DumbAware, which means the class is allowed to run in dumb mode, when indices are in background update.

The buildFoldRegions() method searches down a PSI tree from root to find all literal expressions containing the simple prefix simple:. The remainder of such a string is expected to contain a Simple Language key, and so the text range is stored as a FoldingDescriptor.

The getPlaceholderText() method retrieves the Simple Language value corresponding to the key associated with the (ASTNode) provided. The IntelliJ Platform uses the value to substitute for the key when the code gets folded.

Register the Folding Builder

The SimpleFoldingBuilder implementation is registered with the IntelliJ Platform in the plugin configuration file using the com.intellij.lang.foldingBuilder extension point.

<extensions defaultExtensionNs="com.intellij"> <lang.foldingBuilder language="JAVA" implementationClass="org.intellij.sdk.language.SimpleFoldingBuilder"/> </extensions>

Run the Project

Run the plugin by using the Gradle runIde task.

Now when a Java file is opened in the editor, it shows the property's value instead of the key. This is because SimpleFoldingBuilder.isCollapsedByDefault() always returns true. Try using to show the key rather than the value.

Folding
Last modified: 29 九月 2022