Skip to content

Commit 57ea031

Browse files
committed
Implemented an override method to set child block properties into the parent block accordingly.
1 parent 5a5d382 commit 57ea031

33 files changed

+292
-139
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
3131
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
3232
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
3333
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
34+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
35+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
3436
import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
3537
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
3638
import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock
3739
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
3840
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
3941
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
42+
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
4043
import org.domaframework.doma.intellij.formatter.block.group.column.SqlDataTypeBlock
4144
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
4245
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
@@ -45,7 +48,6 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
4548
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
4649
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock
4750
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
48-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
4951
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock
5052
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
5153
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
@@ -112,7 +114,9 @@ open class SqlBlock(
112114
setParentPropertyBlock(lastGroup)
113115
}
114116

115-
open fun setParentPropertyBlock(lastGroup: SqlBlock?) {}
117+
open fun setParentPropertyBlock(lastGroup: SqlBlock?) {
118+
// This method can be overridden to set additional properties on the parent block if needed.
119+
}
116120

117121
open val isNeedWhiteSpace: Boolean = true
118122

@@ -209,12 +213,18 @@ open class SqlBlock(
209213
SqlColumnDefinitionRawGroupBlock::class,
210214
SqlCreateTableColumnDefinitionGroupBlock::class,
211215
SqlUpdateColumnAssignmentSymbolBlock::class,
216+
SqlDoGroupBlock::class,
212217
)
213218

214219
if (isExpectedClassType(expectedClassTypes, childBlock)) return true
215220

216221
if (isNewLineSqlComment(child, childBlock)) return true
217222

223+
if (lastGroup is SqlConflictClauseBlock) return false
224+
if (childBlock is SqlColumnRawGroupBlock) {
225+
return !childBlock.isFirstColumnGroup
226+
}
227+
218228
return (
219229
isNewLineGroupBlockAfterRegistrationChild(childBlock, lastGroup) ||
220230
(childBlock is SqlRightPatternBlock && childBlock.isNewLine(lastGroup))
@@ -417,15 +427,20 @@ open class SqlBlock(
417427
return blockUtil.getSubGroupBlock(lastGroup, child)
418428
}
419429

420-
SqlTypes.OTHER -> return if (lastGroup is SqlUpdateSetGroupBlock &&
421-
lastGroup.columnDefinitionGroupBlock != null
422-
) {
423-
SqlUpdateColumnAssignmentSymbolBlock(child, defaultFormatCtx)
424-
} else {
425-
SqlOtherBlock(
426-
child,
427-
defaultFormatCtx,
428-
)
430+
SqlTypes.OTHER -> {
431+
return if (lastGroup is SqlUpdateSetGroupBlock &&
432+
lastGroup.columnDefinitionGroupBlock != null
433+
) {
434+
SqlUpdateColumnAssignmentSymbolBlock(
435+
child,
436+
defaultFormatCtx,
437+
)
438+
} else {
439+
SqlOtherBlock(
440+
child,
441+
defaultFormatCtx,
442+
)
443+
}
429444
}
430445

431446
SqlTypes.RIGHT_PAREN -> return SqlRightPatternBlock(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ 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.formatter.block.group.column.SqlColumnRawGroupBlock
2021
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
23-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
26-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock
2727
import org.domaframework.doma.intellij.formatter.util.IndentType
2828
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2929
import org.domaframework.doma.intellij.psi.SqlTypes

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block
1818
import com.intellij.formatting.Indent
1919
import com.intellij.lang.ASTNode
2020
import com.intellij.psi.formatter.common.AbstractBlock
21+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2223
import org.domaframework.doma.intellij.formatter.util.IndentType
2324
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -42,11 +43,11 @@ open class SqlKeywordBlock(
4243
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
4344
}
4445

45-
// override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
46-
// if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) {
47-
// lastGroup.doQueryBlock = this
48-
// }
49-
// }
46+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
47+
if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) {
48+
lastGroup.doQueryBlock = this
49+
}
50+
}
5051

5152
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
5253

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
2121
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
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.insert.SqlInsertQueryGroupBlock
2728
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2931
import org.domaframework.doma.intellij.formatter.util.IndentType
3032
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
3133

@@ -62,6 +64,11 @@ open class SqlRightPatternBlock(
6264
return
6365
}
6466

67+
if (parent.parentBlock is SqlConflictClauseBlock) {
68+
preSpaceRight = false
69+
return
70+
}
71+
6572
if (parent is SqlSubQueryGroupBlock) {
6673
val prevKeywordBlock =
6774
parent.childBlocks
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
enum class OnConflictKeywordType {
19+
CONFLICT,
20+
CONSTRAINT,
21+
UNKNOWN,
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.IndentType
22+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
23+
24+
class SqlConflictClauseBlock(
25+
node: ASTNode,
26+
context: SqlBlockFormattingContext,
27+
) : SqlKeywordGroupBlock(
28+
node,
29+
IndentType.TOP,
30+
context,
31+
) {
32+
var conflictType: OnConflictKeywordType = OnConflictKeywordType.CONFLICT
33+
var doBlock: SqlDoGroupBlock? = null
34+
35+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
36+
super.setParentGroupBlock(lastGroup)
37+
indent.indentLevel = IndentType.CONFLICT
38+
indent.indentLen = createBlockIndentLen()
39+
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.IndentType
22+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
23+
24+
class SqlDoGroupBlock(
25+
node: ASTNode,
26+
context: SqlBlockFormattingContext,
27+
) : SqlKeywordGroupBlock(
28+
node,
29+
IndentType.CONFLICT,
30+
context,
31+
) {
32+
/**
33+
* **NOTHING** or **UPDATE**
34+
*/
35+
var doQueryBlock: SqlBlock? = null
36+
37+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
38+
super.setParentGroupBlock(lastGroup)
39+
indent.indentLen = 0
40+
indent.groupIndentLen = getNodeText().length
41+
}
42+
43+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
44+
if (lastGroup is SqlConflictClauseBlock) {
45+
lastGroup.doBlock = this
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)