Skip to content

Commit e5f4110

Browse files
committed
Support formatting for ALTER TABLE and DROP COLUMN statements
1 parent 4da237f commit e5f4110

File tree

14 files changed

+261
-40
lines changed

14 files changed

+261
-40
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ open class SqlFileBlock(
462462
val childBlock1: SqlBlock? = child1 as? SqlBlock
463463
val childBlock2: SqlBlock = child2 as SqlBlock
464464

465-
if(childBlock1 == null) {
465+
if (childBlock1 == null) {
466466
return SqlCustomSpacingBuilder.nonSpacing
467467
}
468468

@@ -548,7 +548,7 @@ open class SqlFileBlock(
548548
}
549549
}
550550

551-
if (childBlock1?.node?.elementType == SqlTypes.DOT ||
551+
if (childBlock1.node.elementType == SqlTypes.DOT ||
552552
childBlock2.node.elementType == SqlTypes.DOT
553553
) {
554554
return SqlCustomSpacingBuilder.nonSpacing
@@ -592,7 +592,7 @@ open class SqlFileBlock(
592592
}
593593

594594
is SqlFunctionParamBlock -> {
595-
return if (childBlock1?.node?.elementType in listOf(SqlTypes.FUNCTION_NAME, SqlTypes.WORD)) {
595+
return if (childBlock1.node.elementType in listOf(SqlTypes.FUNCTION_NAME, SqlTypes.WORD)) {
596596
SqlCustomSpacingBuilder.nonSpacing
597597
} else {
598598
SqlCustomSpacingBuilder.normalSpacing
@@ -621,7 +621,7 @@ open class SqlFileBlock(
621621
?.let { return it }
622622
}
623623

624-
if (childBlock1 is SqlBlock && (childBlock2 is SqlCommaBlock || childBlock2 is SqlColumnRawGroupBlock)) {
624+
if (childBlock2 is SqlCommaBlock || childBlock2 is SqlColumnRawGroupBlock) {
625625
SqlCustomSpacingBuilder()
626626
.getSpacingWithIndentComma(childBlock1, childBlock2)
627627
?.let { return it }

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlI
2727
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
2828
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
2929
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
@@ -144,6 +145,16 @@ open class SqlCommaBlock(
144145
val parentIndent = firstChild?.indent ?: parent.indent
145146
parentIndent.groupIndentLen.plus(1)
146147
}
148+
is SqlTableModifySecondGroupBlock -> {
149+
val grand = parent.parentBlock
150+
if (grand is SqlElConditionLoopCommentBlock ||
151+
parent.childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock
152+
) {
153+
parent.indent.indentLen.plus(2)
154+
} else {
155+
parent.indent.indentLen
156+
}
157+
}
147158
else -> {
148159
// No indent after ORDER BY within function parameters
149160
val grand = parent.parentBlock
@@ -181,7 +192,8 @@ open class SqlCommaBlock(
181192
if (conditionParent is SqlFunctionParamBlock) {
182193
return false
183194
}
184-
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parent)
195+
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parent) ||
196+
childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock
185197
}
186198
return false
187199
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.domaframework.doma.intellij.formatter.block.conflict
1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
2123
import org.domaframework.doma.intellij.formatter.util.IndentType
2224
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2325

@@ -36,7 +38,7 @@ class SqlDoGroupBlock(
3638

3739
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
3840
super.setParentGroupBlock(lastGroup)
39-
indent.indentLen = 0
41+
indent.indentLen = createBlockIndentLen()
4042
indent.groupIndentLen = getNodeText().length
4143
}
4244

@@ -46,5 +48,13 @@ class SqlDoGroupBlock(
4648
}
4749
}
4850

51+
override fun createBlockIndentLen(): Int {
52+
val prevBlock = prevBlocks.lastOrNull()
53+
if (prevBlock is SqlTableModifySecondGroupBlock) {
54+
return prevBlock.indent.indentLen
55+
}
56+
return 0
57+
}
58+
4959
override fun isSaveSpace(lastGroup: SqlBlock?) = true
5060
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package org.domaframework.doma.intellij.formatter.block.group.column
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
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
2022
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
2123
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
2224
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQueryGroupBlock
@@ -56,15 +58,31 @@ class SqlColumnRawGroupBlock(
5658
}
5759

5860
override fun createBlockIndentLen(): Int =
59-
if (parentBlock is SqlWithQueryGroupBlock) {
60-
parentBlock
61-
?.childBlocks
62-
?.dropLast(1)
63-
?.lastOrNull()
64-
?.indent
65-
?.indentLen ?: offset
66-
} else {
67-
parentBlock?.indent?.groupIndentLen?.plus(1) ?: offset
61+
when (parentBlock) {
62+
is SqlWithQueryGroupBlock -> {
63+
parentBlock
64+
?.childBlocks
65+
?.dropLast(1)
66+
?.lastOrNull()
67+
?.indent
68+
?.indentLen ?: offset
69+
}
70+
71+
is SqlTableModifySecondGroupBlock -> {
72+
parentBlock?.let { parent ->
73+
val grand = parent.parentBlock
74+
if (grand is SqlElConditionLoopCommentBlock ||
75+
parent.childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock
76+
) {
77+
parent.indent.indentLen.plus(2)
78+
} else {
79+
parent.indent.indentLen
80+
}
81+
} ?: offset
82+
}
83+
84+
else ->
85+
parentBlock?.indent?.groupIndentLen?.plus(1) ?: offset
6886
}
6987

7088
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = !isFirstColumnGroup

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ open class SqlKeywordGroupBlock(
176176
} ?: return 1
177177
}
178178

179-
override fun createGroupIndentLen(): Int = indent.indentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1))
179+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getTotalTopKeywordLength())
180+
181+
fun getTotalTopKeywordLength(): Int = topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1)
180182

181183
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
182184
val conditionLastGroup =

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ 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.SqlKeywordGroupBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
2325
import org.domaframework.doma.intellij.formatter.util.IndentType
2426
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2527

@@ -44,11 +46,13 @@ class SqlExistsGroupBlock(
4446

4547
override fun createGroupIndentLen(): Int {
4648
val parentGroupIndent = parentBlock?.indent?.groupIndentLen ?: 0
47-
return topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.plus(parentGroupIndent).minus(1)
49+
return getTotalTopKeywordLength().plus(parentGroupIndent)
4850
}
4951

5052
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
51-
if (lastGroup is SqlCreateTableColumnDefinitionRawGroupBlock) {
53+
if (lastGroup is SqlCreateTableColumnDefinitionRawGroupBlock ||
54+
lastGroup is SqlTableModifySecondGroupBlock
55+
) {
5256
return false
5357
}
5458
return super.isSaveSpace(lastGroup)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
22+
23+
class SqlTableModifySecondGroupBlock(
24+
node: ASTNode,
25+
context: SqlBlockFormattingContext,
26+
) : SqlSecondKeywordBlock(
27+
node,
28+
context,
29+
) {
30+
override fun createBlockIndentLen(): Int {
31+
val prevBlock = prevBlocks.lastOrNull()
32+
if (prevBlock is SqlTableModifySecondGroupBlock) {
33+
return prevBlock.indent.indentLen
34+
}
35+
36+
return super.createBlockIndentLen()
37+
}
38+
39+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock !is SqlColumnRawGroupBlock
40+
}
Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.domaframework.doma.intellij.formatter.block.group.keyword.top
217

318
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
22+
import org.domaframework.doma.intellij.formatter.util.IndentType
423
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
524

6-
class SqlTableModificationKeyword(node: ASTNode, context: SqlBlockFormattingContext)
7-
: SqlTopQueryGroupBlock(
8-
node,context
25+
class SqlTableModificationKeyword(
26+
node: ASTNode,
27+
context: SqlBlockFormattingContext,
28+
) : SqlTopQueryGroupBlock(
29+
node,
30+
context,
931
) {
10-
}
32+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
33+
super.setParentGroupBlock(lastGroup)
34+
indent.indentLen = createBlockIndentLen()
35+
indent.groupIndentLen = createGroupIndentLen()
36+
}
37+
38+
override fun createBlockIndentLen(): Int {
39+
return parentBlock?.let { parent ->
40+
val rootBlock =
41+
if (parent is SqlElConditionLoopCommentBlock) {
42+
parent.tempParentBlock
43+
} else if (parent.indent.indentLevel == IndentType.FILE) {
44+
null
45+
} else {
46+
parent
47+
}
48+
49+
return if (rootBlock is SqlKeywordGroupBlock) {
50+
rootBlock.getTotalTopKeywordLength().minus(getNodeText().length)
51+
} else {
52+
rootBlock?.indent?.groupIndentLen ?: 0
53+
}
54+
} ?: 0
55+
}
56+
57+
override fun createGroupIndentLen(): Int =
58+
parentBlock?.let { parent ->
59+
if (parent is SqlElConditionLoopCommentBlock) {
60+
parent.indent.indentLen
61+
} else {
62+
getTotalTopKeywordLength()
63+
}
64+
} ?: getTotalTopKeywordLength()
65+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.update
1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
2123
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
2224
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2325

@@ -57,5 +59,7 @@ class SqlUpdateQueryGroupBlock(
5759
}
5860
}
5961

60-
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock !is SqlDoGroupBlock
62+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
63+
parentBlock !is SqlDoGroupBlock &&
64+
parentBlock !is SqlTableModifySecondGroupBlock && parentBlock !is SqlTableModificationKeyword
6165
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlE
3232
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
3333
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock
3434
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
35+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
36+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
3537
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
3638
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
3739
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -135,7 +137,11 @@ class SqlBlockRelationBuilder(
135137
) {
136138
val context = SetParentContext(childBlock, blockBuilder)
137139
if (lastGroupBlock is SqlElConditionLoopCommentBlock) {
138-
handleConditionLoopParent(lastGroupBlock, context, childBlock)
140+
if (childBlock is SqlTableModificationKeyword) {
141+
handleConditionLoopParentForTableModification(lastGroupBlock, context, childBlock)
142+
} else {
143+
handleConditionLoopParent(lastGroupBlock, context, childBlock)
144+
}
139145
return
140146
}
141147
if (childBlock.indent.indentLevel == IndentType.TOP) {
@@ -169,6 +175,16 @@ class SqlBlockRelationBuilder(
169175
}
170176
}
171177

178+
private fun handleConditionLoopParentForTableModification(
179+
lastGroupBlock: SqlElConditionLoopCommentBlock,
180+
context: SetParentContext,
181+
childBlock: SqlKeywordGroupBlock,
182+
) {
183+
updateParentGroupLastConditionLoop(lastGroupBlock, context) {
184+
it.indent.indentLevel <= childBlock.indent.indentLevel
185+
}
186+
}
187+
172188
private fun handleTopLevelKeyword(
173189
lastGroupBlock: SqlBlock,
174190
childBlock: SqlKeywordGroupBlock,
@@ -234,6 +250,13 @@ class SqlBlockRelationBuilder(
234250
shouldHandleJoinKeyword(lastIndentLevel, childBlock) -> {
235251
updateGroupBlockParentAndAddGroup(childBlock)
236252
}
253+
childBlock is SqlTableModifySecondGroupBlock ->
254+
setParentGroups(context) { history ->
255+
history.lastOrNull {
256+
it is SqlColumnRawGroupBlock ||
257+
it.indent.indentLevel < childBlock.indent.indentLevel
258+
}
259+
}
237260
else -> {
238261
setParentGroups(context) { history ->
239262
getLastGroupKeywordText(history, childBlock) ?: history.lastOrNull()
@@ -317,7 +340,8 @@ class SqlBlockRelationBuilder(
317340
setParentGroups(context) { history ->
318341
val lastGroup = history.findLast { it is SqlTopQueryGroupBlock }
319342
when {
320-
lastGroup is SqlUpdateQueryGroupBlock && lastGroup.parentBlock is SqlDoGroupBlock ->
343+
lastGroup is SqlUpdateQueryGroupBlock &&
344+
lastGroup.parentBlock is SqlDoGroupBlock ->
321345
lastGroup.parentBlock
322346
else -> lastGroup
323347
}

0 commit comments

Comments
 (0)