Skip to content

Commit d992d7d

Browse files
committed
Refactor SQL block handling to use isParentConditionLoopDirective for improved readability and consistency
1 parent 6729fe1 commit d992d7d

File tree

10 files changed

+20
-18
lines changed

10 files changed

+20
-18
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ open class SqlBlock(
105105
protected fun isConditionLoopDirectiveRegisteredBeforeParent(): Boolean {
106106
val firstPrevBlock = (prevBlocks.lastOrNull() as? SqlElConditionLoopCommentBlock)
107107
parentBlock?.let { parent ->
108-
109108
return (childBlocks.firstOrNull() as? SqlElConditionLoopCommentBlock)?.isBeforeParentBlock() == true ||
110109
firstPrevBlock != null &&
111110
firstPrevBlock.conditionEnd != null &&
@@ -132,7 +131,7 @@ open class SqlBlock(
132131
protected fun isElementAfterConditionLoopDirective(): Boolean =
133132
(parentBlock as? SqlElConditionLoopCommentBlock)?.let { parent ->
134133
parent.childBlocks.firstOrNull() == this &&
135-
(parent.parentBlock is SqlNewGroupBlock || parent.parentBlock is SqlElConditionLoopCommentBlock)
134+
(parent.parentBlock is SqlNewGroupBlock || parent.isParentConditionLoopDirective())
136135
} == true
137136

138137
protected fun isElementAfterConditionLoopEnd(): Boolean {
@@ -160,6 +159,8 @@ open class SqlBlock(
160159
)?.conditionEnd
161160
)
162161

162+
fun isParentConditionLoopDirective(): Boolean = parentBlock is SqlElConditionLoopCommentBlock
163+
163164
protected fun isFirstChildConditionLoopDirective(): Boolean = childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock
164165

165166
fun getChildBlocksDropLast(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ open class SqlFileBlock(
359359
}
360360

361361
is SqlInlineGroupBlock -> {
362-
// case-end
363362
blockRelationBuilder.updateGroupBlockParentAndAddGroup(
364363
childBlock,
365364
)
@@ -471,7 +470,7 @@ open class SqlFileBlock(
471470
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
472471
}
473472

474-
if (childBlock1 is SqlWhitespaceBlock && childBlock2.parentBlock is SqlElConditionLoopCommentBlock) {
473+
if (childBlock1 is SqlWhitespaceBlock && childBlock2.isParentConditionLoopDirective()) {
475474
val child1 = childBlock2.parentBlock as SqlElConditionLoopCommentBlock
476475
SqlCustomSpacingBuilder()
477476
.getSpacingElDirectiveComment(child1, childBlock2)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package org.domaframework.doma.intellij.formatter.block
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
20-
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2120
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2322
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
@@ -96,7 +95,7 @@ open class SqlKeywordBlock(
9695
}
9796

9897
else -> {
99-
if (parentBlock is SqlElConditionLoopCommentBlock) {
98+
if (isParentConditionLoopDirective()) {
10099
parentBlock?.indent?.groupIndentLen ?: 1
101100
} else {
102101
parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.domaframework.doma.intellij.formatter.block.comma
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21-
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2221
import org.domaframework.doma.intellij.formatter.util.IndentType
2322
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2423

@@ -49,5 +48,5 @@ class SqlArrayCommaBlock(
4948

5049
override fun createGroupIndentLen(): Int = indent.indentLen.plus(1)
5150

52-
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock is SqlElConditionLoopCommentBlock
51+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = isParentConditionLoopDirective()
5352
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ open class SqlKeywordGroupBlock(
182182

183183
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
184184
val conditionLastGroup =
185-
if (parentBlock is SqlElConditionLoopCommentBlock) {
185+
if (isParentConditionLoopDirective()) {
186186
parentBlock?.parentBlock
187187
} else {
188188
lastGroup

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

Lines changed: 1 addition & 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.create.SqlCreateKeywordGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.inline
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21-
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2322
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2423
import org.domaframework.doma.intellij.formatter.util.IndentType
@@ -51,7 +50,7 @@ open class SqlInlineGroupBlock(
5150

5251
override fun createBlockIndentLen(): Int =
5352
parentBlock?.let { parent ->
54-
if (parent is SqlElConditionLoopCommentBlock || childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock ||
53+
if (isParentConditionLoopDirective() || isFirstChildConditionLoopDirective() ||
5554
parent is SqlSubGroupBlock
5655
) {
5756
parent.indent.groupIndentLen

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ 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.SqlElConditionLoopCommentBlock
2120
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2322
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
@@ -38,7 +37,7 @@ class SqlExistsGroupBlock(
3837

3938
override fun createBlockIndentLen(): Int {
4039
parentBlock?.let { parent ->
41-
if (parent.parentBlock is SqlElConditionLoopCommentBlock) {
40+
if (parent.isParentConditionLoopDirective()) {
4241
return parent.indent.groupIndentLen
4342
}
4443
}
@@ -47,9 +46,14 @@ class SqlExistsGroupBlock(
4746

4847
override fun createGroupIndentLen(): Int {
4948
// If this group is not the top of a line, there must be one space between it and the block before it.
50-
val correctionSpace = if(childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock){
51-
0
52-
}else 1
49+
val correctionSpace =
50+
if (isFirstChildConditionLoopDirective() ||
51+
isParentConditionLoopDirective()
52+
) {
53+
0
54+
} else {
55+
1
56+
}
5357
val parentGroupIndent = parentBlock?.indent?.groupIndentLen ?: 0
5458
return getTotalTopKeywordLength().plus(parentGroupIndent).plus(correctionSpace)
5559
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class SqlFunctionParamBlock(
9090
}
9191
?: emptyList()
9292
val parentText =
93-
if (parentBlock is SqlElConditionLoopCommentBlock) {
93+
if (isParentConditionLoopDirective()) {
9494
val grand = parentBlock?.parentBlock
9595
grand?.getNodeText() ?: ""
9696
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class SqlEscapeBlock(
3737

3838
override fun createBlockIndentLen(): Int {
3939
val parentEscapeBlock =
40-
if (parentBlock is SqlElConditionLoopCommentBlock) {
40+
if (isParentConditionLoopDirective()) {
4141
if (parentBlock?.parentBlock is SqlEscapeBlock)1 else 0
4242
} else {
4343
0

0 commit comments

Comments
 (0)