Skip to content

Commit 9ba4ebe

Browse files
committed
Fix detection and indentation logic for EXISTS clauses within conditional directives
1 parent 60b4203 commit 9ba4ebe

File tree

6 files changed

+72
-31
lines changed

6 files changed

+72
-31
lines changed

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.block
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2021
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
@@ -95,7 +96,11 @@ open class SqlKeywordBlock(
9596
}
9697

9798
else -> {
98-
parentBlock?.indent?.groupIndentLen ?: 1
99+
if (parentBlock is SqlElConditionLoopCommentBlock) {
100+
parentBlock?.indent?.groupIndentLen ?: 1
101+
} else {
102+
parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
103+
}
99104
}
100105
}
101106
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/option/SqlExistsGroupBlock.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.option
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2021
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
2223
import org.domaframework.doma.intellij.formatter.util.IndentType
@@ -32,13 +33,18 @@ class SqlExistsGroupBlock(
3233
indent.groupIndentLen = createGroupIndentLen()
3334
}
3435

35-
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
36-
37-
override fun createGroupIndentLen(): Int {
36+
override fun createBlockIndentLen(): Int {
3837
parentBlock?.let { parent ->
39-
return parent.indent.groupIndentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) })
38+
if (parent.parentBlock is SqlElConditionLoopCommentBlock) {
39+
return parent.indent.groupIndentLen
40+
}
4041
}
41-
return topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1)
42+
return parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
43+
}
44+
45+
override fun createGroupIndentLen(): Int {
46+
val parentGroupIndent = parentBlock?.indent?.groupIndentLen ?: 0
47+
return topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.plus(parentGroupIndent).minus(1)
4248
}
4349

4450
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ open class SqlSubQueryGroupBlock(
6666
is SqlJoinQueriesGroupBlock -> return parent.indent.indentLen
6767
is SqlJoinGroupBlock -> return parent.indent.groupIndentLen.plus(1)
6868
else -> {
69-
val children =
70-
prevChildren?.filter {
71-
it !is SqlDefaultCommentBlock &&
72-
(parent as? SqlKeywordGroupBlock)?.topKeywordBlocks?.contains(it) == false
73-
}
69+
val children = prevChildren?.filter { shouldIncludeChildBlock(it, parent) }
7470
// Retrieve the list of child blocks excluding the conditional directive that appears immediately before this block,
7571
// as it is already included as a child block.
7672
val sumChildren =
@@ -90,6 +86,13 @@ open class SqlSubQueryGroupBlock(
9086
}
9187
} ?: offset
9288

89+
private fun shouldIncludeChildBlock(
90+
block: SqlBlock,
91+
parent: SqlBlock,
92+
): Boolean =
93+
block !is SqlDefaultCommentBlock &&
94+
(parent as? SqlKeywordGroupBlock)?.topKeywordBlocks?.contains(block) == false
95+
9396
override fun createGroupIndentLen(): Int {
9497
parentBlock?.let { parent ->
9598
if (parent is SqlJoinQueriesGroupBlock) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockGenerator.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
4545
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
4646
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
4747
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
48+
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlExistsGroupBlock
4849
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
4950
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock
5051
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
@@ -148,6 +149,24 @@ class SqlBlockGenerator(
148149
sqlBlockFormattingCtx,
149150
)
150151
}
152+
153+
if (SqlKeywordUtil.isExistsKeyword(keywordText)) {
154+
if (lastGroupBlock is SqlExistsGroupBlock) {
155+
return SqlKeywordBlock(
156+
child,
157+
indentLevel,
158+
sqlBlockFormattingCtx,
159+
)
160+
}
161+
if (lastGroupBlock is SqlElConditionLoopCommentBlock && lastGroupBlock.conditionType.isElse() ||
162+
keywordText == "not" && (lastGroupBlock !is SqlConditionKeywordGroupBlock && lastGroupBlock !is SqlWhereGroupBlock)
163+
) {
164+
return SqlExistsGroupBlock(
165+
child,
166+
sqlBlockFormattingCtx,
167+
)
168+
}
169+
}
151170
}
152171

153172
IndentType.CONFLICT -> {

src/test/testData/sql/formatter/ConditionalExists.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
SELECT e.id
22
, e.name
33
, d.department_name
4-
FROM employees e
4+
FROM employees e /*%if join */
5+
INNER JOIN separators spr
6+
ON EXISTS ( SELECT id FROM spr )
7+
/*%end */
58
WHERE
69
/*%if isNot*/
710
NOT

src/test/testData/sql/formatter/ConditionalExists_format.sql

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ SELECT e.id
22
, e.name
33
, d.department_name
44
FROM employees e
5+
/*%if join */
6+
INNER JOIN separators spr
7+
ON EXISTS ( SELECT id
8+
FROM spr )
9+
/*%end */
510
WHERE
611
/*%if isNot*/
712
NOT
@@ -18,27 +23,27 @@ SELECT e.id
1823
/*%end*/ )
1924
/*%elseif filterByLowPerformers */
2025
/*%if isNot*/
21-
NOT
26+
NOT
2227
/*%end*/
23-
EXISTS ( SELECT 1
24-
FROM performance_reviews pr
25-
WHERE pr.employee_id = e.id
26-
/*%if reviewPeriod != null */
27-
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
28-
/*%end*/
29-
/*%if minScore != null */
30-
AND pr.performance_score >= /* minScore */4.0
31-
/*%end*/ )
28+
EXISTS ( SELECT 1
29+
FROM performance_reviews pr
30+
WHERE pr.employee_id = e.id
31+
/*%if reviewPeriod != null */
32+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
33+
/*%end*/
34+
/*%if minScore != null */
35+
AND pr.performance_score >= /* minScore */4.0
36+
/*%end*/ )
3237
/*%elseif filterByMediumPerformers */
33-
NOT EXISTS ( SELECT 1
34-
FROM performance_reviews pr
35-
WHERE pr.employee_id = e.id
36-
/*%if reviewPeriod != null */
37-
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
38-
/*%end*/
39-
/*%if minScore != null */
40-
AND pr.performance_score >= /* minScore */4.0
41-
/*%end*/ )
38+
NOT EXISTS ( SELECT 1
39+
FROM performance_reviews pr
40+
WHERE pr.employee_id = e.id
41+
/*%if reviewPeriod != null */
42+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
43+
/*%end*/
44+
/*%if minScore != null */
45+
AND pr.performance_score >= /* minScore */4.0
46+
/*%end*/ )
4247
/*%else*/
4348
e.is_active = true
4449
/*%end*/

0 commit comments

Comments
 (0)