Skip to content

Commit b1f6d04

Browse files
committed
Enhance SQL formatting for inline groups with improved parent group handling
1 parent 24f87ab commit b1f6d04

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import org.domaframework.doma.intellij.formatter.block.expr.SqlElStaticFieldAcce
3535
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
3636
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
3737
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
38+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
39+
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
3840
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
3941
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
4042
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
@@ -218,7 +220,7 @@ class SqlElConditionLoopCommentBlock(
218220
}
219221
val openConditionLoopDirectiveCount = getOpenDirectiveCount(parent)
220222
when (parent) {
221-
is SqlKeywordGroupBlock, is SqlCommaBlock -> {
223+
is SqlKeywordGroupBlock, is SqlCommaBlock, is SqlInlineGroupBlock, is SqlInlineSecondGroupBlock -> {
222224
return parent.indent.indentLen.plus(openConditionLoopDirectiveCount * 2)
223225
}
224226

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/inline/SqlInlineGroupBlock.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.inline
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2223
import org.domaframework.doma.intellij.formatter.util.IndentType
2324
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -47,7 +48,14 @@ open class SqlInlineGroupBlock(
4748

4849
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
4950

50-
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
51+
override fun createBlockIndentLen(): Int =
52+
parentBlock?.let { parent ->
53+
if (parent is SqlElConditionLoopCommentBlock) {
54+
parent.indent.groupIndentLen
55+
} else {
56+
parent.indent.groupIndentLen.plus(1)
57+
}
58+
} ?: 1
5159

5260
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
5361
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/inline/SqlInlineSecondGroupBlock.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.inline
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2223
import org.domaframework.doma.intellij.formatter.util.IndentType
2324
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -53,10 +54,11 @@ open class SqlInlineSecondGroupBlock(
5354

5455
override fun createBlockIndentLen(): Int =
5556
parentBlock?.let { parent ->
56-
// TODO:Customize indentation within an inline group
5757
if (isEndCase) {
5858
val diffTextLength = parent.getNodeText().length.minus(getNodeText().length)
5959
parent.indent.indentLen.plus(diffTextLength)
60+
} else if (parent is SqlElConditionLoopCommentBlock) {
61+
parent.indent.groupIndentLen
6062
} else {
6163
parent.indent.groupIndentLen.plus(1)
6264
}

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

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,8 @@ class SqlSetParentGroupProcessor(
117117
)
118118

119119
if (lastGroupBlock is SqlElConditionLoopCommentBlock) {
120-
if (lastGroupBlock.parentBlock != null) {
121-
setParentGroups(context) { history ->
122-
return@setParentGroups lastGroupBlock
123-
}
124-
} else {
125-
val history = blockBuilder.getGroupTopNodeIndexHistory()
126-
val findParent =
127-
history
128-
.lastOrNull {
129-
it.indent.indentLevel < childBlock.indent.indentLevel ||
130-
(it is SqlElConditionLoopCommentBlock && it.parentBlock != null)
131-
}
132-
// Search for keyword groups with a level lower than or equal to the current level,
133-
// or conditional directives that already have a parent assigned.
134-
if (findParent is SqlElConditionLoopCommentBlock) {
135-
// Set the parent of the most recent conditional directive to the current node.
136-
lastGroupBlock.setParentGroupBlock(findParent)
137-
setParentGroups(context) { history ->
138-
return@setParentGroups lastGroupBlock
139-
}
140-
}
141-
if (findParent !is SqlElConditionLoopCommentBlock) {
142-
// If a keyword group with a level lower than or equal to the current level is found,
143-
// set that keyword group as the parent, and set the parent of the most recent conditional directive to the current node.
144-
setParentGroups(context) { history ->
145-
return@setParentGroups findParent
146-
}
147-
lastGroupBlock.setParentGroupBlock(childBlock)
148-
}
120+
updateParentGroupLastConditionLoop(lastGroupBlock, context, childBlock) {
121+
it.indent.indentLevel < childBlock.indent.indentLevel
149122
}
150123
return
151124
}
@@ -319,6 +292,15 @@ class SqlSetParentGroupProcessor(
319292
}
320293
return
321294
}
295+
296+
val lastGroupBlock = blockBuilder.getLastGroupTopNodeIndexHistory()
297+
if (lastGroupBlock is SqlElConditionLoopCommentBlock) {
298+
updateParentGroupLastConditionLoop(lastGroupBlock, context, childBlock) {
299+
it.indent.indentLevel == IndentType.INLINE_SECOND
300+
}
301+
return
302+
}
303+
322304
val inlineSecondIndex =
323305
blockBuilder.getGroupTopNodeIndex { block ->
324306
block.indent.indentLevel == IndentType.INLINE_SECOND
@@ -331,6 +313,47 @@ class SqlSetParentGroupProcessor(
331313
)
332314
}
333315

316+
/**
317+
* Updates the parent of the last conditional directive block and sets the parent of the current block.
318+
*/
319+
private fun updateParentGroupLastConditionLoop(
320+
lastGroupBlock: SqlElConditionLoopCommentBlock,
321+
context: SetParentContext,
322+
childBlock: SqlBlock,
323+
findDefaultParent: (SqlBlock) -> Boolean,
324+
) {
325+
if (lastGroupBlock.parentBlock != null) {
326+
setParentGroups(context) { history ->
327+
return@setParentGroups lastGroupBlock
328+
}
329+
} else {
330+
val history = blockBuilder.getGroupTopNodeIndexHistory()
331+
val findParent =
332+
history
333+
.lastOrNull { block ->
334+
findDefaultParent(block) ||
335+
(block is SqlElConditionLoopCommentBlock && block.parentBlock != null)
336+
}
337+
// Search for keyword groups with a level lower than or equal to the current level,
338+
// or conditional directives that already have a parent assigned.
339+
if (findParent is SqlElConditionLoopCommentBlock) {
340+
// Set the parent of the most recent conditional directive to the current node.
341+
lastGroupBlock.setParentGroupBlock(findParent)
342+
setParentGroups(context) { history ->
343+
return@setParentGroups lastGroupBlock
344+
}
345+
}
346+
if (findParent !is SqlElConditionLoopCommentBlock) {
347+
// If a keyword group with a level lower than or equal to the current level is found,
348+
// set that keyword group as the parent, and set the parent of the most recent conditional directive to the current node.
349+
setParentGroups(context) { history ->
350+
return@setParentGroups findParent
351+
}
352+
lastGroupBlock.setParentGroupBlock(childBlock)
353+
}
354+
}
355+
}
356+
334357
fun updateConditionLoopCommentBlockParent(
335358
lastGroupBlock: SqlBlock,
336359
childBlock: SqlElConditionLoopCommentBlock,

0 commit comments

Comments
 (0)