diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt index 092a8bb5..7a8eeae7 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt @@ -15,9 +15,12 @@ */ package org.domaframework.doma.intellij.formatter.block.expr +import com.intellij.formatting.Block +import com.intellij.formatting.Spacing import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock +import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext import org.domaframework.doma.intellij.psi.SqlTypes @@ -39,8 +42,41 @@ class SqlElParametersBlock( SqlTypes.COMMA -> SqlElCommaBlock(child, context) + SqlTypes.EL_FIELD_ACCESS_EXPR -> + SqlElFieldAccessBlock(child, context) + + SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR -> + SqlElStaticFieldAccessBlock(child, context) + + SqlTypes.EL_PRIMARY_EXPR -> + SqlElPrimaryBlock(child, context) + else -> SqlUnknownBlock(child, context) } override fun isLeaf(): Boolean = false + + override fun getSpacing( + child1: Block?, + child2: Block, + ): Spacing? { + val childBlock1 = child1 as? SqlBlock + val childBlock2 = child2 as? SqlBlock + + if (childBlock1 == null || childBlock1.node.elementType == SqlTypes.LEFT_PAREN) { + return SqlCustomSpacingBuilder.nonSpacing + } + if (childBlock2 != null) { + if (childBlock2.node.elementType == SqlTypes.RIGHT_PAREN) { + return SqlCustomSpacingBuilder.nonSpacing + } + return when (childBlock1) { + is SqlElCommaBlock -> SqlCustomSpacingBuilder.normalSpacing + is SqlElSymbolBlock -> SqlCustomSpacingBuilder.nonSpacing + else -> SqlCustomSpacingBuilder.nonSpacing + } + } + + return SqlCustomSpacingBuilder.normalSpacing + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElPrimaryBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElPrimaryBlock.kt index 16b81e10..f4584860 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElPrimaryBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElPrimaryBlock.kt @@ -18,15 +18,36 @@ package org.domaframework.doma.intellij.formatter.block.expr import com.intellij.formatting.Block import com.intellij.formatting.Spacing import com.intellij.lang.ASTNode +import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.SqlLiteralBlock +import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext +import org.domaframework.doma.intellij.psi.SqlTypes class SqlElPrimaryBlock( node: ASTNode, - context: SqlBlockFormattingContext, + private val context: SqlBlockFormattingContext, ) : SqlExprBlock( node, context, ) { + override fun getBlock(child: ASTNode): SqlBlock = + when (child.elementType) { + SqlTypes.LEFT_PAREN, SqlTypes.RIGHT_PAREN -> + SqlElSymbolBlock(child, context) + + SqlTypes.EL_PRIMARY_EXPR -> + SqlElPrimaryBlock(child, context) + + SqlTypes.COMMA -> + SqlElCommaBlock(child, context) + + SqlTypes.EL_NUMBER, SqlTypes.EL_STRING, SqlTypes.BOOLEAN, SqlTypes.EL_NULL -> + SqlLiteralBlock(child, context) + + else -> SqlUnknownBlock(child, context) + } + override fun getSpacing( child1: Block?, child2: Block, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElStaticFieldAccessBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElStaticFieldAccessBlock.kt index d57c3308..c64f6e1a 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElStaticFieldAccessBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElStaticFieldAccessBlock.kt @@ -22,7 +22,6 @@ import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.util.PsiTreeUtil import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext import org.domaframework.doma.intellij.psi.SqlElClass @@ -56,7 +55,7 @@ class SqlElStaticFieldAccessBlock( SqlElIdentifierBlock(child, context) SqlTypes.EL_PARAMETERS -> - SqlParallelListBlock(child, context) + SqlElParametersBlock(child, context) else -> SqlUnknownBlock(child, context) } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlExprBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlExprBlock.kt index ffe814b3..deec71a9 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlExprBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlExprBlock.kt @@ -21,6 +21,7 @@ import com.intellij.lang.ASTNode import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext abstract class SqlExprBlock( @@ -39,8 +40,7 @@ abstract class SqlExprBlock( var child = node.firstChildNode while (child != null) { if (child !is PsiWhiteSpace) { - val block = getBlock(child) - blocks.add(block) + blocks.add(getBlock(child)) } child = child.treeNext } @@ -50,5 +50,5 @@ abstract class SqlExprBlock( override fun getSpacing( child1: Block?, child2: Block, - ): Spacing? = null + ): Spacing? = SqlCustomSpacingBuilder.nonSpacing } diff --git a/src/test/testData/sql/formatter/Select.sql b/src/test/testData/sql/formatter/Select.sql index 5f6fb7ee..d4b88108 100644 --- a/src/test/testData/sql/formatter/Select.sql +++ b/src/test/testData/sql/formatter/Select.sql @@ -26,7 +26,7 @@ AS nearest WHERE p.TYPE = 'Star' -- Line3 OR ( p.flags & FPHOTOFLAGS('EDGE') = 0 AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20) - /*%if status == 2 */ + /*%if status == 2 && employee.employeeParams(1,null, true ,"A") */ -- Line4 and u.propermotion > 2.0 /** And Group */ diff --git a/src/test/testData/sql/formatter/Select_format.sql b/src/test/testData/sql/formatter/Select_format.sql index 8f173d69..63e66d3a 100644 --- a/src/test/testData/sql/formatter/Select_format.sql +++ b/src/test/testData/sql/formatter/Select_format.sql @@ -30,7 +30,7 @@ SELECT COUNT(DISTINCT x) AS count_x -- Line3 OR (p.flags & FPHOTOFLAGS('EDGE') = 0 AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20) - /*%if status == 2 */ + /*%if status == 2 && employee.employeeParams(1, null, true, "A") */ -- Line4 AND u.propermotion > 2.0 /** And Group */ diff --git a/src/test/testData/sql/formatter/StaticFieldAccess.sql b/src/test/testData/sql/formatter/StaticFieldAccess.sql index c2dbe38b..22fa6528 100644 --- a/src/test/testData/sql/formatter/StaticFieldAccess.sql +++ b/src/test/testData/sql/formatter/StaticFieldAccess.sql @@ -2,4 +2,4 @@ SELECT * FROM configurations WHERE config_key = /*@ com.example.Constants @CONFIG_KEY */'system.timeout' - AND value = /*@ com.example.Utils @ util. getDefaultValue () */'30' \ No newline at end of file + AND value = /*@ com.example.Utils @ util. getDefaultValue (0,null,true , "0") */'30' \ No newline at end of file diff --git a/src/test/testData/sql/formatter/StaticFieldAccess_format.sql b/src/test/testData/sql/formatter/StaticFieldAccess_format.sql index e4022d99..cf476141 100644 --- a/src/test/testData/sql/formatter/StaticFieldAccess_format.sql +++ b/src/test/testData/sql/formatter/StaticFieldAccess_format.sql @@ -1,4 +1,4 @@ SELECT * FROM configurations WHERE config_key = /* @com.example.Constants@CONFIG_KEY */'system.timeout' - AND value = /* @com.example.Utils@util.getDefaultValue() */'30' + AND value = /* @com.example.Utils@util.getDefaultValue(0, null, true, "0") */'30'