Skip to content

Commit 725ec6c

Browse files
authored
Merge pull request #429 from domaframework/fix/sql-format-delete-condition-subquery
Fix: Subquery Indentation
2 parents 267cc7a + be7a079 commit 725ec6c

File tree

8 files changed

+71
-44
lines changed

8 files changed

+71
-44
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,38 @@ open class SqlBlock(
357357
return builder
358358
}
359359

360+
protected fun calculatePrevBlocksLength(
361+
children: List<SqlBlock>,
362+
parent: SqlBlock,
363+
): Int {
364+
// Add the parent's text length to the indentation if the parent is a conditional loop directive.
365+
val directiveParentIndent =
366+
if (parent is SqlElConditionLoopCommentBlock) {
367+
parent.parentBlock
368+
?.getNodeText()
369+
?.length ?: 0
370+
} else {
371+
0
372+
}
373+
374+
return children
375+
.filter { it !is SqlDefaultCommentBlock && it !is SqlElConditionLoopCommentBlock }
376+
.sumOf { prev ->
377+
prev
378+
.getChildrenTextLen()
379+
.plus(
380+
if (prev.node.elementType == SqlTypes.DOT ||
381+
prev.node.elementType == SqlTypes.RIGHT_PAREN
382+
) {
383+
0
384+
} else {
385+
prev.getNodeText().length.plus(1)
386+
},
387+
)
388+
}.plus(parent.indent.groupIndentLen)
389+
.plus(directiveParentIndent)
390+
}
391+
360392
/**
361393
* Returns the child indentation for the block.
362394
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2324
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2425

@@ -63,4 +64,14 @@ class SqlConditionKeywordGroupBlock(
6364
}
6465
} ?: return 1
6566
}
67+
68+
override fun createGroupIndentLen(): Int {
69+
parentBlock?.let { parent ->
70+
if (parent is SqlWhereGroupBlock) {
71+
return indent.indentLen.plus(getNodeText().length)
72+
}
73+
return super.createGroupIndentLen()
74+
}
75+
return 0
76+
}
6677
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SqlConditionalExpressionGroupBlock(
6262
}
6363
groupIndentLen + directiveParentTextLen
6464
} else {
65-
parent.indent.groupIndentLen.plus(1)
65+
calculatePrevBlocksLength(prevBlocks, parent).plus(1)
6666
}
6767
}
6868
?: offset

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ 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.SqlDefaultCommentBlock
2120
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2322
import org.domaframework.doma.intellij.formatter.util.IndentType
2423
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
25-
import org.domaframework.doma.intellij.psi.SqlTypes
2624

2725
class SqlInGroupBlock(
2826
node: ASTNode,
@@ -45,28 +43,7 @@ class SqlInGroupBlock(
4543
) {
4644
return parent.indent.indentLen
4745
}
48-
val prevChildren = this.prevBlocks
49-
val children = prevChildren.filter { it !is SqlDefaultCommentBlock }
50-
val firstChild = children.firstOrNull()
51-
val sumChildren =
52-
if (firstChild is SqlElConditionLoopCommentBlock) {
53-
children.drop(1).dropLastWhile { it == this }
54-
} else {
55-
children
56-
}
57-
58-
val dotCount = sumChildren.count { it.node.elementType == SqlTypes.DOT }
59-
val parentText = (parent as? SqlElConditionLoopCommentBlock)?.parentBlock?.getNodeText()?.length ?: 0
60-
61-
return sumChildren
62-
.sumOf { prev ->
63-
prev
64-
.getChildrenTextLen()
65-
.plus(prev.getNodeText().length.plus(1))
66-
}.minus(dotCount * 2)
67-
.plus(parent.indent.groupIndentLen)
68-
.plus(parentText)
69-
.plus(1)
46+
return calculatePrevBlocksLength(prevBlocks, parent).plus(1)
7047
}
7148
return 0
7249
}

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ class SqlFunctionGroupBlock(
5050
return baseIndent.plus(getNodeText().length)
5151
}
5252

53-
private fun calculatePrevBlocksLength(
54-
children: List<SqlBlock>,
55-
parent: SqlBlock,
56-
): Int =
57-
children
58-
.sumOf { prev ->
59-
prev
60-
.getChildrenTextLen()
61-
.plus(
62-
if (prev.node.elementType == SqlTypes.DOT ||
63-
prev.node.elementType == SqlTypes.RIGHT_PAREN
64-
) {
65-
0
66-
} else {
67-
prev.getNodeText().length.plus(1)
68-
},
69-
)
70-
}.plus(parent.indent.groupIndentLen)
71-
7253
private fun calculateBaseIndent(
7354
parent: SqlBlock,
7455
prevBlocksLength: Int,

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class SqlFormatterTest : BasePlatformTestCase() {
166166
formatSqlFile("WithDelete.sql", "WithDelete$formatDataPrefix.sql")
167167
}
168168

169+
fun testDeleteWithSubQuery() {
170+
formatSqlFile("DeleteWithSubQuery.sql", "DeleteWithSubQuery$formatDataPrefix.sql")
171+
}
172+
169173
fun testNestedDirectivesFormatter() {
170174
formatSqlFile("NestedDirectives.sql", "NestedDirectives$formatDataPrefix.sql")
171175
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DELETE FROM user_session s
2+
WHERE s.count = ( SELECT COUNT(*)
3+
FROM user u
4+
WHERE u.id = /* id */1
5+
AND u.session_id = u.id
6+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
7+
OR EXISTS ( SELECT u.id
8+
FROM user u
9+
WHERE u.id = /* id */1
10+
AND u.session_id = u.id
11+
AND u.time_stamp < /* current */'2099-12-31 00:00:00')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DELETE FROM user_session s
2+
WHERE s.count = ( SELECT COUNT(*)
3+
FROM user u
4+
WHERE u.id = /* id */1
5+
AND u.session_id = u.id
6+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
7+
OR EXISTS ( SELECT u.id
8+
FROM user u
9+
WHERE u.id = /* id */1
10+
AND u.session_id = u.id
11+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )

0 commit comments

Comments
 (0)