Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,39 @@ 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) return SqlCustomSpacingBuilder.nonSpacing
if (childBlock2 != null) {
if (childBlock2.node.elementType == SqlTypes.RIGHT_PAREN) {
return SqlCustomSpacingBuilder.nonSpacing
}
return when (childBlock1) {
is SqlElSymbolBlock -> SqlCustomSpacingBuilder.nonSpacing
is SqlElCommaBlock -> SqlCustomSpacingBuilder.normalSpacing
else -> return SqlCustomSpacingBuilder.normalSpacing
}
}

return SqlCustomSpacingBuilder.normalSpacing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,35 @@ 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.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_PRIMARY_EXPR, SqlTypes.EL_NUMBER, SqlTypes.EL_STRING, SqlTypes.BOOLEAN, SqlTypes.EL_NULL ->
SqlElPrimaryBlock(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 @@ -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/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) */'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) */'30'
Loading