Skip to content

Commit 0979977

Browse files
committed
Refactor inline group block classes and update package structure for better organization
1 parent ee1e798 commit 0979977

File tree

10 files changed

+62
-38
lines changed

10 files changed

+62
-38
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
3636
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
3737
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
3838
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
39-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
40-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
4139
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
40+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
41+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
4242
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock
4343
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
4444
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
@@ -257,8 +257,6 @@ open class SqlBlock(
257257

258258
is SqlInlineSecondGroupBlock -> {
259259
parentSetProcessor.updateInlineSecondGroupBlockParentAndAddGroup(
260-
lastGroupBlock,
261-
lastIndentLevel,
262260
childBlock,
263261
)
264262
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil
2121
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
@@ -125,6 +126,12 @@ open class SqlCommaBlock(
125126
}
126127

127128
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
129+
val excludeTypes =
130+
listOf(
131+
SqlConditionalExpressionGroupBlock::class,
132+
)
133+
if (TypeUtil.isExpectedClassType(excludeTypes, parentBlock)) return false
134+
128135
val expectedTypes =
129136
listOf(
130137
SqlInsertColumnGroupBlock::class,
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.inline
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
@@ -29,6 +29,8 @@ open class SqlInlineGroupBlock(
2929
node,
3030
context,
3131
) {
32+
val inlineConditions: MutableList<SqlInlineSecondGroupBlock> = mutableListOf()
33+
3234
override val indent =
3335
ElementIndent(
3436
IndentType.INLINE,
@@ -40,15 +42,12 @@ open class SqlInlineGroupBlock(
4042
super.setParentGroupBlock(lastGroup)
4143
indent.indentLevel = IndentType.INLINE
4244
indent.indentLen = createBlockIndentLen()
43-
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
45+
indent.groupIndentLen = createGroupIndentLen()
4446
}
4547

4648
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
4749

48-
override fun createBlockIndentLen(): Int =
49-
parentBlock?.let {
50-
it.indent.groupIndentLen
51-
.plus(it.getNodeText().length)
52-
.plus(1)
53-
} ?: 1
50+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
51+
52+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
5453
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.inline
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
@@ -29,7 +29,7 @@ open class SqlInlineSecondGroupBlock(
2929
node,
3030
context,
3131
) {
32-
val isEndCase = getNodeText().lowercase() == "end"
32+
val isEndCase = getNodeText() == "end"
3333

3434
override val indent =
3535
ElementIndent(
@@ -42,19 +42,28 @@ open class SqlInlineSecondGroupBlock(
4242
super.setParentGroupBlock(lastGroup)
4343
indent.indentLevel = IndentType.INLINE_SECOND
4444
indent.indentLen = createBlockIndentLen()
45-
indent.groupIndentLen = indent.indentLen
45+
indent.groupIndentLen = createGroupIndentLen()
46+
}
47+
48+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
49+
(lastGroup as? SqlInlineGroupBlock)?.inlineConditions?.add(this)
4650
}
4751

4852
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
4953

5054
override fun createBlockIndentLen(): Int =
51-
parentBlock?.let {
55+
parentBlock?.let { parent ->
5256
// TODO:Customize indentation within an inline group
5357
if (isEndCase) {
54-
it.indent.indentLen
58+
val diff = parent.getNodeText().length.minus(getNodeText().length)
59+
parent.indent.indentLen.plus(diff)
5560
} else {
56-
it.indent.groupIndentLen
57-
.plus(it.getNodeText().length)
61+
parent.indent.groupIndentLen.plus(1)
5862
}
5963
} ?: 1
64+
65+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
66+
67+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
68+
(parentBlock as? SqlInlineGroupBlock)?.inlineConditions?.dropLast(1)?.isEmpty() != true
6069
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
2626
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
27-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
28-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
2927
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
3028
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
3129
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
31+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
3232
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
3333
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
3434
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
@@ -251,11 +251,7 @@ class SqlSetParentGroupProcessor(
251251
}
252252
}
253253

254-
fun updateInlineSecondGroupBlockParentAndAddGroup(
255-
lastGroupBlock: SqlBlock,
256-
lastIndentLevel: IndentType,
257-
childBlock: SqlInlineSecondGroupBlock,
258-
) {
254+
fun updateInlineSecondGroupBlockParentAndAddGroup(childBlock: SqlInlineSecondGroupBlock) {
259255
val context =
260256
SetParentContext(
261257
childBlock,
@@ -276,13 +272,12 @@ class SqlSetParentGroupProcessor(
276272
}
277273
return
278274
}
279-
if (lastIndentLevel == IndentType.INLINE_SECOND) {
280-
blockBuilder.removeLastGroupTopNodeIndexHistory()
281-
updateGroupBlockLastGroupParentAddGroup(
282-
lastGroupBlock,
283-
childBlock,
284-
)
285-
return
275+
val inlineSecondIndex =
276+
blockBuilder.getGroupTopNodeIndex { block ->
277+
block.indent.indentLevel == IndentType.INLINE_SECOND
278+
}
279+
if (inlineSecondIndex >= 0) {
280+
blockBuilder.clearSubListGroupTopNodeIndexHistory(inlineSecondIndex)
286281
}
287282
updateGroupBlockParentAndAddGroup(
288283
childBlock,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,11 @@ class SqlBlockUtil(
244244
)
245245
} else {
246246
WithClauseUtil
247-
.getWithClauseKeywordGroup(lastGroupBlock, child, sqlBlockFormattingCtx)
248-
?.let { return it }
247+
.getWithClauseKeywordGroup(
248+
lastGroupBlock,
249+
child,
250+
sqlBlockFormattingCtx,
251+
)?.let { return it }
249252
return SqlSecondKeywordBlock(
250253
child,
251254
sqlBlockFormattingCtx,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ enum class IndentType(
2424
TOP(1, true),
2525
SECOND(2, true),
2626
JOIN(3, true),
27+
INLINE(2),
28+
INLINE_SECOND(3, true),
2729
SECOND_OPTION(4, true),
2830
TIRD(5),
2931
ATTACHED(6),
30-
INLINE_SECOND(8, true),
31-
COLUMN(9, true),
32+
COLUMN(7, true),
3233
SUB(90),
3334
ATTRIBUTE(91),
3435
LITERAL(92),
3536
COMMA(93),
3637
OPTIONS(94),
3738
PARAM(95),
38-
INLINE(96),
3939
NONE(99),
4040
;
4141

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class SqlFormatterTest : BasePlatformTestCase() {
5454
formatSqlFile("Select.sql", "Select$formatDataPrefix.sql")
5555
}
5656

57+
fun testSelectCaseEndFormatter() {
58+
formatSqlFile("SelectCaseEnd.sql", "SelectCaseEnd$formatDataPrefix.sql")
59+
}
60+
5761
fun testSelectFromLateralFormatter() {
5862
formatSqlFile("SelectFromLateral.sql", "SelectFromLateral$formatDataPrefix.sql")
5963
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Select case when div = 'A' then 'AAA'
2+
when div = 'B' then 'BBB'
3+
else 'CCC' end as divName
4+
from users
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT CASE WHEN div = 'A' THEN 'AAA'
2+
WHEN div = 'B' THEN 'BBB'
3+
ELSE 'CCC'
4+
END AS divName
5+
FROM users

0 commit comments

Comments
 (0)