File tree Expand file tree Collapse file tree 8 files changed +59
-30
lines changed
main/kotlin/org/domaframework/doma/intellij/formatter/block
kotlin/org/domaframework/doma/intellij/formatter Expand file tree Collapse file tree 8 files changed +59
-30
lines changed Original file line number Diff line number Diff line change @@ -357,6 +357,25 @@ open class SqlBlock(
357357 return builder
358358 }
359359
360+ protected fun calculatePrevBlocksLength (
361+ children : List <SqlBlock >,
362+ parent : SqlBlock ,
363+ ): Int =
364+ children
365+ .sumOf { prev ->
366+ prev
367+ .getChildrenTextLen()
368+ .plus(
369+ if (prev.node.elementType == SqlTypes .DOT ||
370+ prev.node.elementType == SqlTypes .RIGHT_PAREN
371+ ) {
372+ 0
373+ } else {
374+ prev.getNodeText().length.plus(1 )
375+ },
376+ )
377+ }.plus(parent.indent.groupIndentLen)
378+
360379 /* *
361380 * Returns the child indentation for the block.
362381 *
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode
1919import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2121import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
22+ import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2223import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2324import 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}
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -56,17 +56,9 @@ class SqlInGroupBlock(
5656 }
5757
5858 val dotCount = sumChildren.count { it.node.elementType == SqlTypes .DOT }
59- val parentText = (parent as ? SqlElConditionLoopCommentBlock )?.parentBlock?.getNodeText()?.length ? : 0
59+ val parentText = prevChildren.dropLast( 1 ).filter { it !is SqlDefaultCommentBlock }
6060
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 )
61+ return calculatePrevBlocksLength(parentText, parent)
7062 }
7163 return 0
7264 }
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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' )
Original file line number Diff line number Diff line change 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' )
You can’t perform that action at this time.
0 commit comments