From f6861c1b0f31fdcc42e52d1aba027d629fb413eb Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Fri, 18 Jul 2025 23:05:11 +0200 Subject: [PATCH] add html folding in gts --- .../com/emberjs/gts/GtsFoldingBuilder.kt | 64 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 1 + 2 files changed, 65 insertions(+) create mode 100644 src/main/kotlin/com/emberjs/gts/GtsFoldingBuilder.kt diff --git a/src/main/kotlin/com/emberjs/gts/GtsFoldingBuilder.kt b/src/main/kotlin/com/emberjs/gts/GtsFoldingBuilder.kt new file mode 100644 index 00000000..871e32fb --- /dev/null +++ b/src/main/kotlin/com/emberjs/gts/GtsFoldingBuilder.kt @@ -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 { + + val registeredRanges = mutableSetOf() + + val descriptors = mutableListOf() + + 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 + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 1b026140..41cc7de3 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -82,6 +82,7 @@ +