1 package com
.intellij
.indentation
;
3 import com
.intellij
.lang
.ASTNode
;
4 import com
.intellij
.lang
.folding
.FoldingBuilder
;
5 import com
.intellij
.lang
.folding
.FoldingDescriptor
;
6 import com
.intellij
.openapi
.editor
.Document
;
7 import com
.intellij
.openapi
.project
.DumbAware
;
8 import com
.intellij
.psi
.tree
.TokenSet
;
9 import org
.jetbrains
.annotations
.NotNull
;
10 import org
.jetbrains
.annotations
.Nullable
;
12 import java
.util
.LinkedList
;
13 import java
.util
.List
;
18 public abstract class IndentationFoldingBuilder
implements FoldingBuilder
, DumbAware
{
19 private final TokenSet myTokenSet
;
21 public IndentationFoldingBuilder(final TokenSet tokenSet
) {
22 myTokenSet
= tokenSet
;
26 public FoldingDescriptor
[] buildFoldRegions(@NotNull ASTNode astNode
, @NotNull Document document
) {
27 List
<FoldingDescriptor
> descriptors
= new LinkedList
<FoldingDescriptor
>();
28 collectDescriptors(astNode
, descriptors
);
29 return descriptors
.toArray(new FoldingDescriptor
[descriptors
.size()]);
32 private void collectDescriptors(@NotNull final ASTNode node
, @NotNull final List
<FoldingDescriptor
> descriptors
) {
33 final ASTNode
[] children
= node
.getChildren(myTokenSet
);
34 if (children
.length
> 0) {
35 if (node
.getTreeParent() !=null) {
36 descriptors
.add(new FoldingDescriptor(node
, node
.getTextRange()));
38 for (ASTNode child
: children
) {
39 collectDescriptors(child
, descriptors
);
45 public String
getPlaceholderText(@NotNull final ASTNode node
) {
46 final StringBuilder builder
= new StringBuilder();
47 ASTNode child
= node
.getFirstChildNode();
49 while (child
!= null && (text
= child
.getText()) != null && !text
.contains("\n")){
51 child
= child
.getTreeNext();
53 return builder
.toString();
56 public boolean isCollapsedByDefault(@NotNull ASTNode node
) {