Skip to content

Commit 3821fe7

Browse files
committed
Refactor SQL formatting utilities to improve structure and rename for clarity
1 parent fb4e727 commit 3821fe7

File tree

11 files changed

+171
-118
lines changed

11 files changed

+171
-118
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
5757
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
5858
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
5959
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
60+
import org.domaframework.doma.intellij.formatter.handler.CreateClauseHandler
6061
import org.domaframework.doma.intellij.formatter.processor.SqlSetParentGroupProcessor
61-
import org.domaframework.doma.intellij.formatter.util.CreateTableUtil
6262
import org.domaframework.doma.intellij.formatter.util.IndentType
6363
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
6464
import org.domaframework.doma.intellij.formatter.util.SqlBlockGenerator
@@ -552,7 +552,7 @@ class SqlFileBlock(
552552
}
553553

554554
// Create Table Column Definition Raw Group Block
555-
CreateTableUtil
555+
CreateClauseHandler
556556
.getColumnDefinitionRawGroupSpacing(childBlock1, childBlock2)
557557
?.let { return it }
558558

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/CommaRawUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/CommaRawClauseHandler.kt

Lines changed: 4 additions & 2 deletions
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.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -22,8 +22,10 @@ import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoo
2222
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
25+
import org.domaframework.doma.intellij.formatter.util.IndentType
26+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2527

26-
object CommaRawUtil {
28+
object CommaRawClauseHandler {
2729
/**
2830
* Creates an appropriate comma block based on the last group context.
2931
* Handles special cases for condition/loop comment blocks and different group types.

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/CreateTableUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/CreateClauseHandler.kt

Lines changed: 3 additions & 2 deletions
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.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.formatting.Block
1919
import com.intellij.formatting.Spacing
@@ -24,9 +24,10 @@ import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlo
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
2626
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
27+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2728
import org.domaframework.doma.intellij.psi.SqlTypes
2829

29-
object CreateTableUtil {
30+
object CreateClauseHandler {
3031
fun getCreateTableClauseSubGroup(
3132
lastGroup: SqlBlock,
3233
child: ASTNode,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/InsertClauseUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/InsertClauseHandler.kt

Lines changed: 3 additions & 2 deletions
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.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -22,8 +22,9 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlI
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
25+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2526

26-
object InsertClauseUtil {
27+
object InsertClauseHandler {
2728
fun getInsertClauseSubGroup(
2829
lastGroup: SqlBlock,
2930
child: ASTNode,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/JoinGroupUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/JoinClauseHandler.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock
22+
import org.domaframework.doma.intellij.formatter.util.IndentType
23+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
24+
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
2225

23-
object JoinGroupUtil {
26+
object JoinClauseHandler {
2427
fun getJoinKeywordGroupBlock(
2528
lastGroupBlock: SqlBlock?,
2629
keywordText: String,
2730
child: ASTNode,
2831
sqlBlockFormattingCtx: SqlBlockFormattingContext,
2932
): SqlBlock =
30-
if (SqlKeywordUtil.isJoinKeyword(keywordText)) {
33+
if (SqlKeywordUtil.Companion.isJoinKeyword(keywordText)) {
3134
SqlJoinGroupBlock(
3235
child,
3336
sqlBlockFormattingCtx,
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.handler
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock
21+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
22+
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictExpressionSubGroupBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
25+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
26+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
28+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
29+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlValuesParamGroupBlock
30+
import org.domaframework.doma.intellij.formatter.block.word.SqlAliasBlock
31+
import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
32+
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
33+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
34+
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
35+
36+
object NotQueryGroupHandler {
37+
private const val IN_KEYWORD = "in"
38+
private const val RETURNING_KEYWORD = "returning"
39+
40+
/**
41+
* Creates an appropriate [org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock] based on the last group context and node type.
42+
* Returns null if no specific subgroup is needed.
43+
*/
44+
fun getSubGroup(
45+
lastGroup: SqlBlock?,
46+
child: ASTNode,
47+
sqlBlockFormattingCtx: SqlBlockFormattingContext,
48+
): SqlBlock? =
49+
when {
50+
hasInKeyword(lastGroup) -> SqlParallelListBlock(child, sqlBlockFormattingCtx)
51+
lastGroup is SqlConditionKeywordGroupBlock -> createConditionalExpressionGroup(child, sqlBlockFormattingCtx)
52+
hasFunctionOrAliasContext(lastGroup) -> createFunctionOrValueBlock(lastGroup, child, sqlBlockFormattingCtx)
53+
lastGroup is SqlConflictClauseBlock -> SqlConflictExpressionSubGroupBlock(child, sqlBlockFormattingCtx)
54+
hasValuesContext(lastGroup) -> SqlValuesParamGroupBlock(child, sqlBlockFormattingCtx)
55+
else -> null
56+
}
57+
58+
/**
59+
* Creates a keyword group block for specific keywords.
60+
* Currently only handles 'returning' keyword.
61+
*/
62+
fun getKeywordGroup(
63+
child: ASTNode,
64+
sqlBlockFormattingCtx: SqlBlockFormattingContext,
65+
): SqlBlock? {
66+
val keyword = child.text.lowercase()
67+
return when (keyword) {
68+
RETURNING_KEYWORD -> SqlReturningGroupBlock(child, sqlBlockFormattingCtx)
69+
else -> null
70+
}
71+
}
72+
73+
/**
74+
* Checks if the last group has an 'IN' keyword as its last option keyword.
75+
*/
76+
private fun hasInKeyword(lastGroup: SqlBlock?): Boolean {
77+
val lastKeyword =
78+
lastGroup
79+
?.childBlocks
80+
?.lastOrNull { SqlKeywordUtil.isOptionSqlKeyword(it.getNodeText()) }
81+
return lastKeyword?.getNodeText()?.lowercase() == IN_KEYWORD
82+
}
83+
84+
/**
85+
* Creates a conditional expression group block.
86+
*/
87+
private fun createConditionalExpressionGroup(
88+
child: ASTNode,
89+
sqlBlockFormattingCtx: SqlBlockFormattingContext,
90+
): SqlConditionalExpressionGroupBlock = SqlConditionalExpressionGroupBlock(child, sqlBlockFormattingCtx)
91+
92+
/**
93+
* Checks if the last group has a word block context that requires function or alias handling.
94+
*/
95+
private fun hasFunctionOrAliasContext(lastGroup: SqlBlock?): Boolean = lastGroup?.childBlocks?.lastOrNull() is SqlWordBlock
96+
97+
/**
98+
* Creates either a function parameter block or values parameter block based on the previous child type.
99+
*/
100+
private fun createFunctionOrValueBlock(
101+
lastGroup: SqlBlock?,
102+
child: ASTNode,
103+
sqlBlockFormattingCtx: SqlBlockFormattingContext,
104+
): SqlBlock {
105+
val prevChild = lastGroup?.childBlocks?.lastOrNull()
106+
return when {
107+
prevChild is SqlAliasBlock || prevChild is SqlTableBlock ->
108+
SqlValuesParamGroupBlock(child, sqlBlockFormattingCtx)
109+
else ->
110+
SqlFunctionParamBlock(child, sqlBlockFormattingCtx)
111+
}
112+
}
113+
114+
/**
115+
* Checks if the context indicates a values parameter group should be created.
116+
*/
117+
private fun hasValuesContext(lastGroup: SqlBlock?): Boolean =
118+
lastGroup is SqlValuesGroupBlock ||
119+
(lastGroup is SqlCommaBlock && lastGroup.parentBlock is SqlValuesGroupBlock)
120+
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/UpdateClauseUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/UpdateClauseHandler.kt

Lines changed: 4 additions & 2 deletions
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.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -24,8 +24,10 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlU
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
2626
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
27+
import org.domaframework.doma.intellij.formatter.util.IndentType
28+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2729

28-
object UpdateClauseUtil {
30+
object UpdateClauseHandler {
2931
fun getUpdateClauseSubGroup(
3032
lastGroup: SqlBlock,
3133
child: ASTNode,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/WithClauseUtil.kt renamed to src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/WithClauseHandler.kt

Lines changed: 6 additions & 3 deletions
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.util
16+
package org.domaframework.doma.intellij.formatter.handler
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -23,9 +23,12 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWit
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithOptionGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
26+
import org.domaframework.doma.intellij.formatter.util.IndentType
27+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
28+
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
2629
import org.domaframework.doma.intellij.psi.SqlTypes
2730

28-
object WithClauseUtil {
31+
object WithClauseHandler {
2932
fun getWithClauseSubGroup(
3033
lastGroup: SqlBlock,
3134
child: ASTNode,
@@ -54,7 +57,7 @@ object WithClauseUtil {
5457
child: ASTNode,
5558
sqlBlockFormattingCtx: SqlBlockFormattingContext,
5659
): SqlBlock? {
57-
if (SqlKeywordUtil.isWithOptionKeyword(child.text)) {
60+
if (SqlKeywordUtil.Companion.isWithOptionKeyword(child.text)) {
5861
return SqlWithOptionGroupBlock(child, sqlBlockFormattingCtx)
5962
}
6063
if (lastGroup is SqlWithOptionGroupBlock) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWit
3737
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
3838
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
3939
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
40+
import org.domaframework.doma.intellij.formatter.handler.UpdateClauseHandler
4041
import org.domaframework.doma.intellij.formatter.util.IndentType
4142
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
42-
import org.domaframework.doma.intellij.formatter.util.UpdateClauseUtil
4343
import org.domaframework.doma.intellij.psi.SqlTypes
4444
import kotlin.collections.lastOrNull
4545

@@ -160,7 +160,7 @@ class SqlSetParentGroupProcessor(
160160
if (isExpectedClassType(TOP_LEVEL_EXPECTED_TYPES, lastGroupBlock)) {
161161
lastGroupBlock
162162
} else if (childBlock is SqlUpdateQueryGroupBlock) {
163-
UpdateClauseUtil.getParentGroupBlock(blockBuilder, childBlock)
163+
UpdateClauseHandler.getParentGroupBlock(blockBuilder, childBlock)
164164
} else {
165165
findTopLevelParent()
166166
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/NotQueryGroupUtil.kt

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)