Skip to content

Commit 624d806

Browse files
committed
Enhance SQL formatting by refining indentation logic and preventing unnecessary spaces in function parameters
1 parent 6fc5fe2 commit 624d806

File tree

7 files changed

+106
-29
lines changed

7 files changed

+106
-29
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ open class SqlBlock(
7676
private val SPACING_ONE_NO_KEEP = Spacing.createSpacing(1, 1, 0, false, 0)
7777
}
7878

79-
fun getChildrenTextLen(): Int = childBlocks.sumOf { child -> calculateChildTextLength(child) }
79+
fun getChildrenTextLen(): Int = childBlocks.sumOf { child -> calculateChildTextLength(child).plus(1) }
8080

8181
private fun calculateChildTextLength(child: SqlBlock): Int {
8282
val nonCommentChildren = child.childBlocks.filterNot { it is SqlDefaultCommentBlock }
@@ -139,7 +139,7 @@ open class SqlBlock(
139139
val firstConditionBlock = (prevChildren?.firstOrNull() as? SqlElConditionLoopCommentBlock)
140140
val endBlock = firstConditionBlock?.conditionEnd
141141
if (endBlock == null) return false
142-
val lastBlock = prevChildren.lastOrNull()
142+
val lastBlock = prevBlocks.lastOrNull()
143143

144144
return endBlock.node.startOffset > (lastBlock?.node?.startOffset ?: 0)
145145
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import com.intellij.formatting.Wrap
2626
import com.intellij.lang.ASTNode
2727
import com.intellij.psi.PsiWhiteSpace
2828
import com.intellij.psi.formatter.common.AbstractBlock
29+
import com.intellij.psi.util.PsiTreeUtil
30+
import com.intellij.psi.util.elementType
31+
import com.intellij.psi.util.nextLeaf
32+
import com.intellij.psi.util.nextLeafs
2933
import org.domaframework.doma.intellij.common.util.TypeUtil
3034
import org.domaframework.doma.intellij.formatter.block.comma.SqlCommaBlock
3135
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
@@ -221,8 +225,23 @@ open class SqlFileBlock(
221225
}
222226
}
223227

224-
SqlTypes.FUNCTION_NAME ->
225-
return SqlFunctionGroupBlock(child, defaultFormatCtx)
228+
SqlTypes.FUNCTION_NAME -> {
229+
val notWhiteSpaceElement =
230+
child.psi.nextLeafs
231+
.takeWhile { it is PsiWhiteSpace }
232+
.lastOrNull()
233+
?.nextLeaf(true)
234+
if (notWhiteSpaceElement?.elementType == SqlTypes.LEFT_PAREN ||
235+
PsiTreeUtil.nextLeaf(child.psi)?.elementType == SqlTypes.LEFT_PAREN
236+
) {
237+
return SqlFunctionGroupBlock(child, defaultFormatCtx)
238+
}
239+
return SqlKeywordBlock(
240+
child,
241+
IndentType.ATTACHED,
242+
defaultFormatCtx,
243+
)
244+
}
226245

227246
SqlTypes.WORD -> {
228247
return if (lastGroup is SqlWithQueryGroupBlock) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlFunctionParamBlock.kt

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2121
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
22+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElAtSignBlock
23+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock
24+
import org.domaframework.doma.intellij.formatter.block.other.SqlOtherBlock
2225
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
2326
import org.domaframework.doma.intellij.formatter.util.IndentType
2427
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -79,22 +82,51 @@ class SqlFunctionParamBlock(
7982
}
8083

8184
override fun createGroupIndentLen(): Int {
82-
val parentFunctionName = parentBlock as? SqlFunctionGroupBlock
83-
parentFunctionName?.let { parent ->
84-
return parent.indent.groupIndentLen
85-
.plus(getNodeText().length)
86-
}
87-
85+
val parentGroupIndent = parentBlock?.indent?.groupIndentLen ?: 0
8886
val prevChildrenDropLast =
8987
prevChildren?.dropLast(1)?.filter {
9088
it !is SqlDefaultCommentBlock &&
9189
it.node.elementType != SqlTypes.DOT
9290
}
9391
?: emptyList()
92+
val parentText =
93+
if (parentBlock is SqlElConditionLoopCommentBlock) {
94+
val grand = parentBlock?.parentBlock
95+
grand?.getNodeText() ?: ""
96+
} else {
97+
""
98+
}
9499
val prevLength =
95100
prevChildrenDropLast
96-
.sumOf { it.getNodeText().length }
97-
.plus(getNodeText().length)
98-
return prevLength.plus(prevChildrenDropLast.count()).plus(1)
101+
.sumOf {
102+
it.getChildrenTextLen().plus(it.getNodeText().length)
103+
}.plus(getNodeText().length)
104+
105+
// Avoid unnecessary spaces when operators are consecutive
106+
val consecutiveSymbolCount =
107+
calculateConsecutiveSymbolCount(prevChildrenDropLast)
108+
val spaces = prevChildrenDropLast.count().minus(consecutiveSymbolCount)
109+
110+
// parentFunctionName?.let { parent ->
111+
// return parentGroupIndent
112+
// .plus(prevLength)
113+
// }
114+
115+
return prevLength.plus(spaces).plus(parentText.length).plus(parentGroupIndent)
116+
}
117+
118+
private fun calculateConsecutiveSymbolCount(prevChildrenDropLast: List<SqlBlock>): Int {
119+
var count = 0
120+
var total = 0
121+
for (block in prevChildrenDropLast) {
122+
if (block is SqlOtherBlock || block is SqlElAtSignBlock || block is SqlElSymbolBlock) {
123+
count++
124+
} else {
125+
if (count > 1) total++
126+
count = 0
127+
}
128+
}
129+
if (count > 1) total += count
130+
return total
99131
}
100132
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class SqlFunctionGroupBlock(
3737
indent.groupIndentLen = createGroupIndentLen()
3838
}
3939

40-
override fun createBlockIndentLen(): Int {
40+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen ?: 0
41+
42+
override fun createGroupIndentLen(): Int {
43+
var baseIndent = 0
4144
parentBlock?.let { parent ->
4245
val children = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
4346
val prevBlocksLength =
@@ -55,15 +58,14 @@ class SqlFunctionGroupBlock(
5558
},
5659
)
5760
}.plus(parent.indent.groupIndentLen)
58-
return if (parent is SqlSubGroupBlock) {
59-
// parent.indent.groupIndentLen
60-
prevBlocksLength
61-
} else {
62-
prevBlocksLength.plus(1)
63-
}
61+
baseIndent =
62+
if (parent is SqlSubGroupBlock) {
63+
// parent.indent.groupIndentLen
64+
prevBlocksLength
65+
} else {
66+
prevBlocksLength.plus(1)
67+
}
6468
}
65-
return 0
69+
return baseIndent.plus(getNodeText().length)
6670
}
67-
68-
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
6971
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlFormatPreProcessor.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class SqlFormatPreProcessor : PreFormatProcessor {
111111
}
112112

113113
SqlTypes.LEFT_PAREN -> {
114-
newKeyword = getNewLineString(it.prevSibling, getUpperText(it))
114+
newKeyword = getNewLineLeftParenString(it.prevSibling, getUpperText(it))
115115
}
116116

117117
SqlTypes.RIGHT_PAREN -> {
@@ -233,6 +233,22 @@ class SqlFormatPreProcessor : PreFormatProcessor {
233233
}
234234
}
235235

236+
private fun getNewLineLeftParenString(
237+
prevElement: PsiElement?,
238+
text: String,
239+
): String =
240+
if (prevElement?.elementType == SqlTypes.BLOCK_COMMENT ||
241+
(
242+
prevElement?.text?.contains(LINE_SEPARATE) == false &&
243+
prevElement.prevSibling != null &&
244+
prevElement.elementType != SqlTypes.FUNCTION_NAME
245+
)
246+
) {
247+
"$LINE_SEPARATE$text"
248+
} else {
249+
text
250+
}
251+
236252
private fun getNewLineString(
237253
prevElement: PsiElement?,
238254
text: String,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockGenerator.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,18 @@ class SqlBlockGenerator(
220220
}
221221
}
222222
"from" -> {
223-
SqlFromGroupBlock(
224-
child,
225-
sqlBlockFormattingCtx,
226-
)
223+
if (lastGroupBlock is SqlSubGroupBlock) {
224+
SqlKeywordBlock(
225+
child,
226+
IndentType.ATTACHED,
227+
sqlBlockFormattingCtx,
228+
)
229+
} else {
230+
SqlFromGroupBlock(
231+
child,
232+
sqlBlockFormattingCtx,
233+
)
234+
}
227235
}
228236
"where" -> {
229237
SqlWhereGroupBlock(

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class SqlKeywordUtil {
304304
private val SET_LINE_KEYWORDS =
305305
mapOf(
306306
"into" to setOf("insert"),
307-
"from" to setOf("delete", "distinct"),
307+
"from" to setOf("delete", "distinct", "year"),
308308
"distinct" to setOf("select"),
309309
"table" to setOf("create", "alter", "rename", "truncate", "drop"),
310310
"index" to setOf("create", "alter", "rename", "truncate", "drop"),

0 commit comments

Comments
 (0)