Skip to content

Commit 1ba0b6b

Browse files
committed
wip
1 parent ef9ac9f commit 1ba0b6b

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

src/main/kotlin/com/emberjs/gts/GtsSupport.kt

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.intellij.lang.javascript.dialects.ECMA6ParserDefinition
2828
import com.intellij.lang.javascript.dialects.TypeScriptParserDefinition
2929
import com.intellij.lang.javascript.editing.JavascriptCommenter
3030
import com.intellij.lang.javascript.formatter.JavascriptFormattingModelBuilder
31+
import com.intellij.lang.javascript.formatter.blocks.CompositeJSBlock
3132
import com.intellij.lang.javascript.formatter.blocks.JSBlock
3233
import com.intellij.lang.javascript.highlighting.JSHighlighter
3334
import com.intellij.lang.javascript.index.IndexedFileTypeProvider
@@ -798,18 +799,43 @@ val NoWrap by lazy {
798799
Wrap.createWrap(WrapType.NONE, false).apply { ignoreParentWraps() }
799800
}
800801

801-
class BlockInfo(val parent: Block, val originalBlock: Block)
802+
class BlockInfo(var parent: Block?, var originalBlock: Block)
802803

803804
val blockinfo = WeakHashMap<Block, BlockInfo>()
804805

805-
val Block.block: Block
806+
var Block.block: Block
806807
get() = blockinfo.get(this)!!.originalBlock
808+
set(v: Block) {
809+
if (!blockinfo.contains(this)) {
810+
blockinfo[this] = BlockInfo(null, v)
811+
}
812+
blockinfo[this]!!.originalBlock = v
813+
}
807814

808-
val Block.parent: Block
809-
get() = blockinfo.get(this)!!.parent
815+
var Block.parent: Block?
816+
get() = blockinfo.get(this)?.parent
817+
set(v: Block?) {
818+
if (!blockinfo.contains(this)) {
819+
blockinfo[this] = BlockInfo(v, v!!)
820+
}
821+
blockinfo[this]!!.parent = v
822+
}
810823

824+
fun Block.nexOuterLanguageBlock(block: Block): RootBlockWrapper? {
825+
val i = this.block.subBlocks.indexOf(block)
826+
if (this.block.subBlocks.getOrNull(i+1) is RootBlockWrapper) {
827+
return this.block.subBlocks.getOrNull(i+1) as RootBlockWrapper
828+
}
829+
return null
830+
}
831+
832+
open class SyntheticBlockWrapper(_block: Block, val hbsBlock: Block?, val styleSettings: CodeStyleSettings): Block by _block {
833+
834+
init {
835+
this.block = _block
836+
}
811837

812-
open class SyntheticBlockWrapper(val block: SyntheticBlock, val hbsBlock: Block?, val styleSettings: CodeStyleSettings): Block by block {
838+
private var cachedBlocks: MutableList<Block>? = null
813839

814840
override fun getDebugName(): String {
815841
var b = block
@@ -819,6 +845,19 @@ open class SyntheticBlockWrapper(val block: SyntheticBlock, val hbsBlock: Block?
819845
return b.debugName ?: b.javaClass.simpleName
820846
}
821847

848+
override fun getWrap(): Wrap? {
849+
if (parent?.subBlocks?.lastOrNull()?.block is RootBlockWrapper) {
850+
return NoWrap
851+
}
852+
if (subBlocks.lastOrNull()?.block is RootBlockWrapper) {
853+
return NoWrap
854+
}
855+
if (parent?.block is RootBlockWrapper.SynteticBlockWrapper) {
856+
return NoWrap
857+
}
858+
return block.wrap
859+
}
860+
822861
fun mapToWrapper(block: Block): Block {
823862
if (block is ASTBlock && block.node != null) {
824863
return JSAstBlockWrapper(block, block.node!!, this, hbsBlock, styleSettings)
@@ -829,18 +868,23 @@ open class SyntheticBlockWrapper(val block: SyntheticBlock, val hbsBlock: Block?
829868
if (block is RootBlockWrapper.SynteticBlockWrapper) {
830869
return JSAstBlockWrapper(block, block.parent.node!!, this, hbsBlock, styleSettings)
831870
}
832-
if (block is SyntheticBlock) {
833-
return SyntheticBlockWrapper(block, hbsBlock, styleSettings)
871+
if (block is JSBlock) {
872+
return JsBlockWrapper(block, block.node, this, hbsBlock, styleSettings)
834873
}
835-
return JsBlockWrapper(block, (block as ASTBlock).node!!, this, hbsBlock, styleSettings)
874+
return SyntheticBlockWrapper(block, hbsBlock, styleSettings)
836875
}
837876

838-
override fun getSubBlocks(): MutableList<JsBlockWrapper> {
877+
override fun getSubBlocks(): MutableList<Block> {
878+
879+
if (this.cachedBlocks != null) {
880+
return this.cachedBlocks!!
881+
}
882+
839883
val blocks = block.subBlocks.map { mapToWrapper(it) }.toMutableList()
840884

841885
blocks.toTypedArray().forEach {
842886
// blocks.removeIf { it.block is RootBlockWrapper && it.block.patched }
843-
if (it.block is RootBlockWrapper && !it.block.patched) {
887+
if (it.block is RootBlockWrapper && !(it.block as RootBlockWrapper).patched) {
844888
it.block.parent = this
845889
}
846890
}
@@ -852,12 +896,14 @@ open class SyntheticBlockWrapper(val block: SyntheticBlock, val hbsBlock: Block?
852896

853897

854898
// wrapper to patch JsBlocks to include outer language block into JSAssignmentExpression and JSVarStatement
855-
open class JsBlockWrapper(val block: Block, val nnode: ASTNode, val parent: Block?, var hbsBlock: Block? = null, val styleSettings: CodeStyleSettings): JSBlock(
856-
nnode, block.alignment, block.indent, block.wrap, styleSettings) {
899+
open class JsBlockWrapper(_block: Block, nnode: ASTNode, _parent: Block?, var hbsBlock: Block? = null, val styleSettings: CodeStyleSettings): JSBlock(
900+
nnode, _block.alignment, _block.indent, _block.wrap, styleSettings) {
857901

858902
private var cachedBlocks: MutableList<Block>? = null
859903

860904
init {
905+
this.block = _block
906+
this.parent = _parent
861907
this.subBlocks
862908
}
863909

@@ -890,14 +936,6 @@ open class JsBlockWrapper(val block: Block, val nnode: ASTNode, val parent: Bloc
890936
return TextRange(subBlocks.first().textRange.startOffset, subBlocks.last().textRange.endOffset)
891937
}
892938

893-
private fun nexOuterLanguageBlock(block: Block): RootBlockWrapper? {
894-
val i = this.block.subBlocks.indexOf(block)
895-
if (this.block.subBlocks.getOrNull(i+1) is RootBlockWrapper) {
896-
return this.block.subBlocks.getOrNull(i+1) as RootBlockWrapper
897-
}
898-
return null
899-
}
900-
901939
fun mapToWrapper(block: Block, hbsBlock: Block?): Block? {
902940
if (block is ASTBlock && block.node != null) {
903941
return JSAstBlockWrapper(block, block.node!!, this, hbsBlock, styleSettings)
@@ -908,30 +946,30 @@ open class JsBlockWrapper(val block: Block, val nnode: ASTNode, val parent: Bloc
908946
if (block is RootBlockWrapper.SynteticBlockWrapper) {
909947
return JSAstBlockWrapper(block, block.parent.node!!, this, hbsBlock, styleSettings)
910948
}
911-
if (block is SyntheticBlock) {
912-
return SyntheticBlockWrapper(block, this, hbsBlock)
949+
if (block is JSBlock) {
950+
return JsBlockWrapper(block, block.node, this, hbsBlock, styleSettings)
913951
}
914-
return JsBlockWrapper(block, (block as ASTBlock).node!!, this, hbsBlock, styleSettings)
952+
return SyntheticBlockWrapper(block, hbsBlock, styleSettings)
915953
}
916954

917-
override fun getSubBlocks(): MutableList<JsBlockWrapper> {
955+
override fun getSubBlocks(): MutableList<Block> {
918956
if (this.cachedBlocks != null) {
919957
return this.cachedBlocks!!
920958
}
921959
val blocks = block.subBlocks.mapNotNull { mapToWrapper(it, hbsBlock) }.toMutableList()
922960

923961
blocks.toTypedArray().forEach {
924962
// blocks.removeIf { it.block is RootBlockWrapper && it.block.patched }
925-
if (it.block is RootBlockWrapper && !it.block.patched) {
963+
if (it.block is RootBlockWrapper && !(it.block as RootBlockWrapper).patched) {
926964
it.block.parent = this
927965
}
928966
}
929967

930-
val psiElement = this.node?.psi
968+
val psiElement = this.node.psi
931969
if (psiElement is JSVariable || (psiElement is JSExpressionStatement) && psiElement.children.find { it is JSAssignmentExpression } != null) {
932970
val last = PsiTreeUtil.collectElements(psiElement) { it is JSLiteralExpression}.lastOrNull()
933971
if (last is JSLiteralExpression && last.textLength == 0 && last.textOffset == psiElement.endOffset) {
934-
val outerLanguageBlock = parent?.parent?.nexOuterLanguageBlock(parent.block) ?: parent?.nexOuterLanguageBlock(this.block)
972+
val outerLanguageBlock = parent?.parent?.nexOuterLanguageBlock(parent!!.block) ?: parent?.nexOuterLanguageBlock(this.block)
935973
if (outerLanguageBlock != null) {
936974
outerLanguageBlock.patched = true
937975
outerLanguageBlock.parent = this
@@ -1058,8 +1096,8 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
10581096
if (block?.block is RootBlockWrapper && block.textRange.contains(element.textRange)) {
10591097
return block
10601098
}
1061-
block?.subBlocks?.forEach {
1062-
val b = findRootBlock(it, element)
1099+
block?.subBlocks?.filter { it is JsBlockWrapper }?.forEach {
1100+
val b = findRootBlock(it as JsBlockWrapper, element)
10631101
if (b != null) {
10641102
return b
10651103
}

0 commit comments

Comments
 (0)