Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,7 +55,7 @@ class SqlElStaticFieldAccessBlock(
SqlElIdentifierBlock(child, context)

SqlTypes.EL_PARAMETERS ->
SqlParallelListBlock(child, context)
SqlElParametersBlock(child, context)

else -> SqlUnknownBlock(child, context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
Expand All @@ -50,5 +50,5 @@ abstract class SqlExprBlock(
override fun getSpacing(
child1: Block?,
child2: Block,
): Spacing? = null
): Spacing? = SqlCustomSpacingBuilder.nonSpacing
}
2 changes: 1 addition & 1 deletion src/test/testData/sql/formatter/Select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/test/testData/sql/formatter/Select_format.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/test/testData/sql/formatter/StaticFieldAccess.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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'
AND value = /*@ com.example.Utils @ util. getDefaultValue (0,null,true , "0") */'30'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SELECT *
FROM configurations
WHERE config_key = /* @com.example.Constants@CONFIG_KEY */'system.timeout'
AND value = /* @[email protected]() */'30'
AND value = /* @[email protected](0, null, true, "0") */'30'
Loading