Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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
@@ -0,0 +1,22 @@
/*
* 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

enum class OnConflictKeywordType {
CONFLICT,
CONSTRAINT,
UNKNOWN,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 SqlConflictClauseBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlKeywordGroupBlock(
node,
IndentType.TOP,
context,
) {
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 @@ -28,10 +28,10 @@ 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.comment.SqlBlockCommentBlock
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.create.SqlCreateKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
import org.domaframework.doma.intellij.formatter.util.IndentType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* 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.column

import com.intellij.lang.ASTNode
Expand All @@ -33,7 +18,7 @@ open class SqlColumnDefinitionRawGroupBlock(
context,
) {
// TODO:Customize indentation within an inline group
val defaultOffset = 5
open val defaultOffset = 0
val isFirstColumnRaw = node.elementType != SqlTypes.COMMA

open var columnBlock: SqlBlock? = if (isFirstColumnRaw) this else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.subgroup
package org.domaframework.doma.intellij.formatter.block.group.column

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlSelectKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.util.IndentType
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

Expand All @@ -38,8 +38,6 @@ class SqlColumnRawGroupBlock(
node,
context,
) {
var isFirstColumnGroup = getNodeText() != ","

override fun setParentGroupBlock(lastGroup: SqlBlock?) {
super.setParentGroupBlock(lastGroup)
indent.indentLevel = IndentType.COLUMN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.formatter.block.group.subgroup
package org.domaframework.doma.intellij.formatter.block.group.column

import com.intellij.lang.ASTNode
import com.intellij.psi.formatter.common.AbstractBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

abstract class SqlColumnSelectionGroupBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class SqlRawGroupBlock(
context.enableFormat,
context.formatMode,
) {
var isFirstColumnGroup = getNodeText() != ","

override val indent =
ElementIndent(
IndentType.COLUMN,
Expand Down
Loading
Loading