diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt index 50e1312d..505e526b 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt @@ -136,14 +136,25 @@ open class SqlBlock( .firstOrNull() ?.childBlocks - val firstConditionBlock = (prevChildren?.firstOrNull() as? SqlElConditionLoopCommentBlock) - val endBlock = firstConditionBlock?.conditionEnd + val firstConditionBlock = prevChildren?.firstOrNull { it is SqlElConditionLoopCommentBlock } as? SqlElConditionLoopCommentBlock + val endBlock = + findConditionEndBlock(firstConditionBlock) if (endBlock == null) return false val lastBlock = prevBlocks.lastOrNull() return endBlock.node.startOffset > (lastBlock?.node?.startOffset ?: 0) } + private fun findConditionEndBlock(firstConditionBlock: SqlElConditionLoopCommentBlock?): SqlElConditionLoopCommentBlock? = + ( + firstConditionBlock?.conditionEnd + ?: ( + firstConditionBlock?.childBlocks?.lastOrNull { + it is SqlElConditionLoopCommentBlock + } as? SqlElConditionLoopCommentBlock + )?.conditionEnd + ) + protected fun isFirstChildConditionLoopDirective(): Boolean = childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock fun getChildBlocksDropLast( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlElConditionLoopCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlElConditionLoopCommentBlock.kt index 66f081f6..8306b514 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlElConditionLoopCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlElConditionLoopCommentBlock.kt @@ -137,7 +137,7 @@ class SqlElConditionLoopCommentBlock( } override fun setParentPropertyBlock(lastGroup: SqlBlock?) { - if (lastGroup is SqlElConditionLoopCommentBlock && conditionType.isEnd()) { + if (lastGroup is SqlElConditionLoopCommentBlock && !conditionType.isStartDirective()) { lastGroup.conditionEnd = this } } @@ -195,7 +195,7 @@ class SqlElConditionLoopCommentBlock( is SqlSubGroupBlock -> return calculateSubGroupBlockIndent(parent, openConditionLoopDirectiveCount) is SqlElConditionLoopCommentBlock -> { - if (conditionType.isEnd()) { + if (!conditionType.isStartDirective()) { parent.conditionEnd = this conditionStart = parent return parent.indent.indentLen diff --git a/src/test/testData/sql/formatter/CalculationDirectives.sql b/src/test/testData/sql/formatter/CalculationDirectives.sql index 24cb8e1c..5fc7c430 100644 --- a/src/test/testData/sql/formatter/CalculationDirectives.sql +++ b/src/test/testData/sql/formatter/CalculationDirectives.sql @@ -3,6 +3,11 @@ SELECT employee_id , salary , salary * /* yearBonusRate */1.2 AS yearly_bonus , salary + (salary * /* @raiseRate() */0.05) AS new_salary + , /*%if parent*/ + number1.d, + /*%else*/ + number2.e, + /*%end*/ number.x FROM employee WHERE department_id = /* departmentId */1 /*%if @minSalary() +extraAmount*/ diff --git a/src/test/testData/sql/formatter/CalculationDirectives_format.sql b/src/test/testData/sql/formatter/CalculationDirectives_format.sql index 0a8c4c7f..cc3977c5 100644 --- a/src/test/testData/sql/formatter/CalculationDirectives_format.sql +++ b/src/test/testData/sql/formatter/CalculationDirectives_format.sql @@ -3,6 +3,14 @@ SELECT employee_id , salary , salary * /* yearBonusRate */1.2 AS yearly_bonus , salary + (salary * /* @raiseRate() */0.05) AS new_salary + , /*%if parent*/ + number1.d + , + /*%else*/ + number2.e + , + /*%end*/ + number.x FROM employee WHERE department_id = /* departmentId */1 /*%if @minSalary() + extraAmount*/