Skip to content

Commit 219f753

Browse files
committed
Implemented an override method to set child block properties into the parent block accordingly.
1 parent ae7dce7 commit 219f753

38 files changed

+357
-190
lines changed

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,28 @@ 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
4346
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
4447
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
4548
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
49+
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock
4650
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
47-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
4851
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock
4952
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
5053
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
5154
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlRightPatternBlock
5255
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
53-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateColumnAssignmentSymbolBlock
5456
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
5557
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
5658
import org.domaframework.doma.intellij.formatter.processor.SqlSetParentGroupProcessor
@@ -109,6 +111,11 @@ open class SqlBlock(
109111
open fun setParentGroupBlock(lastGroup: SqlBlock?) {
110112
parentBlock = lastGroup
111113
parentBlock?.addChildBlock(this)
114+
setParentPropertyBlock(lastGroup)
115+
}
116+
117+
open fun setParentPropertyBlock(lastGroup: SqlBlock?) {
118+
// This method can be overridden to set additional properties on the parent block if needed.
112119
}
113120

114121
open val isNeedWhiteSpace: Boolean = true
@@ -206,12 +213,18 @@ open class SqlBlock(
206213
SqlColumnDefinitionRawGroupBlock::class,
207214
SqlCreateTableColumnDefinitionGroupBlock::class,
208215
SqlUpdateColumnAssignmentSymbolBlock::class,
216+
SqlDoGroupBlock::class,
209217
)
210218

211219
if (isExpectedClassType(expectedClassTypes, childBlock)) return true
212220

213221
if (isNewLineSqlComment(child, childBlock)) return true
214222

223+
if (lastGroup is SqlConflictClauseBlock) return false
224+
if (childBlock is SqlColumnRawGroupBlock) {
225+
return !childBlock.isFirstColumnGroup
226+
}
227+
215228
return (
216229
isNewLineGroupBlockAfterRegistrationChild(childBlock, lastGroup) ||
217230
(childBlock is SqlRightPatternBlock && childBlock.isNewLine(lastGroup))
@@ -256,6 +269,9 @@ open class SqlBlock(
256269

257270
if (parentSetProcessor.isNewGroupAndNotSetLineKeywords(childBlock, lastGroup)) {
258271
return if (lastGroup is SqlSubQueryGroupBlock) {
272+
// サブグループ内の最初の要素以外なら改行
273+
// この時、childBlocksもサブグループの最初の要素に入るため
274+
// 最初の要素を無視して、他の要素が登録されているかをチェックする
259275
val lastGroupChildren = lastGroup.childBlocks
260276
(lastGroupChildren.isNotEmpty() && lastGroupChildren.drop(1).isNotEmpty())
261277
} else {
@@ -414,13 +430,20 @@ open class SqlBlock(
414430
return blockUtil.getSubGroupBlock(lastGroup, child)
415431
}
416432

417-
SqlTypes.OTHER -> return if (lastGroup is SqlUpdateSetGroupBlock) {
418-
SqlUpdateColumnAssignmentSymbolBlock(child, defaultFormatCtx)
419-
} else {
420-
SqlOtherBlock(
421-
child,
422-
defaultFormatCtx,
423-
)
433+
SqlTypes.OTHER -> {
434+
return if (lastGroup is SqlUpdateSetGroupBlock &&
435+
lastGroup.columnDefinitionGroupBlock != null
436+
) {
437+
SqlUpdateColumnAssignmentSymbolBlock(
438+
child,
439+
defaultFormatCtx,
440+
)
441+
} else {
442+
SqlOtherBlock(
443+
child,
444+
defaultFormatCtx,
445+
)
446+
}
424447
}
425448

426449
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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ 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.keyword.create.SqlCreateKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.CreateQueryType
2022
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2123

2224
class SqlTableBlock(
@@ -32,11 +34,11 @@ class SqlTableBlock(
3234
super.setParentGroupBlock(lastGroup)
3335
}
3436

35-
// override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
36-
// if (lastGroup is SqlCreateKeywordGroupBlock && lastGroup.createType == CreateQueryType.TABLE) {
37-
// lastGroup.tableBlock = this
38-
// }
39-
// }
37+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
38+
if (lastGroup is SqlCreateKeywordGroupBlock && lastGroup.createType == CreateQueryType.TABLE) {
39+
lastGroup.tableBlock = this
40+
}
41+
}
4042

4143
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
4244
}
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+
}

0 commit comments

Comments
 (0)