Skip to content

Commit 0afbd7b

Browse files
authored
Merge pull request #394 from domaframework/fix/sql-format-freeze-during
Fix: Freeze During SQL Formatting
2 parents 33b0414 + 7c49978 commit 0afbd7b

File tree

8 files changed

+66
-10
lines changed

8 files changed

+66
-10
lines changed

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

Lines changed: 36 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,41 @@ 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 || childBlock1.node.elementType == SqlTypes.LEFT_PAREN) {
67+
return SqlCustomSpacingBuilder.nonSpacing
68+
}
69+
if (childBlock2 != null) {
70+
if (childBlock2.node.elementType == SqlTypes.RIGHT_PAREN) {
71+
return SqlCustomSpacingBuilder.nonSpacing
72+
}
73+
return when (childBlock1) {
74+
is SqlElCommaBlock -> SqlCustomSpacingBuilder.normalSpacing
75+
is SqlElSymbolBlock -> SqlCustomSpacingBuilder.nonSpacing
76+
else -> SqlCustomSpacingBuilder.nonSpacing
77+
}
78+
}
79+
80+
return SqlCustomSpacingBuilder.normalSpacing
81+
}
4682
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,36 @@ 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.SqlLiteralBlock
23+
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2124
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
25+
import org.domaframework.doma.intellij.psi.SqlTypes
2226

2327
class SqlElPrimaryBlock(
2428
node: ASTNode,
25-
context: SqlBlockFormattingContext,
29+
private val context: SqlBlockFormattingContext,
2630
) : SqlExprBlock(
2731
node,
2832
context,
2933
) {
34+
override fun getBlock(child: ASTNode): SqlBlock =
35+
when (child.elementType) {
36+
SqlTypes.LEFT_PAREN, SqlTypes.RIGHT_PAREN ->
37+
SqlElSymbolBlock(child, context)
38+
39+
SqlTypes.EL_PRIMARY_EXPR ->
40+
SqlElPrimaryBlock(child, context)
41+
42+
SqlTypes.COMMA ->
43+
SqlElCommaBlock(child, context)
44+
45+
SqlTypes.EL_NUMBER, SqlTypes.EL_STRING, SqlTypes.BOOLEAN, SqlTypes.EL_NULL ->
46+
SqlLiteralBlock(child, context)
47+
48+
else -> SqlUnknownBlock(child, context)
49+
}
50+
3051
override fun getSpacing(
3152
child1: Block?,
3253
child2: Block,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.intellij.psi.PsiWhiteSpace
2222
import com.intellij.psi.util.PsiTreeUtil
2323
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2424
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
25-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
2625
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2726
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2827
import org.domaframework.doma.intellij.psi.SqlElClass
@@ -56,7 +55,7 @@ class SqlElStaticFieldAccessBlock(
5655
SqlElIdentifierBlock(child, context)
5756

5857
SqlTypes.EL_PARAMETERS ->
59-
SqlParallelListBlock(child, context)
58+
SqlElParametersBlock(child, context)
6059

6160
else -> SqlUnknownBlock(child, context)
6261
}

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/Select.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ AS nearest
2626
WHERE p.TYPE = 'Star'
2727
-- Line3
2828
OR ( p.flags & FPHOTOFLAGS('EDGE') = 0 AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
29-
/*%if status == 2 */
29+
/*%if status == 2 && employee.employeeParams(1,null, true ,"A") */
3030
-- Line4
3131
and u.propermotion > 2.0
3232
/** And Group */

src/test/testData/sql/formatter/Select_format.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SELECT COUNT(DISTINCT x) AS count_x
3030
-- Line3
3131
OR (p.flags & FPHOTOFLAGS('EDGE') = 0
3232
AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
33-
/*%if status == 2 */
33+
/*%if status == 2 && employee.employeeParams(1, null, true, "A") */
3434
-- Line4
3535
AND u.propermotion > 2.0
3636
/** And Group */

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,null,true , "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, null, true, "0") */'30'

0 commit comments

Comments
 (0)