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 @@ -16,13 +16,17 @@
package org.domaframework.doma.intellij.formatter

import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.SqlCommentBlock
import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock

open class SqlBlockBuilder {
private val groupTopNodeIndexHistory = mutableListOf<Pair<Int, SqlBlock>>()

private val commentBlocks = mutableListOf<SqlBlock>()
private val commentBlocks = mutableListOf<SqlCommentBlock>()

private val conditionOrLoopBlocks = mutableListOf<SqlElConditionLoopCommentBlock>()

fun getGroupTopNodeIndexHistory(): List<Pair<Int, SqlBlock>> = groupTopNodeIndexHistory

Expand All @@ -32,7 +36,7 @@ open class SqlBlockBuilder {
groupTopNodeIndexHistory.add(block)
}

fun addCommentBlock(block: SqlBlock) {
fun addCommentBlock(block: SqlCommentBlock) {
commentBlocks.add(block)
}

Expand All @@ -41,23 +45,28 @@ open class SqlBlockBuilder {
var index = 0
commentBlocks.forEach { block ->
if (block !is SqlElBlockCommentBlock) {
val indentLen =
if (index == 0 &&
baseIndent.parentBlock is SqlSubGroupBlock &&
baseIndent.parentBlock?.childBlocks?.size == 1
) {
1
} else {
baseIndent.indent.indentLen
}
block.indent.indentLevel = IndentType.NONE
block.indent.indentLen = indentLen
block.indent.groupIndentLen = 0
if (index == 0 &&
baseIndent.parentBlock is SqlSubGroupBlock &&
baseIndent.parentBlock?.childBlocks?.size == 1
) {
block.indent.indentLevel = IndentType.NONE
block.indent.indentLen = 1
block.indent.groupIndentLen = 0
} else {
block.setParentGroupBlock(baseIndent)
}
index++
}
}
commentBlocks.clear()
}
if (conditionOrLoopBlocks.isNotEmpty()) {
conditionOrLoopBlocks.forEach { block ->
if (block.parentBlock == null) {
block.setParentGroupBlock(baseIndent)
}
}
}
}

fun getLastGroupTopNodeIndexHistory(): Pair<Int, SqlBlock>? = groupTopNodeIndexHistory.lastOrNull()
Expand All @@ -76,8 +85,23 @@ open class SqlBlockBuilder {
).clear()
}

fun getGroupTopNodeIndexByIndentType(indentType: IndentType): Int =
fun getGroupTopNodeIndex(condition: (SqlBlock) -> Boolean): Int =
groupTopNodeIndexHistory.indexOfLast {
it.second.indent.indentLevel == indentType
condition(it.second)
}

fun getConditionOrLoopBlocksLast(): SqlElConditionLoopCommentBlock? = conditionOrLoopBlocks.lastOrNull()

fun addConditionOrLoopBlock(block: SqlElConditionLoopCommentBlock) {
if (!block.conditionType.isInvalid() && !block.conditionType.isEnd()
) {
conditionOrLoopBlocks.add(block)
}
}

fun removeConditionOrLoopBlockLast() {
if (conditionOrLoopBlocks.isNotEmpty()) {
conditionOrLoopBlocks.removeLast()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SqlBlockUtil(
IndentType.INLINE -> {
if (!SqlKeywordUtil.isSetLineKeyword(
child.text,
lastGroupBlock?.node?.text ?: "",
lastGroupBlock?.getNodeText() ?: "",
)
) {
return SqlInlineGroupBlock(child, wrap, alignment, spacingBuilder)
Expand Down Expand Up @@ -152,8 +152,8 @@ class SqlBlockUtil(
is SqlKeywordGroupBlock -> {
val lastKeyword =
lastGroup.childBlocks
.lastOrNull { SqlKeywordUtil.isOptionSqlKeyword(it.node.text) }
if (lastKeyword != null && lastKeyword.node.text.lowercase() == "in") {
.lastOrNull { SqlKeywordUtil.isOptionSqlKeyword(it.getNodeText()) }
if (lastKeyword != null && lastKeyword.getNodeText().lowercase() == "in") {
return SqlParallelListBlock(child, wrap, alignment, spacingBuilder)
}
if (lastGroup is SqlCreateKeywordGroupBlock) {
Expand Down Expand Up @@ -213,7 +213,7 @@ class SqlBlockUtil(
when (lastGroup) {
is SqlKeywordGroupBlock -> {
when {
SqlKeywordUtil.isBeforeTableKeyword(lastGroup.node.text) ->
SqlKeywordUtil.isBeforeTableKeyword(lastGroup.getNodeText()) ->
SqlTableBlock(
child,
wrap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class SqlCustomSpacingBuilder {
null -> return nonSpacing
is SqlWhitespaceBlock -> {
val indentLen: Int = child2.indent.indentLen
val afterNewLine = child1.node.text.substringAfterLast("\n", "")
if (child1.node.text.contains("\n")) {
val afterNewLine = child1.getNodeText().substringAfterLast("\n", "")
if (child1.getNodeText().contains("\n")) {
val currentIndent = afterNewLine.length
val newIndent =
if (currentIndent != indentLen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
.spacing(0, 0, 0, false, 0)
.around(SqlTypes.WORD)
.spacing(1, 1, 0, false, 0)
.around(SqlTypes.NUMBER)
.spacing(1, 1, 0, false, 0)
.before(SqlTypes.RIGHT_PAREN)
.spacing(0, 0, 0, false, 0)
.around(SqlTypes.PLUS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ open class SqlBlock(
childBlocks.add(childBlock)
}

fun getNodeText() = node.text.lowercase()

public override fun buildChildren(): MutableList<AbstractBlock> {
if (isLeaf || !isEnableFormat()) return mutableListOf()

Expand All @@ -114,12 +116,18 @@ open class SqlBlock(
}
}
prevNonWhiteSpaceNode = child
updateSearchKeywordLevelHistory(childBlock, child)
setRightSpace(childBlock)
blocks.add(childBlock)
if (childBlock is SqlCommentBlock) {
blockBuilder.addCommentBlock(childBlock)
when (childBlock) {
is SqlElConditionLoopCommentBlock ->
blockBuilder.addConditionOrLoopBlock(
childBlock,
)

else -> blockBuilder.addCommentBlock(childBlock)
}
}
updateSearchKeywordLevelHistory(childBlock, child)
blocks.add(childBlock)
} else {
if (lastBlock !is SqlLineCommentBlock) {
blocks.add(
Expand Down Expand Up @@ -160,24 +168,19 @@ open class SqlBlock(
) ||
(childBlock is SqlElConditionLoopCommentBlock)

private fun setRightSpace(currentBlock: SqlBlock?) {
val rightBlock = currentBlock as? SqlRightPatternBlock
rightBlock?.enableLastRight()
}

private fun isNewGroup(childBlock: SqlBlock): Boolean {
val isNewGroupType = childBlock.indent.indentLevel.isNewLineGroup()
val lastGroup = blockBuilder.getLastGroupTopNodeIndexHistory()?.second
val lastKeywordText =
if (lastGroup?.indent?.indentLevel == IndentType.JOIN) {
lastGroup.node.text
lastGroup.getNodeText()
} else {
getLastGroupKeywordText(lastGroup)
}

val isSetLineGroup =
SqlKeywordUtil.isSetLineKeyword(
childBlock.node.text,
childBlock.getNodeText(),
lastKeywordText,
)

Expand All @@ -201,7 +204,7 @@ open class SqlBlock(
val isNewGroupType = childBlock.indent.indentLevel.isNewLineGroup()
val lastKeywordText =
if (lastGroup?.indent?.indentLevel == IndentType.JOIN) {
lastGroup.node.text
lastGroup.getNodeText()
} else {
getLastGroupKeywordText(lastGroup)
}
Expand Down Expand Up @@ -229,7 +232,7 @@ open class SqlBlock(
?.childBlocks
?.lastOrNull { it.node.elementType == SqlTypes.KEYWORD }
?.node
?.text ?: lastGroup?.node?.text ?: ""
?.text ?: lastGroup?.getNodeText() ?: ""

protected open fun updateSearchKeywordLevelHistory(
childBlock: SqlBlock,
Expand All @@ -255,11 +258,36 @@ open class SqlBlock(
return@setParentGroups lastGroupBlock
}
} else if (lastIndentLevel == childBlock.indent.indentLevel) {
blockBuilder.removeLastGroupTopNodeIndexHistory()
setParentGroups(
childBlock,
) { history ->
return@setParentGroups lastGroupBlock.parentBlock
// The AND following an OR will be a child of OR unless surrounded by a subgroup
if (childBlock.getNodeText() == "and" && lastGroupBlock.getNodeText() == "or") {
setParentGroups(
childBlock,
) { history ->
return@setParentGroups lastGroupBlock
}
} else {
if (childBlock.getNodeText() == "or" &&
lastGroupBlock.getNodeText() == "and" &&
lastGroupBlock.parentBlock?.getNodeText() == "or"
) {
val orParentIndex =
blockBuilder.getGroupTopNodeIndex { block ->
block is SqlKeywordGroupBlock && block.getNodeText() == "or"
}
blockBuilder.clearSubListGroupTopNodeIndexHistory(orParentIndex)
setParentGroups(
childBlock,
) { history ->
return@setParentGroups history.lastOrNull()?.second
}
} else {
blockBuilder.removeLastGroupTopNodeIndexHistory()
setParentGroups(
childBlock,
) { history ->
return@setParentGroups lastGroupBlock.parentBlock
}
}
}
} else if (lastIndentLevel < childBlock.indent.indentLevel) {
setParentGroups(
Expand Down Expand Up @@ -300,6 +328,7 @@ open class SqlBlock(
return@setParentGroups lastGroupBlock.parentBlock
}
}

else -> {
setParentGroups(
childBlock,
Expand All @@ -322,7 +351,9 @@ open class SqlBlock(
is SqlInlineSecondGroupBlock -> {
if (childBlock.isEndCase) {
val inlineIndex =
blockBuilder.getGroupTopNodeIndexByIndentType(IndentType.INLINE)
blockBuilder.getGroupTopNodeIndex { block ->
block.indent.indentLevel == IndentType.INLINE
}
if (inlineIndex >= 0) {
setParentGroups(
childBlock,
Expand Down Expand Up @@ -357,8 +388,9 @@ open class SqlBlock(
if (parentGroupBlock is SqlColumnDefinitionRawGroupBlock &&
parentGroupBlock.columnName != ","
) {
parentGroupBlock.columnName = childBlock.node.text
val columnDefinition = parentGroupBlock.parentBlock as? SqlColumnDefinitionGroupBlock
parentGroupBlock.columnName = childBlock.getNodeText()
val columnDefinition =
parentGroupBlock.parentBlock as? SqlColumnDefinitionGroupBlock
if (columnDefinition != null && columnDefinition.alignmentColumnName.length < parentGroupBlock.columnName.length) {
columnDefinition.alignmentColumnName = parentGroupBlock.columnName
}
Expand All @@ -385,7 +417,12 @@ open class SqlBlock(
setParentGroups(
childBlock,
) { history ->
return@setParentGroups history.last().second
if (childBlock.conditionType.isEnd()) {
val lastConditionLoopCommentBlock = blockBuilder.getConditionOrLoopBlocksLast()
blockBuilder.removeConditionOrLoopBlockLast()
return@setParentGroups lastConditionLoopCommentBlock
}
return@setParentGroups null
}
}

Expand All @@ -406,7 +443,10 @@ open class SqlBlock(
}

is SqlRightPatternBlock -> {
val paramIndex = blockBuilder.getGroupTopNodeIndexByIndentType(IndentType.PARAM)
val paramIndex =
blockBuilder.getGroupTopNodeIndex { block ->
block.indent.indentLevel == IndentType.PARAM
}
if (paramIndex >= 0) {
setParentGroups(
childBlock,
Expand All @@ -417,7 +457,10 @@ open class SqlBlock(
return
}

val leftIndex = blockBuilder.getGroupTopNodeIndexByIndentType(IndentType.SUB)
val leftIndex =
blockBuilder.getGroupTopNodeIndex { block ->
block.indent.indentLevel == IndentType.SUB
}
if (leftIndex >= 0) {
setParentGroups(
childBlock,
Expand Down Expand Up @@ -472,7 +515,14 @@ open class SqlBlock(
) {
val parentGroup =
getParentGroup(blockBuilder.getGroupTopNodeIndexHistory() as MutableList<Pair<Int, SqlBlock>>)
childBlock.setParentGroupBlock(parentGroup)

// // The parent block for SqlElConditionLoopCommentBlock will be set later
if (childBlock !is SqlElConditionLoopCommentBlock ||
childBlock.conditionType.isEnd()
) {
childBlock.setParentGroupBlock(parentGroup)
}

if (isNewGroup(childBlock) ||
(childBlock is SqlSubGroupBlock) ||
childBlock is SqlViewGroupBlock ||
Expand Down Expand Up @@ -504,8 +554,20 @@ open class SqlBlock(
return blockUtil.getSubGroupBlock(lastGroup, child)
}

SqlTypes.OTHER -> return SqlOtherBlock(child, wrap, alignment, spacingBuilder, blockBuilder.getLastGroup())
SqlTypes.RIGHT_PAREN -> return SqlRightPatternBlock(child, wrap, alignment, spacingBuilder)
SqlTypes.OTHER -> return SqlOtherBlock(
child,
wrap,
alignment,
spacingBuilder,
blockBuilder.getLastGroup(),
)

SqlTypes.RIGHT_PAREN -> return SqlRightPatternBlock(
child,
wrap,
alignment,
spacingBuilder,
)

SqlTypes.COMMA -> {
return blockUtil.getCommaGroupBlock(lastGroup, child)
Expand Down Expand Up @@ -575,7 +637,7 @@ open class SqlBlock(
}

// Do not leave a space after the comment block of the bind variable
if (child1 is SqlElBlockCommentBlock && child2 !is SqlCommentBlock) {
if (child1 is SqlElBlockCommentBlock && child1 !is SqlElConditionLoopCommentBlock && child2 !is SqlCommentBlock) {
return SqlCustomSpacingBuilder.nonSpacing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SqlColumnBlock(
if (parentGroupDefinition == null) return 1

val groupMaxAlimentLen = parentGroupDefinition.alignmentColumnName.length
val diffColumnName = groupMaxAlimentLen.minus(node.text.length)
val diffColumnName = groupMaxAlimentLen.minus(getNodeText().length)
return diffColumnName.plus(1)
}
return 1
Expand Down
Loading
Loading