Skip to content

Commit 8fe2ce5

Browse files
authored
Merge pull request #314 from domaframework/feature/sql-format-support-case-end
Fix Formatting Rules for CASE ... END Clause
2 parents ee1e798 + 683b2b3 commit 8fe2ce5

File tree

13 files changed

+118
-99
lines changed

13 files changed

+118
-99
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: 17 additions & 12 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
@@ -48,6 +49,20 @@ open class SqlCommaBlock(
4849
context.enableFormat,
4950
context.formatMode,
5051
) {
52+
companion object {
53+
private val EXPECTED_TYPES =
54+
listOf(
55+
SqlInsertColumnGroupBlock::class,
56+
SqlInsertValueGroupBlock::class,
57+
SqlUpdateSetGroupBlock::class,
58+
SqlUpdateColumnGroupBlock::class,
59+
SqlUpdateValueGroupBlock::class,
60+
SqlFunctionParamBlock::class,
61+
SqlWithColumnGroupBlock::class,
62+
SqlKeywordGroupBlock::class,
63+
)
64+
}
65+
5166
override val indent =
5267
ElementIndent(
5368
IndentType.COMMA,
@@ -125,17 +140,7 @@ open class SqlCommaBlock(
125140
}
126141

127142
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
128-
val expectedTypes =
129-
listOf(
130-
SqlInsertColumnGroupBlock::class,
131-
SqlInsertValueGroupBlock::class,
132-
SqlUpdateSetGroupBlock::class,
133-
SqlUpdateColumnGroupBlock::class,
134-
SqlUpdateValueGroupBlock::class,
135-
SqlFunctionParamBlock::class,
136-
SqlWithColumnGroupBlock::class,
137-
SqlKeywordGroupBlock::class,
138-
)
139-
return TypeUtil.isExpectedClassType(expectedTypes, parentBlock)
143+
if (parentBlock is SqlConditionalExpressionGroupBlock) return false
144+
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parentBlock)
140145
}
141146
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ open class SqlDataTypeBlock(
4040
0,
4141
)
4242

43-
override fun setParentGroupBlock(lastGroupBlock: SqlBlock?) {
44-
super.setParentGroupBlock(lastGroupBlock)
43+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
44+
super.setParentGroupBlock(lastGroup)
4545
indent.indentLevel = IndentType.NONE
4646
indent.indentLen = 1
4747
indent.groupIndentLen = indent.indentLen
4848

49-
if (lastGroupBlock is SqlCreateTableColumnDefinitionRawGroupBlock) {
50-
lastGroupBlock.columnDataTypeBlock = this
49+
if (lastGroup is SqlCreateTableColumnDefinitionRawGroupBlock) {
50+
lastGroup.columnDataTypeBlock = this
5151
}
5252
}
5353

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,48 @@ open class SqlRightPatternBlock(
5252
) {
5353
var preSpaceRight = false
5454

55+
companion object {
56+
private val NOT_INSERT_SPACE_TYPES =
57+
listOf(
58+
SqlFunctionParamBlock::class,
59+
SqlInsertColumnGroupBlock::class,
60+
SqlWithQuerySubGroupBlock::class,
61+
SqlConflictExpressionSubGroupBlock::class,
62+
)
63+
64+
private val INDENT_EXPECTED_TYPES =
65+
listOf(
66+
SqlUpdateColumnGroupBlock::class,
67+
SqlUpdateValueGroupBlock::class,
68+
SqlCreateTableColumnDefinitionGroupBlock::class,
69+
)
70+
71+
private val NEW_LINE_EXPECTED_TYPES =
72+
listOf(
73+
SqlUpdateColumnGroupBlock::class,
74+
SqlUpdateValueGroupBlock::class,
75+
SqlCreateTableColumnDefinitionGroupBlock::class,
76+
SqlColumnDefinitionRawGroupBlock::class,
77+
SqlUpdateSetGroupBlock::class,
78+
SqlWithQuerySubGroupBlock::class,
79+
)
80+
81+
private val NEW_LINE_EXCLUDE_TYPES =
82+
listOf(
83+
SqlDataTypeParamBlock::class,
84+
SqlConditionalExpressionGroupBlock::class,
85+
SqlConflictExpressionSubGroupBlock::class,
86+
SqlFunctionParamBlock::class,
87+
)
88+
}
89+
5590
/**
5691
* Configures whether to add a space to the right side when the group ends.
5792
*/
5893
private fun enableLastRight() {
5994
parentBlock?.let { parent ->
6095
// Check if parent is in the notInsertSpaceClassList
61-
val notInsertSpaceClassList =
62-
listOf(
63-
SqlFunctionParamBlock::class,
64-
SqlInsertColumnGroupBlock::class,
65-
SqlWithQuerySubGroupBlock::class,
66-
SqlConflictExpressionSubGroupBlock::class,
67-
)
68-
if (isExpectedClassType(notInsertSpaceClassList, parent)) {
96+
if (isExpectedClassType(NOT_INSERT_SPACE_TYPES, parent)) {
6997
preSpaceRight = false
7098
return
7199
}
@@ -118,13 +146,7 @@ open class SqlRightPatternBlock(
118146

119147
parentBlock?.let { parent ->
120148
if (parent is SqlWithQuerySubGroupBlock) return 0
121-
val exceptionalTypes =
122-
listOf(
123-
SqlUpdateColumnGroupBlock::class,
124-
SqlUpdateValueGroupBlock::class,
125-
SqlCreateTableColumnDefinitionGroupBlock::class,
126-
)
127-
if (isExpectedClassType(exceptionalTypes, parent)) return parent.indent.indentLen
149+
if (isExpectedClassType(INDENT_EXPECTED_TYPES, parent)) return parent.indent.indentLen
128150
return parent.indent.indentLen
129151
} ?: return 0
130152
}
@@ -135,29 +157,11 @@ open class SqlRightPatternBlock(
135157
if (preSpaceRight) return false
136158

137159
parentBlock?.let { parent ->
138-
val exceptionalTypes =
139-
listOf(
140-
SqlCreateTableColumnDefinitionGroupBlock::class,
141-
SqlColumnDefinitionRawGroupBlock::class,
142-
SqlUpdateSetGroupBlock::class,
143-
SqlUpdateColumnGroupBlock::class,
144-
SqlUpdateValueGroupBlock::class,
145-
SqlWithQuerySubGroupBlock::class,
146-
)
147-
148-
val excludeTypes =
149-
listOf(
150-
SqlDataTypeParamBlock::class,
151-
SqlConditionalExpressionGroupBlock::class,
152-
SqlConflictExpressionSubGroupBlock::class,
153-
SqlFunctionParamBlock::class,
154-
)
155-
156160
if ((
157-
isExpectedClassType(exceptionalTypes, parent) ||
158-
isExpectedClassType(exceptionalTypes, parent.parentBlock)
161+
isExpectedClassType(NEW_LINE_EXPECTED_TYPES, parent) ||
162+
isExpectedClassType(NEW_LINE_EXPECTED_TYPES, parent.parentBlock)
159163
) &&
160-
!isExpectedClassType(excludeTypes, parent)
164+
!isExpectedClassType(NEW_LINE_EXCLUDE_TYPES, parent)
161165
) {
162166
return true
163167
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlRawGroupBlock.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.group.column
1717

18-
import com.intellij.formatting.Block
19-
import com.intellij.formatting.Spacing
2018
import com.intellij.lang.ASTNode
2119
import com.intellij.psi.formatter.common.AbstractBlock
2220
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -44,11 +42,6 @@ abstract class SqlRawGroupBlock(
4442
0,
4543
)
4644

47-
override fun getSpacing(
48-
p0: Block?,
49-
p1: Block,
50-
): Spacing? = null
51-
5245
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
5346

5447
override fun isLeaf(): Boolean = true
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 diffTextLength = parent.getNodeText().length.minus(getNodeText().length)
59+
parent.indent.indentLen.plus(diffTextLength)
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCo
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.SqlJoinGroupBlock
4240
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
4341
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
@@ -47,6 +45,8 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
4745
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
4846
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
4947
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
48+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
49+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
5050
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
5151
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
5252
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
@@ -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,

0 commit comments

Comments
 (0)