Skip to content

Commit 6b18951

Browse files
committed
Fix SQL formatting logic for condition loop comments and improve indentation handling
1 parent 63aa0c4 commit 6b18951

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2828
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2929
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
31-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
3231
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3332
import org.domaframework.doma.intellij.formatter.util.IndentType
3433
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,16 @@ class SqlElConditionLoopCommentBlock(
249249
// At this point, it's not possible to determine whether the parent keyword group appears before or after this block based solely on the parent-child relationship.
250250
// Therefore, determine the position directly using the text offset.
251251
return if (!isBeforeParentBlock()) {
252-
// The child branch applies in cases where a conditional directive is included as a child of this block.
253-
val questOffset = if (parent is SqlWithQueryGroupBlock) 0 else 1
254-
parent.indent.groupIndentLen
255-
.plus(openConditionLoopDirectiveCount * 2)
256-
.plus(questOffset)
252+
val lastBlockConditionLoopCommentBlock: SqlElConditionLoopCommentBlock? = getLastBlockHasConditionLoopDirective()
253+
if (lastBlockConditionLoopCommentBlock != null && lastBlockConditionLoopCommentBlock.conditionEnd != null) {
254+
lastBlockConditionLoopCommentBlock.indent.indentLen
255+
} else {
256+
// The child branch applies in cases where a conditional directive is included as a child of this block.
257+
val questOffset = if (parent is SqlWithQueryGroupBlock) 0 else 1
258+
parent.indent.groupIndentLen
259+
.plus(openConditionLoopDirectiveCount * 2)
260+
.plus(questOffset)
261+
}
257262
} else {
258263
parent.indent.indentLen.plus(openConditionLoopDirectiveCount * 2)
259264
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.top
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.SqlFileBlock
2021
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
2223
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -41,8 +42,8 @@ class SqlJoinQueriesGroupBlock(
4142
override fun createBlockIndentLen(): Int {
4243
parentBlock?.let { parent ->
4344
return when (parent) {
44-
is SqlWithQuerySubGroupBlock,
45-
-> parent.indent.groupIndentLen
45+
is SqlFileBlock -> 0
46+
is SqlWithQuerySubGroupBlock -> parent.indent.groupIndentLen
4647
is SqlElConditionLoopCommentBlock -> createIndentLenInConditionLoopDirective(parent)
4748
else -> parent.indent.groupIndentLen.plus(1)
4849
}

0 commit comments

Comments
 (0)