Skip to content

Commit b99a60c

Browse files
authored
Merge pull request #305 from domaframework/feature/sql-format-support-conflict-options
Optional Keyword Support for ON CONFLICT Syntax
2 parents 06e32c9 + bcdcc0c commit b99a60c

35 files changed

+227
-52
lines changed

src/main/java/org/domaframework/doma/intellij/Sql.flex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
3737
"conflict",
3838
"constraint",
3939
"column",
40+
"collate",
4041
"comment",
4142
"create",
4243
"cross",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ open class SqlBlock(
8989
open var parentBlock: SqlBlock? = null
9090
open val childBlocks = mutableListOf<SqlBlock>()
9191

92+
fun getChildBlocksDropLast(dropIndex: Int = 1): List<SqlBlock> = childBlocks.dropLast(dropIndex)
93+
9294
open val indent: ElementIndent =
9395
ElementIndent(
9496
IndentType.FILE,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class SqlLineCommentBlock(
4141
override fun createBlockIndentLen(): Int {
4242
parentBlock?.let { parent ->
4343
if (parent is SqlSubQueryGroupBlock) {
44-
if (parent.childBlocks.dropLast(1).isEmpty()) {
44+
if (parent.getChildBlocksDropLast().isEmpty()) {
4545
return 0
4646
}
4747
if (parent.isFirstLineComment) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ open class SqlElBlockCommentBlock(
130130
override fun createBlockIndentLen(): Int {
131131
parentBlock?.let {
132132
if (it is SqlSubQueryGroupBlock) {
133-
if (it.childBlocks.dropLast(1).isEmpty()) {
133+
if (it.getChildBlocksDropLast().isEmpty()) {
134134
return 0
135135
}
136136
if (it.isFirstLineComment) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword.insert
1717

1818
import com.intellij.lang.ASTNode
19-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlValuesGroupBlock
19+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2020
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
2121
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2222

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.group.keyword.second
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
20+
21+
class SqlReturningGroupBlock(
22+
node: ASTNode,
23+
context: SqlBlockFormattingContext,
24+
) : SqlSecondKeywordBlock(node, context) {
25+
override fun createBlockIndentLen(): Int {
26+
parentBlock?.let { parent ->
27+
return parent.indent.indentLen
28+
}
29+
return 0
30+
}
31+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2022
import org.domaframework.doma.intellij.formatter.util.IndentType
2123
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2224
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
@@ -46,8 +48,12 @@ open class SqlSecondKeywordBlock(
4648
}
4749

4850
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
49-
lastGroup?.let {
50-
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), lastGroup.getNodeText())
51+
lastGroup?.let { last ->
52+
val prevKeyword = last.childBlocks.findLast { it is SqlKeywordBlock }
53+
prevKeyword?.let { prev ->
54+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), prev.getNodeText())
55+
}
56+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), last.getNodeText())
5157
}
5258
return true
5359
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.top
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.SqlKeywordGroupBlock
2021
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
2122
import org.domaframework.doma.intellij.formatter.util.IndentType
2223
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SqlSelectQueryGroupBlock(
6565
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
6666
lastGroup?.let { lastBlock ->
6767
if (lastGroup is SqlWithQuerySubGroupBlock) return true
68-
if (lastBlock is SqlSubGroupBlock) return lastBlock.childBlocks.dropLast(1).isNotEmpty()
68+
if (lastBlock is SqlSubGroupBlock) return lastBlock.getChildBlocksDropLast().isNotEmpty()
6969
}
7070
return true
7171
}

0 commit comments

Comments
 (0)