Skip to content

Commit 3037f58

Browse files
committed
Enhance SQL formatting for condition and loop directives with improved indentation handling
1 parent f3f3b7f commit 3037f58

22 files changed

+481
-158
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.intellij.lang.ASTNode
2626
import com.intellij.psi.formatter.common.AbstractBlock
2727
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
2828
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
29+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
2930
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3031
import org.domaframework.doma.intellij.formatter.util.IndentType
3132
import org.domaframework.doma.intellij.psi.SqlTypes
@@ -114,7 +115,14 @@ open class SqlBlock(
114115

115116
fun isEnableFormat(): Boolean = enableFormat
116117

117-
open fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
118+
open fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
119+
parentBlock?.let { parent ->
120+
if (parent is SqlElConditionLoopCommentBlock) {
121+
parent.childBlocks.dropLast(1).isEmpty()
122+
} else {
123+
false
124+
}
125+
} == true
118126

119127
/**
120128
* Creates the indentation length for the block.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ 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 org.domaframework.doma.intellij.common.util.TypeUtil
2930
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
3031
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
3132
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
@@ -440,6 +441,15 @@ class SqlFileBlock(
440441
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
441442
}
442443

444+
if (childBlock1 is SqlWhitespaceBlock && childBlock2.parentBlock is SqlElConditionLoopCommentBlock) {
445+
val child1 = childBlock2.parentBlock as SqlElConditionLoopCommentBlock
446+
SqlCustomSpacingBuilder().getSpacingElDirectiveComment(child1, childBlock2)?.let { return it }
447+
}
448+
449+
if (childBlock1 is SqlElBlockCommentBlock) {
450+
SqlCustomSpacingBuilder().getSpacingElDirectiveComment(childBlock1, childBlock2)?.let { return it }
451+
}
452+
443453
if (childBlock2 is SqlWithColumnGroupBlock) {
444454
return SqlCustomSpacingBuilder.normalSpacing
445455
}
@@ -454,8 +464,11 @@ class SqlFileBlock(
454464
}
455465

456466
if (childBlock2 is SqlElBlockCommentBlock) {
467+
if (TypeUtil.isExpectedClassType(SqlRightPatternBlock.NOT_INSERT_SPACE_TYPES, childBlock1)) {
468+
return SqlCustomSpacingBuilder.nonSpacing
469+
}
457470
return when (childBlock1) {
458-
is SqlElBlockCommentBlock, is SqlWhitespaceBlock -> {
471+
is SqlWhitespaceBlock -> {
459472
SqlCustomSpacingBuilder().getSpacing(childBlock2)
460473
}
461474

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ open class SqlRightPatternBlock(
5252
var preSpaceRight = false
5353

5454
companion object {
55-
private val NOT_INSERT_SPACE_TYPES =
55+
val NOT_INSERT_SPACE_TYPES =
5656
listOf(
5757
SqlFunctionParamBlock::class,
5858
SqlInsertColumnGroupBlock::class,
5959
SqlWithQuerySubGroupBlock::class,
6060
SqlConflictExpressionSubGroupBlock::class,
61+
SqlConditionalExpressionGroupBlock::class,
6162
)
6263

6364
private val INDENT_EXPECTED_TYPES =
@@ -67,7 +68,7 @@ open class SqlRightPatternBlock(
6768
SqlCreateTableColumnDefinitionGroupBlock::class,
6869
)
6970

70-
private val NEW_LINE_EXPECTED_TYPES =
71+
val NEW_LINE_EXPECTED_TYPES =
7172
listOf(
7273
SqlUpdateColumnGroupBlock::class,
7374
SqlUpdateValueGroupBlock::class,
@@ -96,6 +97,10 @@ open class SqlRightPatternBlock(
9697
preSpaceRight = false
9798
return
9899
}
100+
if (isExpectedClassType(INDENT_EXPECTED_TYPES, parent)) {
101+
preSpaceRight = true
102+
return
103+
}
99104

100105
// Check if parent is SqlSubQueryGroupBlock
101106
if (parent is SqlSubQueryGroupBlock) {
@@ -144,8 +149,6 @@ open class SqlRightPatternBlock(
144149
if (preSpaceRight) return 1
145150

146151
parentBlock?.let { parent ->
147-
if (parent is SqlWithQuerySubGroupBlock) return 0
148-
if (isExpectedClassType(INDENT_EXPECTED_TYPES, parent)) return parent.indent.indentLen
149152
return parent.indent.indentLen
150153
} ?: return 0
151154
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ open class SqlLineCommentBlock(
3939
override fun isLeaf(): Boolean = true
4040

4141
override fun createBlockIndentLen(): Int {
42+
val prevElement = PsiTreeUtil.prevLeaf(node.psi, false)
43+
if (prevElement?.text?.contains("\n") != true &&
44+
prevElement != null &&
45+
PsiTreeUtil.prevLeaf(prevElement) !is SqlLineCommentBlock
46+
) {
47+
return 1
48+
}
4249
parentBlock?.let { parent ->
4350
if (parent is SqlSubQueryGroupBlock) {
4451
if (parent.getChildBlocksDropLast().isEmpty()) {

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

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import com.intellij.formatting.Spacing
2020
import com.intellij.lang.ASTNode
2121
import com.intellij.psi.PsiWhiteSpace
2222
import com.intellij.psi.formatter.common.AbstractBlock
23+
import com.intellij.psi.util.PsiTreeUtil
24+
import com.intellij.psi.util.elementType
2325
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2426
import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
2527
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
@@ -30,13 +32,27 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQuer
3032
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3133
import org.domaframework.doma.intellij.formatter.util.IndentType
3234
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
35+
import org.domaframework.doma.intellij.psi.SqlElElseifDirective
36+
import org.domaframework.doma.intellij.psi.SqlElForDirective
37+
import org.domaframework.doma.intellij.psi.SqlElIfDirective
3338
import org.domaframework.doma.intellij.psi.SqlTypes
3439

3540
open class SqlElBlockCommentBlock(
3641
node: ASTNode,
3742
private val context: SqlBlockFormattingContext,
3843
open val customSpacingBuilder: SqlCustomSpacingBuilder?,
3944
) : SqlCommentBlock(node, context) {
45+
enum class SqlElCommentDirectiveType {
46+
CONDITION_LOOP,
47+
EXPAND,
48+
POPULATE,
49+
LITERAL,
50+
EMBEDDED,
51+
NORMAL,
52+
}
53+
54+
val directiveType: SqlElCommentDirectiveType = initDirectiveType()
55+
4056
override val indent =
4157
ElementIndent(
4258
IndentType.NONE,
@@ -51,6 +67,34 @@ open class SqlElBlockCommentBlock(
5167
indent.groupIndentLen = 0
5268
}
5369

70+
private fun initDirectiveType(): SqlElCommentDirectiveType {
71+
val element = this.node.psi
72+
val contentElement = PsiTreeUtil.firstChild(element).nextSibling
73+
74+
if (contentElement is SqlElIfDirective ||
75+
contentElement is SqlElForDirective ||
76+
contentElement is SqlElElseifDirective ||
77+
contentElement.elementType == SqlTypes.EL_ELSE ||
78+
contentElement.elementType == SqlTypes.EL_END
79+
) {
80+
return SqlElCommentDirectiveType.CONDITION_LOOP
81+
}
82+
if (contentElement.elementType == SqlTypes.HASH) {
83+
return SqlElCommentDirectiveType.EMBEDDED
84+
}
85+
if (contentElement.elementType == SqlTypes.EL_EXPAND) {
86+
return SqlElCommentDirectiveType.EXPAND
87+
}
88+
if (contentElement.elementType == SqlTypes.EL_POPULATE) {
89+
return SqlElCommentDirectiveType.POPULATE
90+
}
91+
if (contentElement.elementType == SqlTypes.CARET) {
92+
return SqlElCommentDirectiveType.LITERAL
93+
}
94+
95+
return SqlElCommentDirectiveType.NORMAL
96+
}
97+
5498
override fun buildChildren(): MutableList<AbstractBlock> {
5599
val blocks = mutableListOf<AbstractBlock>()
56100
var child = node.firstChildNode
@@ -130,20 +174,30 @@ open class SqlElBlockCommentBlock(
130174

131175
override fun createBlockIndentLen(): Int {
132176
parentBlock?.let { parent ->
133-
if (parent is SqlSubQueryGroupBlock) {
134-
if (parent.getChildBlocksDropLast().isEmpty()) {
135-
return 0
177+
return when (parent) {
178+
is SqlElConditionLoopCommentBlock -> parent.indent.groupIndentLen
179+
is SqlSubQueryGroupBlock -> {
180+
if (parent.getChildBlocksDropLast().isEmpty()) {
181+
0
182+
} else if (parent.isFirstLineComment) {
183+
parent.indent.groupIndentLen.minus(2)
184+
} else {
185+
parent.indent.groupIndentLen
186+
}
136187
}
137-
if (parent.isFirstLineComment) {
138-
return parent.indent.groupIndentLen.minus(2)
139-
}
140-
}
141-
if (parent is SqlValuesGroupBlock) {
142-
return parent.indent.indentLen
188+
is SqlValuesGroupBlock -> parent.indent.indentLen
189+
else -> parent.indent.groupIndentLen
143190
}
144191
}
145192
return 0
146193
}
147194

148-
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock is SqlValuesGroupBlock
195+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
196+
parentBlock?.let { parent ->
197+
(
198+
parent is SqlValuesGroupBlock ||
199+
parent is SqlElConditionLoopCommentBlock
200+
) &&
201+
parent.childBlocks.dropLast(1).isEmpty()
202+
} == true
149203
}

0 commit comments

Comments
 (0)