Skip to content

Commit 3a07e63

Browse files
committed
Refactor parent group assignment logic and improve block indentation handling
1 parent 18ba6cd commit 3a07e63

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlBlockRelationBuilder.kt

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -575,31 +575,64 @@ class SqlBlockRelationBuilder(
575575
getParentGroup: (MutableList<SqlBlock>) -> SqlBlock?,
576576
) {
577577
val parentGroup =
578-
getParentGroup(context.blockBuilder.getGroupTopNodeIndexHistory() as MutableList<SqlBlock>)
579-
578+
getParentGroup(
579+
context.blockBuilder.getGroupTopNodeIndexHistory() as MutableList<SqlBlock>,
580+
)
580581
val targetChildBlock = context.childBlock
581582

582-
if (targetChildBlock is SqlDefaultCommentBlock) return
583+
if (shouldSkipParentSetting(targetChildBlock)) return
584+
585+
assignParentGroup(targetChildBlock, parentGroup)
586+
registerAsNewGroupIfNeeded(targetChildBlock, context.blockBuilder)
587+
updateBlockIndents(targetChildBlock, context.blockBuilder)
588+
}
589+
590+
private fun shouldSkipParentSetting(block: SqlBlock) = block is SqlDefaultCommentBlock
583591

584-
// The parent block for SqlElConditionLoopCommentBlock will be set later
585-
if (targetChildBlock is SqlElConditionLoopCommentBlock && targetChildBlock.conditionType.isStartDirective()) {
586-
targetChildBlock.tempParentBlock = parentGroup
587-
if ((parentGroup is SqlElConditionLoopCommentBlock || parentGroup is SqlSubGroupBlock) && parentGroup.parentBlock != null) {
588-
targetChildBlock.setParentGroupBlock(parentGroup)
592+
private fun assignParentGroup(
593+
targetChildBlock: SqlBlock,
594+
parentGroup: SqlBlock?,
595+
) {
596+
when {
597+
isStartDirectiveConditionLoop(targetChildBlock) -> {
598+
val conditionLoop = targetChildBlock as SqlElConditionLoopCommentBlock
599+
conditionLoop.tempParentBlock = parentGroup
600+
if (shouldSetParentImmediately(parentGroup)) {
601+
conditionLoop.setParentGroupBlock(parentGroup)
602+
}
589603
}
590-
} else {
591-
targetChildBlock.setParentGroupBlock(parentGroup)
604+
else -> targetChildBlock.setParentGroupBlock(parentGroup)
592605
}
606+
}
593607

594-
if (isNewGroup(targetChildBlock, context.blockBuilder) ||
595-
TypeUtil.isExpectedClassType(NEW_GROUP_EXPECTED_TYPES, targetChildBlock)
596-
) {
597-
context.blockBuilder.addGroupTopNodeIndexHistory(targetChildBlock)
608+
private fun isStartDirectiveConditionLoop(block: SqlBlock) =
609+
block is SqlElConditionLoopCommentBlock && block.conditionType.isStartDirective()
610+
611+
private fun shouldSetParentImmediately(parentGroup: SqlBlock?) =
612+
(parentGroup is SqlElConditionLoopCommentBlock || parentGroup is SqlSubGroupBlock) &&
613+
parentGroup.parentBlock != null
614+
615+
private fun registerAsNewGroupIfNeeded(
616+
targetChildBlock: SqlBlock,
617+
blockBuilder: SqlBlockBuilder,
618+
) {
619+
if (shouldRegisterAsNewGroup(targetChildBlock, blockBuilder)) {
620+
blockBuilder.addGroupTopNodeIndexHistory(targetChildBlock)
598621
}
622+
}
599623

600-
context.blockBuilder.updateCommentBlockIndent(targetChildBlock)
601-
// Set parent-child relationship and indent for preceding comment at beginning of block group
602-
context.blockBuilder.updateConditionLoopBlockIndent(targetChildBlock)
624+
private fun shouldRegisterAsNewGroup(
625+
block: SqlBlock,
626+
blockBuilder: SqlBlockBuilder,
627+
) = isNewGroup(block, blockBuilder) ||
628+
TypeUtil.isExpectedClassType(NEW_GROUP_EXPECTED_TYPES, block)
629+
630+
private fun updateBlockIndents(
631+
targetChildBlock: SqlBlock,
632+
blockBuilder: SqlBlockBuilder,
633+
) {
634+
blockBuilder.updateCommentBlockIndent(targetChildBlock)
635+
blockBuilder.updateConditionLoopBlockIndent(targetChildBlock)
603636
}
604637

605638
/**
@@ -634,7 +667,7 @@ class SqlBlockRelationBuilder(
634667
}
635668

636669
val isSetLineGroup =
637-
SqlKeywordUtil.Companion.isSetLineKeyword(
670+
SqlKeywordUtil.isSetLineKeyword(
638671
childBlock.getNodeText(),
639672
lastKeywordText,
640673
)

0 commit comments

Comments
 (0)