Skip to content

Commit a0e0ce9

Browse files
committed
Use NamedFoldingDescriptors
1 parent e264ba5 commit a0e0ce9

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/com/goide/editor/GoFoldingBuilder.java

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.intellij.lang.ASTNode;
2424
import com.intellij.lang.folding.CustomFoldingBuilder;
2525
import com.intellij.lang.folding.FoldingDescriptor;
26+
import com.intellij.lang.folding.NamedFoldingDescriptor;
2627
import com.intellij.openapi.editor.Document;
2728
import com.intellij.openapi.project.DumbAware;
2829
import com.intellij.openapi.util.TextRange;
@@ -43,24 +44,28 @@ public class GoFoldingBuilder extends CustomFoldingBuilder implements DumbAware
4344
private static void foldTypes(@Nullable PsiElement e, @NotNull List<FoldingDescriptor> result) {
4445
if (e instanceof GoStructType) {
4546
if (((GoStructType)e).getFieldDeclarationList().isEmpty()) return;
46-
fold(e, ((GoStructType)e).getLbrace(), ((GoStructType)e).getRbrace(), result);
47+
fold(e, ((GoStructType)e).getLbrace(), ((GoStructType)e).getRbrace(), "{...}", result);
4748
}
4849
if (e instanceof GoInterfaceType) {
4950
if (e.getChildren().length == 0) return;
50-
fold(e, ((GoInterfaceType)e).getLbrace(), ((GoInterfaceType)e).getRbrace(), result);
51+
fold(e, ((GoInterfaceType)e).getLbrace(), ((GoInterfaceType)e).getRbrace(), "{...}", result);
5152
}
5253
}
5354

54-
private static void fold(@NotNull PsiElement e, @Nullable PsiElement l, @Nullable PsiElement r, @NotNull List<FoldingDescriptor> result) {
55+
private static void fold(@NotNull PsiElement e,
56+
@Nullable PsiElement l,
57+
@Nullable PsiElement r,
58+
@NotNull String placeholderText,
59+
@NotNull List<FoldingDescriptor> result) {
5560
if (l != null && r != null) {
56-
result.add(new FoldingDescriptor(e, TextRange.create(l.getTextRange().getStartOffset(), r.getTextRange().getEndOffset())));
61+
result.add(new NamedFoldingDescriptor(e, l.getTextRange().getStartOffset(), r.getTextRange().getEndOffset(), null, placeholderText));
5762
}
5863
}
5964

6065
// com.intellij.codeInsight.folding.impl.JavaFoldingBuilderBase.addCodeBlockFolds()
6166
private static void addCommentFolds(@NotNull PsiElement comment,
6267
@NotNull Set<PsiElement> processedComments,
63-
@NotNull List<FoldingDescriptor> foldElements) {
68+
@NotNull List<FoldingDescriptor> result) {
6469
if (processedComments.contains(comment)) return;
6570

6671
PsiElement end = null;
@@ -78,8 +83,9 @@ private static void addCommentFolds(@NotNull PsiElement comment,
7883
}
7984

8085
if (end != null) {
81-
TextRange textRange = TextRange.create(comment.getTextRange().getStartOffset(), end.getTextRange().getEndOffset());
82-
foldElements.add(new FoldingDescriptor(comment, textRange));
86+
int startOffset = comment.getTextRange().getStartOffset();
87+
int endOffset = end.getTextRange().getEndOffset();
88+
result.add(new NamedFoldingDescriptor(comment, startOffset, endOffset, null, "/.../"));
8389
}
8490
}
8591

@@ -99,25 +105,25 @@ protected void buildLanguageFoldRegions(@NotNull final List<FoldingDescriptor> r
99105
PsiElement importKeyword = firstImport.getImport();
100106
int offset = importKeyword.getTextRange().getEndOffset();
101107
int startOffset = importKeyword.getNextSibling() instanceof PsiWhiteSpace ? offset + 1 : offset;
102-
TextRange range = TextRange.create(startOffset, importList.getTextRange().getEndOffset());
103-
if (range.getLength() > 3) {
104-
result.add(new FoldingDescriptor(importList, range));
108+
int endOffset = importList.getTextRange().getEndOffset();
109+
if (endOffset - startOffset > 3) {
110+
result.add(new NamedFoldingDescriptor(importList, startOffset, endOffset, null, "..."));
105111
}
106112
}
107113
}
108114

109115
for (GoBlock block : PsiTreeUtil.findChildrenOfType(file, GoBlock.class)) {
110116
if (block.getTextRange().getLength() > 1) {
111-
result.add(new FoldingDescriptor(block, block.getTextRange()));
117+
result.add(new NamedFoldingDescriptor(block.getNode(), block.getTextRange(), null, "{...}"));
112118
}
113119
}
114120

115121
for (GoExprSwitchStatement switchStatement : PsiTreeUtil.findChildrenOfType(file, GoExprSwitchStatement.class)) {
116-
fold(switchStatement, switchStatement.getLbrace(), switchStatement.getRbrace(), result);
122+
fold(switchStatement, switchStatement.getLbrace(), switchStatement.getRbrace(), "{...}", result);
117123
}
118124

119125
for (GoSelectStatement selectStatement : PsiTreeUtil.findChildrenOfType(file, GoSelectStatement.class)) {
120-
fold(selectStatement, selectStatement.getLbrace(), selectStatement.getRbrace(), result);
126+
fold(selectStatement, selectStatement.getLbrace(), selectStatement.getRbrace(), "{...}", result);
121127
}
122128

123129
for (GoTypeSpec type : file.getTypes()) {
@@ -127,39 +133,39 @@ protected void buildLanguageFoldRegions(@NotNull final List<FoldingDescriptor> r
127133
for (GoExprCaseClause caseClause : PsiTreeUtil.findChildrenOfType(file, GoExprCaseClause.class)) {
128134
PsiElement colon = caseClause.getColon();
129135
if (colon != null && !caseClause.getStatementList().isEmpty()) {
130-
fold(caseClause, colon.getNextSibling(), caseClause, result);
136+
fold(caseClause, colon.getNextSibling(), caseClause, "...", result);
131137
}
132138
}
133139

134140
for (GoCommClause commClause : PsiTreeUtil.findChildrenOfType(file, GoCommClause.class)) {
135141
PsiElement colon = commClause.getColon();
136142
if (colon != null && !commClause.getStatementList().isEmpty()) {
137-
fold(commClause, colon.getNextSibling(), commClause, result);
143+
fold(commClause, colon.getNextSibling(), commClause, "...", result);
138144
}
139145
}
140146

141147
for (GoVarDeclaration varDeclaration : PsiTreeUtil.findChildrenOfType(file, GoVarDeclaration.class)) {
142148
if (varDeclaration.getVarSpecList().size() > 1) {
143-
fold(varDeclaration, varDeclaration.getLparen(), varDeclaration.getRparen(), result);
149+
fold(varDeclaration, varDeclaration.getLparen(), varDeclaration.getRparen(), "(...)", result);
144150
}
145151
}
146152

147153
for (GoConstDeclaration constDeclaration : PsiTreeUtil.findChildrenOfType(file, GoConstDeclaration.class)) {
148154
if (constDeclaration.getConstSpecList().size() > 1) {
149-
fold(constDeclaration, constDeclaration.getLparen(), constDeclaration.getRparen(), result);
155+
fold(constDeclaration, constDeclaration.getLparen(), constDeclaration.getRparen(), "(...)", result);
150156
}
151157
}
152158

153159
for (GoTypeDeclaration typeDeclaration : PsiTreeUtil.findChildrenOfType(file, GoTypeDeclaration.class)) {
154160
if (typeDeclaration.getTypeSpecList().size() > 1) {
155-
fold(typeDeclaration, typeDeclaration.getLparen(), typeDeclaration.getRparen(), result);
161+
fold(typeDeclaration, typeDeclaration.getLparen(), typeDeclaration.getRparen(), "(...)", result);
156162
}
157163
}
158164

159165
for (GoCompositeLit compositeLit : PsiTreeUtil.findChildrenOfType(file, GoCompositeLit.class)) {
160166
GoLiteralValue literalValue = compositeLit.getLiteralValue();
161167
if (literalValue.getElementList().size() > 1) {
162-
fold(literalValue, literalValue.getLbrace(), literalValue.getRbrace(), result);
168+
fold(literalValue, literalValue.getLbrace(), literalValue.getRbrace(), "{...}", result);
163169
}
164170
}
165171

@@ -168,9 +174,11 @@ protected void buildLanguageFoldRegions(@NotNull final List<FoldingDescriptor> r
168174
PsiTreeUtil.processElements(file, new PsiElementProcessor() {
169175
@Override
170176
public boolean execute(@NotNull PsiElement element) {
171-
IElementType type = element.getNode().getElementType();
172-
if (type == GoParserDefinition.MULTILINE_COMMENT && element.getTextRange().getLength() > 2) {
173-
result.add(new FoldingDescriptor(element, element.getTextRange()));
177+
ASTNode node = element.getNode();
178+
IElementType type = node.getElementType();
179+
TextRange range = element.getTextRange();
180+
if (type == GoParserDefinition.MULTILINE_COMMENT && range.getLength() > 2) {
181+
result.add(new NamedFoldingDescriptor(node, range, null, "/*...*/"));
174182
}
175183
if (type == GoParserDefinition.LINE_COMMENT) {
176184
addCommentFolds(element, processedComments, result);
@@ -185,20 +193,7 @@ public boolean execute(@NotNull PsiElement element) {
185193
@Nullable
186194
@Override
187195
protected String getLanguagePlaceholderText(@NotNull ASTNode node, @NotNull TextRange range) {
188-
PsiElement psi = node.getPsi();
189-
IElementType type = node.getElementType();
190-
if (psi instanceof GoBlock || psi instanceof GoStructType ||
191-
psi instanceof GoInterfaceType || psi instanceof GoLiteralValue ||
192-
psi instanceof GoSelectStatement || psi instanceof GoExprSwitchStatement) {
193-
return "{...}";
194-
}
195-
if (psi instanceof GoVarDeclaration || psi instanceof GoConstDeclaration || psi instanceof GoTypeDeclaration) {
196-
return "(...)";
197-
}
198-
if (psi instanceof GoImportDeclaration || psi instanceof GoCommClause || psi instanceof GoCaseClause) return "...";
199-
if (GoParserDefinition.LINE_COMMENT == type) return "/.../";
200-
if (GoParserDefinition.MULTILINE_COMMENT == type) return "/*...*/";
201-
return null;
196+
return "...";
202197
}
203198

204199
@Override

0 commit comments

Comments
 (0)