Skip to content

Commit 80d21de

Browse files
committed
[ cubicaltt ] Folding builder
1 parent 25fc950 commit 80d21de

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ intellij {
6868
.mapNotNull { fromToolbox(root, it) }.firstOrNull()
6969
pycharmPath?.absolutePath?.let { alternativeIdePath = it }
7070

71-
setPlugins("PsiViewer:192-SNAPSHOT")
71+
setPlugins("PsiViewer:191.4212")
7272
}
7373

7474
java {

res/META-INF/change-notes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<ul>
33
<li>IntelliJ Platform 2019.2 compatibility</li>
44
<li>Tons of internal refactorings: generate more codes</li>
5+
<li>CubicalTT folding</li>
56
</ul>
67
0.4.2<br/>
78
<ul>

res/META-INF/description.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ <h3>Features (listed for each supported language):</h3><br/>
8484
<li>Running (type checking) your code</li>
8585
<li>Live template contexts</li>
8686
<li>Executable (cubical) management</li>
87+
<li>Folding code blocks and imports</li>
8788
<li>Syntax highlighter, color settings page</li>
8889
<li>Completion/find usages/goto declaration</li>
8990
<li>Stub indices, goto symbol (search everywhere)</li>

res/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<lang.findUsagesProvider language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTFindUsagesProvider"/>
6565
<lang.syntaxHighlighterFactory language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTHighlighterFactory"/>
6666
<lang.refactoringSupport language="CubicalTT" implementationClass="org.ice1000.tt.editing.InplaceRenameRefactoringSupportProvider"/>
67+
<lang.foldingBuilder language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTFoldingBuilder"/>
6768
<colorSettingsPage implementation="org.ice1000.tt.editing.cubicaltt.CubicalTTColorSettingsPage"/>
6869
<annotator language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTAnnotator"/>
6970
<stubIndex implementation="org.ice1000.tt.psi.cubicaltt.CubicalTTModuleStubKey"/>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.ice1000.tt.editing.cubicaltt
2+
3+
import com.intellij.lang.ASTNode
4+
import com.intellij.lang.folding.FoldingBuilderEx
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.openapi.util.TextRange
9+
import com.intellij.psi.PsiComment
10+
import com.intellij.psi.PsiElement
11+
import com.intellij.psi.TokenType
12+
import org.ice1000.tt.FOLDING_PLACEHOLDER
13+
import org.ice1000.tt.editing.collectFoldRegions
14+
import org.ice1000.tt.psi.*
15+
import org.ice1000.tt.psi.cubicaltt.*
16+
17+
class CubicalTTFoldingBuilder : FoldingBuilderEx(), DumbAware {
18+
override fun getPlaceholderText(node: ASTNode) = when (node.elementType) {
19+
CubicalTTTokenType.BLOCK_COMMENT -> "{---}"
20+
CubicalTTTypes.SYSTEM -> "[$FOLDING_PLACEHOLDER]"
21+
else -> FOLDING_PLACEHOLDER
22+
}
23+
24+
override fun isCollapsedByDefault(node: ASTNode) = false
25+
26+
override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
27+
if (root !is CubicalTTFileImpl) return emptyArray()
28+
return collectFoldRegions(root) { FoldingVisitor(it, document) }
29+
}
30+
}
31+
32+
private class FoldingVisitor(
33+
private val descriptors: MutableList<FoldingDescriptor>,
34+
private val document: Document
35+
) : CubicalTTVisitor() {
36+
override fun visitData(o: CubicalTTData) {
37+
val labels = o.labelList.takeIf { it.size > 1 } ?: return
38+
descriptors += FoldingDescriptor(o, TextRange(labels.first().startOffset, labels.last().endOffset))
39+
}
40+
41+
override fun visitSystem(o: CubicalTTSystem) {
42+
val startLine = document.getLineNumber(o.startOffset)
43+
val endLine = document.getLineNumber(o.endOffset)
44+
if (startLine != endLine) descriptors.add(FoldingDescriptor(o, o.textRange))
45+
}
46+
47+
override fun visitComment(comment: PsiComment?) {
48+
if (comment?.elementType == CubicalTTTokenType.BLOCK_COMMENT)
49+
descriptors += FoldingDescriptor(comment, comment.textRange)
50+
}
51+
52+
override fun visitSplitBody(o: CubicalTTSplitBody) = layout(o)
53+
override fun visitExpWhere(o: CubicalTTExpWhere) = layout(o)
54+
override fun visitMutual(o: CubicalTTMutual) = layout(o)
55+
override fun visitLetExp(o: CubicalTTLetExp) {
56+
val layoutStart = o.childrenWithLeaves.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_START } ?: return
57+
val layoutEnd = layoutStart.rightSiblings.firstOrNull {
58+
it.elementType == CubicalTTTypes.KW_IN
59+
}?.prevSiblingIgnoring<PsiElement>(TokenType.WHITE_SPACE) ?: return
60+
descriptors += FoldingDescriptor(o, TextRange(layoutStart.startOffset, layoutEnd.endOffset))
61+
}
62+
63+
private fun layout(o: PsiElement) {
64+
val layoutStart = o.childrenWithLeaves.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_START } ?: return
65+
val layoutEnd = layoutStart.rightSiblings.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_END } ?: o.lastChild
66+
?: return
67+
descriptors += FoldingDescriptor(o, TextRange(layoutStart.startOffset, layoutEnd.endOffset))
68+
}
69+
70+
override fun visitModule(o: CubicalTTModule) {
71+
val imports = o.importList.takeIf { it.size > 1 } ?: return
72+
val startOffset = imports.first().moduleUsage?.startOffset ?: return
73+
descriptors += FoldingDescriptor(o, TextRange(startOffset, imports.last().endOffset))
74+
}
75+
}

src/org/ice1000/tt/editing/redprl/folding.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ private class FoldingVisitor(
3737
private val document: Document
3838
) : RedPrlVisitor() {
3939

40-
4140
override fun visitComment(comment: PsiComment?) {
4241
if (comment?.elementType == RedPrlTokenType.BLOCK_COMMENT)
4342
descriptors += FoldingDescriptor(comment, comment.textRange)

0 commit comments

Comments
 (0)