Skip to content

Commit ee1e798

Browse files
authored
Merge pull request #310 from domaframework/feature/sql-format-support-values
VALUES Formatting Support
2 parents 5314405 + 82ae71d commit ee1e798

31 files changed

+393
-117
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTyp
4848
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
4949
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
5050
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
51+
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
5152
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
5253
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
5354
import org.domaframework.doma.intellij.formatter.processor.SqlSetParentGroupProcessor
@@ -89,7 +90,16 @@ open class SqlBlock(
8990
open var parentBlock: SqlBlock? = null
9091
open val childBlocks = mutableListOf<SqlBlock>()
9192

92-
fun getChildBlocksDropLast(dropIndex: Int = 1): List<SqlBlock> = childBlocks.dropLast(dropIndex)
93+
fun getChildBlocksDropLast(
94+
dropIndex: Int = 1,
95+
skipCommentBlock: Boolean = false,
96+
): List<SqlBlock> {
97+
val children = childBlocks.dropLast(dropIndex)
98+
if (skipCommentBlock) {
99+
return children.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
100+
}
101+
return children
102+
}
93103

94104
open val indent: ElementIndent =
95105
ElementIndent(

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordG
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
26+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2628
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
2729
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
2830
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2931
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
3032
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
3133
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
3234
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
35+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlValuesParamGroupBlock
3336
import org.domaframework.doma.intellij.formatter.util.IndentType
3437
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
3538

@@ -59,6 +62,12 @@ open class SqlCommaBlock(
5962
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
6063
}
6164

65+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
66+
if (lastGroup is SqlFromGroupBlock) {
67+
if (lastGroup.tableBlocks.isNotEmpty()) lastGroup.tableBlocks.add(this)
68+
}
69+
}
70+
6271
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
6372

6473
override fun createBlockIndentLen(): Int {
@@ -89,6 +98,8 @@ open class SqlCommaBlock(
8998
return parentIndentLen.plus(1)
9099
}
91100

101+
if (parent is SqlValuesParamGroupBlock) return 0
102+
92103
val grand = parent.parentBlock
93104
grand?.let { grand ->
94105
if (grand is SqlCreateKeywordGroupBlock) {
@@ -106,6 +117,7 @@ open class SqlCommaBlock(
106117
}
107118
return parentIndentLen.plus(1)
108119
} else {
120+
if (parent is SqlValuesGroupBlock) return parent.indent.indentLen
109121
return parent.indent.groupIndentLen.plus(1)
110122
}
111123
}

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

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ package org.domaframework.doma.intellij.formatter.block
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
21-
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
21+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictExpressionSubGroupBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
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
2728
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2930
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
31+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock
3032
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
3133
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
3234
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
@@ -53,41 +55,40 @@ open class SqlRightPatternBlock(
5355
/**
5456
* Configures whether to add a space to the right side when the group ends.
5557
*/
56-
fun enableLastRight() {
58+
private fun enableLastRight() {
5759
parentBlock?.let { parent ->
58-
// TODO:Customize spacing
60+
// Check if parent is in the notInsertSpaceClassList
5961
val notInsertSpaceClassList =
6062
listOf(
6163
SqlFunctionParamBlock::class,
6264
SqlInsertColumnGroupBlock::class,
6365
SqlWithQuerySubGroupBlock::class,
66+
SqlConflictExpressionSubGroupBlock::class,
6467
)
6568
if (isExpectedClassType(notInsertSpaceClassList, parent)) {
6669
preSpaceRight = false
6770
return
6871
}
6972

70-
if (parent.parentBlock is SqlConflictClauseBlock) {
71-
preSpaceRight = false
72-
return
73-
}
74-
73+
// Check if parent is SqlSubQueryGroupBlock
7574
if (parent is SqlSubQueryGroupBlock) {
7675
val prevKeywordBlock =
7776
parent.childBlocks
7877
.filter { it.node.startOffset < node.startOffset }
79-
.find { it is SqlKeywordGroupBlock && it.indent.indentLevel == IndentType.TOP }
80-
if (prevKeywordBlock != null) {
81-
preSpaceRight = true
82-
return
83-
}
78+
.find { it is SqlKeywordGroupBlock }
79+
80+
preSpaceRight = prevKeywordBlock?.indent?.indentLevel == IndentType.TOP
81+
return
8482
}
8583

86-
parent.parentBlock?.let { grand ->
87-
preSpaceRight = grand.childBlocks.find { it is SqlKeywordGroupBlock } != null
84+
// Check grandparent for SqlKeywordGroupBlock
85+
parent.parentBlock?.let { grandParent ->
86+
preSpaceRight = grandParent.childBlocks.any { it is SqlKeywordGroupBlock }
8887
return
8988
}
9089
}
90+
91+
// Default case
9192
preSpaceRight = false
9293
}
9394

@@ -100,16 +101,21 @@ open class SqlRightPatternBlock(
100101

101102
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
102103
super.setParentGroupBlock(lastGroup)
104+
enableLastRight()
103105
indent.indentLevel = IndentType.NONE
104106
indent.indentLen = createBlockIndentLen()
105107
indent.groupIndentLen = indent.indentLen
106-
enableLastRight()
108+
}
109+
110+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
107111
(lastGroup as? SqlSubGroupBlock)?.endPatternBlock = this
108112
}
109113

110114
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
111115

112116
override fun createBlockIndentLen(): Int {
117+
if (preSpaceRight) return 1
118+
113119
parentBlock?.let { parent ->
114120
if (parent is SqlWithQuerySubGroupBlock) return 0
115121
val exceptionalTypes =
@@ -119,29 +125,51 @@ open class SqlRightPatternBlock(
119125
SqlCreateTableColumnDefinitionGroupBlock::class,
120126
)
121127
if (isExpectedClassType(exceptionalTypes, parent)) return parent.indent.indentLen
122-
return parent.indent.groupIndentLen
128+
return parent.indent.indentLen
123129
} ?: return 0
124130
}
125131

126132
override fun isLeaf(): Boolean = true
127133

128134
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
129-
val exceptionalTypes =
130-
listOf(
131-
SqlCreateTableColumnDefinitionGroupBlock::class,
132-
SqlColumnDefinitionRawGroupBlock::class,
133-
SqlUpdateColumnGroupBlock::class,
134-
SqlUpdateValueGroupBlock::class,
135-
SqlWithQuerySubGroupBlock::class,
136-
)
137-
if (isExpectedClassType(exceptionalTypes, parentBlock)) return true
138-
139-
val parentExceptionalTypes =
140-
listOf(
141-
SqlUpdateSetGroupBlock::class,
142-
SqlUpdateColumnGroupBlock::class,
143-
SqlUpdateValueGroupBlock::class,
144-
)
145-
return isExpectedClassType(parentExceptionalTypes, parentBlock?.parentBlock)
135+
if (preSpaceRight) return false
136+
137+
parentBlock?.let { parent ->
138+
val exceptionalTypes =
139+
listOf(
140+
SqlCreateTableColumnDefinitionGroupBlock::class,
141+
SqlColumnDefinitionRawGroupBlock::class,
142+
SqlUpdateSetGroupBlock::class,
143+
SqlUpdateColumnGroupBlock::class,
144+
SqlUpdateValueGroupBlock::class,
145+
SqlWithQuerySubGroupBlock::class,
146+
)
147+
148+
val excludeTypes =
149+
listOf(
150+
SqlDataTypeParamBlock::class,
151+
SqlConditionalExpressionGroupBlock::class,
152+
SqlConflictExpressionSubGroupBlock::class,
153+
SqlFunctionParamBlock::class,
154+
)
155+
156+
if ((
157+
isExpectedClassType(exceptionalTypes, parent) ||
158+
isExpectedClassType(exceptionalTypes, parent.parentBlock)
159+
) &&
160+
!isExpectedClassType(excludeTypes, parent)
161+
) {
162+
return true
163+
}
164+
165+
if (parent is SqlSubGroupBlock) {
166+
val firstChild =
167+
parent.getChildBlocksDropLast(skipCommentBlock = true).firstOrNull()
168+
if (firstChild is SqlKeywordGroupBlock) {
169+
return firstChild.indent.indentLevel != IndentType.TOP
170+
}
171+
}
172+
}
173+
return false
146174
}
147175
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.conflict
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
20+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
21+
22+
class SqlConflictExpressionSubGroupBlock(
23+
node: ASTNode,
24+
context: SqlBlockFormattingContext,
25+
) : SqlSubGroupBlock(node, context)

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
2525
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2626
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
2727
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
28+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
2930
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3031
import org.domaframework.doma.intellij.formatter.util.IndentType
@@ -128,16 +129,21 @@ open class SqlElBlockCommentBlock(
128129
override fun isLeaf(): Boolean = false
129130

130131
override fun createBlockIndentLen(): Int {
131-
parentBlock?.let {
132-
if (it is SqlSubQueryGroupBlock) {
133-
if (it.getChildBlocksDropLast().isEmpty()) {
132+
parentBlock?.let { parent ->
133+
if (parent is SqlSubQueryGroupBlock) {
134+
if (parent.getChildBlocksDropLast().isEmpty()) {
134135
return 0
135136
}
136-
if (it.isFirstLineComment) {
137-
return it.indent.groupIndentLen.minus(2)
137+
if (parent.isFirstLineComment) {
138+
return parent.indent.groupIndentLen.minus(2)
138139
}
139140
}
141+
if (parent is SqlValuesGroupBlock) {
142+
return parent.indent.indentLen
143+
}
140144
}
141145
return 0
142146
}
147+
148+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock is SqlValuesGroupBlock
143149
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnBlock.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.column
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.SqlWordBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
2323
import org.domaframework.doma.intellij.formatter.util.IndentType
2424
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2525

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil
2121
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2222
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
23-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
24-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2523
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2624
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
2725
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -40,8 +38,7 @@ open class SqlKeywordGroupBlock(
4038

4139
fun updateTopKeywordBlocks(block: SqlBlock) {
4240
val lastChild =
43-
getChildBlocksDropLast()
44-
.findLast { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
41+
getChildBlocksDropLast(skipCommentBlock = true).lastOrNull()
4542
val topKeywordTypes =
4643
listOf(
4744
SqlKeywordBlock::class,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword
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.second.SqlFromGroupBlock
2021
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2122
import org.domaframework.doma.intellij.formatter.util.IndentType
2223
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -37,6 +38,12 @@ class SqlLateralGroupBlock(
3738
indent.groupIndentLen = createGroupIndentLen()
3839
}
3940

41+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
42+
if (lastGroup is SqlFromGroupBlock) {
43+
if (lastGroup.tableBlocks.isEmpty()) lastGroup.tableBlocks.add(this)
44+
}
45+
}
46+
4047
override fun createBlockIndentLen(): Int {
4148
parentBlock?.let { parent ->
4249
return parent.indent.groupIndentLen.plus(1)

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateKeywordGroupBlock.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.keyword.create
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.SqlTableBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
2323
import org.domaframework.doma.intellij.formatter.util.CreateQueryType
2424
import org.domaframework.doma.intellij.formatter.util.IndentType
2525
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.insert
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.subgroup.SqlSubGroupBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlValuesParamGroupBlock
2121
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2222

2323
class SqlInsertValueGroupBlock(
2424
node: ASTNode,
2525
context: SqlBlockFormattingContext,
26-
) : SqlSubGroupBlock(
26+
) : SqlValuesParamGroupBlock(
2727
node,
2828
context,
2929
) {
@@ -41,4 +41,6 @@ class SqlInsertValueGroupBlock(
4141
return parent.indent.groupIndentLen.plus(1)
4242
} ?: return 1
4343
}
44+
45+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
4446
}

0 commit comments

Comments
 (0)