Skip to content

Commit 94197a1

Browse files
committed
Modify conditions to treat subgroups after WHERE as SqlConditionalExpressionGroupBlock and properly handle spacing when subqueries are present
1 parent c99e3a1 commit 94197a1

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ open class SqlBlock(
102102
protected fun isConditionLoopDirectiveRegisteredBeforeParent(): Boolean {
103103
val firstPrevBlock = (prevBlocks.lastOrNull() as? SqlElConditionLoopCommentBlock)
104104
parentBlock?.let { parent ->
105-
return firstPrevBlock != null &&
105+
106+
return (childBlocks.firstOrNull() as? SqlElConditionLoopCommentBlock)?.isBeforeParentBlock() == true ||
107+
firstPrevBlock != null &&
106108
firstPrevBlock.conditionEnd != null &&
107109
firstPrevBlock.node.startOffset > parent.node.startOffset
108110
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,19 @@ open class SqlFileBlock(
555555
SqlCustomSpacingBuilder.nonSpacing
556556
}
557557

558-
else -> SqlCustomSpacingBuilder.normalSpacing
558+
is SqlSubGroupBlock -> {
559+
val includeSpaceRight = childBlock1.endPatternBlock?.isPreSpaceRight()
560+
561+
if (includeSpaceRight == false) {
562+
SqlCustomSpacingBuilder.nonSpacing
563+
} else {
564+
SqlCustomSpacingBuilder.normalSpacing
565+
}
566+
}
567+
568+
else -> {
569+
SqlCustomSpacingBuilder.normalSpacing
570+
}
559571
}
560572
}
561573

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
21+
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2122
import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictExpressionSubGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
@@ -26,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
2627
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2728
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
2931
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock
3032
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock
3133
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
@@ -60,6 +62,9 @@ open class SqlRightPatternBlock(
6062
}
6163

6264
private var preSpaceRight = false
65+
66+
fun isPreSpaceRight() = preSpaceRight
67+
6368
var lineBreakAndSpacingType: LineBreakAndSpacingType = LineBreakAndSpacingType.NONE
6469

6570
companion object {
@@ -77,7 +82,6 @@ open class SqlRightPatternBlock(
7782
SqlInsertColumnGroupBlock::class,
7883
SqlWithQuerySubGroupBlock::class,
7984
SqlConflictExpressionSubGroupBlock::class,
80-
SqlConditionalExpressionGroupBlock::class,
8185
)
8286

8387
val NEW_LINE_EXPECTED_TYPES =
@@ -103,11 +107,21 @@ open class SqlRightPatternBlock(
103107
*/
104108
private fun enableLastRight() {
105109
parentBlock?.let { parent ->
110+
val isFirstChildQuery =
111+
parent.childBlocks.firstOrNull {
112+
it !is SqlCommentBlock
113+
} is SqlTopQueryGroupBlock
106114
// Check if parent is in the notInsertSpaceClassList
107115
if (isExpectedClassType(NOT_INDENT_EXPECTED_TYPES, parent)) {
108116
preSpaceRight = false
109117
return
110118
}
119+
120+
if (parent is SqlConditionalExpressionGroupBlock) {
121+
preSpaceRight = isFirstChildQuery
122+
return
123+
}
124+
111125
if (isExpectedClassType(
112126
INDENT_EXPECTED_TYPES,
113127
parent,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2727
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFieldAccessBlock
2828
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFunctionCallBlock
2929
import org.domaframework.doma.intellij.formatter.block.expr.SqlElStaticFieldAccessBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithQuerySubGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
@@ -168,6 +169,7 @@ open class SqlElBlockCommentBlock(
168169
}
169170
}
170171
is SqlValuesGroupBlock -> parent.indent.indentLen
172+
is SqlKeywordGroupBlock -> parent.indent.groupIndentLen.plus(1)
171173
else -> parent.indent.groupIndentLen
172174
}
173175
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2828
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2929
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
31+
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
3334
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -217,9 +218,9 @@ class SqlElConditionLoopCommentBlock(
217218
return lastBlock.indent.indentLen
218219
}
219220
}
220-
return parent.indent.groupIndentLen +
221-
openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP +
222-
if (parent !is SqlWithQueryGroupBlock) 1 else 0
221+
val withQuerySpace = if (parent !is SqlWithQueryGroupBlock) 1 else 0
222+
return parent.indent.groupIndentLen.plus(withQuerySpace) +
223+
openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP
223224
}
224225
else -> return parent.indent.indentLen + openConditionLoopDirectiveCount * DIRECTIVE_INDENT_STEP
225226
}
@@ -302,5 +303,6 @@ class SqlElConditionLoopCommentBlock(
302303

303304
private fun shouldNotIndent(parent: SqlSubGroupBlock): Boolean =
304305
TypeUtil.isExpectedClassType(SqlRightPatternBlock.NOT_INDENT_EXPECTED_TYPES, parent) ||
305-
parent is SqlWithCommonTableGroupBlock
306+
parent is SqlWithCommonTableGroupBlock ||
307+
parent is SqlConditionalExpressionGroupBlock
306308
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,18 @@ class SqlConditionalExpressionGroupBlock(
4949
override fun createBlockIndentLen(): Int =
5050
parentBlock?.let { parent ->
5151
if (parent is SqlElConditionLoopCommentBlock) {
52-
parent.indent.groupIndentLen
52+
val groupIndentLen = parent.indent.groupIndentLen
53+
val grand = parent.parentBlock
54+
val directiveParentTextLen =
55+
if (grand !is SqlElConditionLoopCommentBlock) {
56+
grand
57+
?.getNodeText()
58+
?.length
59+
?.plus(1) ?: 0
60+
} else {
61+
0
62+
}
63+
groupIndentLen + directiveParentTextLen
5364
} else {
5465
parent.indent.groupIndentLen.plus(1)
5566
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/NotQueryGroupHandler.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.S
2626
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
2727
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
2828
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
29+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2930
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlValuesParamGroupBlock
@@ -55,7 +56,9 @@ object NotQueryGroupHandler {
5556
else -> null
5657
}
5758

58-
private fun lastGroupParentConditionKeywordGroup(lastGroup: SqlBlock?): Boolean = lastGroup is SqlConditionKeywordGroupBlock
59+
private fun lastGroupParentConditionKeywordGroup(lastGroup: SqlBlock?): Boolean =
60+
lastGroup is SqlConditionKeywordGroupBlock ||
61+
lastGroup is SqlWhereGroupBlock
5962

6063
/**
6164
* Creates a keyword group block for specific keywords.

0 commit comments

Comments
 (0)