1+ package com.emberjs.gts
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.lang.javascript.JavaScriptSupportLoader
7+ import com.intellij.lang.javascript.folding.TypeScriptFoldingBuilder
8+ import com.intellij.lang.xml.XmlFoldingBuilder
9+ import com.intellij.openapi.editor.Document
10+ import com.intellij.openapi.project.DumbAware
11+ import com.intellij.openapi.util.TextRange
12+ import com.intellij.psi.PsiElement
13+ import com.intellij.psi.util.PsiTreeUtil
14+ import com.intellij.psi.util.elementType
15+
16+
17+ internal class GtsFoldingBuilder : FoldingBuilderEx (), DumbAware {
18+
19+ public override fun buildFoldRegions (
20+ root : PsiElement ,
21+ p1 : Document ,
22+ quick : Boolean
23+ ): Array <out FoldingDescriptor ?> {
24+
25+ val registeredRanges = mutableSetOf<TextRange >()
26+
27+ val descriptors = mutableListOf<FoldingDescriptor >()
28+
29+ val view = root.containingFile.viewProvider
30+ val JS = JavaScriptSupportLoader .ECMA_SCRIPT_6
31+ val TS = JavaScriptSupportLoader .TYPESCRIPT
32+ val tsView = view.getPsi(TS )
33+ val jsView = view.getPsi(JS )
34+
35+ val tsRegions = TypeScriptFoldingBuilder ().buildFoldRegions(tsView ? : jsView, p1, quick)
36+ val htmlRegions = XmlFoldingBuilder ().buildFoldRegions(root, p1, quick)
37+
38+ val templates = PsiTreeUtil .collectElements(tsView ? : jsView) { it.elementType == GtsElementTypes .GTS_OUTER_ELEMENT_TYPE }
39+
40+ registeredRanges.addAll(templates.map { it.textRange })
41+
42+ for (region in tsRegions) {
43+ if (! registeredRanges.contains(region.range)) {
44+ descriptors.add(region)
45+ }
46+ }
47+
48+ for (region in htmlRegions) {
49+ if (registeredRanges.contains(region.range)) {
50+ descriptors.add(region)
51+ }
52+ }
53+
54+ return descriptors.toTypedArray()
55+ }
56+
57+ public override fun getPlaceholderText (node : ASTNode ): String? {
58+ return null
59+ }
60+
61+ public override fun isCollapsedByDefault (p0 : ASTNode ): Boolean {
62+ return true
63+ }
64+ }
0 commit comments