Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ import org.domaframework.doma.intellij.extension.psi.isDataType
import org.domaframework.doma.intellij.extension.psi.isDomain
import org.domaframework.doma.intellij.extension.psi.isEntity
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.comma.SqlCommaBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import kotlin.reflect.KClass

object TypeUtil {
private val TOP_LEVEL_EXPECTED_TYPES =
listOf(
SqlSubGroupBlock::class,
SqlCommaBlock::class,
SqlWithQuerySubGroupBlock::class,
SqlCreateViewGroupBlock::class,
)

/**
* Unwraps the type parameter from Optional if present, otherwise returns the original type.
*/
Expand Down Expand Up @@ -118,6 +130,8 @@ object TypeUtil {
return PsiTypeChecker.isBaseClassType(type) || DomaClassName.isOptionalWrapperType(type.canonicalText)
}

fun isTopLevelExpectedType(childBlock: SqlBlock?): Boolean = isExpectedClassType(TOP_LEVEL_EXPECTED_TYPES, childBlock)

/**
* Determines whether the specified class instance matches.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ open class SqlBlock(
protected fun isConditionLoopDirectiveRegisteredBeforeParent(): Boolean {
val firstPrevBlock = (prevBlocks.lastOrNull() as? SqlElConditionLoopCommentBlock)
parentBlock?.let { parent ->
return firstPrevBlock != null &&

return (childBlocks.firstOrNull() as? SqlElConditionLoopCommentBlock)?.isBeforeParentBlock() == true ||
firstPrevBlock != null &&
firstPrevBlock.conditionEnd != null &&
firstPrevBlock.node.startOffset > parent.node.startOffset
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import com.intellij.formatting.Wrap
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.formatter.common.AbstractBlock
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType
import com.intellij.psi.util.nextLeaf
import com.intellij.psi.util.nextLeafs
import org.domaframework.doma.intellij.common.util.TypeUtil
import org.domaframework.doma.intellij.formatter.block.comma.SqlCommaBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
Expand Down Expand Up @@ -62,7 +58,6 @@ import org.domaframework.doma.intellij.formatter.block.other.SqlEscapeBlock
import org.domaframework.doma.intellij.formatter.block.other.SqlOtherBlock
import org.domaframework.doma.intellij.formatter.block.word.SqlAliasBlock
import org.domaframework.doma.intellij.formatter.block.word.SqlArrayWordBlock
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
Expand Down Expand Up @@ -161,9 +156,16 @@ open class SqlFileBlock(
formatMode,
)
val lastGroup = blockBuilder.getLastGroupTopNodeIndexHistory()

val lastGroupFilteredDirective = blockBuilder.getLastGroupFilterDirective()
return when (child.elementType) {
SqlTypes.KEYWORD -> {
if (blockUtil.hasEscapeBeforeWhiteSpace(blocks.lastOrNull() as? SqlBlock?, child)) {
return SqlWordBlock(
child,
defaultFormatCtx,
)
}
return blockUtil.getKeywordBlock(
child,
blockBuilder.getLastGroupTopNodeIndexHistory(),
Expand Down Expand Up @@ -226,21 +228,16 @@ open class SqlFileBlock(
}

SqlTypes.FUNCTION_NAME -> {
val notWhiteSpaceElement =
child.psi.nextLeafs
.takeWhile { it is PsiWhiteSpace }
.lastOrNull()
?.nextLeaf(true)
if (notWhiteSpaceElement?.elementType == SqlTypes.LEFT_PAREN ||
PsiTreeUtil.nextLeaf(child.psi)?.elementType == SqlTypes.LEFT_PAREN
) {
return SqlFunctionGroupBlock(child, defaultFormatCtx)
val block = blockUtil.getFunctionName(child, defaultFormatCtx)
if (block != null) {
return block
}
// If it is not followed by a left parenthesis, treat it as a word block
return if (lastGroup is SqlWithQueryGroupBlock) {
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
} else {
blockUtil.getWordBlock(lastGroup, child)
}
return SqlKeywordBlock(
child,
IndentType.ATTACHED,
defaultFormatCtx,
)
}

SqlTypes.WORD -> {
Expand Down Expand Up @@ -554,7 +551,19 @@ open class SqlFileBlock(
SqlCustomSpacingBuilder.nonSpacing
}

else -> SqlCustomSpacingBuilder.normalSpacing
is SqlSubGroupBlock -> {
val includeSpaceRight = childBlock1.endPatternBlock?.isPreSpaceRight()

if (includeSpaceRight == false) {
SqlCustomSpacingBuilder.nonSpacing
} else {
SqlCustomSpacingBuilder.normalSpacing
}
}

else -> {
SqlCustomSpacingBuilder.normalSpacing
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block
import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictExpressionSubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
Expand All @@ -26,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
Expand Down Expand Up @@ -60,6 +62,9 @@ open class SqlRightPatternBlock(
}

private var preSpaceRight = false

fun isPreSpaceRight() = preSpaceRight

var lineBreakAndSpacingType: LineBreakAndSpacingType = LineBreakAndSpacingType.NONE

companion object {
Expand All @@ -77,7 +82,6 @@ open class SqlRightPatternBlock(
SqlInsertColumnGroupBlock::class,
SqlWithQuerySubGroupBlock::class,
SqlConflictExpressionSubGroupBlock::class,
SqlConditionalExpressionGroupBlock::class,
)

val NEW_LINE_EXPECTED_TYPES =
Expand All @@ -103,11 +107,21 @@ open class SqlRightPatternBlock(
*/
private fun enableLastRight() {
parentBlock?.let { parent ->
val isFirstChildQuery =
parent.childBlocks.firstOrNull {
it !is SqlCommentBlock
} is SqlTopQueryGroupBlock
// Check if parent is in the notInsertSpaceClassList
if (isExpectedClassType(NOT_INDENT_EXPECTED_TYPES, parent)) {
preSpaceRight = false
return
}

if (parent is SqlConditionalExpressionGroupBlock) {
preSpaceRight = isFirstChildQuery
return
}

if (isExpectedClassType(
INDENT_EXPECTED_TYPES,
parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFieldAccessBlock
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFunctionCallBlock
import org.domaframework.doma.intellij.formatter.block.expr.SqlElStaticFieldAccessBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
Expand Down Expand Up @@ -168,6 +169,7 @@ open class SqlElBlockCommentBlock(
}
}
is SqlValuesGroupBlock -> parent.indent.indentLen
is SqlKeywordGroupBlock -> parent.indent.groupIndentLen.plus(1)
else -> parent.indent.groupIndentLen
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
Expand Down Expand Up @@ -217,9 +218,9 @@ class SqlElConditionLoopCommentBlock(
return lastBlock.indent.indentLen
}
}
return parent.indent.groupIndentLen +
openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP +
if (parent !is SqlWithQueryGroupBlock) 1 else 0
val withQuerySpace = if (parent !is SqlWithQueryGroupBlock) 1 else 0
return parent.indent.groupIndentLen.plus(withQuerySpace) +
openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP
}
else -> return parent.indent.indentLen + openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP
}
Expand Down Expand Up @@ -302,5 +303,6 @@ class SqlElConditionLoopCommentBlock(

private fun shouldNotIndent(parent: SqlSubGroupBlock): Boolean =
TypeUtil.isExpectedClassType(SqlRightPatternBlock.NOT_INDENT_EXPECTED_TYPES, parent) ||
parent is SqlWithCommonTableGroupBlock
parent is SqlWithCommonTableGroupBlock ||
parent is SqlConditionalExpressionGroupBlock
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ class SqlConditionalExpressionGroupBlock(
override fun createBlockIndentLen(): Int =
parentBlock?.let { parent ->
if (parent is SqlElConditionLoopCommentBlock) {
parent.indent.groupIndentLen
val groupIndentLen = parent.indent.groupIndentLen
val grand = parent.parentBlock
val directiveParentTextLen =
if (grand !is SqlElConditionLoopCommentBlock) {
grand
?.getNodeText()
?.length
?.plus(1) ?: 0
} else {
0
}
groupIndentLen + directiveParentTextLen
} else {
parent.indent.groupIndentLen.plus(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,14 @@ abstract class SqlTopQueryGroupBlock(

return parent.indent.indentLen
}

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
if (TypeUtil.isTopLevelExpectedType(lastGroup) &&
lastGroup !is SqlWithQuerySubGroupBlock &&
lastGroup !is SqlCreateViewGroupBlock
) {
return false
}
return super.isSaveSpace(lastGroup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package org.domaframework.doma.intellij.formatter.block.other

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlArrayListGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

class SqlEscapeBlock(
Expand All @@ -34,12 +36,53 @@ class SqlEscapeBlock(
}

override fun createBlockIndentLen(): Int {
val hasEvenEscapeBlocks = parentBlock?.childBlocks?.count { it is SqlEscapeBlock }?.let { it % 2 == 0 } == true
val parentEscapeBlock =
if (parentBlock is SqlElConditionLoopCommentBlock) {
if (parentBlock?.parentBlock is SqlEscapeBlock)1 else 0
} else {
0
}
val prevBlocks = parentBlock?.childBlocks?.count { it is SqlEscapeBlock }?.plus(parentEscapeBlock) ?: 0

val hasEvenEscapeBlocks = prevBlocks.let { it % 2 == 0 } == true
isEndEscape = hasEvenEscapeBlocks || getNodeText() == "]"
return if (isEndEscape) {
0
} else {
1
calculateIndentLen()
}
}

private fun calculateIndentLen(): Int {
parentBlock?.let { parent ->
when (parent) {
is SqlSubQueryGroupBlock -> {
val parentIndentLen = parent.indent.groupIndentLen
val grand = parent.parentBlock
if (grand != null && grand.getNodeText().lowercase() == "create") {
val grandIndentLen = grand.indent.groupIndentLen
return grandIndentLen.plus(parentIndentLen).plus(1)
}
return parentIndentLen.plus(1)
}

is SqlElConditionLoopCommentBlock -> {
return parent.indent.groupIndentLen
}

else -> {
if (isSaveSpace(parentBlock))return parentBlock?.indent?.groupIndentLen ?: 1
return 1
}
}
}
return 1
}

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
if (isEndEscape) {
false
} else {
super.isSaveSpace(lastGroup)
}
}
Loading
Loading