Skip to content

Commit

Permalink
[ cubicaltt ] Folding builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed May 24, 2019
1 parent 25fc950 commit 80d21de
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ intellij {
.mapNotNull { fromToolbox(root, it) }.firstOrNull()
pycharmPath?.absolutePath?.let { alternativeIdePath = it }

setPlugins("PsiViewer:192-SNAPSHOT")
setPlugins("PsiViewer:191.4212")
}

java {
Expand Down
1 change: 1 addition & 0 deletions res/META-INF/change-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ul>
<li>IntelliJ Platform 2019.2 compatibility</li>
<li>Tons of internal refactorings: generate more codes</li>
<li>CubicalTT folding</li>
</ul>
0.4.2<br/>
<ul>
Expand Down
1 change: 1 addition & 0 deletions res/META-INF/description.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ <h3>Features (listed for each supported language):</h3><br/>
<li>Running (type checking) your code</li>
<li>Live template contexts</li>
<li>Executable (cubical) management</li>
<li>Folding code blocks and imports</li>
<li>Syntax highlighter, color settings page</li>
<li>Completion/find usages/goto declaration</li>
<li>Stub indices, goto symbol (search everywhere)</li>
Expand Down
1 change: 1 addition & 0 deletions res/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<lang.findUsagesProvider language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTFindUsagesProvider"/>
<lang.syntaxHighlighterFactory language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTHighlighterFactory"/>
<lang.refactoringSupport language="CubicalTT" implementationClass="org.ice1000.tt.editing.InplaceRenameRefactoringSupportProvider"/>
<lang.foldingBuilder language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTFoldingBuilder"/>
<colorSettingsPage implementation="org.ice1000.tt.editing.cubicaltt.CubicalTTColorSettingsPage"/>
<annotator language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTAnnotator"/>
<stubIndex implementation="org.ice1000.tt.psi.cubicaltt.CubicalTTModuleStubKey"/>
Expand Down
75 changes: 75 additions & 0 deletions src/org/ice1000/tt/editing/cubicaltt/folding.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.ice1000.tt.editing.cubicaltt

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.project.DumbAware
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.TokenType
import org.ice1000.tt.FOLDING_PLACEHOLDER
import org.ice1000.tt.editing.collectFoldRegions
import org.ice1000.tt.psi.*
import org.ice1000.tt.psi.cubicaltt.*

class CubicalTTFoldingBuilder : FoldingBuilderEx(), DumbAware {
override fun getPlaceholderText(node: ASTNode) = when (node.elementType) {
CubicalTTTokenType.BLOCK_COMMENT -> "{---}"
CubicalTTTypes.SYSTEM -> "[$FOLDING_PLACEHOLDER]"
else -> FOLDING_PLACEHOLDER
}

override fun isCollapsedByDefault(node: ASTNode) = false

override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
if (root !is CubicalTTFileImpl) return emptyArray()
return collectFoldRegions(root) { FoldingVisitor(it, document) }
}
}

private class FoldingVisitor(
private val descriptors: MutableList<FoldingDescriptor>,
private val document: Document
) : CubicalTTVisitor() {
override fun visitData(o: CubicalTTData) {
val labels = o.labelList.takeIf { it.size > 1 } ?: return
descriptors += FoldingDescriptor(o, TextRange(labels.first().startOffset, labels.last().endOffset))
}

override fun visitSystem(o: CubicalTTSystem) {
val startLine = document.getLineNumber(o.startOffset)
val endLine = document.getLineNumber(o.endOffset)
if (startLine != endLine) descriptors.add(FoldingDescriptor(o, o.textRange))
}

override fun visitComment(comment: PsiComment?) {
if (comment?.elementType == CubicalTTTokenType.BLOCK_COMMENT)
descriptors += FoldingDescriptor(comment, comment.textRange)
}

override fun visitSplitBody(o: CubicalTTSplitBody) = layout(o)
override fun visitExpWhere(o: CubicalTTExpWhere) = layout(o)
override fun visitMutual(o: CubicalTTMutual) = layout(o)
override fun visitLetExp(o: CubicalTTLetExp) {
val layoutStart = o.childrenWithLeaves.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_START } ?: return
val layoutEnd = layoutStart.rightSiblings.firstOrNull {
it.elementType == CubicalTTTypes.KW_IN
}?.prevSiblingIgnoring<PsiElement>(TokenType.WHITE_SPACE) ?: return
descriptors += FoldingDescriptor(o, TextRange(layoutStart.startOffset, layoutEnd.endOffset))
}

private fun layout(o: PsiElement) {
val layoutStart = o.childrenWithLeaves.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_START } ?: return
val layoutEnd = layoutStart.rightSiblings.firstOrNull { it.elementType == CubicalTTTypes.LAYOUT_END } ?: o.lastChild
?: return
descriptors += FoldingDescriptor(o, TextRange(layoutStart.startOffset, layoutEnd.endOffset))
}

override fun visitModule(o: CubicalTTModule) {
val imports = o.importList.takeIf { it.size > 1 } ?: return
val startOffset = imports.first().moduleUsage?.startOffset ?: return
descriptors += FoldingDescriptor(o, TextRange(startOffset, imports.last().endOffset))
}
}
1 change: 0 additions & 1 deletion src/org/ice1000/tt/editing/redprl/folding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private class FoldingVisitor(
private val document: Document
) : RedPrlVisitor() {


override fun visitComment(comment: PsiComment?) {
if (comment?.elementType == RedPrlTokenType.BLOCK_COMMENT)
descriptors += FoldingDescriptor(comment, comment.textRange)
Expand Down

0 comments on commit 80d21de

Please sign in to comment.