Skip to content

Commit 7be9abb

Browse files
committed
Fix the line break and indentation for comma lines.
1 parent e779778 commit 7be9abb

File tree

10 files changed

+64
-59
lines changed

10 files changed

+64
-59
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ package org.domaframework.doma.intellij.formatter.block
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
20+
import org.domaframework.doma.intellij.common.util.TypeUtil
2021
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
22-
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
@@ -63,8 +64,14 @@ open class SqlCommaBlock(
6364
return 0
6465
}
6566

67+
val parentIndentSyncBlockTypes =
68+
listOf(
69+
SqlUpdateColumnGroupBlock::class,
70+
SqlUpdateValueGroupBlock::class,
71+
SqlInsertColumnGroupBlock::class,
72+
)
6673
val parentIndentLen = parent.indent.groupIndentLen
67-
if (parent is SqlUpdateColumnGroupBlock || parent is SqlUpdateValueGroupBlock) {
74+
if (TypeUtil.isExpectedClassType(parentIndentSyncBlockTypes, parent)) {
6875
return parentIndentLen
6976
}
7077

@@ -74,17 +81,15 @@ open class SqlCommaBlock(
7481
val grandIndentLen = grand.indent.groupIndentLen
7582
return grandIndentLen.plus(parentIndentLen).minus(1)
7683
}
77-
if (grand is SqlInsertQueryGroupBlock) {
78-
return parentIndentLen
79-
}
84+
8085
if (grand is SqlColumnRawGroupBlock) {
8186
val grandIndentLen = grand.indent.groupIndentLen
8287
var prevTextLen = 1
8388
parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.getNodeText().length) }
8489
return grandIndentLen.plus(prevTextLen).plus(1)
8590
}
8691
}
87-
return parentIndentLen
92+
return parentIndentLen.plus(1)
8893
} else {
8994
var prevLen = 0
9095
parent.childBlocks

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class SqlInsertValueGroupBlock(
2727
override fun createBlockIndentLen(): Int {
2828
parentBlock?.let { parent ->
2929
return parent.indent.groupIndentLen.plus(1)
30-
return 1
3130
} ?: return 1
3231
}
3332
}

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
22-
import org.domaframework.doma.intellij.formatter.util.IndentType
2322
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
24-
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
2523

2624
class SqlUpdateQueryGroupBlock(
2725
node: ASTNode,
@@ -43,30 +41,22 @@ class SqlUpdateQueryGroupBlock(
4341
createGroupIndentLen()
4442
}
4543

44+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
45+
(lastGroup as? SqlDoGroupBlock)?.doQueryBlock = this
46+
}
47+
4648
override fun getBaseIndentLen(
4749
preChildBlock: SqlBlock?,
48-
block: SqlBlock?,
50+
lastGroup: SqlBlock?,
4951
): Int {
50-
if (block == null) {
52+
if (lastGroup == null) {
5153
return createBlockIndentLen(preChildBlock)
5254
}
53-
if (preChildBlock == null) return createBlockIndentLen(preChildBlock)
5455

55-
if (preChildBlock.indent.indentLevel == this.indent.indentLevel &&
56-
!SqlKeywordUtil.Companion.isSetLineKeyword(getNodeText(), preChildBlock.getNodeText())
57-
) {
58-
if (indent.indentLevel == IndentType.SECOND) {
59-
val diffPreBlockTextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
60-
return preChildBlock.indent.indentLen.minus(diffPreBlockTextLen)
61-
} else {
62-
val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
63-
return preChildBlock.indent.indentLen.minus(diffPretextLen)
64-
}
56+
return if (lastGroup is SqlDoGroupBlock) {
57+
lastGroup.getNodeText().length.plus(1)
6558
} else {
66-
if (preChildBlock is SqlDoGroupBlock && getNodeText() == "update") {
67-
preChildBlock.doQueryBlock = this
68-
}
69-
return createBlockIndentLen(preChildBlock)
59+
createBlockIndentLen(preChildBlock)
7060
}
7161
}
7262
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ open class SqlUpdateSetGroupBlock(
3131
IndentType.SECOND,
3232
context,
3333
) {
34-
val updateColumnRaws: MutableList<SqlBlock> = mutableListOf()
35-
3634
// A block used for bulk updates.
3735
// It contains a **column group**, an **equals sign (`=`)**, and a **value group block**.
3836
var columnDefinitionGroupBlock: SqlUpdateColumnGroupBlock? = null
@@ -55,17 +53,13 @@ open class SqlUpdateSetGroupBlock(
5553
override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen)
5654

5755
override fun createBlockIndentLen(preChildBlock: SqlBlock?): Int =
58-
if (!isAdjustIndentOnEnter()) {
56+
if (isAdjustIndentOnEnter()) {
57+
0
58+
} else {
5959
parentBlock?.let { parent ->
60-
if (parent.indent.indentLevel == IndentType.SUB) {
61-
parent.indent.groupIndentLen.plus(1)
62-
} else {
63-
val parentTextLen = parent.getNodeText().length
64-
val diffTextLen = parentTextLen.minus(getNodeText().length)
65-
parent.indent.indentLen.plus(diffTextLen)
66-
}
60+
val parentTextLen = parent.getNodeText().length
61+
val diffTextLen = parentTextLen.minus(getNodeText().length)
62+
parent.indent.indentLen.plus(diffTextLen)
6763
} ?: 0
68-
} else {
69-
0
7064
}
7165
}

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class SqlFormatterTest : BasePlatformTestCase() {
2727

2828
override fun getTestDataPath(): String? = "src/test/testData/sql/formatter"
2929

30+
private val formatDataPrefix = "_format"
31+
3032
override fun setUp() {
3133
super.setUp()
3234
settingSqlFormat(true)
@@ -49,47 +51,51 @@ class SqlFormatterTest : BasePlatformTestCase() {
4951
}
5052

5153
fun testSelectFormatter() {
52-
formatSqlFile("Select.sql", "Select_format.sql")
54+
formatSqlFile("Select.sql", "Select$formatDataPrefix.sql")
5355
}
5456

5557
fun testCreateTableFormatter() {
56-
formatSqlFile("CreateTable.sql", "CreateTable_format.sql")
58+
formatSqlFile("CreateTable.sql", "CreateTable$formatDataPrefix.sql")
5759
}
5860

5961
fun testCreateViewFormatter() {
60-
formatSqlFile("CreateView.sql", "CreateView_format.sql")
62+
formatSqlFile("CreateView.sql", "CreateView$formatDataPrefix.sql")
6163
}
6264

6365
fun testInsertFormatter() {
64-
formatSqlFile("Insert.sql", "Insert_format.sql")
66+
formatSqlFile("Insert.sql", "Insert$formatDataPrefix.sql")
6567
}
6668

6769
fun testInsertWithBindVariableFormatter() {
68-
formatSqlFile("InsertWithBindVariable.sql", "InsertWithBindVariable_format.sql")
70+
formatSqlFile("InsertWithBindVariable.sql", "InsertWithBindVariable$formatDataPrefix.sql")
6971
}
7072

7173
fun testUpdateFormatter() {
72-
formatSqlFile("Update.sql", "Update_format.sql")
74+
formatSqlFile("Update.sql", "Update$formatDataPrefix.sql")
7375
}
7476

7577
fun testUpdateBindVariableFormatter() {
76-
formatSqlFile("UpdateBindVariable.sql", "UpdateBindVariable_format.sql")
78+
formatSqlFile("UpdateBindVariable.sql", "UpdateBindVariable$formatDataPrefix.sql")
7779
}
7880

7981
fun testUpdateTupleAssignmentFormatter() {
80-
formatSqlFile("UpdateTupleAssignment.sql", "UpdateTupleAssignment_format.sql")
82+
formatSqlFile("UpdateTupleAssignment.sql", "UpdateTupleAssignment$formatDataPrefix.sql")
8183
}
8284

8385
fun testDeleteFormatter() {
84-
formatSqlFile("Delete.sql", "Delete_format.sql")
86+
formatSqlFile("Delete.sql", "Delete$formatDataPrefix.sql")
8587
}
8688

8789
fun testInsertConflictUpdateFormatter() {
88-
formatSqlFile("InsertConflictUpdate.sql", "InsertConflictUpdate_format.sql")
90+
formatSqlFile("InsertConflictUpdate.sql", "InsertConflictUpdate$formatDataPrefix.sql")
8991
}
9092

9193
fun testInsertConflictNothingFormatter() {
92-
formatSqlFile("InsertConflictNothing.sql", "InsertConflictNothing_format.sql")
94+
formatSqlFile("InsertConflictNothing.sql", "InsertConflictNothing$formatDataPrefix.sql")
95+
}
96+
97+
fun testInsertConflictUpdateWithOutTableFormatter() {
98+
formatSqlFile("InsertConflictUpdateWithOutTable.sql", "InsertConflictUpdateWithOutTable$formatDataPrefix.sql")
9399
}
94100

95101
private fun formatSqlFile(
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
insert into users (username, email)
2-
values ('user', '[email protected]')
3-
on CONFLICT(username) do nothing
1+
insert into employee (id, username) values ( /* employees.id */0, /* employees.name */'name')
2+
on conflict (username) do nothing
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
INSERT INTO users
2-
(username
3-
, email)
4-
VALUES ('user'
5-
6-
ON CONFLICT(username)
1+
INSERT INTO employee
2+
(id
3+
, username)
4+
VALUES ( /* employees.id */0
5+
, /* employees.name */'name')
6+
ON CONFLICT (username)
77
DO NOTHING
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
insert into users (username, email)
2+
values ('user', '[email protected]')
3+
on CONFLICT ON constraint do update set email = EXCLUDED.email, created_at = CURRENT_TIMESTAMP
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
INSERT INTO users
2+
(username
3+
, email)
4+
VALUES ('user'
5+
6+
ON CONFLICT ON CONSTRAINT
7+
DO UPDATE
8+
SET email = EXCLUDED.email
9+
, created_at = CURRENT_TIMESTAMP

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ INSERT INTO users
33
, email)
44
VALUES ('user'
55
6-
ON CONFLICT(username) ON CONSTRAINT
6+
ON CONFLICT (username) ON CONSTRAINT
77
DO UPDATE
88
SET email = EXCLUDED.email
99
, created_at = CURRENT_TIMESTAMP

0 commit comments

Comments
 (0)