Skip to content

Commit 0ee229d

Browse files
committed
Fix the line break and indentation for comma lines.
1 parent 6855265 commit 0ee229d

File tree

7 files changed

+35
-48
lines changed

7 files changed

+35
-48
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
}
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

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)