Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -38,6 +38,7 @@ public class OracleFunctionToken {
"length",
"lengthb",
"length4",
"listagg",
"cast",
"numtodsinterval",
"numtoyminterval",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,32 @@ public class SqlFunctionToken {
static {
TOKENS.addAll(
Set.of(
"coalesce",
"row_number",
"row_count",
"sum",
"avg",
"btrim",
"coalesce",
"count",
"max",
"min",
"current_date",
"current_timestamp",
"dense_rank",
"filter",
"log",
"substring",
"trim",
"ltrim",
"rtlim",
"trim_array",
"btrim",
"replace",
"regexp_replace",
"max",
"min",
"mod",
"now",
"over",
"rank",
"percent_rank",
"dense_rank",
"current_date",
"current_timestamp",
"now",
"mod"));
"rank",
"regexp_replace",
"replace",
"row_count",
"row_number",
"rtlim",
"substring",
"sum",
"trim",
"trim_array"));
}

public static Set<String> getTokens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class SqlKeywordTokenUtil {
"then",
"to",
"truncate",
"unbounded",
"union",
"unique",
"update",
Expand All @@ -115,7 +116,8 @@ public class SqlKeywordTokenUtil {
"view",
"when",
"where",
"with"));
"with",
"within"));
}

public static Set<String> getTokens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.domaframework.doma.intellij.extension.psi.isDomain
import org.domaframework.doma.intellij.extension.psi.isEntity
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.comma.SqlCommaBlock
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
Expand All @@ -38,6 +39,7 @@ object TypeUtil {
listOf(
SqlSubGroupBlock::class,
SqlCommaBlock::class,
SqlColumnRawGroupBlock::class,
SqlWithQuerySubGroupBlock::class,
SqlCreateViewGroupBlock::class,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ open class SqlBlock(

return when {
nonCommentChildren.isNotEmpty() -> child.getChildrenTextLen() + child.getNodeText().length
isExcludedFromTextLength(child) -> 0
isExcludedFromTextLength(child) -> if (childBlocks.firstOrNull() == child) child.getNodeText().length else 0
else -> child.getNodeText().length + DEFAULT_TEXT_LENGTH_INCREMENT
}
}
Expand Down Expand Up @@ -371,24 +371,51 @@ open class SqlBlock(
0
}

var prevBlock: SqlBlock? = null
return children
.filter { it !is SqlDefaultCommentBlock && it !is SqlElConditionLoopCommentBlock }
.sumOf { prev ->
prev
.getChildrenTextLen()
.plus(
if (prev.node.elementType == SqlTypes.DOT ||
prev.node.elementType == SqlTypes.RIGHT_PAREN
) {
0
} else {
prev.getNodeText().length.plus(1)
},
)
val sum =
prev
.getChildrenTextLen()
.plus(
if (prev.node.elementType == SqlTypes.DOT ||
prev.node.elementType == SqlTypes.RIGHT_PAREN
) {
0
} else if (prev.isOperationSymbol() && prevBlock?.isOperationSymbol() == true) {
// When operators appear consecutively, the first symbol includes the text length for the last space.
// Subsequent symbols add only their own symbol length.
prev.getNodeText().length
} else {
prev.getNodeText().length.plus(1)
},
)
prevBlock = prev
return@sumOf sum
}.plus(parent.indent.groupIndentLen)
.plus(directiveParentIndent)
}

fun isOperationSymbol(): Boolean =
node.elementType in
listOf(
SqlTypes.PLUS,
SqlTypes.MINUS,
SqlTypes.ASTERISK,
SqlTypes.AT_SIGN,
SqlTypes.SLASH,
SqlTypes.HASH,
SqlTypes.LE,
SqlTypes.LT,
SqlTypes.EL_EQ,
SqlTypes.EL_NE,
SqlTypes.GE,
SqlTypes.GT,
SqlTypes.TILDE,
SqlTypes.OTHER,
)

/**
* Returns the child indentation for the block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,10 @@ open class SqlFileBlock(
}

if (childBlock1 is SqlSubGroupBlock) {
if (childBlock2 is SqlSubGroupBlock) {
if (childBlock2 is SqlSubGroupBlock || childBlock1 is SqlFunctionParamBlock) {
return SqlCustomSpacingBuilder.nonSpacing
}

if (childBlock1 is SqlInsertValueGroupBlock ||
childBlock1 is SqlUpdateValueGroupBlock
) {
Expand Down Expand Up @@ -586,7 +587,15 @@ open class SqlFileBlock(
}
}

is SqlDataTypeParamBlock, is SqlFunctionParamBlock -> return SqlCustomSpacingBuilder.nonSpacing
is SqlFunctionParamBlock -> {
return if (childBlock1?.node?.elementType in listOf(SqlTypes.FUNCTION_NAME, SqlTypes.WORD)) {
SqlCustomSpacingBuilder.nonSpacing
} else {
SqlCustomSpacingBuilder.normalSpacing
}
}

is SqlDataTypeParamBlock -> return SqlCustomSpacingBuilder.nonSpacing
}
}

Expand Down Expand Up @@ -647,7 +656,8 @@ open class SqlFileBlock(
childBlock1 is SqlOtherBlock && childBlock2 is SqlElSymbolBlock ||
childBlock1 is SqlElSymbolBlock && childBlock2 is SqlElAtSignBlock ||
childBlock1 is SqlOtherBlock && childBlock2 is SqlOtherBlock ||
childBlock1 is SqlElSymbolBlock && childBlock2 is SqlOtherBlock
childBlock1 is SqlElSymbolBlock && childBlock2 is SqlOtherBlock ||
childBlock1?.isOperationSymbol() == true && childBlock2.isOperationSymbol()

override fun isLeaf(): Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ open class SqlRightPatternBlock(
val firstChild =
parent.getChildBlocksDropLast().firstOrNull()
if (firstChild is SqlKeywordGroupBlock) {
// For subgroups other than function parameters, if the first element is a keyword group, add a line break before the closing parenthesis except at the top level.
// For subgroups created by WITHIN GROUP (), do not add a line break.
val lineBreak =
firstChild.indent.indentLevel != IndentType.TOP &&
!isExpectedClassType(NOT_NEW_LINE_EXPECTED_TYPES, parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.S
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
Expand Down Expand Up @@ -143,7 +144,21 @@ open class SqlCommaBlock(
val parentIndent = firstChild?.indent ?: parent.indent
parentIndent.groupIndentLen.plus(1)
}
else -> parent.indent.groupIndentLen.plus(1)
else -> {
// No indent after ORDER BY within function parameters
val grand = parent.parentBlock
val conditionParent =
if (grand is SqlElConditionLoopCommentBlock) {
grand.parentBlock
} else {
grand
}
if (parent is SqlSecondKeywordBlock && conditionParent is SqlFunctionParamBlock) {
0
} else {
parent.indent.groupIndentLen.plus(1)
}
}
}
}
}
Expand All @@ -153,7 +168,21 @@ open class SqlCommaBlock(
override fun createGroupIndentLen(): Int = indent.indentLen.plus(1)

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
if (parentBlock is SqlConditionalExpressionGroupBlock) return false
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parentBlock)
parentBlock?.let { parent ->
if (parent is SqlConditionalExpressionGroupBlock) return false
// Don't allow line breaks after ORDER BY within function parameters
val grand = parent.parentBlock
val conditionParent =
if (grand is SqlElConditionLoopCommentBlock) {
grand.parentBlock
} else {
grand
}
if (conditionParent is SqlFunctionParamBlock) {
return false
}
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parent)
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class SqlElConditionLoopCommentBlock(
listOf(
SqlSubGroupBlock::class,
SqlColumnRawGroupBlock::class,
SqlElConditionLoopCommentBlock::class,
)
}

Expand Down Expand Up @@ -173,10 +172,14 @@ class SqlElConditionLoopCommentBlock(
if (conditionType.isEnd() || conditionType.isElse()) {
return true
}
if (lastGroup is SqlElConditionLoopCommentBlock) {
return true
}
val firstChild = lastGroup?.childBlocks?.firstOrNull()
if (TypeUtil.isExpectedClassType(LINE_BREAK_PARENT_TYPES, lastGroup)) {
return lastGroup?.childBlocks?.dropLast(1)?.isNotEmpty() == true || lastGroup is SqlElConditionLoopCommentBlock
return firstChild != null && firstChild != this
}
return lastGroup?.childBlocks?.firstOrNull() != this
return firstChild == null || firstChild != this
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoo
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
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 Down Expand Up @@ -178,8 +179,16 @@ open class SqlKeywordGroupBlock(
override fun createGroupIndentLen(): Int = indent.indentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1))

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
val prevWord = prevBlocks.lastOrNull()
val conditionLastGroup =
if (parentBlock is SqlElConditionLoopCommentBlock) {
parentBlock?.parentBlock
} else {
lastGroup
}
val prevWord = prevBlocks.findLast { it is SqlKeywordBlock || it is SqlKeywordGroupBlock }
return !SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), prevWord?.getNodeText() ?: "") &&
!SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), lastGroup?.getNodeText() ?: "")
!SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), conditionLastGroup?.getNodeText() ?: "") &&
!SqlKeywordUtil.isSetLineKeyword(this.getNodeText(), lastGroup?.getNodeText() ?: "") &&
lastGroup !is SqlFunctionParamBlock
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package org.domaframework.doma.intellij.formatter.block.group.keyword.second

import com.intellij.lang.ASTNode
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.SqlElConditionLoopCommentBlock
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
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
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil

open class SqlSecondKeywordBlock(
node: ASTNode,
Expand All @@ -44,7 +45,13 @@ open class SqlSecondKeywordBlock(
return if (parent.indent.indentLevel == IndentType.FILE) {
offset
} else if (parent is SqlSubGroupBlock) {
groupLen.plus(1)
val space =
if (TypeUtil.isExpectedClassType(SqlRightPatternBlock.NOT_INDENT_EXPECTED_TYPES, parent)) {
0
} else {
1
}
groupLen.plus(space)
} else if (parent is SqlElConditionLoopCommentBlock) {
groupLen
} else {
Expand All @@ -55,12 +62,20 @@ open class SqlSecondKeywordBlock(
}

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
lastGroup?.let { last ->
val prevKeyword = last.childBlocks.findLast { it is SqlKeywordBlock }
prevKeyword?.let { prev ->
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), prev.getNodeText()) && last !is SqlFunctionParamBlock
parentBlock?.let { parent ->
val conditionParent =
if (parent is SqlElConditionLoopCommentBlock) {
parent.parentBlock
} else {
parent
}
if (conditionParent is SqlFunctionParamBlock) {
val firstKeywordParam =
conditionParent.childBlocks.firstOrNull { it is SqlNewGroupBlock }
return firstKeywordParam != null && firstKeywordParam != this
} else {
return super.isSaveSpace(lastGroup)
}
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), last.getNodeText())
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ abstract class SqlSubGroupBlock(
return false
}
}
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, lastBlock.parentBlock)
val lastParent = lastBlock.parentBlock
val expectedParent =
if (lastParent is SqlElConditionLoopCommentBlock) {
lastParent.parentBlock?.parentBlock
} else {
lastBlock.parentBlock
}
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, expectedParent)
}
return false
}
Expand Down
Loading
Loading