override fun getChildAttributes()

in gdscript/src/main/kotlin/gdscript/formatter/block/GdBlock.kt [103:180]


    override fun getChildAttributes(newChildIndex: Int): ChildAttributes {
        val index = newChildIndex - 1
        val previousBlock = if (index >= 0) this.subBlocks[index] else null
        val preceding = if (previousBlock is GdBlock) previousBlock.node.psi else null
        val atEndOfStmt = node is FileElement
        val precedingOffset = when (previousBlock?.indent?.type) {
            Indent.Type.NORMAL -> 2
            Indent.Type.CONTINUATION -> 3
            else -> 0
        }

        // Check is it is right after COLON
        if (previousBlock is GdBlock) {
            val lastNode = PsiTreeUtil.getDeepestVisibleLast(previousBlock.node.psi)
            if (arrayOf(COLON).contains(lastNode?.elementType)) {
                if (precedingOffset > 0) {
                    if (preceding != null) {
                        return ChildAttributes(settings.calculateSpaceIndents(preceding), null)
                    }
                    return ChildAttributes(Indent.getContinuationIndent(false), null)
                }

//                if (atEndOfStmt && preceding != null) {
//                    return ChildAttributes(settings.calculateSpaceIndents(preceding, 1 - precedingOffset), null)
//                } else if (preceding != null && lastNode?.nextLeaf(false) is PsiErrorElement) {
//                    return ChildAttributes(settings.calculateSpaceIndents(preceding, 1, true), null)
//                }
                return ChildAttributes(Indent.getNormalIndent(false), null)
            }
        }

        val caretOffset = node.psi.getCaretOffsetIfSingle()
        if (
            previousBlock is GdBlock && preceding != null
            && previousBlock.node.lastChildNode?.elementType == STMT_OR_SUITE
            && previousBlock.node.lastChildNode?.firstChildNode?.elementType == SUITE
        ) {
            if (caretOffset != null) {
                val emptyLines = PsiTreeUtil.getDeepestLast(preceding).precedingNewLines(caretOffset)
                if (emptyLines > 2) {
                    return ChildAttributes(
                        settings.calculateSpaceIndents(preceding, 2 - emptyLines - precedingOffset),
                        null
                    )
                }
            }

            if (atEndOfStmt) {
                return ChildAttributes(settings.calculateSpaceIndents(preceding), null)
            }

            return ChildAttributes(Indent.getContinuationIndent(false), null)
        }

        if (node.elementType == CALL_EX) {
            return ChildAttributes(Indent.getNormalIndent(), null)
        }

        // Inside indented blocks directly
        if (GdBlocks.INDENT_CHILDREN_ATTRIBUTE.contains(node.elementType)) {
            if (caretOffset != null) {
                val emptyLines = PsiTreeUtil.getDeepestLast(node.psi).precedingNewLines(caretOffset)
                if (emptyLines > 2) {
                    return ChildAttributes(
                        settings.calculateSpaceIndents(node.psi, 2 - emptyLines - precedingOffset),
                        null
                    )
                } else {
                    return ChildAttributes(Indent.getNormalIndent(), null)
                }
            }

            return ChildAttributes(Indent.getNormalIndent(), null)
        }


        return ChildAttributes(Indent.getNoneIndent(), null)
    }