Skip to content

Commit be7a079

Browse files
committed
Update IN clause indentation calculation to consider preceding block strings and conditional loop directives
1 parent 991489c commit be7a079

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,19 @@ open class SqlBlock(
360360
protected fun calculatePrevBlocksLength(
361361
children: List<SqlBlock>,
362362
parent: SqlBlock,
363-
): Int =
364-
children
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 }
365376
.sumOf { prev ->
366377
prev
367378
.getChildrenTextLen()
@@ -375,6 +386,8 @@ open class SqlBlock(
375386
},
376387
)
377388
}.plus(parent.indent.groupIndentLen)
389+
.plus(directiveParentIndent)
390+
}
378391

379392
/**
380393
* Returns the child indentation for the block.

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

Lines changed: 1 addition & 16 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,20 +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 = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
60-
61-
return calculatePrevBlocksLength(parentText, parent)
46+
return calculatePrevBlocksLength(prevBlocks, parent).plus(1)
6247
}
6348
return 0
6449
}

0 commit comments

Comments
 (0)