override fun buildChildren()

in gdscript/src/main/kotlin/gdscript/formatter/block/GdBlock.kt [45:101]


    override fun buildChildren(): MutableList<Block> {
        val blocks = mutableListOf<Block>()
        val children: MutableList<ASTNode> = node.getChildren(null).toMutableList()

        var suited = false
        var indented = false
        var lastBlock: GdBlock? = null
        val rootPosition = this.node is FileElement

        while (!children.isEmpty()) {
            val child = children.removeFirstOrNull()!!
            val type = child.elementType
            if (suited || rootPosition) { // Roots consume new_line instead of passing it as white_space
                alignments.reset(type, if (rootPosition) 1 else 2)
            }

            // Due to elif & else being siblings and not children
            if (GdBlocks.DEDENT_TOKENS.contains(type)) indented = false

            if (GdBlocks.EMPTY_TOKENS.contains(type)) {
                if (type == INDENT) {
                    indented = true
                }
            } else if (GdBlocks.SKIP_TOKENS.contains(type)) {
                if (type == SUITE) {
                    suited = true
                    alignments.initialize()
                }

                child.getChildren(null).forEachReversed { children.add(0, it) }
            } else {
                var toIndent = indented || GdBlocks.ALWAYS_INDENTED_TOKENS.contains(type)
                // Unique case of comment before Indentation
                if (!toIndent && type == COMMENT && child.psi.nextNonWhiteCommentToken().elementType == INDENT) {
                    toIndent = true
                }

                val currentBlock = GdBlock(
                    child,
                    null, //Wrap.createWrap(WrapType.NONE, false),
                    alignments.getAlignment(type),
                    settings,
                    spacing,
                    if (toIndent) Indent.getNormalIndent() else Indent.getNoneIndent(),
                    alignments.clone(type),
                )
                if (lastBlock != null) {
                    lastBlock.nextBlock = currentBlock
                }

                blocks.add(currentBlock)
                lastBlock = currentBlock
            }
        }

        return blocks
    }