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 @@ -112,9 +112,9 @@ open class SqlBlock(
*
* @example
* ```sql
* WHERE
* /*%if status == "pending" */
* status = 'pending'
* WHERE -- grand
* /*%if status == "pending" */ -- parent
* status = 'pending' -- child
* ```
*/
protected fun isElementAfterConditionLoopDirective(): Boolean =
Expand All @@ -123,15 +123,27 @@ open class SqlBlock(
(parent.parentBlock is SqlNewGroupBlock || parent.parentBlock is SqlElConditionLoopCommentBlock)
} == true

protected fun isElementAfterConditionLoopEnd(): Boolean =
(
prevBlocks
.lastOrNull()
?.childBlocks
?.firstOrNull() as? SqlElConditionLoopCommentBlock
)?.conditionEnd != null

protected fun isFirstChildConditionLoopDirective(): Boolean = childBlocks.firstOrNull() is SqlElConditionLoopCommentBlock

fun getChildBlocksDropLast(
dropIndex: Int = 1,
skipCommentBlock: Boolean = true,
skipConditionLoopCommentBlock: Boolean = true,
): List<SqlBlock> {
val children = childBlocks.dropLast(dropIndex)
var children = childBlocks.dropLast(dropIndex)
if (skipCommentBlock) {
return children.filter { it !is SqlDefaultCommentBlock }
children = children.filter { it !is SqlDefaultCommentBlock }
}
if (skipConditionLoopCommentBlock) {
children = children.filter { it !is SqlElConditionLoopCommentBlock }
}
return children
}
Expand Down Expand Up @@ -175,7 +187,8 @@ open class SqlBlock(
private fun shouldSaveSpaceForConditionLoop(): Boolean =
isConditionLoopDirectiveRegisteredBeforeParent() ||
isElementAfterConditionLoopDirective() ||
isFirstChildConditionLoopDirective()
isFirstChildConditionLoopDirective() ||
isElementAfterConditionLoopEnd()

private fun shouldSaveSpaceForNewGroup(parent: SqlNewGroupBlock): Boolean {
val prevWord = prevBlocks.lastOrNull { it !is SqlCommentBlock }
Expand All @@ -202,6 +215,9 @@ open class SqlBlock(
lastPrevBlock.node.psi.startOffset > parent.node.psi.startOffset
}

protected fun getLastBlockHasConditionLoopDirective(): SqlElConditionLoopCommentBlock? =
(prevBlocks.lastOrNull()?.childBlocks?.firstOrNull() as? SqlElConditionLoopCommentBlock)

/**
* Creates the indentation length for the block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,17 @@ class SqlElConditionLoopCommentBlock(
is SqlKeywordGroupBlock -> {
// At this point, it's not possible to determine whether the parent keyword group appears before or after this block based solely on the parent-child relationship.
// Therefore, determine the position directly using the text offset.
return if (parent.node.startOffset <
node.startOffset
) {
// The child branch applies in cases where a conditional directive is included as a child of this block.
val questOffset = if (parent is SqlWithQueryGroupBlock) 0 else 1
parent.indent.groupIndentLen
.plus(openConditionLoopDirectiveCount * 2)
.plus(questOffset)
return if (!isBeforeParentBlock()) {
val lastBlockConditionLoopCommentBlock: SqlElConditionLoopCommentBlock? = getLastBlockHasConditionLoopDirective()
if (lastBlockConditionLoopCommentBlock != null && lastBlockConditionLoopCommentBlock.conditionEnd != null) {
lastBlockConditionLoopCommentBlock.indent.indentLen
} else {
// The child branch applies in cases where a conditional directive is included as a child of this block.
val questOffset = if (parent is SqlWithQueryGroupBlock) 0 else 1
parent.indent.groupIndentLen
.plus(openConditionLoopDirectiveCount * 2)
.plus(questOffset)
}
} else {
parent.indent.indentLen.plus(openConditionLoopDirectiveCount * 2)
}
Expand Down Expand Up @@ -285,4 +288,14 @@ class SqlElConditionLoopCommentBlock(
val diffCount = startDirectives.minus(endDirectives)
return if (diffCount > 0) diffCount.minus(1) else 0
}

/**
* Determine if this conditional loop directive block is positioned before its parent block.
*/
fun isBeforeParentBlock(): Boolean {
parentBlock?.let { parent ->
return parent.node.startOffset > node.startOffset
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
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.keyword.option.SqlLateralGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.condition
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.keyword.SqlSecondOptionKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Doma Tools Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.keyword.option

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
import org.domaframework.doma.intellij.psi.SqlTypes

class SqlInGroupBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlKeywordGroupBlock(
node,
IndentType.OPTIONS,
context,
) {
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
super.setParentGroupBlock(lastGroup)
indent.indentLen = createBlockIndentLen()
indent.groupIndentLen = createGroupIndentLen()
}

override fun createBlockIndentLen(): Int {
parentBlock?.let { parent ->
if (parent is SqlElConditionLoopCommentBlock) return parent.indent.groupIndentLen
val prevChildren = this.prevBlocks
val children = prevChildren.filter { it !is SqlDefaultCommentBlock }
val firstChild = children.firstOrNull()
val sumChildren =
if (firstChild is SqlElConditionLoopCommentBlock) {
children.drop(1).dropLastWhile { it == this }
} else {
children
}

val dotCount = sumChildren.count { it.node.elementType == SqlTypes.DOT }
return sumChildren
.sumOf { prev ->
prev
.getChildrenTextLen()
.plus(prev.getNodeText().length.plus(1))
}.minus(dotCount * 2)
.plus(parent.indent.groupIndentLen)
.plus(1)
}
return 0
}

override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
if (lastGroup is SqlElConditionLoopCommentBlock) {
if (lastGroup.conditionType.isElse()) return false
return !lastGroup.isBeforeParentBlock()
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.keyword
package org.domaframework.doma.intellij.formatter.block.group.keyword.option

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.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.keyword
package org.domaframework.doma.intellij.formatter.block.group.keyword.option

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Doma Tools Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.keyword.second

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

class SqlWhereGroupBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlSecondKeywordBlock(node, context) {
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.top

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.SqlFileBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
Expand All @@ -41,8 +42,8 @@ class SqlJoinQueriesGroupBlock(
override fun createBlockIndentLen(): Int {
parentBlock?.let { parent ->
return when (parent) {
is SqlWithQuerySubGroupBlock,
-> parent.indent.groupIndentLen
is SqlFileBlock -> 0
is SqlWithQuerySubGroupBlock -> parent.indent.groupIndentLen
is SqlElConditionLoopCommentBlock -> createIndentLenInConditionLoopDirective(parent)
else -> parent.indent.groupIndentLen.plus(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class SqlTopQueryGroupBlock(
SqlWithQuerySubGroupBlock::class,
SqlElConditionLoopCommentBlock::class,
)
private val offset = 0
private const val OFFSET = 0
}

override fun setParentGroupBlock(lastGroup: SqlBlock?) {
Expand All @@ -55,7 +55,7 @@ abstract class SqlTopQueryGroupBlock(

override fun createBlockIndentLen(): Int {
parentBlock?.let { parent ->
if (parent.indent.indentLevel == IndentType.FILE) return offset
if (parent.indent.indentLevel == IndentType.FILE) return OFFSET
if (parent is SqlElConditionLoopCommentBlock) {
return createIndentLenInConditionLoopDirective(parent)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ abstract class SqlTopQueryGroupBlock(
// align with the indent of that parent's parent
prevGroupBlock?.let { prev ->
if (prev.indent.indentLevel >= indent.indentLevel) {
p.indent.indentLen = prev.parentBlock?.indent?.indentLen ?: offset
p.indent.indentLen = prev.parentBlock?.indent?.indentLen ?: OFFSET
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SqlWithCommonTableGroupBlock(
parentBlock?.let { parent ->
val prevBlock =
parent
.getChildBlocksDropLast()
.getChildBlocksDropLast(skipConditionLoopCommentBlock = false)
.lastOrNull()
return if (prevBlock is SqlElConditionLoopCommentBlock) 4 else 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.common.util.TypeUtil
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
Expand Down Expand Up @@ -122,11 +122,14 @@ abstract class SqlSubGroupBlock(
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
lastGroup?.let { lastBlock ->
if (lastBlock is SqlJoinQueriesGroupBlock) return true
val prevBlock = prevChildren?.dropLast(1)?.lastOrNull()
if (prevBlock is SqlKeywordBlock) {
if (prevBlock.getNodeText() == "in") return false
}
if (lastGroup is SqlInGroupBlock) return false
if (lastGroup is SqlElConditionLoopCommentBlock) return true
val grand = lastBlock.parentBlock
if (grand is SqlElConditionLoopCommentBlock) {
if (grand.conditionType.isElse()) {
return false
}
}
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, lastBlock.parentBlock)
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
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.SqlLateralGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock
Expand Down Expand Up @@ -62,6 +63,7 @@ class SqlBlockRelationBuilder(
SqlInlineSecondGroupBlock::class,
SqlColumnDefinitionRawGroupBlock::class,
SqlLateralGroupBlock::class,
SqlInGroupBlock::class,
)

private val TOP_LEVEL_EXPECTED_TYPES =
Expand Down Expand Up @@ -136,11 +138,27 @@ class SqlBlockRelationBuilder(
val context = SetParentContext(childBlock, blockBuilder)
if (lastGroupBlock is SqlElConditionLoopCommentBlock) {
handleConditionLoopParent(lastGroupBlock, context, childBlock)
} else if (childBlock.indent.indentLevel == IndentType.TOP) {
return
}
if (childBlock.indent.indentLevel == IndentType.TOP) {
handleTopLevelKeyword(lastGroupBlock, childBlock, context)
} else {
handleNonTopLevelKeyword(lastGroupBlock, lastIndentLevel, childBlock, context)
return
}
if (lastGroupBlock !is SqlSubGroupBlock) {
if (lastIndentLevel > childBlock.indent.indentLevel) {
val findLastGroup =
blockBuilder.getGroupTopNodeIndexHistory().findLast {
it.indent.indentLevel < childBlock.indent.indentLevel ||
it is
SqlElConditionLoopCommentBlock
}
if (findLastGroup is SqlElConditionLoopCommentBlock) {
handleConditionLoopParent(findLastGroup, context, childBlock)
return
}
}
}
handleNonTopLevelKeyword(lastGroupBlock, lastIndentLevel, childBlock, context)
}

private fun handleConditionLoopParent(
Expand Down
Loading
Loading