From 9f137e74130dbc2f61f7faeff1ebf9535f0782dc Mon Sep 17 00:00:00 2001 From: xterao Date: Fri, 11 Apr 2025 18:40:16 +0900 Subject: [PATCH 1/5] Fix And Group And Or Group Indent --- .../intellij/formatter/SqlBlockBuilder.kt | 4 +- .../doma/intellij/formatter/SqlBlockUtil.kt | 8 +-- .../formatter/SqlCustomSpacingBuilder.kt | 4 +- .../doma/intellij/formatter/block/SqlBlock.kt | 59 +++++++++++++++---- .../formatter/block/SqlColumnBlock.kt | 2 +- .../intellij/formatter/block/SqlCommaBlock.kt | 8 ++- .../formatter/block/SqlKeywordBlock.kt | 8 +-- .../intellij/formatter/block/SqlWordBlock.kt | 4 +- .../expr/SqlElConditionLoopCommentBlock.kt | 6 +- .../group/SqlColumnDefinitionRawGroupBlock.kt | 2 +- .../keyword/SqlCreateKeywordGroupBlock.kt | 2 +- .../group/keyword/SqlInlineGroupBlock.kt | 4 +- .../keyword/SqlInlineSecondGroupBlock.kt | 4 +- .../keyword/SqlInsertKeywordGroupBlock.kt | 2 +- .../block/group/keyword/SqlJoinGroupBlock.kt | 2 +- .../group/keyword/SqlKeywordGroupBlock.kt | 54 ++++++++++------- .../keyword/SqlUpdateKeywordGroupBlock.kt | 6 +- .../group/subgroup/SqlColumnGroupBlock.kt | 4 +- .../subgroup/SqlInsertColumnGroupBlock.kt | 8 +-- .../block/group/subgroup/SqlSubGroupBlock.kt | 2 +- .../group/subgroup/SqlSubQueryGroupBlock.kt | 6 +- .../subgroup/SqlUpdateColumnGroupBlock.kt | 8 +-- .../subgroup/SqlUpdateValueGroupBlock.kt | 2 +- .../block/group/subgroup/SqlViewGroupBlock.kt | 2 +- 24 files changed, 129 insertions(+), 82 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt index d9ad3434..7341adff 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt @@ -76,8 +76,8 @@ 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) } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt index d26f6d09..9824ec6f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt @@ -110,7 +110,7 @@ class SqlBlockUtil( IndentType.INLINE -> { if (!SqlKeywordUtil.isSetLineKeyword( child.text, - lastGroupBlock?.node?.text ?: "", + lastGroupBlock?.getNodeText() ?: "", ) ) { return SqlInlineGroupBlock(child, wrap, alignment, spacingBuilder) @@ -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) { @@ -213,7 +213,7 @@ class SqlBlockUtil( when (lastGroup) { is SqlKeywordGroupBlock -> { when { - SqlKeywordUtil.isBeforeTableKeyword(lastGroup.node.text) -> + SqlKeywordUtil.isBeforeTableKeyword(lastGroup.getNodeText()) -> SqlTableBlock( child, wrap, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt index 324f1d0c..825c4b51 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt @@ -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) { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt index ed40be7c..c3f2863a 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt @@ -93,6 +93,8 @@ open class SqlBlock( childBlocks.add(childBlock) } + fun getNodeText() = node.text.lowercase() + public override fun buildChildren(): MutableList { if (isLeaf || !isEnableFormat()) return mutableListOf() @@ -170,14 +172,14 @@ open class SqlBlock( 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, ) @@ -201,7 +203,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) } @@ -229,7 +231,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, @@ -255,11 +257,20 @@ open class SqlBlock( return@setParentGroups lastGroupBlock } } else if (lastIndentLevel == childBlock.indent.indentLevel) { - blockBuilder.removeLastGroupTopNodeIndexHistory() + if (!(lastGroupBlock.getNodeText() == "or" && childBlock.getNodeText() == "and")) { + blockBuilder.removeLastGroupTopNodeIndexHistory() + } + if (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 lastGroupBlock.parentBlock + return@setParentGroups history.lastOrNull()?.second } } else if (lastIndentLevel < childBlock.indent.indentLevel) { setParentGroups( @@ -300,6 +311,7 @@ open class SqlBlock( return@setParentGroups lastGroupBlock.parentBlock } } + else -> { setParentGroups( childBlock, @@ -322,7 +334,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, @@ -357,8 +371,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 } @@ -406,7 +421,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, @@ -417,7 +435,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, @@ -504,8 +525,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) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt index 20cfd46b..9752d372 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt @@ -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 diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt index 3adfdd3c..7430368d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt @@ -54,7 +54,7 @@ open class SqlCommaBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.COMMA indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() @@ -85,7 +85,7 @@ open class SqlCommaBlock( if (grand is SqlColumnGroupBlock) { val grandIndentLen = grand.indent.groupIndentLen var prevTextLen = 1 - parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.node.text.length) } + parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.getNodeText().length) } return grandIndentLen.plus(prevTextLen).plus(1) } } @@ -97,7 +97,9 @@ open class SqlCommaBlock( .forEach { prev -> prevLen = prevLen.plus( - prev.node.text.length + prev + .getNodeText() + .length .plus(1), ) } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt index b6671d86..17c1551b 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt @@ -48,7 +48,7 @@ open class SqlKeywordBlock( indent.indentLevel = indentLevel indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) // updateParentIndentLen() } @@ -76,15 +76,15 @@ open class SqlKeywordBlock( IndentType.SECOND -> { parentBlock?.let { it.indent.groupIndentLen - .plus(it.node.text.length) - .minus(this.node.text.length) + .plus(it.getNodeText().length) + .minus(this.getNodeText().length) } ?: 1 } IndentType.INLINE_SECOND -> { parentBlock?.let { it.indent.groupIndentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(1) } ?: 1 } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlWordBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlWordBlock.kt index 2ad23efc..fe1a4f9a 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlWordBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlWordBlock.kt @@ -59,7 +59,7 @@ open class SqlWordBlock( is SqlSubQueryGroupBlock -> { val parentIndentLen = it.indent.groupIndentLen val grand = it.parentBlock - if (grand != null && grand.node.text.lowercase() == "create") { + if (grand != null && grand.getNodeText().lowercase() == "create") { val grandIndentLen = grand.indent.groupIndentLen return grandIndentLen.plus(parentIndentLen).plus(1) } @@ -67,7 +67,7 @@ open class SqlWordBlock( } else -> { - val parentLen = it.node.text.length + val parentLen = it.getNodeText().length return it.indent.groupIndentLen.plus(parentLen.plus(1)) } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt index dfd9c436..bd901590 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt @@ -183,7 +183,7 @@ class SqlElConditionLoopCommentBlock( if (grand is SqlColumnGroupBlock) { val grandIndentLen = grand.indent.groupIndentLen var prevTextLen = 1 - parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.node.text.length) } + parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.getNodeText().length) } return grandIndentLen.plus(prevTextLen).plus(1) } } @@ -195,7 +195,9 @@ class SqlElConditionLoopCommentBlock( .forEach { prev -> prevLen = prevLen.plus( - prev.node.text.length + prev + .getNodeText() + .length .plus(1), ) } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt index 28644c94..a1c37908 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt @@ -45,7 +45,7 @@ class SqlColumnDefinitionRawGroupBlock( val defaultOffset = 5 val isFirstColumnRaw = node.elementType != SqlTypes.COMMA - var columnName = node.text + var columnName = getNodeText() override fun setParentGroupBlock(block: SqlBlock?) { super.setParentGroupBlock(block) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlCreateKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlCreateKeywordGroupBlock.kt index 6c7d6d68..f30a7436 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlCreateKeywordGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlCreateKeywordGroupBlock.kt @@ -43,7 +43,7 @@ open class SqlCreateKeywordGroupBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.TOP indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineGroupBlock.kt index e966ac21..095093c9 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineGroupBlock.kt @@ -47,7 +47,7 @@ open class SqlInlineGroupBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.INLINE indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() @@ -62,7 +62,7 @@ open class SqlInlineGroupBlock( override fun createBlockIndentLen(): Int = parentBlock?.let { it.indent.groupIndentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(1) } ?: 1 } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineSecondGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineSecondGroupBlock.kt index b45a817d..635c89bf 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineSecondGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineSecondGroupBlock.kt @@ -36,7 +36,7 @@ open class SqlInlineSecondGroupBlock( alignment, spacingBuilder, ) { - val isEndCase = node.text.lowercase() == "end" + val isEndCase = getNodeText().lowercase() == "end" override val indent = ElementIndent( @@ -63,7 +63,7 @@ open class SqlInlineSecondGroupBlock( it.indent.indentLen } else { it.indent.groupIndentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) } } ?: 1 } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInsertKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInsertKeywordGroupBlock.kt index 91d80e1b..6317b491 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInsertKeywordGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInsertKeywordGroupBlock.kt @@ -40,7 +40,7 @@ open class SqlInsertKeywordGroupBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.TOP indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlJoinGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlJoinGroupBlock.kt index 73487f11..169eb3c5 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlJoinGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlJoinGroupBlock.kt @@ -48,7 +48,7 @@ open class SqlJoinGroupBlock( parentBlock?.childBlocks?.add(this) indent.indentLevel = IndentType.JOIN indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt index ca4d9097..266fdc71 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt @@ -55,7 +55,7 @@ open class SqlKeywordGroupBlock( indent.indentLevel = indentLevel val baseIndentLen = getBaseIndentLen(preChildBlock, block) - indent.groupIndentLen = baseIndentLen.plus(node.text.length) + indent.groupIndentLen = baseIndentLen.plus(getNodeText().length) indent.indentLen = adjustIndentIfFirstChildIsLineComment(baseIndentLen) createGroupIndentLen() } @@ -69,13 +69,13 @@ open class SqlKeywordGroupBlock( } if (preChildBlock != null && preChildBlock.indent.indentLevel == this.indent.indentLevel && - !SqlKeywordUtil.isSetLineKeyword(preChildBlock.node.text, block.node.text) + !SqlKeywordUtil.isSetLineKeyword(preChildBlock.getNodeText(), block.getNodeText()) ) { if (indent.indentLevel == IndentType.SECOND) { - val diffPreBlockTextLen = node.text.length.minus(preChildBlock.node.text.length) + val diffPreBlockTextLen = getNodeText().length.minus(preChildBlock.getNodeText().length) return preChildBlock.indent.indentLen.minus(diffPreBlockTextLen) } else { - val diffPretextLen = node.text.length.minus(preChildBlock.node.text.length) + val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length) return preChildBlock.indent.indentLen.minus(diffPretextLen) } } else { @@ -100,7 +100,7 @@ open class SqlKeywordGroupBlock( if (indent.indentLevel == IndentType.TOP) { return if (it is SqlSubGroupBlock) { return if (it.isFirstLineComment) { - it.indent.groupIndentLen.minus(it.node.text.length) + it.indent.groupIndentLen.minus(it.getNodeText().length) } else { val newIndentLen = baseIndent.minus(1) return if (newIndentLen >= 0) newIndentLen else 0 @@ -135,37 +135,47 @@ open class SqlKeywordGroupBlock( } else { parent.parentBlock?.let { grand -> return if (grand is SqlViewGroupBlock) { - groupLen.minus(this.node.text.length) + groupLen.minus(this.getNodeText().length) } else if (grand is SqlSubGroupBlock) { - groupLen.minus(node.text.length).plus(1) + groupLen.minus(getNodeText().length).plus(1) } else { - groupLen.minus(this.node.text.length) + groupLen.minus(this.getNodeText().length) } - } ?: return groupLen.minus(this.node.text.length) + } ?: return groupLen.minus(this.getNodeText().length) } } ?: return 1 } IndentType.SECOND_OPTION -> { - parentBlock?.let { - val groupLen = it.indent.groupIndentLen.plus(1) - if (it.indent.indentLevel == IndentType.FILE) { + parentBlock?.let { parent -> + val groupLen = parent.indent.groupIndentLen + if (parent.indent.indentLevel == IndentType.FILE) { return 0 } - val subGroupBlock = it.parentBlock as? SqlSubGroupBlock + val subGroupBlock = parent.parentBlock as? SqlSubGroupBlock + // val isSubGroupChildParent = parent.isTopParentInSubGroup() val newIndent = - if (it is SqlSubQueryGroupBlock) { - groupLen - } else if (it is SqlKeywordGroupBlock && subGroupBlock != null && subGroupBlock.isFirstLineComment) { + if (parent is SqlSubQueryGroupBlock) { + return if (getNodeText() == "and") { + groupLen + } else { + groupLen.plus(1) + } + } else if (getNodeText() == "and" && parent.getNodeText() == "or") { + return groupLen.plus(1) + } else if (parent is SqlKeywordGroupBlock && subGroupBlock != null && subGroupBlock.isFirstLineComment) { groupLen } else { var parentLen = 0 - val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } + val keywords = + parent.childBlocks + .dropLast(1) + .takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } - val parentTextLen = it.indent.groupIndentLen.plus(parentLen) - return parentTextLen.minus(node.text.length) + val parentTextLen = parent.indent.groupIndentLen.plus(parentLen) + return parentTextLen.minus(getNodeText().length) } return newIndent } ?: 1 @@ -190,11 +200,11 @@ open class SqlKeywordGroupBlock( var parentLen = 0 val keywords = it.childBlocks.dropLast(1).filter { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } it.indent.groupIndentLen .plus(parentLen) - .minus(node.text.length) + .minus(getNodeText().length) } } ?: 1 return 1 diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt index a5d8b130..0e2df19f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt @@ -40,7 +40,7 @@ open class SqlUpdateKeywordGroupBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.SECOND indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun buildChildren(): MutableList = mutableListOf() @@ -52,8 +52,8 @@ open class SqlUpdateKeywordGroupBlock( if (it.indent.indentLevel == IndentType.SUB) { it.indent.groupIndentLen.plus(1) } else { - val parentTextLen = it.node.text.length - val diffTextLen = parentTextLen.minus(node.text.length) + val parentTextLen = it.getNodeText().length + val diffTextLen = parentTextLen.minus(getNodeText().length) it.indent.indentLen.plus(diffTextLen) } } ?: 0 diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnGroupBlock.kt index 18e9f04e..e1d66bbc 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnGroupBlock.kt @@ -39,7 +39,7 @@ class SqlColumnGroupBlock( alignment, spacingBuilder, ) { - var isFirstColumnGroup = node.text != "," + var isFirstColumnGroup = getNodeText() != "," override val indent = ElementIndent( @@ -63,7 +63,7 @@ class SqlColumnGroupBlock( override fun createBlockIndentLen(): Int = parentBlock?.let { if (it is SqlKeywordGroupBlock) { - val parentIndentLen = it.indent.indentLen.plus(it.node.text.length) + val parentIndentLen = it.indent.indentLen.plus(it.getNodeText().length) val subGroup = it.parentBlock as? SqlSubGroupBlock if (subGroup is SqlSubGroupBlock && !subGroup.isFirstLineComment) { parentIndentLen.plus(3) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlInsertColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlInsertColumnGroupBlock.kt index b9e05849..5e2cf105 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlInsertColumnGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlInsertColumnGroupBlock.kt @@ -57,10 +57,10 @@ class SqlInsertColumnGroupBlock( val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } return it.indent.indentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(1) .plus(parentLen) } @@ -76,11 +76,11 @@ class SqlInsertColumnGroupBlock( val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } it.indent.groupIndentLen = it.indent.indentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(parentLen) } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt index 928bd9e7..7f75e550 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt @@ -52,7 +52,7 @@ abstract class SqlSubGroupBlock( prevChildren = parentBlock?.childBlocks?.toList() indent.indentLevel = indent.indentLevel indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(node.text.length) + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } override fun addChildBlock(childBlock: SqlBlock) { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt index dd09087d..18e81883 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt @@ -56,10 +56,10 @@ open class SqlSubQueryGroupBlock( val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } return it.indent.indentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(2) .plus(parentLen) } else { @@ -68,7 +68,7 @@ open class SqlSubQueryGroupBlock( prevChildren ?.dropLast(1) ?.forEach { prev -> - parentLen = parentLen.plus(prev.node.text.length).plus(1) + parentLen = parentLen.plus(prev.getNodeText().length).plus(1) } } return it.indent.groupIndentLen diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt index d9d37443..3310ac01 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt @@ -58,10 +58,10 @@ class SqlUpdateColumnGroupBlock( val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } return it.indent.indentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(1) .plus(parentLen) } @@ -77,11 +77,11 @@ class SqlUpdateColumnGroupBlock( val keywords = it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.node.text.length).plus(1) + parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } it.indent.groupIndentLen = it.indent.indentLen - .plus(it.node.text.length) + .plus(it.getNodeText().length) .plus(parentLen) } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt index 74e8ef44..8a462545 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt @@ -58,7 +58,7 @@ class SqlUpdateValueGroupBlock( .dropLast(1) .takeWhile { parent.node.elementType == SqlTypes.KEYWORD } return parent.indent.indentLen - .plus(parent.node.text.length) + .plus(parent.getNodeText().length) .plus(3) } // TODO:Customize indentation diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlViewGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlViewGroupBlock.kt index 5a42379c..29f46e1b 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlViewGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlViewGroupBlock.kt @@ -48,7 +48,7 @@ open class SqlViewGroupBlock( super.setParentGroupBlock(block) indent.indentLevel = IndentType.SUB indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = node.text.length + indent.groupIndentLen = getNodeText().length } override fun buildChildren(): MutableList = mutableListOf() From 82c54070db254ed04d154c47144870973e8f9e90 Mon Sep 17 00:00:00 2001 From: xterao Date: Mon, 14 Apr 2025 18:28:13 +0900 Subject: [PATCH 2/5] Add indentation rules for AND/OR operators and indentation conditions for conditional/loop block comments --- .../intellij/formatter/SqlBlockBuilder.kt | 53 ++++++--- .../doma/intellij/formatter/block/SqlBlock.kt | 77 +++++++++---- .../formatter/block/SqlCommentBlock.kt | 14 ++- .../formatter/block/SqlRightPatternBlock.kt | 16 +-- .../block/expr/SqlElBlockCommentBlock.kt | 58 ---------- .../expr/SqlElConditionLoopCommentBlock.kt | 104 ++++++++++++------ .../group/keyword/SqlKeywordGroupBlock.kt | 9 +- .../group/subgroup/SqlSubQueryGroupBlock.kt | 26 ++--- 8 files changed, 204 insertions(+), 153 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt index 7341adff..2d3b0e07 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt @@ -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>() - private val commentBlocks = mutableListOf() + private val commentBlocks = mutableListOf() + + private val conditionOrLoopBlocks = mutableListOf() fun getGroupTopNodeIndexHistory(): List> = groupTopNodeIndexHistory @@ -32,7 +36,7 @@ open class SqlBlockBuilder { groupTopNodeIndexHistory.add(block) } - fun addCommentBlock(block: SqlBlock) { + fun addCommentBlock(block: SqlCommentBlock) { commentBlocks.add(block) } @@ -41,23 +45,29 @@ 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 -> + println("Update ParentGroup:${block.getNodeText()}") + if (block.parentBlock == null) { + block.setParentGroupBlock(baseIndent) + } + } + } } fun getLastGroupTopNodeIndexHistory(): Pair? = groupTopNodeIndexHistory.lastOrNull() @@ -80,4 +90,19 @@ open class SqlBlockBuilder { groupTopNodeIndexHistory.indexOfLast { 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() + } + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt index c3f2863a..f8f61909 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt @@ -116,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( @@ -162,11 +168,6 @@ 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 @@ -257,20 +258,36 @@ open class SqlBlock( return@setParentGroups lastGroupBlock } } else if (lastIndentLevel == childBlock.indent.indentLevel) { - if (!(lastGroupBlock.getNodeText() == "or" && childBlock.getNodeText() == "and")) { - blockBuilder.removeLastGroupTopNodeIndexHistory() - } - if (lastGroupBlock.getNodeText() == "and" && lastGroupBlock.parentBlock?.getNodeText() == "or") { - val orParentIndex = - blockBuilder.getGroupTopNodeIndex { block -> - block is SqlKeywordGroupBlock && block.getNodeText() == "or" + // 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 } - 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( @@ -400,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 } } @@ -493,7 +515,14 @@ open class SqlBlock( ) { val parentGroup = getParentGroup(blockBuilder.getGroupTopNodeIndexHistory() as MutableList>) - 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 || diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt index c1aef0b5..ac98a241 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt @@ -21,6 +21,7 @@ import com.intellij.formatting.Wrap import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.IndentType +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock abstract class SqlCommentBlock( node: ASTNode, @@ -44,11 +45,22 @@ abstract class SqlCommentBlock( override fun setParentGroupBlock(block: SqlBlock?) { super.setParentGroupBlock(block) indent.indentLevel = IndentType.NONE - indent.indentLen = 0 + indent.indentLen = createBlockIndentLen() indent.groupIndentLen = 0 } override fun buildChildren(): MutableList = mutableListOf() override fun isLeaf(): Boolean = true + + override fun createBlockIndentLen(): Int { + parentBlock?.let { parent -> + if (parent.parentBlock !is SqlSubGroupBlock || + parent.parentBlock?.childBlocks?.size != 1 + ) { + return parent.indent.indentLen + } + } + return 1 + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt index 882217d3..e8ad5fc9 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt @@ -48,21 +48,22 @@ open class SqlRightPatternBlock( var preSpaceRight = false fun enableLastRight() { - parentBlock?.let { - if (it.node.treePrev.elementType == SqlTypes.WORD) { + parentBlock?.let { parent -> + + if (parent.node.treePrev.elementType == SqlTypes.WORD) { preSpaceRight = false return } - if (it is SqlInsertColumnGroupBlock) { + if (parent is SqlInsertColumnGroupBlock) { preSpaceRight = false return } - it.parentBlock?.let { + parent.parentBlock?.let { grand -> preSpaceRight = ( - it.indent.indentLevel <= IndentType.SECOND && - it.parentBlock !is SqlInsertKeywordGroupBlock + grand.indent.indentLevel <= IndentType.SECOND && + grand.parentBlock !is SqlInsertKeywordGroupBlock ) || - it.indent.indentLevel == IndentType.JOIN + grand.indent.indentLevel == IndentType.JOIN return } } @@ -81,6 +82,7 @@ open class SqlRightPatternBlock( indent.indentLevel = IndentType.NONE indent.indentLen = createBlockIndentLen() indent.groupIndentLen = indent.indentLen + enableLastRight() } override fun buildChildren(): MutableList = mutableListOf() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt index 8b03e428..23b3c2f2 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt @@ -21,11 +21,8 @@ import com.intellij.formatting.Spacing import com.intellij.formatting.SpacingBuilder import com.intellij.formatting.Wrap import com.intellij.lang.ASTNode -import com.intellij.psi.PsiElement 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 org.domaframework.doma.intellij.formatter.IndentType import org.domaframework.doma.intellij.formatter.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.block.SqlBlock @@ -34,20 +31,8 @@ import org.domaframework.doma.intellij.formatter.block.SqlCommentBlock import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock -import org.domaframework.doma.intellij.psi.SqlElElseifDirective -import org.domaframework.doma.intellij.psi.SqlElForDirective -import org.domaframework.doma.intellij.psi.SqlElIfDirective import org.domaframework.doma.intellij.psi.SqlTypes -enum class SqlDirectiveType { - IF, - ELSEIF, - ELSE, - FOR, - END, - Variable, -} - open class SqlElBlockCommentBlock( node: ASTNode, wrap: Wrap?, @@ -60,8 +45,6 @@ open class SqlElBlockCommentBlock( alignment, spacingBuilder, ) { - // open var isConditionLoopBlock = getConditionOrLoopBlock(node) - override val indent = ElementIndent( IndentType.NONE, @@ -129,47 +112,6 @@ open class SqlElBlockCommentBlock( else -> SqlUnknownBlock(child, wrap, alignment, spacingBuilder) } - private fun getConditionOrLoopBlock(node: ASTNode): Boolean { - val directiveType = - when { - PsiTreeUtil.getChildOfType(node.psi, SqlElIfDirective::class.java) != null -> { - SqlDirectiveType.IF - } - - PsiTreeUtil.getChildOfType(node.psi, SqlElElseifDirective::class.java) != null -> { - SqlDirectiveType.ELSEIF - } - - PsiTreeUtil.getChildOfType(node.psi, SqlElForDirective::class.java) != null -> { - SqlDirectiveType.FOR - } - - PsiTreeUtil.getChildOfType(node.psi, SqlElElseifDirective::class.java) != null -> { - SqlDirectiveType.ELSE - } - - PsiTreeUtil.getChildOfType(node.psi, SqlElForDirective::class.java) != null -> { - SqlDirectiveType.END - } - - else -> { - val children = - PsiTreeUtil - .getChildrenOfType(node.psi, PsiElement::class.java) - ?.firstOrNull { it.elementType == SqlTypes.EL_ELSE || it.elementType == SqlTypes.EL_END } - children?.let { - when (it.elementType) { - SqlTypes.EL_ELSE -> SqlDirectiveType.ELSE - SqlTypes.EL_END -> SqlDirectiveType.END - else -> SqlDirectiveType.Variable - } - } ?: SqlDirectiveType.Variable - } - } - - return directiveType != SqlDirectiveType.Variable - } - private fun createFieldAccessSpacingBuilder(): SqlCustomSpacingBuilder = SqlCustomSpacingBuilder() .withSpacing( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt index bd901590..3d698db3 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt @@ -23,16 +23,24 @@ 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 org.domaframework.doma.intellij.extension.expr.isConditionOrLoopDirective import org.domaframework.doma.intellij.formatter.IndentType import org.domaframework.doma.intellij.formatter.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlBlockCommentBlock +import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlCreateKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInsertKeywordGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock +import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr +import org.domaframework.doma.intellij.psi.SqlElForDirective +import org.domaframework.doma.intellij.psi.SqlElIfDirective import org.domaframework.doma.intellij.psi.SqlTypes class SqlElConditionLoopCommentBlock( @@ -48,6 +56,38 @@ class SqlElConditionLoopCommentBlock( customSpacingBuilder, spacingBuilder, ) { + enum class SqlConditionLoopCommentBlockType { + CONDITION, + LOOP, + END, + UNKNOWN, + ; + + fun isEnd(): Boolean = this == END + + fun isInvalid(): Boolean = this == UNKNOWN + } + + val conditionType: SqlConditionLoopCommentBlockType = initConditionOrLoopType(node) + + private fun initConditionOrLoopType(node: ASTNode): SqlConditionLoopCommentBlockType { + val psi = node.psi + if (psi is SqlCustomElCommentExpr && psi.isConditionOrLoopDirective()) { + if (PsiTreeUtil.getChildOfType(psi, SqlElForDirective::class.java) != null) { + return SqlConditionLoopCommentBlockType.LOOP + } + if (PsiTreeUtil.getChildOfType(psi, SqlElIfDirective::class.java) != null || + psi.findElementAt(2)?.elementType == SqlTypes.EL_ELSE + ) { + return SqlConditionLoopCommentBlockType.CONDITION + } + if (psi.findElementAt(2)?.elementType == SqlTypes.EL_END) { + return SqlConditionLoopCommentBlockType.END + } + } + return SqlConditionLoopCommentBlockType.UNKNOWN + } + override val indent = ElementIndent( IndentType.NONE, @@ -60,6 +100,7 @@ class SqlElConditionLoopCommentBlock( indent.indentLevel = IndentType.NONE indent.indentLen = createBlockIndentLen() indent.groupIndentLen = 0 + println("setParentGroupBlock:${block?.getNodeText()}") } override fun buildChildren(): MutableList { @@ -169,41 +210,38 @@ class SqlElConditionLoopCommentBlock( override fun createBlockIndentLen(): Int { parentBlock?.let { parent -> - if (parent is SqlSubGroupBlock) { - val parentIndentLen = parent.indent.groupIndentLen - val grand = parent.parentBlock - grand?.let { grand -> - if (grand is SqlCreateKeywordGroupBlock) { - val grandIndentLen = grand.indent.groupIndentLen - return grandIndentLen.plus(parentIndentLen).minus(1) - } - if (grand is SqlInsertKeywordGroupBlock) { - return parentIndentLen - } - if (grand is SqlColumnGroupBlock) { - val grandIndentLen = grand.indent.groupIndentLen - var prevTextLen = 1 - parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.getNodeText().length) } - return grandIndentLen.plus(prevTextLen).plus(1) + when (parent) { + is SqlSubGroupBlock -> { + val parentGroupIndentLen = parent.indent.groupIndentLen + val grand = parent.parentBlock + grand?.let { grand -> + if (grand is SqlCreateKeywordGroupBlock) { + val grandIndentLen = grand.indent.groupIndentLen + return grandIndentLen.plus(parentGroupIndentLen).minus(1) + } + if (grand is SqlInsertKeywordGroupBlock) { + return parentGroupIndentLen + } + if (grand is SqlColumnGroupBlock) { + val grandIndentLen = grand.indent.groupIndentLen + var prevTextLen = 1 + parent.prevChildren?.dropLast(1)?.forEach { prev -> + prevTextLen = prevTextLen.plus(prev.getNodeText().length) + } + return grandIndentLen.plus(prevTextLen).plus(1) + } } + return parentGroupIndentLen + } + + is SqlKeywordGroupBlock, is SqlCommaBlock -> { + return parent.indent.indentLen + } + + // Parent of End Block is SqlElConditionLoopCommentBlock + is SqlElConditionLoopCommentBlock -> { + return parent.indent.indentLen } - return parentIndentLen - } else { - var prevLen = 0 - parent.childBlocks - .filter { it.node.elementType == SqlTypes.KEYWORD } - .forEach { prev -> - prevLen = - prevLen.plus( - prev - .getNodeText() - .length - .plus(1), - ) - } - return parent.indent.groupIndentLen - .plus(prevLen) - .plus(1) } } return 1 diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt index 266fdc71..18ada20f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt @@ -153,7 +153,6 @@ open class SqlKeywordGroupBlock( return 0 } val subGroupBlock = parent.parentBlock as? SqlSubGroupBlock - // val isSubGroupChildParent = parent.isTopParentInSubGroup() val newIndent = if (parent is SqlSubQueryGroupBlock) { return if (getNodeText() == "and") { @@ -167,9 +166,13 @@ open class SqlKeywordGroupBlock( groupLen } else { var parentLen = 0 + val removeStartOffsetLess = + parent.childBlocks.dropLast(1).filter { + it.node.startOffset > + parent.node.startOffset + } val keywords = - parent.childBlocks - .dropLast(1) + removeStartOffsetLess .takeWhile { it.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt index 18e81883..faab92e0 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt @@ -21,7 +21,6 @@ import com.intellij.formatting.SpacingBuilder import com.intellij.formatting.Wrap import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock -import org.domaframework.doma.intellij.formatter.IndentType import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock import org.domaframework.doma.intellij.psi.SqlTypes @@ -50,32 +49,33 @@ open class SqlSubQueryGroupBlock( override fun createBlockIndentLen(): Int = 1 private fun createGroupIndentLen(): Int { - parentBlock?.let { - if (it is SqlJoinGroupBlock) { + parentBlock?.let { parent -> + if (parent is SqlJoinGroupBlock) { var parentLen = 0 val keywords = - it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } + parent.childBlocks.dropLast(1).takeWhile { parent.node.elementType == SqlTypes.KEYWORD } keywords.forEach { keyword -> parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) } - return it.indent.indentLen - .plus(it.getNodeText().length) + return parent.indent.indentLen + .plus(parent.getNodeText().length) .plus(2) .plus(parentLen) } else { var parentLen = 0 - if (prevChildren?.findLast { it.indent.indentLevel == IndentType.COMMA } == null) { + val prevBlocks = prevChildren ?.dropLast(1) - ?.forEach { prev -> - parentLen = parentLen.plus(prev.getNodeText().length).plus(1) - } - } - return it.indent.groupIndentLen + ?.filter { it.node.startOffset > parent.node.startOffset } + prevBlocks + ?.forEach { prev -> + parentLen = parentLen.plus(prev.getNodeText().length).plus(1) + } + return parent.indent.groupIndentLen .plus(parentLen) .plus(2) } - return it.indent.groupIndentLen + return parent.indent.groupIndentLen .plus(2) } ?: return 1 } From 3fe85244dffa16457b2f126f1db22b1992d42406 Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 15 Apr 2025 10:48:15 +0900 Subject: [PATCH 3/5] Refactor spacing logic and remove debug print statements in SQL formatting classes --- .../intellij/formatter/SqlBlockBuilder.kt | 1 - .../formatter/SqlCustomSpacingBuilder.kt | 9 +++++++++ .../formatter/SqlFormattingModelBuilder.kt | 2 ++ .../doma/intellij/formatter/block/SqlBlock.kt | 2 +- .../formatter/block/SqlRightPatternBlock.kt | 20 ++++++++++++++++--- .../expr/SqlElConditionLoopCommentBlock.kt | 1 - 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt index 2d3b0e07..0da7da49 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockBuilder.kt @@ -62,7 +62,6 @@ open class SqlBlockBuilder { } if (conditionOrLoopBlocks.isNotEmpty()) { conditionOrLoopBlocks.forEach { block -> - println("Update ParentGroup:${block.getNodeText()}") if (block.parentBlock == null) { block.setParentGroupBlock(baseIndent) } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt index 825c4b51..a57dc2e0 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt @@ -21,6 +21,7 @@ import com.intellij.formatting.Spacing import com.intellij.psi.tree.IElementType import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlColumnBlock +import org.domaframework.doma.intellij.formatter.block.SqlOtherBlock import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock @@ -30,6 +31,7 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTyp import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock +import org.domaframework.doma.intellij.psi.SqlTypes class SqlCustomSpacingBuilder { companion object { @@ -147,4 +149,11 @@ class SqlCustomSpacingBuilder { else -> nonSpacing } } + + fun getSpacingOther(block: SqlOtherBlock): Spacing? = + when { + listOf("=", "-", "+", "*", "/", "%").contains(block.getNodeText()) -> normalSpacing + listOf(SqlTypes.LE, SqlTypes.GE, SqlTypes.LT, SqlTypes.GT).contains(block.node.elementType) -> normalSpacing + else -> nonSpacing + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt index c6e62ed4..fbfb01ff 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt @@ -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) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt index f8f61909..269fef56 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt @@ -637,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 } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt index e8ad5fc9..b6a89a55 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt @@ -23,12 +23,14 @@ import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.IndentType import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInsertKeywordGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlUpdateKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnDefinitionGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlInsertColumnGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock -import org.domaframework.doma.intellij.psi.SqlTypes /** * Parent is always a subclass of a subgroup @@ -49,8 +51,8 @@ open class SqlRightPatternBlock( fun enableLastRight() { parentBlock?.let { parent -> - - if (parent.node.treePrev.elementType == SqlTypes.WORD) { + // TODO:Customize indentation + if (parent is SqlFunctionParamBlock) { preSpaceRight = false return } @@ -58,6 +60,18 @@ open class SqlRightPatternBlock( preSpaceRight = false return } + + if (parent is SqlSubQueryGroupBlock) { + val prevKeywordBlock = + parent.childBlocks + .filter { it.node.startOffset < node.startOffset } + .find { it is SqlKeywordGroupBlock && it.indent.indentLevel == IndentType.TOP } + if (prevKeywordBlock != null) { + preSpaceRight = true + return + } + } + parent.parentBlock?.let { grand -> preSpaceRight = ( grand.indent.indentLevel <= IndentType.SECOND && diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt index 3d698db3..725f9552 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt @@ -100,7 +100,6 @@ class SqlElConditionLoopCommentBlock( indent.indentLevel = IndentType.NONE indent.indentLen = createBlockIndentLen() indent.groupIndentLen = 0 - println("setParentGroupBlock:${block?.getNodeText()}") } override fun buildChildren(): MutableList { From 5d6b60b1de01b9c6bf28b166de91f1eae0866fe1 Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 15 Apr 2025 10:49:07 +0900 Subject: [PATCH 4/5] Add AND and OR groups and conditional comment blocks to Select test cases --- .../sql/formatter/FormattedSelect.sql | 28 +++++++++++-------- src/test/testData/sql/formatter/Select.sql | 21 ++++++++------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/test/testData/sql/formatter/FormattedSelect.sql b/src/test/testData/sql/formatter/FormattedSelect.sql index 78ce90f7..c728a915 100644 --- a/src/test/testData/sql/formatter/FormattedSelect.sql +++ b/src/test/testData/sql/formatter/FormattedSelect.sql @@ -24,23 +24,29 @@ SELECT COUNT(DISTINCT (x)) AND p.plate = s.plate /** Where */ WHERE p.TYPE = DBO.FPHOTOTYPE('Star') - AND (p.flags & DBO.FPHOTOFLAGS('EDGE')) = 0 - AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20 - AND u.propermotion > 2. - /** And Group */ - AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i)) - OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.) ) AS o + OR (p.flags & DBO.FPHOTOFLAGS('EDGE') = 0 + AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20) + /*%if status == 2 */ + AND u.propermotion > 2. + /** And Group */ + AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i)) + OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.) + /*%end*/ ) AS o LEFT OUTER JOIN ( SELECT n.objid , MIN(n.distance) AS nearest FROM neighbors n JOIN phototag x ON n.neighborobjid = x.objid + AND n.neighbormode = DBO.FPHOTOMODE('Primary') + OR n.MODE = /*# "active" */'mode' + AND n.status = 2 + AND n.flag = true WHERE n.TYPE = DBO.FPHOTOTYPE('Star') AND n.MODE = DBO.FPHOTOMODE('Primary') - AND n.neighbormode = DBO.FPHOTOMODE('Primary') - AND (x.TYPE = DBO.FPHOTOTYPE('Star') - OR x.TYPE = DBO.FPHOTOTYPE('Galaxy')) - AND x.modelmag_g BETWEEN 10 AND 21 + OR n.neighbormode = DBO.FPHOTOMODE('Primary') + AND (x.TYPE = DBO.FPHOTOTYPE('Star') + AND x.TYPE = DBO.FPHOTOTYPE('Galaxy')) + OR x.modelmag_g BETWEEN 10 AND 21 GROUP BY n.objid ) AS nbor ON o.objid = nbor.objid - WHERE p.list IN /* params */(1, 2, 3) + WHERE p.params IN /* params */(1, 2, 3) diff --git a/src/test/testData/sql/formatter/Select.sql b/src/test/testData/sql/formatter/Select.sql index d505db2b..c586ef65 100644 --- a/src/test/testData/sql/formatter/Select.sql +++ b/src/test/testData/sql/formatter/Select.sql @@ -25,14 +25,16 @@ SELECT COUNT( DISTINCT (x)),o.* ON p.objid = s.bestobjid AND p.plate = s.plate /** Where */ WHERE p.TYPE = DBO.FPHOTOTYPE('Star') - AND (p.flags & DBO.FPHOTOFLAGS('EDGE')) = 0 + + OR (p.flags & DBO.FPHOTOFLAGS('EDGE') = 0 - AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20 + AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20) +/*%if status == 2 */ AND u.propermotion > 2. /** And Group */ AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i)) - OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.) ) AS o + OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.)/*%end*/) AS o LEFT OUTER JOIN ( SELECT n.objid , MIN(n.distance) AS nearest @@ -40,18 +42,21 @@ SELECT COUNT( DISTINCT (x)),o.* FROM neighbors n JOIN phototag x ON n.neighborobjid = x.objid - + AND n.neighbormode = DBO.FPHOTOMODE('Primary') + OR n.MODE = /*# "active" */'mode' + AND n.status =2 + AND n.flag = true WHERE n.TYPE = DBO.FPHOTOTYPE('Star') AND n.MODE = DBO.FPHOTOMODE('Primary') - AND n.neighbormode = DBO.FPHOTOMODE('Primary') + OR n.neighbormode = DBO.FPHOTOMODE('Primary') AND (x.TYPE = DBO.FPHOTOTYPE('Star') - OR x.TYPE = DBO.FPHOTOTYPE('Galaxy')) - AND x.modelmag_g BETWEEN 10 AND 21 + AND x.TYPE = DBO.FPHOTOTYPE('Galaxy')) + OR x.modelmag_g BETWEEN 10 AND 21 GROUP BY n.objid ) AS nbor ON o.objid = nbor.objid - WHERE p.list IN /* params */(1 + WHERE p.params IN /* params */(1 ,2 ,3) \ No newline at end of file From 956a59f606eff531446c201544a881f4b3302919 Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 15 Apr 2025 11:17:19 +0900 Subject: [PATCH 5/5] Remove unused SqlOtherBlock import and related spacing logic --- .../doma/intellij/formatter/SqlCustomSpacingBuilder.kt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt index a57dc2e0..825c4b51 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt @@ -21,7 +21,6 @@ import com.intellij.formatting.Spacing import com.intellij.psi.tree.IElementType import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlColumnBlock -import org.domaframework.doma.intellij.formatter.block.SqlOtherBlock import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock @@ -31,7 +30,6 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTyp import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock -import org.domaframework.doma.intellij.psi.SqlTypes class SqlCustomSpacingBuilder { companion object { @@ -149,11 +147,4 @@ class SqlCustomSpacingBuilder { else -> nonSpacing } } - - fun getSpacingOther(block: SqlOtherBlock): Spacing? = - when { - listOf("=", "-", "+", "*", "/", "%").contains(block.getNodeText()) -> normalSpacing - listOf(SqlTypes.LE, SqlTypes.GE, SqlTypes.LT, SqlTypes.GT).contains(block.node.elementType) -> normalSpacing - else -> nonSpacing - } }