Skip to content

Commit 7c22324

Browse files
committed
Add condition to treat function names as regular word blocks
1 parent 3b84c06 commit 7c22324

File tree

3 files changed

+93
-33
lines changed

3 files changed

+93
-33
lines changed

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlFileBlock.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,12 @@ open class SqlFileBlock(
236236
) {
237237
return SqlFunctionGroupBlock(child, defaultFormatCtx)
238238
}
239-
return SqlKeywordBlock(
240-
child,
241-
IndentType.ATTACHED,
242-
defaultFormatCtx,
243-
)
239+
// If it is not followed by a left parenthesis, treat it as a word block
240+
return if (lastGroup is SqlWithQueryGroupBlock) {
241+
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
242+
} else {
243+
blockUtil.getWordBlock(lastGroup, child)
244+
}
244245
}
245246

246247
SqlTypes.WORD -> {

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/other/SqlEscapeBlock.kt

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ package org.domaframework.doma.intellij.formatter.block.other
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2021
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlArrayListGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
2123
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2224

2325
class SqlEscapeBlock(
@@ -34,12 +36,53 @@ class SqlEscapeBlock(
3436
}
3537

3638
override fun createBlockIndentLen(): Int {
37-
val hasEvenEscapeBlocks = parentBlock?.childBlocks?.count { it is SqlEscapeBlock }?.let { it % 2 == 0 } == true
39+
val parentEscapeBlock =
40+
if (parentBlock is SqlElConditionLoopCommentBlock) {
41+
if (parentBlock?.parentBlock is SqlEscapeBlock)1 else 0
42+
} else {
43+
0
44+
}
45+
val prevBlocks = parentBlock?.childBlocks?.count { it is SqlEscapeBlock }?.plus(parentEscapeBlock) ?: 0
46+
47+
val hasEvenEscapeBlocks = prevBlocks.let { it % 2 == 0 } == true
3848
isEndEscape = hasEvenEscapeBlocks || getNodeText() == "]"
3949
return if (isEndEscape) {
4050
0
4151
} else {
42-
1
52+
calculateIndentLen()
4353
}
4454
}
55+
56+
private fun calculateIndentLen(): Int {
57+
parentBlock?.let { parent ->
58+
when (parent) {
59+
is SqlSubQueryGroupBlock -> {
60+
val parentIndentLen = parent.indent.groupIndentLen
61+
val grand = parent.parentBlock
62+
if (grand != null && grand.getNodeText().lowercase() == "create") {
63+
val grandIndentLen = grand.indent.groupIndentLen
64+
return grandIndentLen.plus(parentIndentLen).plus(1)
65+
}
66+
return parentIndentLen.plus(1)
67+
}
68+
69+
is SqlElConditionLoopCommentBlock -> {
70+
return parent.indent.groupIndentLen
71+
}
72+
73+
else -> {
74+
if (isSaveSpace(parentBlock))return parentBlock?.indent?.groupIndentLen ?: 1
75+
return 1
76+
}
77+
}
78+
}
79+
return 1
80+
}
81+
82+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
83+
if (isEndEscape) {
84+
false
85+
} else {
86+
super.isSaveSpace(lastGroup)
87+
}
4588
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/word/SqlFunctionGroupBlock.kt

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,48 @@ class SqlFunctionGroupBlock(
4040
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen ?: 0
4141

4242
override fun createGroupIndentLen(): Int {
43-
var baseIndent = 0
44-
parentBlock?.let { parent ->
45-
val children = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
46-
val prevBlocksLength =
47-
children
48-
.sumOf { prev ->
49-
prev
50-
.getChildrenTextLen()
51-
.plus(
52-
if (prev.node.elementType == SqlTypes.DOT ||
53-
prev.node.elementType == SqlTypes.RIGHT_PAREN
54-
) {
55-
0
56-
} else {
57-
prev.getNodeText().length.plus(1)
58-
},
59-
)
60-
}.plus(parent.indent.groupIndentLen)
61-
baseIndent =
62-
if (parent is SqlSubGroupBlock) {
63-
// parent.indent.groupIndentLen
64-
prevBlocksLength
65-
} else {
66-
prevBlocksLength.plus(1)
67-
}
68-
}
43+
val baseIndent =
44+
parentBlock?.let { parent ->
45+
val children = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
46+
val prevBlocksLength = calculatePrevBlocksLength(children, parent)
47+
calculateBaseIndent(parent, prevBlocksLength)
48+
} ?: 0
6949
return baseIndent.plus(getNodeText().length)
7050
}
51+
52+
private fun calculatePrevBlocksLength(
53+
children: List<SqlBlock>,
54+
parent: SqlBlock,
55+
): Int =
56+
children
57+
.sumOf { prev ->
58+
prev
59+
.getChildrenTextLen()
60+
.plus(
61+
if (prev.node.elementType == SqlTypes.DOT ||
62+
prev.node.elementType == SqlTypes.RIGHT_PAREN
63+
) {
64+
0
65+
} else {
66+
prev.getNodeText().length.plus(1)
67+
},
68+
)
69+
}.plus(parent.indent.groupIndentLen)
70+
71+
private fun calculateBaseIndent(
72+
parent: SqlBlock,
73+
prevBlocksLength: Int,
74+
): Int =
75+
when (parent) {
76+
is SqlSubGroupBlock ->
77+
prevBlocksLength
78+
79+
is SqlElConditionLoopCommentBlock -> {
80+
val directiveParent = parentBlock?.parentBlock
81+
val directiveParentLen = directiveParent?.getNodeText()?.length?.plus(1) ?: 1
82+
prevBlocksLength.plus(directiveParentLen)
83+
}
84+
85+
else -> prevBlocksLength.plus(1)
86+
}
7187
}

0 commit comments

Comments
 (0)