Skip to content

Commit b2e0132

Browse files
committed
Enhance SQL formatting by adding support for literal types and refining spacing logic
1 parent b211f68 commit b2e0132

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.expr
1717

18+
import com.intellij.formatting.Block
19+
import com.intellij.formatting.Spacing
1820
import com.intellij.lang.ASTNode
1921
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2022
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
23+
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2124
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2225
import org.domaframework.doma.intellij.psi.SqlTypes
2326

@@ -39,8 +42,39 @@ class SqlElParametersBlock(
3942
SqlTypes.COMMA ->
4043
SqlElCommaBlock(child, context)
4144

45+
SqlTypes.EL_FIELD_ACCESS_EXPR ->
46+
SqlElFieldAccessBlock(child, context)
47+
48+
SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR ->
49+
SqlElStaticFieldAccessBlock(child, context)
50+
51+
SqlTypes.EL_PRIMARY_EXPR ->
52+
SqlElPrimaryBlock(child, context)
53+
4254
else -> SqlUnknownBlock(child, context)
4355
}
4456

4557
override fun isLeaf(): Boolean = false
58+
59+
override fun getSpacing(
60+
child1: Block?,
61+
child2: Block,
62+
): Spacing? {
63+
val childBlock1 = child1 as? SqlBlock
64+
val childBlock2 = child2 as? SqlBlock
65+
66+
if (childBlock1 == null) return SqlCustomSpacingBuilder.nonSpacing
67+
if (childBlock2 != null) {
68+
if (childBlock2.node.elementType == SqlTypes.RIGHT_PAREN) {
69+
return SqlCustomSpacingBuilder.nonSpacing
70+
}
71+
return when (childBlock1) {
72+
is SqlElSymbolBlock -> SqlCustomSpacingBuilder.nonSpacing
73+
is SqlElCommaBlock -> SqlCustomSpacingBuilder.normalSpacing
74+
else -> return SqlCustomSpacingBuilder.normalSpacing
75+
}
76+
}
77+
78+
return SqlCustomSpacingBuilder.normalSpacing
79+
}
4680
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,35 @@ package org.domaframework.doma.intellij.formatter.block.expr
1818
import com.intellij.formatting.Block
1919
import com.intellij.formatting.Spacing
2020
import com.intellij.lang.ASTNode
21+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22+
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2123
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
24+
import org.domaframework.doma.intellij.psi.SqlTypes
2225

2326
class SqlElPrimaryBlock(
2427
node: ASTNode,
25-
context: SqlBlockFormattingContext,
28+
private val context: SqlBlockFormattingContext,
2629
) : SqlExprBlock(
2730
node,
2831
context,
2932
) {
33+
override fun getBlock(child: ASTNode): SqlBlock =
34+
when (child.elementType) {
35+
SqlTypes.LEFT_PAREN, SqlTypes.RIGHT_PAREN ->
36+
SqlElSymbolBlock(child, context)
37+
38+
SqlTypes.EL_PRIMARY_EXPR ->
39+
SqlElPrimaryBlock(child, context)
40+
41+
SqlTypes.COMMA ->
42+
SqlElCommaBlock(child, context)
43+
44+
SqlTypes.EL_PRIMARY_EXPR, SqlTypes.EL_NUMBER, SqlTypes.EL_STRING, SqlTypes.BOOLEAN, SqlTypes.EL_NULL ->
45+
SqlElPrimaryBlock(child, context)
46+
47+
else -> SqlUnknownBlock(child, context)
48+
}
49+
3050
override fun getSpacing(
3151
child1: Block?,
3252
child2: Block,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.lang.ASTNode
2121
import com.intellij.psi.PsiWhiteSpace
2222
import com.intellij.psi.formatter.common.AbstractBlock
2323
import org.domaframework.doma.intellij.formatter.block.SqlBlock
24+
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2425
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2526

2627
abstract class SqlExprBlock(
@@ -39,8 +40,7 @@ abstract class SqlExprBlock(
3940
var child = node.firstChildNode
4041
while (child != null) {
4142
if (child !is PsiWhiteSpace) {
42-
val block = getBlock(child)
43-
blocks.add(block)
43+
blocks.add(getBlock(child))
4444
}
4545
child = child.treeNext
4646
}
@@ -50,5 +50,5 @@ abstract class SqlExprBlock(
5050
override fun getSpacing(
5151
child1: Block?,
5252
child2: Block,
53-
): Spacing? = null
53+
): Spacing? = SqlCustomSpacingBuilder.nonSpacing
5454
}

src/test/testData/sql/formatter/StaticFieldAccess.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ SELECT *
22
FROM configurations
33
WHERE
44
config_key = /*@ com.example.Constants @CONFIG_KEY */'system.timeout'
5-
AND value = /*@ com.example.Utils @ util. getDefaultValue () */'30'
5+
AND value = /*@ com.example.Utils @ util. getDefaultValue (0) */'30'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
SELECT *
22
FROM configurations
33
WHERE config_key = /* @com.example.Constants@CONFIG_KEY */'system.timeout'
4-
AND value = /* @[email protected]() */'30'
4+
AND value = /* @[email protected](0) */'30'

0 commit comments

Comments
 (0)