Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/test/testData/sql/formatter/CalculationDirectives.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down
Loading