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
4 changes: 4 additions & 0 deletions src/main/java/org/domaframework/doma/intellij/Sql.flex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
"case",
"change",
"check",
"conflict",
"constraint",
"column",
"comment",
"create",
Expand All @@ -42,6 +44,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
"delete",
"desc",
"distinct",
"do",
"drop",
"else",
"end",
Expand All @@ -67,6 +70,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
"like",
"limit",
"not",
"nothing",
"null",
"modify",
"offset",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
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.expr.SqlElSymbolBlock
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
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.column.SqlDataTypeBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
Expand All @@ -45,7 +48,6 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
Expand Down Expand Up @@ -112,7 +114,9 @@ open class SqlBlock(
setParentPropertyBlock(lastGroup)
}

open fun setParentPropertyBlock(lastGroup: SqlBlock?) {}
open fun setParentPropertyBlock(lastGroup: SqlBlock?) {
// This method can be overridden to set additional properties on the parent block if needed.
}

open val isNeedWhiteSpace: Boolean = true

Expand Down Expand Up @@ -209,12 +213,18 @@ open class SqlBlock(
SqlColumnDefinitionRawGroupBlock::class,
SqlCreateTableColumnDefinitionGroupBlock::class,
SqlUpdateColumnAssignmentSymbolBlock::class,
SqlDoGroupBlock::class,
)

if (isExpectedClassType(expectedClassTypes, childBlock)) return true

if (isNewLineSqlComment(child, childBlock)) return true

if (lastGroup is SqlConflictClauseBlock) return false
if (childBlock is SqlColumnRawGroupBlock) {
return !childBlock.isFirstColumnGroup
}

return (
isNewLineGroupBlockAfterRegistrationChild(childBlock, lastGroup) ||
(childBlock is SqlRightPatternBlock && childBlock.isNewLine(lastGroup))
Expand Down Expand Up @@ -417,15 +427,20 @@ open class SqlBlock(
return blockUtil.getSubGroupBlock(lastGroup, child)
}

SqlTypes.OTHER -> return if (lastGroup is SqlUpdateSetGroupBlock &&
lastGroup.columnDefinitionGroupBlock != null
) {
SqlUpdateColumnAssignmentSymbolBlock(child, defaultFormatCtx)
} else {
SqlOtherBlock(
child,
defaultFormatCtx,
)
SqlTypes.OTHER -> {
return if (lastGroup is SqlUpdateSetGroupBlock &&
lastGroup.columnDefinitionGroupBlock != null
) {
SqlUpdateColumnAssignmentSymbolBlock(
child,
defaultFormatCtx,
)
} else {
SqlOtherBlock(
child,
defaultFormatCtx,
)
}
}

SqlTypes.RIGHT_PAREN -> return SqlRightPatternBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ package org.domaframework.doma.intellij.formatter.block

import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.common.util.TypeUtil
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
import org.domaframework.doma.intellij.psi.SqlTypes
Expand Down Expand Up @@ -63,8 +64,14 @@ open class SqlCommaBlock(
return 0
}

val parentIndentSyncBlockTypes =
listOf(
SqlUpdateColumnGroupBlock::class,
SqlUpdateValueGroupBlock::class,
SqlInsertColumnGroupBlock::class,
)
val parentIndentLen = parent.indent.groupIndentLen
if (parent is SqlUpdateColumnGroupBlock || parent is SqlUpdateValueGroupBlock) {
if (TypeUtil.isExpectedClassType(parentIndentSyncBlockTypes, parent)) {
return parentIndentLen
}

Expand All @@ -74,17 +81,15 @@ open class SqlCommaBlock(
val grandIndentLen = grand.indent.groupIndentLen
return grandIndentLen.plus(parentIndentLen).minus(1)
}
if (grand is SqlInsertQueryGroupBlock) {
return parentIndentLen
}

if (grand is SqlColumnRawGroupBlock) {
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 parentIndentLen
return parentIndentLen.plus(1)
} else {
var prevLen = 0
parent.childBlocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block
import com.intellij.formatting.Indent
import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
Expand All @@ -42,11 +43,11 @@ open class SqlKeywordBlock(
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
}

// override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
// if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) {
// lastGroup.doQueryBlock = this
// }
// }
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) {
lastGroup.doQueryBlock = this
}
}

override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

Expand Down Expand Up @@ -62,6 +64,11 @@ open class SqlRightPatternBlock(
return
}

if (parent.parentBlock is SqlConflictClauseBlock) {
preSpaceRight = false
return
}

if (parent is SqlSubQueryGroupBlock) {
val prevKeywordBlock =
parent.childBlocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
* 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.conflict

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

class SqlWhereGroupBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlKeywordGroupBlock(node, IndentType.SECOND, context)
enum class OnConflictKeywordType {
CONFLICT,
CONSTRAINT,
UNKNOWN,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.function
package org.domaframework.doma.intellij.formatter.block.conflict

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
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

/**
* Parameter block of the function call
*/
class SqlParamBlock(
class SqlConflictClauseBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlBlock(
) : SqlKeywordGroupBlock(
node,
context.wrap,
context.alignment,
null,
context.spacingBuilder,
context.enableFormat,
context.formatMode,
IndentType.TOP,
context,
) {
val isFirstParam = node.elementType != SqlTypes.COMMA
override var parentBlock: SqlBlock?
get() = super.parentBlock as? SqlParamGroupBlock
set(value) {
if (value is SqlParamGroupBlock) {
super.parentBlock = value
}
}
var conflictType: OnConflictKeywordType = OnConflictKeywordType.CONFLICT
var doBlock: SqlDoGroupBlock? = null

override fun setParentGroupBlock(lastGroup: SqlBlock?) {
super.setParentGroupBlock(lastGroup)
indent.indentLevel = IndentType.CONFLICT
indent.indentLen = createBlockIndentLen()
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.conflict

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
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

class SqlDoGroupBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlKeywordGroupBlock(
node,
IndentType.CONFLICT,
context,
) {
/**
* **NOTHING** or **UPDATE**
*/
var doQueryBlock: SqlBlock? = null

override fun setParentGroupBlock(lastGroup: SqlBlock?) {
super.setParentGroupBlock(lastGroup)
indent.indentLen = 0
indent.groupIndentLen = getNodeText().length
}

override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
if (lastGroup is SqlConflictClauseBlock) {
lastGroup.doBlock = this
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ open class SqlElBlockCommentBlock(
SqlElFunctionCallBlock(
child,
context,
createSpacingBuilder(),
)

SqlTypes.BLOCK_COMMENT_CONTENT ->
Expand Down Expand Up @@ -116,26 +115,6 @@ open class SqlElBlockCommentBlock(
Spacing.createSpacing(0, 0, 0, false, 0),
)

private fun createStaticFieldSpacingBuilder(): SqlCustomSpacingBuilder =
SqlCustomSpacingBuilder()
.withSpacing(
SqlTypes.AT_SIGN,
SqlTypes.EL_CLASS,
Spacing.createSpacing(0, 0, 0, false, 0),
).withSpacing(
SqlTypes.AT_SIGN,
SqlTypes.EL_IDENTIFIER,
Spacing.createSpacing(0, 0, 0, false, 0),
).withSpacing(
SqlTypes.EL_IDENTIFIER,
SqlTypes.DOT,
Spacing.createSpacing(0, 0, 0, false, 0),
).withSpacing(
SqlTypes.EL_IDENTIFIER,
SqlTypes.EL_PARAMETERS,
Spacing.createSpacing(0, 0, 0, false, 0),
)

override fun getSpacing(
child1: Block?,
child2: Block,
Expand Down
Loading
Loading