Skip to content

Commit 0344060

Browse files
committed
Fix keyword group formatting inside parameter groups when enclosed by conditional loop directives
1 parent cb47585 commit 0344060

File tree

10 files changed

+125
-11
lines changed

10 files changed

+125
-11
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ open class SqlCommaBlock(
146146
}
147147
else -> {
148148
// No indent after ORDER BY within function parameters
149-
if (parent is SqlSecondKeywordBlock && parent.parentBlock is SqlFunctionParamBlock) {
149+
val grand = parent.parentBlock
150+
val conditionParent =
151+
if (grand is SqlElConditionLoopCommentBlock) {
152+
grand.parentBlock
153+
} else {
154+
grand
155+
}
156+
if (parent is SqlSecondKeywordBlock && conditionParent is SqlFunctionParamBlock) {
150157
0
151158
} else {
152159
parent.indent.groupIndentLen.plus(1)
@@ -164,7 +171,14 @@ open class SqlCommaBlock(
164171
parentBlock?.let { parent ->
165172
if (parent is SqlConditionalExpressionGroupBlock) return false
166173
// Don't allow line breaks after ORDER BY within function parameters
167-
if (parent.parentBlock is SqlFunctionParamBlock) {
174+
val grand = parent.parentBlock
175+
val conditionParent =
176+
if (grand is SqlElConditionLoopCommentBlock) {
177+
grand.parentBlock
178+
} else {
179+
grand
180+
}
181+
if (conditionParent is SqlFunctionParamBlock) {
168182
return false
169183
}
170184
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parent)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class SqlElConditionLoopCommentBlock(
7474
listOf(
7575
SqlSubGroupBlock::class,
7676
SqlColumnRawGroupBlock::class,
77-
SqlElConditionLoopCommentBlock::class,
7877
)
7978
}
8079

@@ -173,10 +172,14 @@ class SqlElConditionLoopCommentBlock(
173172
if (conditionType.isEnd() || conditionType.isElse()) {
174173
return true
175174
}
175+
if (lastGroup is SqlElConditionLoopCommentBlock) {
176+
return true
177+
}
178+
val firstChild = lastGroup?.childBlocks?.firstOrNull()
176179
if (TypeUtil.isExpectedClassType(LINE_BREAK_PARENT_TYPES, lastGroup)) {
177-
return lastGroup?.childBlocks?.dropLast(1)?.isNotEmpty() == true || lastGroup is SqlElConditionLoopCommentBlock
180+
return firstChild != null && firstChild != this
178181
}
179-
return lastGroup?.childBlocks?.firstOrNull() != this
182+
return firstChild == null || firstChild != this
180183
}
181184

182185
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,15 @@ open class SqlKeywordGroupBlock(
179179
override fun createGroupIndentLen(): Int = indent.indentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1))
180180

181181
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
182+
val conditionLastGroup =
183+
if (parentBlock is SqlElConditionLoopCommentBlock) {
184+
parentBlock?.parentBlock
185+
} else {
186+
lastGroup
187+
}
182188
val prevWord = prevBlocks.findLast { it is SqlKeywordBlock || it is SqlKeywordGroupBlock }
183189
return !SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), prevWord?.getNodeText() ?: "") &&
190+
!SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), conditionLastGroup?.getNodeText() ?: "") &&
184191
!SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), lastGroup?.getNodeText() ?: "") &&
185192
lastGroup !is SqlFunctionParamBlock
186193
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/second/SqlSecondKeywordBlock.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ open class SqlSecondKeywordBlock(
6363

6464
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
6565
parentBlock?.let { parent ->
66-
if (parent is SqlFunctionParamBlock) {
66+
val conditionParent =
67+
if (parent is SqlElConditionLoopCommentBlock) {
68+
parent.parentBlock
69+
} else {
70+
parent
71+
}
72+
if (conditionParent is SqlFunctionParamBlock) {
6773
val firstKeywordParam =
68-
parent.childBlocks.firstOrNull { it is SqlNewGroupBlock }
74+
conditionParent.childBlocks.firstOrNull { it is SqlNewGroupBlock }
6975
return firstKeywordParam != null && firstKeywordParam != this
7076
} else {
7177
return super.isSaveSpace(lastGroup)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ abstract class SqlSubGroupBlock(
128128
return false
129129
}
130130
}
131-
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, lastBlock.parentBlock)
131+
val lastParent = lastBlock.parentBlock
132+
val expectedParent =
133+
if (lastParent is SqlElConditionLoopCommentBlock) {
134+
lastParent.parentBlock?.parentBlock
135+
} else {
136+
lastBlock.parentBlock
137+
}
138+
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, expectedParent)
132139
}
133140
return false
134141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SqlFunctionGroupBlock(
3939
indent.groupIndentLen = createGroupIndentLen()
4040
}
4141

42-
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen ?: 0
42+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 0
4343

4444
override fun createGroupIndentLen(): Int {
4545
val baseIndent =

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,15 @@ class SqlBlockRelationBuilder(
529529
if (parentSubGroup is SqlFunctionParamBlock) {
530530
// If the parent is a function parameter group, remove up to the parent function name and keyword group.
531531
val parent = blockBuilder.getGroupTopNodeIndexHistory()[paramIndex].parentBlock
532+
val searchFunctionName =
533+
if (parent is SqlElConditionLoopCommentBlock) {
534+
parent.parentBlock
535+
} else {
536+
parent
537+
}
532538
val functionParent =
533539
blockBuilder.getGroupTopNodeIndex {
534-
it == parent
540+
it == searchFunctionName
535541
}
536542
if (functionParent >= 0) {
537543
blockBuilder.clearSubListGroupTopNodeIndexHistory(functionParent)

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlFormatPreProcessor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ class SqlFormatPreProcessor : PreFormatProcessor {
224224
private fun getKeywordNewText(element: PsiElement): String {
225225
val keywordText = element.text.lowercase()
226226
val upperText = getUpperText(element)
227-
return if (SqlKeywordUtil.getIndentType(keywordText).isNewLineGroup()) {
227+
return if (SqlKeywordUtil.getIndentType(keywordText).isNewLineGroup() ||
228+
element.prevSibling.elementType == SqlTypes.BLOCK_COMMENT
229+
) {
228230
val prevElement = element.prevSibling
229231
getNewLineString(prevElement, upperText)
230232
} else {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
SELECT e.id
2+
-- with condition
3+
, ROW_NUMBER() OVER(/*%if order */ORDER BY e.manager_id DESC/*%end*/) AS row_num
4+
, RANK() OVER(PARTITION BY department_id
5+
/*%if order */ORDER BY e.manager_id DESC/*%end */
6+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS rank_num
7+
, DENSE_RANK() OVER(PARTITION BY department_id
8+
/*%if order */
9+
ORDER BY e.id ASC, e.manager_id ASC, created_at DESC
10+
/*%else */
11+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING/*%end */) AS dense_rank_num
12+
, SUM(amount)/*%if filter */ FILTER(WHERE status = 'active')/*%end*/ AS dept_salary_avg
13+
, COUNT(*) OVER(ORDER BY e.id, e.manager_id, created_at
14+
/*%if rows */
15+
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING/*%end*/)
16+
, FIRST_VALUE(salary) IGNORE NULLS OVER(PARTITION BY department_id
17+
ORDER BY e.id ASC, e.manager_id ASC, created_at DESC)
18+
, LAST_VALUE() OVER(ORDER BY e.manager_id ASC
19+
/*%if rows */
20+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
21+
/*%end*/) AS bottom_salary
22+
, LISTAGG(e.name
23+
, ', ') /*%if filter */WITHIN GROUP(ORDER BY name DESC)/*%end*/
24+
FROM employees e
25+
WHERE e.status = 1
26+
ORDER BY e.id
27+
, e
28+
, manager_id
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
SELECT e.id
2+
-- with condition
3+
, ROW_NUMBER() OVER(/*%if order */
4+
ORDER BY e.manager_id DESC
5+
/*%end*/) AS row_num
6+
, RANK() OVER(PARTITION BY department_id
7+
/*%if order */
8+
ORDER BY e.manager_id DESC
9+
/*%end */
10+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS rank_num
11+
, DENSE_RANK() OVER(PARTITION BY department_id
12+
/*%if order */
13+
ORDER BY e.id ASC, e.manager_id ASC, created_at DESC
14+
/*%else */
15+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
16+
/*%end */) AS dense_rank_num
17+
, SUM(amount)
18+
/*%if filter */
19+
FILTER(WHERE status = 'active')
20+
/*%end*/
21+
AS dept_salary_avg
22+
, COUNT(*) OVER(ORDER BY e.id, e.manager_id, created_at
23+
/*%if rows */
24+
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
25+
/*%end*/)
26+
, FIRST_VALUE(salary) IGNORE NULLS OVER(PARTITION BY department_id
27+
ORDER BY e.id ASC, e.manager_id ASC, created_at DESC)
28+
, LAST_VALUE() OVER(ORDER BY e.manager_id ASC
29+
/*%if rows */
30+
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
31+
/*%end*/) AS bottom_salary
32+
, LISTAGG(e.name
33+
, ', ')
34+
/*%if filter */
35+
WITHIN GROUP (ORDER BY name DESC)
36+
/*%end*/
37+
FROM employees e
38+
WHERE e.status = 1
39+
ORDER BY e.id
40+
, e
41+
, manager_id

0 commit comments

Comments
 (0)