Skip to content

Commit a67afa5

Browse files
committed
Enhance line-breaking and indentation behavior.
1 parent a3cddd7 commit a67afa5

File tree

10 files changed

+94
-12
lines changed

10 files changed

+94
-12
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class SqlCommaBlock(
8282
super.setParentGroupBlock(lastGroup)
8383
indent.indentLevel = IndentType.COMMA
8484
indent.indentLen = createBlockIndentLen()
85-
indent.groupIndentLen = indent.indentLen.plus(getNodeText().length)
85+
indent.groupIndentLen = createGroupIndentLen()
8686
}
8787

8888
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
@@ -142,6 +142,8 @@ open class SqlCommaBlock(
142142
return 1
143143
}
144144

145+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(1)
146+
145147
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
146148
if (parentBlock is SqlConditionalExpressionGroupBlock) return false
147149
return TypeUtil.isExpectedClassType(EXPECTED_TYPES, parentBlock)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ abstract class SqlCommentBlock(
6262
return 0
6363
}
6464

65-
override fun createGroupIndentLen(): Int = 0
65+
override fun createGroupIndentLen(): Int = indent.indentLen
6666
}

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ class SqlElConditionLoopCommentBlock(
7070
fun isElse(): Boolean = this == ELSE
7171
}
7272

73+
companion object {
74+
private val LINE_BREAK_PARENT_TYPES =
75+
listOf(
76+
SqlSubGroupBlock::class,
77+
SqlColumnRawGroupBlock::class,
78+
SqlElConditionLoopCommentBlock::class,
79+
)
80+
}
81+
7382
var tempParentBlock: SqlBlock? = null
7483
val conditionType: SqlConditionLoopCommentBlockType = initConditionOrLoopType(node)
7584
var conditionStart: SqlElConditionLoopCommentBlock? = null
@@ -102,6 +111,13 @@ class SqlElConditionLoopCommentBlock(
102111
*
103112
* If the next element is a **[SqlKeywordGroupBlock]** or **[SqlSubGroupBlock]**, align the directive’s indentation to that keyword’s indentation.
104113
* Otherwise, use the **provisional indentation element as the base** to align the indentation of child elements under the directive.
114+
*
115+
* @note
116+
* When a keyword group appears immediately before a conditional or loop directive, and a non-group block appears immediately after, the directive can end up being registered as a child of both.
117+
*
118+
* To ensure correct group block generation in the later sub-group block processing, the conditional/loop directive must remain registered as a child of the preceding keyword group.
119+
* If it is not retained, the resulting group block structure will be inaccurate.
120+
*
105121
*/
106122
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
107123
super.setParentGroupBlock(lastGroup)
@@ -122,6 +138,12 @@ class SqlElConditionLoopCommentBlock(
122138
}
123139
}
124140

141+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
142+
if (lastGroup is SqlElConditionLoopCommentBlock && conditionType.isEnd()) {
143+
lastGroup.conditionEnd = this
144+
}
145+
}
146+
125147
override fun buildChildren(): MutableList<AbstractBlock> {
126148
val blocks = mutableListOf<AbstractBlock>()
127149
var child = node.firstChildNode
@@ -200,10 +222,10 @@ class SqlElConditionLoopCommentBlock(
200222
if (conditionType.isEnd() || conditionType.isElse()) {
201223
return true
202224
}
203-
if (lastGroup is SqlSubGroupBlock) {
204-
return lastGroup.childBlocks.dropLast(1).isNotEmpty()
225+
if (TypeUtil.isExpectedClassType(LINE_BREAK_PARENT_TYPES, lastGroup)) {
226+
return lastGroup?.childBlocks?.dropLast(1)?.isNotEmpty() == true || lastGroup is SqlElConditionLoopCommentBlock
205227
}
206-
return true
228+
return lastGroup?.childBlocks?.firstOrNull() != this
207229
}
208230

209231
/**
@@ -243,7 +265,8 @@ class SqlElConditionLoopCommentBlock(
243265
return if (TypeUtil.isExpectedClassType(
244266
SqlRightPatternBlock.NOT_INDENT_EXPECTED_TYPES,
245267
parent,
246-
)
268+
) ||
269+
parent is SqlWithCommonTableGroupBlock
247270
) {
248271
parentGroupIndentLen.plus(openConditionLoopDirectiveCount * 2)
249272
} else {
@@ -262,6 +285,22 @@ class SqlElConditionLoopCommentBlock(
262285
return parent.indent.indentLen.plus(2)
263286
}
264287
}
288+
289+
is SqlKeywordGroupBlock -> {
290+
// At this point, it's not possible to determine whether the parent keyword group appears before or after this block based solely on the parent-child relationship.
291+
// Therefore, determine the position directly using the text offset.
292+
return if (parent.node.startOffset <
293+
node.startOffset
294+
) {
295+
// The child branch applies in cases where a conditional directive is included as a child of this block.
296+
val questOffset = if (parent is SqlWithQueryGroupBlock) 0 else 1
297+
parent.indent.groupIndentLen
298+
.plus(openConditionLoopDirectiveCount * 2)
299+
.plus(questOffset)
300+
} else {
301+
parent.indent.indentLen.plus(openConditionLoopDirectiveCount * 2)
302+
}
303+
}
265304
else -> return parent.indent.indentLen.plus(openConditionLoopDirectiveCount * 2)
266305
}
267306
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnDefinitionRawGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open class SqlColumnDefinitionRawGroupBlock(
3232
node,
3333
context,
3434
) {
35-
// TODO:Customize indentation within an inline group
35+
// TODO Customize indentation within an inline group
3636
open val defaultOffset = 0
3737
val isFirstColumnRaw = node.elementType != SqlTypes.COMMA
3838

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/condition/SqlConditionKeywordGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SqlConditionKeywordGroupBlock(
4646
}
4747
}
4848

49-
// TODO If AND appears after OR, change it so that it is right-justified.
49+
// If AND appears after OR, change it so that it is right-justified.
5050
override fun createBlockIndentLen(): Int {
5151
parentBlock?.let { parent ->
5252
val groupLen = parent.indent.groupIndentLen

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateTableColumnDefinitionGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SqlCreateTableColumnDefinitionGroupBlock(
4141
node,
4242
context,
4343
) {
44-
// TODO:Customize indentation
44+
// TODO Customize indentation
4545
override val offset = 2
4646
private val groupOffset = 5
4747
val columnRawGroupBlocks = mutableListOf<SqlColumnDefinitionRawGroupBlock>()

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertColumnGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class SqlInsertColumnGroupBlock(
3737
node,
3838
context,
3939
) {
40-
// TODO:Customize indentation
40+
// TODO Customize indentation
4141
override val offset = 2
4242

4343
override fun setParentGroupBlock(lastGroup: SqlBlock?) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/top/SqlTopQueryGroupBlock.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ abstract class SqlTopQueryGroupBlock(
4141
SqlWithQuerySubGroupBlock::class,
4242
SqlElConditionLoopCommentBlock::class,
4343
)
44+
private val offset = 0
4445
}
4546

4647
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
@@ -54,7 +55,10 @@ abstract class SqlTopQueryGroupBlock(
5455

5556
override fun createBlockIndentLen(): Int {
5657
parentBlock?.let { parent ->
57-
if (parent.indent.indentLevel == IndentType.FILE) return 0
58+
if (parent.indent.indentLevel == IndentType.FILE) return offset
59+
if (parent is SqlElConditionLoopCommentBlock) {
60+
return createIndentLenInConditionLoopDirective(parent)
61+
}
5862
var baseIndent = parent.indent.groupIndentLen
5963
if (!TypeUtil.isExpectedClassType(PARENT_INDENT_SYNC_TYPES, parent)) {
6064
baseIndent = baseIndent.plus(1)
@@ -63,4 +67,36 @@ abstract class SqlTopQueryGroupBlock(
6367
}
6468
return 0
6569
}
70+
71+
protected fun createIndentLenInConditionLoopDirective(parent: SqlElConditionLoopCommentBlock): Int {
72+
// When the parent is a conditional directive, adjust the indent considering loop nesting
73+
val parentConditionLoopNests = mutableListOf<SqlBlock>()
74+
var blockParent: SqlBlock? = parent
75+
parentConditionLoopNests.add(parent)
76+
while (blockParent is SqlElConditionLoopCommentBlock) {
77+
blockParent = blockParent.parentBlock
78+
if (blockParent != null) parentConditionLoopNests.add(blockParent)
79+
}
80+
val prevGroupBlock = parentConditionLoopNests.lastOrNull()
81+
parentConditionLoopNests.dropLast(1).reversed().forEachIndexed { index, p ->
82+
if (index == 0) {
83+
// For the first conditional loop directive, if it has a parent block whose indent level is lower than itself,
84+
// align with the indent of that parent's parent
85+
prevGroupBlock?.let { prev ->
86+
if (prev.indent.indentLevel >= indent.indentLevel) {
87+
p.indent.indentLen = prev.parentBlock?.indent?.indentLen ?: offset
88+
}
89+
}
90+
} else {
91+
// For subsequent conditional loop directives, adjust the indent by the nesting count * 2
92+
p.indent.indentLen = p.parentBlock
93+
?.indent
94+
?.indentLen
95+
?.plus(2) ?: (index * 2)
96+
}
97+
p.indent.groupIndentLen = p.indent.indentLen
98+
}
99+
100+
return parent.indent.indentLen
101+
}
66102
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateColumnGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SqlUpdateColumnGroupBlock(
3232
node,
3333
context,
3434
) {
35-
// TODO:Customize indentation
35+
// TODO Customize indentation
3636
override val offset = 2
3737
val columnRawGroupBlocks: MutableList<SqlColumnRawGroupBlock> = mutableListOf()
3838

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ abstract class SqlSubGroupBlock(
121121
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
122122
lastGroup?.let { lastBlock ->
123123
if (lastBlock is SqlJoinQueriesGroupBlock) return true
124+
val prevBlock = prevChildren?.dropLast(1)?.lastOrNull()
125+
if (prevBlock is SqlKeywordBlock) {
126+
if (prevBlock.getNodeText() == "in") return false
127+
}
128+
if (lastGroup is SqlElConditionLoopCommentBlock) return true
124129
return TypeUtil.isExpectedClassType(NEW_LINE_EXPECTED_TYPES, lastBlock.parentBlock)
125130
}
126131
return false

0 commit comments

Comments
 (0)