Skip to content

Commit e4ca6d6

Browse files
authored
Merge pull request #328 from domaframework/feature/sql-format-sub-group-spacing
Adjust Value Group Spacing for INSERT and UPDATE Statements
2 parents 28c32fd + 4f668da commit e4ca6d6

File tree

11 files changed

+56
-46
lines changed

11 files changed

+56
-46
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRaw
4040
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
4141
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
4242
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
43+
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
4344
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock
4445
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
46+
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
4547
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
4648
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
4749
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQueryGroupBlock
@@ -483,8 +485,15 @@ class SqlFileBlock(
483485
return SqlCustomSpacingBuilder.normalSpacing
484486
}
485487

486-
if (childBlock1 is SqlSubGroupBlock && childBlock2 is SqlSubGroupBlock) {
487-
return SqlCustomSpacingBuilder.nonSpacing
488+
if (childBlock1 is SqlSubGroupBlock) {
489+
if (childBlock2 is SqlSubGroupBlock) {
490+
return SqlCustomSpacingBuilder.nonSpacing
491+
}
492+
if (childBlock1 is SqlInsertValueGroupBlock ||
493+
childBlock1 is SqlUpdateValueGroupBlock
494+
) {
495+
return SqlCustomSpacingBuilder.normalSpacing
496+
}
488497
}
489498

490499
// Do not leave a space after the comment block of the bind variable
@@ -546,8 +555,6 @@ class SqlFileBlock(
546555
}
547556

548557
is SqlDataTypeParamBlock, is SqlFunctionParamBlock -> return SqlCustomSpacingBuilder.nonSpacing
549-
550-
// else -> return SqlCustomSpacingBuilder.normalSpacing
551558
}
552559
}
553560

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordG
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
2626
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
2728
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
2930
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
@@ -66,6 +67,7 @@ open class SqlRightPatternBlock(
6667
SqlUpdateColumnGroupBlock::class,
6768
SqlUpdateValueGroupBlock::class,
6869
SqlCreateTableColumnDefinitionGroupBlock::class,
70+
SqlInsertValueGroupBlock::class,
6971
)
7072

7173
val NOT_INDENT_EXPECTED_TYPES =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SqlInsertValueGroupBlock(
2929
) {
3030
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
3131
super.setParentGroupBlock(lastGroup)
32-
indent.groupIndentLen = createBlockIndentLen()
32+
indent.groupIndentLen = createBlockIndentLen().plus(1)
3333
}
3434

3535
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class SqlUpdateValueGroupBlock(
6262
parentBlock?.let { parent ->
6363
if (parent is SqlUpdateSetGroupBlock) {
6464
val parentGroupIndent = parent.indent.groupIndentLen
65-
return parentGroupIndent.plus(3)
65+
// Add four spaces after the SET keyword: " = " and "("
66+
return parentGroupIndent.plus(4)
6667
}
6768
} ?: return offset
6869
return offset

src/test/testData/sql/formatter/InsertConflictNothing_format.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ INSERT INTO employee
22
(id
33
, username)
44
VALUES ( /* employees.id */0
5-
, /* employees.name */'name')
5+
, /* employees.name */'name' )
66
ON CONFLICT (username) ON CONSTRAINT
77
DO NOTHING

src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable_format.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
INSERT INTO users
22
(username
33
, email)
4-
VALUES ('user'
5-
4+
VALUES ( 'user'
5+
66
ON CONFLICT ON CONSTRAINT
77
DO UPDATE
88
SET email = EXCLUDED.email

src/test/testData/sql/formatter/InsertReturning_format.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ INSERT INTO /*# tableName */
77
, x3
88
, x4)
99
VALUES ( /* reportId */1
10-
, /* reportId */1
11-
/*%for entity : entities */
12-
, /* entity.value */'abc'
13-
/*%end*/
14-
, /* @userId() */1
15-
, x5
16-
, /* @userId() */1
17-
, x6
18-
, 1
19-
, /* @maxDateTime() */'9999-12-31')
10+
, /* reportId */1
11+
/*%for entity : entities */
12+
, /* entity.value */'abc'
13+
/*%end*/
14+
, /* @userId() */1
15+
, x5
16+
, /* @userId() */1
17+
, x6
18+
, 1
19+
, /* @maxDateTime() */'9999-12-31' )
2020
RETURNING x1
2121
, x2

src/test/testData/sql/formatter/InsertWithBindVariable_format.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ INSERT INTO /*# tableName */
77
, x3
88
, x4)
99
VALUES ( /* reportId */1
10-
, /* reportId */1
11-
/*%for entity : entities */
12-
, /* entity.value */'abc'
13-
/*%end*/
14-
, /* @userId() */1
15-
, x5
16-
, /* @userId() */1
17-
, x6
18-
, 1
19-
, /* @maxDateTime() */'9999-12-31')
10+
, /* reportId */1
11+
/*%for entity : entities */
12+
, /* entity.value */'abc'
13+
/*%end*/
14+
, /* @userId() */1
15+
, x5
16+
, /* @userId() */1
17+
, x6
18+
, 1
19+
, /* @maxDateTime() */'9999-12-31' )

src/test/testData/sql/formatter/Insert_format.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ INSERT INTO /*# tableName */
77
, x3
88
, x4)
99
VALUES ( /* reportId */1
10-
, /* reportId */1
11-
/*%for entity : entities */
12-
, /* entity.value */'abc'
13-
/*%end*/
14-
, /* @userId() */1
15-
, x5
16-
, /* @userId() */1
17-
, x6
18-
, 1
19-
, /* @maxDateTime() */'9999-12-31')
10+
, /* reportId */1
11+
/*%for entity : entities */
12+
, /* entity.value */'abc'
13+
/*%end*/
14+
, /* @userId() */1
15+
, x5
16+
, /* @userId() */1
17+
, x6
18+
, 1
19+
, /* @maxDateTime() */'9999-12-31' )

src/test/testData/sql/formatter/UpdateTupleAssignment_format.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ UPDATE /*# tableName */
77
/*%end*/
88
)
99
= ( /* @userId() */1
10-
, x
11-
, x + 1
12-
/*%for entity : entities */
13-
, /* entity.value */'abc'
14-
/*%end*/
10+
, x
11+
, x + 1
12+
/*%for entity : entities */
13+
, /* entity.value */'abc'
14+
/*%end*/
1515
)
1616
WHERE x = /* reportId */1

0 commit comments

Comments
 (0)