Skip to content

Commit 2487440

Browse files
committed
Adjust keyword group indentation within conditional directives
1 parent 6b18951 commit 2487440

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
20+
21+
class SqlWhereGroupBlock(
22+
node: ASTNode,
23+
context: SqlBlockFormattingContext,
24+
) : SqlSecondKeywordBlock(node, context) {
25+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
26+
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/with/SqlWithCommonTableGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SqlWithCommonTableGroupBlock(
7777
parentBlock?.let { parent ->
7878
val prevBlock =
7979
parent
80-
.getChildBlocksDropLast()
80+
.getChildBlocksDropLast(skipConditionLoopCommentBlock = false)
8181
.lastOrNull()
8282
return if (prevBlock is SqlElConditionLoopCommentBlock) 4 else 0
8383
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,27 @@ class SqlBlockRelationBuilder(
138138
val context = SetParentContext(childBlock, blockBuilder)
139139
if (lastGroupBlock is SqlElConditionLoopCommentBlock) {
140140
handleConditionLoopParent(lastGroupBlock, context, childBlock)
141-
} else if (childBlock.indent.indentLevel == IndentType.TOP) {
141+
return
142+
}
143+
if (childBlock.indent.indentLevel == IndentType.TOP) {
142144
handleTopLevelKeyword(lastGroupBlock, childBlock, context)
143-
} else {
144-
handleNonTopLevelKeyword(lastGroupBlock, lastIndentLevel, childBlock, context)
145+
return
146+
}
147+
if (lastGroupBlock !is SqlSubGroupBlock) {
148+
if (lastIndentLevel > childBlock.indent.indentLevel) {
149+
val findLastGroup =
150+
blockBuilder.getGroupTopNodeIndexHistory().findLast {
151+
it.indent.indentLevel < childBlock.indent.indentLevel ||
152+
it is
153+
SqlElConditionLoopCommentBlock
154+
}
155+
if (findLastGroup is SqlElConditionLoopCommentBlock) {
156+
handleConditionLoopParent(findLastGroup, context, childBlock)
157+
return
158+
}
159+
}
145160
}
161+
handleNonTopLevelKeyword(lastGroupBlock, lastIndentLevel, childBlock, context)
146162
}
147163

148164
private fun handleConditionLoopParent(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlS
5151
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
5252
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
5353
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
54+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
5455
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlDeleteQueryGroupBlock
5556
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
5657
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
@@ -271,6 +272,12 @@ class SqlBlockGenerator(
271272
sqlBlockFormattingCtx,
272273
)
273274
}
275+
"where" -> {
276+
SqlWhereGroupBlock(
277+
child,
278+
sqlBlockFormattingCtx,
279+
)
280+
}
274281

275282
"values" ->
276283
SqlValuesGroupBlock(

src/test/testData/sql/formatter/ConditionalWhereClause.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ AND p.category_id IN /* categoryIds */(1, 2, 3)
1414
/*%end*/
1515
/*%if status != null */
1616
/*%if status == "available" */
17-
AND p.stock_quantity > IN /* quantitys */(0, 1, 2) AND p.is_active = true
17+
AND p.stock_quantitys IN /* quantitys */(0, 1, 2) or p.is_active = true
1818
/*%elseif status == "outofstock" */
19-
AND p.stock_quantity = IN /* quantitys */(0, 1, 2)
19+
OR p.is_active = true
20+
AND p.stock_quantitys IN /* quantitys */(0, 1, 2)
2021
/*%else*/
2122
AND p.is_active = false
2223
/*%end*/

src/test/testData/sql/formatter/ConditionalWhereClause_format.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ SELECT *
1515
/*%end*/
1616
/*%if status != null */
1717
/*%if status == "available" */
18-
AND p.stock_quantity > IN /* quantitys */(0, 1, 2)
19-
AND p.is_active = true
18+
AND p.stock_quantitys IN /* quantitys */(0, 1, 2)
19+
OR p.is_active = true
2020
/*%elseif status == "outofstock" */
21+
OR p.is_active = true
2122
AND p.stock_quantitys IN /* quantitys */(0, 1, 2)
2223
/*%else*/
2324
AND p.is_active = false

0 commit comments

Comments
 (0)