Skip to content

Commit 9eb7f91

Browse files
committed
Implement line break formatting rule for Returning.
1 parent f1d8023 commit 9eb7f91

File tree

14 files changed

+68
-31
lines changed

14 files changed

+68
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
2121
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2627
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
27-
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
2828
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2929
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword.insert
1717

1818
import com.intellij.lang.ASTNode
19-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlValuesGroupBlock
19+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2020
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
2121
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2222

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
2+
3+
import com.intellij.lang.ASTNode
4+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
5+
6+
class SqlReturningGroupBlock(
7+
node: ASTNode,
8+
context: SqlBlockFormattingContext,
9+
) : SqlSecondKeywordBlock(node, context) {
10+
override fun createBlockIndentLen(): Int {
11+
parentBlock?.let { parent ->
12+
return parent.indent.indentLen
13+
}
14+
return 0
15+
}
16+
}
Lines changed: 1 addition & 1 deletion
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.second
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
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
1+
package org.domaframework.doma.intellij.formatter.block.group.keyword.top
172

183
import com.intellij.lang.ASTNode
194
import org.domaframework.doma.intellij.formatter.block.SqlBlock
5+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
206
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
217
import org.domaframework.doma.intellij.formatter.util.IndentType
228
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2525
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2626
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2727
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
28-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinQueriesGroupBlock
2928
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
29+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
3131
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
3232
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQueryGroupBlock

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ package org.domaframework.doma.intellij.formatter.processor
1818
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock
21+
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
2122
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
23+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2224
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
2325
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
2426
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2527
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
2628
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
2729
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2830
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
31+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
2932
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
3033
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
3134
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
@@ -167,6 +170,19 @@ class SqlSetParentGroupProcessor(
167170
}
168171

169172
blockBuilder.removeLastGroupTopNodeIndexHistory()
173+
if (childBlock is SqlReturningGroupBlock) {
174+
// Since `DO UPDATE` does not include a `RETURNING` clause, it should be registered as a child of the parent `INSERT` query.
175+
// The `DO` keyword should align with the `INSERT` query, and therefore it will serve as the **indentation anchor** for the following update block.
176+
setParentGroups(context) { history ->
177+
val lastGroup = history.lastOrNull()
178+
return@setParentGroups if (lastGroup is SqlUpdateQueryGroupBlock && lastGroup.parentBlock is SqlDoGroupBlock) {
179+
lastGroup.parentBlock
180+
} else {
181+
history.lastOrNull()
182+
}
183+
}
184+
return
185+
}
170186
updateGroupBlockLastGroupParentAddGroup(
171187
lastGroupBlock,
172188
childBlock,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ package org.domaframework.doma.intellij.formatter.util
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlValuesGroupBlock
2120
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
2322
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2525

2626
object InsertClauseUtil {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.psi.util.elementType
2121
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
2627
import org.domaframework.doma.intellij.psi.SqlTypes
@@ -51,4 +52,15 @@ object NotQueryGroupUtil {
5152

5253
return null
5354
}
55+
56+
fun getKeywordGroup(
57+
child: ASTNode,
58+
sqlBlockFormattingCtx: SqlBlockFormattingContext,
59+
): SqlBlock? {
60+
val keyword = child.text.lowercase()
61+
if (keyword == "returning") {
62+
return SqlReturningGroupBlock(child, sqlBlockFormattingCtx)
63+
}
64+
return null
65+
}
5466
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRaw
4242
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
4343
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
4444
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock
45-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinQueriesGroupBlock
4645
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
47-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlSecondKeywordBlock
4846
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlSecondOptionKeywordGroupBlock
49-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlValuesGroupBlock
5047
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock
5148
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
5249
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
5350
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
5451
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
5552
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
53+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
54+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
5655
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlDeleteQueryGroupBlock
56+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
5757
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
5858
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
5959
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
@@ -260,6 +260,13 @@ class SqlBlockUtil(
260260
WithClauseUtil
261261
.getWithClauseKeywordGroup(lastGroupBlock, child, sqlBlockFormattingCtx)
262262
?.let { return it }
263+
264+
NotQueryGroupUtil
265+
.getKeywordGroup(
266+
child,
267+
sqlBlockFormattingCtx,
268+
)?.let { return it }
269+
263270
SqlSecondKeywordBlock(
264271
child,
265272
sqlBlockFormattingCtx,

0 commit comments

Comments
 (0)