Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/kotlin/com/emberjs/gts/GtsFoldingBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.emberjs.gts

import com.intellij.lang.ASTNode
import com.intellij.lang.folding.FoldingBuilderEx
import com.intellij.lang.folding.FoldingDescriptor
import com.intellij.lang.javascript.JavaScriptSupportLoader
import com.intellij.lang.javascript.folding.TypeScriptFoldingBuilder
import com.intellij.lang.xml.XmlFoldingBuilder
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType


internal class GtsFoldingBuilder : FoldingBuilderEx(), DumbAware {

public override fun buildFoldRegions(
root: PsiElement,
p1: Document,
quick: Boolean
): Array<out FoldingDescriptor?> {

val registeredRanges = mutableSetOf<TextRange>()

val descriptors = mutableListOf<FoldingDescriptor>()

val view = root.containingFile.viewProvider
val JS = JavaScriptSupportLoader.ECMA_SCRIPT_6
val TS = JavaScriptSupportLoader.TYPESCRIPT
val tsView = view.getPsi(TS)
val jsView = view.getPsi(JS)

val tsRegions = TypeScriptFoldingBuilder().buildFoldRegions(tsView ?: jsView, p1, quick)
val htmlRegions = XmlFoldingBuilder().buildFoldRegions(root, p1, quick)

val templates = PsiTreeUtil.collectElements(tsView ?: jsView) { it.elementType == GtsElementTypes.GTS_OUTER_ELEMENT_TYPE }

registeredRanges.addAll(templates.map { it.textRange })

for (region in tsRegions) {
if (!registeredRanges.contains(region.range)) {
descriptors.add(region)
}
}

for (region in htmlRegions) {
if (registeredRanges.contains(region.range)) {
descriptors.add(region)
}
}

return descriptors.toTypedArray()
}

public override fun getPlaceholderText(node: ASTNode): String? {
return null
}

public override fun isCollapsedByDefault(p0: ASTNode): Boolean {
return true
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<lang.ast.factory language="Gts" implementationClass="com.emberjs.gts.GtsAstFactory" />
<lang.formatter language="Gts" implementationClass="com.emberjs.gts.GtsFormattingModelBuilder" />
<lang.formatter language="Gjs" implementationClass="com.emberjs.gts.GtsFormattingModelBuilder" />
<lang.foldingBuilder language="Gts" implementationClass="com.emberjs.gts.GtsFoldingBuilder" />
<editorNotificationProvider implementation="com.emberjs.glint.GlintEditorNotificationsProvider"/>
<editorFactoryListener implementation="com.emberjs.hbs.HbMustacheNameSynchronizer$MyEditorFactoryListener"/>
<typedHandler implementation="com.emberjs.gts.GtsTypeHandler" order="before xmlSlash"/>
Expand Down
Loading