Skip to content

Commit 573e1d4

Browse files
committed
Fixed indentation of Retuning when there are multiple column update.
1 parent 9eb7f91 commit 573e1d4

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open class SqlSecondKeywordBlock(
4949

5050
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
5151
lastGroup?.let { last ->
52-
val prevKeyword = last.childBlocks.dropLast(1).findLast { it is SqlKeywordBlock }
52+
val prevKeyword = last.childBlocks.findLast { it is SqlKeywordBlock }
5353
prevKeyword?.let { prev ->
5454
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), prev.getNodeText())
5555
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package org.domaframework.doma.intellij.formatter.block.group.subgroup
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinQueriesGroupBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
2525
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,24 @@ class SqlCustomSpacingBuilder {
123123
}
124124

125125
fun getSpacingRightPattern(block: SqlRightPatternBlock): Spacing? {
126+
val paramBlock = block.parentBlock
126127
return when {
127-
block.parentBlock is SqlCreateTableColumnDefinitionGroupBlock ||
128-
block.parentBlock is SqlUpdateColumnGroupBlock ||
129-
block.parentBlock is SqlUpdateValueGroupBlock -> {
128+
paramBlock is SqlCreateTableColumnDefinitionGroupBlock ||
129+
paramBlock is SqlUpdateColumnGroupBlock ||
130+
(paramBlock is SqlUpdateValueGroupBlock && !paramBlock.subQueryValue) -> {
130131
return getSpacing(block)
131132
}
132133

133-
block.parentBlock is SqlParallelListBlock -> {
134-
if (block.parentBlock
135-
?.childBlocks
136-
?.dropLast(1)
137-
?.lastOrNull() is SqlKeywordGroupBlock
138-
) {
139-
return normalSpacing
134+
paramBlock is SqlParallelListBlock -> {
135+
val lastKeywordGroup = paramBlock.childBlocks.dropLast(1).lastOrNull()
136+
return if (lastKeywordGroup is SqlKeywordGroupBlock) {
137+
normalSpacing
138+
} else {
139+
nonSpacing
140140
}
141-
return nonSpacing
142141
}
143142

144-
block.parentBlock is SqlDataTypeParamBlock -> nonSpacing
143+
paramBlock is SqlDataTypeParamBlock -> nonSpacing
145144

146145
block.preSpaceRight -> normalSpacing
147146
else -> nonSpacing

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSe
2929
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
3131
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
32+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
3334
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
3435
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
@@ -174,7 +175,7 @@ class SqlSetParentGroupProcessor(
174175
// Since `DO UPDATE` does not include a `RETURNING` clause, it should be registered as a child of the parent `INSERT` query.
175176
// The `DO` keyword should align with the `INSERT` query, and therefore it will serve as the **indentation anchor** for the following update block.
176177
setParentGroups(context) { history ->
177-
val lastGroup = history.lastOrNull()
178+
val lastGroup = history.findLast { it is SqlTopQueryGroupBlock }
178179
return@setParentGroups if (lastGroup is SqlUpdateQueryGroupBlock && lastGroup.parentBlock is SqlDoGroupBlock) {
179180
lastGroup.parentBlock
180181
} else {

src/test/testData/sql/formatter/InsertConflictUpdate.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SELECT name, user_id
33
FROM user_settings
44
WHERE user_id = /*employee.id*/0 AND name = /*employee.name*/'name'
55
on conflict (id) do
6-
update set name = EXCLUDED.name
6+
update set name = EXCLUDED.name,
7+
email = default
78
WHERE employees.name is Distinct from EXCLUDED.name
89
returning id, manager_id, name

src/test/testData/sql/formatter/InsertConflictUpdate_format.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SELECT name
99
ON CONFLICT (id)
1010
DO UPDATE
1111
SET name = EXCLUDED.name
12+
, email = DEFAULT
1213
WHERE employees.name IS DISTINCT
1314
FROM EXCLUDED.name
1415
RETURNING id

0 commit comments

Comments
 (0)