Skip to content

Commit f8b140e

Browse files
committed
Refactor the logic for retrieving child block lists into a separate function.
1 parent 428baf7 commit f8b140e

File tree

8 files changed

+13
-12
lines changed

8 files changed

+13
-12
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ open class SqlBlock(
8989
open var parentBlock: SqlBlock? = null
9090
open val childBlocks = mutableListOf<SqlBlock>()
9191

92+
fun getChildBlocksDropLast(dropIndex: Int = 1): List<SqlBlock> = childBlocks.dropLast(dropIndex)
93+
9294
open val indent: ElementIndent =
9395
ElementIndent(
9496
IndentType.FILE,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class SqlLineCommentBlock(
4141
override fun createBlockIndentLen(): Int {
4242
parentBlock?.let { parent ->
4343
if (parent is SqlSubQueryGroupBlock) {
44-
if (parent.childBlocks.dropLast(1).isEmpty()) {
44+
if (parent.getChildBlocksDropLast().isEmpty()) {
4545
return 0
4646
}
4747
if (parent.isFirstLineComment) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ open class SqlElBlockCommentBlock(
130130
override fun createBlockIndentLen(): Int {
131131
parentBlock?.let {
132132
if (it is SqlSubQueryGroupBlock) {
133-
if (it.childBlocks.dropLast(1).isEmpty()) {
133+
if (it.getChildBlocksDropLast().isEmpty()) {
134134
return 0
135135
}
136136
if (it.isFirstLineComment) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SqlSelectQueryGroupBlock(
6565
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
6666
lastGroup?.let { lastBlock ->
6767
if (lastGroup is SqlWithQuerySubGroupBlock) return true
68-
if (lastBlock is SqlSubGroupBlock) return lastBlock.childBlocks.dropLast(1).isNotEmpty()
68+
if (lastBlock is SqlSubGroupBlock) return lastBlock.getChildBlocksDropLast().isNotEmpty()
6969
}
7070
return true
7171
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SqlWithCommonTableGroupBlock(
5656
private fun findWithQueryChildBlocks(): SqlBlock? {
5757
parentBlock?.let { parent ->
5858
if (parent is SqlWithQueryGroupBlock) {
59-
return parent.childBlocks.dropLast(1).find { it is SqlWithCommonTableGroupBlock }
59+
return parent.getChildBlocksDropLast().find { it is SqlWithCommonTableGroupBlock }
6060
}
6161
}
6262
return null
@@ -75,7 +75,7 @@ class SqlWithCommonTableGroupBlock(
7575

7676
override fun createGroupIndentLen(): Int {
7777
parentBlock?.let { parent ->
78-
childBlocks.dropLast(1).sumOf { it.getNodeText().length.plus(1) }
78+
getChildBlocksDropLast().sumOf { it.getNodeText().length.plus(1) }
7979
}
8080
return offset
8181
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.domaframework.doma.intellij.formatter.block.group.subgroup
1717

1818
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
1921
import org.domaframework.doma.intellij.formatter.util.IndentType
2022
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2123

@@ -41,8 +43,9 @@ class SqlParallelListBlock(
4143

4244
override fun createBlockIndentLen(): Int {
4345
parentBlock?.let { parent ->
44-
return parent.childBlocks
45-
.dropLast(1)
46+
return parent
47+
.getChildBlocksDropLast()
48+
.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
4649
.sumOf { it.getNodeText().length.plus(1) }
4750
.plus(parent.indent.indentLen)
4851
.plus(parent.getNodeText().length)

src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class SqlCustomSpacingBuilder {
132132
}
133133

134134
paramBlock is SqlParallelListBlock -> {
135-
val lastKeywordGroup = paramBlock.childBlocks.dropLast(1).lastOrNull()
135+
val lastKeywordGroup = paramBlock.getChildBlocksDropLast().lastOrNull()
136136
return if (lastKeywordGroup is SqlKeywordGroupBlock) {
137137
normalSpacing
138138
} else {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ class SqlFormatterTest : BasePlatformTestCase() {
9090
formatSqlFile("UpdateTupleAssignment.sql", "UpdateTupleAssignment$formatDataPrefix.sql")
9191
}
9292

93-
fun testUpdateTupleAssignmentSubQueryFormatter() {
94-
formatSqlFile("UpdateTupleAssignmentSubQuery.sql", "UpdateTupleAssignmentSubQuery$formatDataPrefix.sql")
95-
}
96-
9793
fun testUpdateTupleSubUseQueryRowFormatter() {
9894
formatSqlFile("UpdateTupleSubUseQueryRow.sql", "UpdateTupleSubUseQueryRow$formatDataPrefix.sql")
9995
}

0 commit comments

Comments
 (0)