Skip to content

Commit 56508df

Browse files
committed
Added indentation processing for CASE-END
1 parent 878104c commit 56508df

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.intellij.formatting.Block
2020
import com.intellij.formatting.Spacing
2121
import com.intellij.psi.tree.IElementType
2222
import org.domaframework.doma.intellij.formatter.block.SqlBlock
23-
import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock
2423
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2524
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
2625
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
@@ -66,7 +65,7 @@ class SqlCustomSpacingBuilder {
6665

6766
fun getSpacingWithIndentComma(
6867
child1: SqlBlock?,
69-
child2: SqlCommaBlock,
68+
child2: SqlBlock,
7069
): Spacing? {
7170
val indentLen: Int = child2.indent.indentLen
7271
when (child1) {
@@ -107,12 +106,12 @@ class SqlCustomSpacingBuilder {
107106
return null
108107
}
109108

110-
fun getSpacingColumnRaw(child: SqlColumnDefinitionRawGroupBlock): Spacing? {
109+
fun getSpacingColumnDefinitionRaw(child: SqlColumnDefinitionRawGroupBlock): Spacing? {
111110
val indentLen = child.indent.indentLen
112111
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
113112
}
114113

115-
fun getSpacingColumnRowEndRight(child: SqlRightPatternBlock): Spacing? {
114+
fun getSpacingColumnDefinitionRawEndRight(child: SqlRightPatternBlock): Spacing? {
116115
val indentLen = child.indent.indentLen
117116
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
118117
}
@@ -157,7 +156,7 @@ class SqlCustomSpacingBuilder {
157156
}
158157

159158
IndentType.INLINE -> {
160-
return Spacing.createSpacing(0, 0, 0, false, 0, 0)
159+
return Spacing.createSpacing(1, 1, 0, false, 0, 0)
161160
}
162161

163162
IndentType.INLINE_SECOND -> {

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.domaframework.doma.intellij.formatter.block.expr.SqlElDotBlock
3333
import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock
3434
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionGroupBlock
3535
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
36+
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnGroupBlock
3637
import org.domaframework.doma.intellij.formatter.block.group.SqlCreateKeywordGroupBlock
3738
import org.domaframework.doma.intellij.formatter.block.group.SqlDataTypeParamBlock
3839
import org.domaframework.doma.intellij.formatter.block.group.SqlInlineGroupBlock
@@ -190,6 +191,26 @@ open class SqlBlock(
190191
}
191192
}
192193

194+
is SqlColumnGroupBlock -> {
195+
when (lastIndentLevel) {
196+
childBlock.indent.indentLevel -> {
197+
groupTopNodeIndexHistory.removeLast()
198+
setParentGroups(
199+
childBlock,
200+
) { history ->
201+
return@setParentGroups latestGroupTopBlock.parentBlock
202+
}
203+
}
204+
else -> {
205+
setParentGroups(
206+
childBlock,
207+
) { history ->
208+
return@setParentGroups history.last().second
209+
}
210+
}
211+
}
212+
}
213+
193214
is SqlInlineGroupBlock -> {
194215
// case-end
195216
setParentGroups(
@@ -200,14 +221,36 @@ open class SqlBlock(
200221
}
201222

202223
is SqlInlineSecondGroupBlock -> {
203-
groupTopNodeIndexHistory.removeLast()
224+
if (childBlock.isEndCase) {
225+
val inlineIndex =
226+
groupTopNodeIndexHistory.indexOfLast { it.second.indent.indentLevel == IndentType.INLINE }
227+
if (inlineIndex >= 0) {
228+
setParentGroups(
229+
childBlock,
230+
) { history ->
231+
return@setParentGroups history[inlineIndex].second
232+
}
233+
groupTopNodeIndexHistory
234+
.subList(
235+
inlineIndex,
236+
groupTopNodeIndexHistory.size,
237+
).clear()
238+
}
239+
return
240+
}
241+
if (lastIndentLevel == IndentType.INLINE_SECOND) {
242+
groupTopNodeIndexHistory.removeLast()
243+
setParentGroups(
244+
childBlock,
245+
) { history ->
246+
return@setParentGroups latestGroupTopBlock.parentBlock
247+
}
248+
return
249+
}
204250
setParentGroups(
205251
childBlock,
206252
) { history ->
207-
return@setParentGroups latestGroupTopBlock.parentBlock
208-
}
209-
if (childBlock.isEndCase) {
210-
groupTopNodeIndexHistory.removeLast()
253+
return@setParentGroups history.last().second
211254
}
212255
}
213256

@@ -333,7 +376,9 @@ open class SqlBlock(
333376
spacingBuilder,
334377
)
335378

336-
else -> SqlCommaBlock(child, wrap, alignment, spacingBuilder)
379+
is SqlColumnGroupBlock -> SqlColumnGroupBlock(child, wrap, alignment, spacingBuilder)
380+
381+
else -> SqlColumnGroupBlock(child, wrap, alignment, spacingBuilder)
337382
}
338383
}
339384

@@ -472,14 +517,14 @@ open class SqlBlock(
472517
}
473518

474519
if (child2 is SqlColumnDefinitionRawGroupBlock) {
475-
SqlCustomSpacingBuilder().getSpacingColumnRaw(child2)?.let { return it }
520+
SqlCustomSpacingBuilder().getSpacingColumnDefinitionRaw(child2)?.let { return it }
476521
}
477522

478523
if (child2 is SqlRightPatternBlock && child2.parentBlock is SqlColumnDefinitionGroupBlock) {
479-
SqlCustomSpacingBuilder().getSpacingColumnRowEndRight(child2)?.let { return it }
524+
SqlCustomSpacingBuilder().getSpacingColumnDefinitionRawEndRight(child2)?.let { return it }
480525
}
481526

482-
if (child1 is SqlBlock && child2 is SqlCommaBlock) {
527+
if (child1 is SqlBlock && (child2 is SqlCommaBlock || child2 is SqlColumnGroupBlock)) {
483528
SqlCustomSpacingBuilder().getSpacingWithIndentComma(child1, child2)?.let { return it }
484529
}
485530

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class SqlColumnGroupBlock(
4949

5050
override fun setParentGroupBlock(block: SqlBlock?) {
5151
super.setParentGroupBlock(block)
52+
indent.indentLevel = IndentType.COLUMN
5253
indent.indentLen = createIndentLen()
53-
indent.groupIndentLen = indent.indentLen
54+
indent.groupIndentLen = if (isFirstColumnGroup) indent.indentLen else indent.indentLen.plus(1)
5455
}
5556

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ open class SqlInlineSecondGroupBlock(
5757

5858
private fun createIndentLen(): Int =
5959
parentBlock?.let {
60-
it.indent.groupIndentLen
61-
.plus(it.node.text.length)
62-
.plus(1)
60+
if (isEndCase) {
61+
it.indent.indentLen
62+
} else {
63+
it.indent.groupIndentLen
64+
.plus(it.node.text.length)
65+
}
6366
} ?: 1
6467
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ open class SqlJoinGroupBlock(
2929
wrap: Wrap?,
3030
alignment: Alignment?,
3131
spacingBuilder: SpacingBuilder,
32-
) : SqlNewGroupBlock(
32+
) : SqlKeywordGroupBlock(
3333
node,
34+
IndentType.JOIN,
3435
wrap,
3536
alignment,
3637
spacingBuilder,
3738
) {
39+
override val indent =
40+
ElementIndent(
41+
IndentType.JOIN,
42+
0,
43+
0,
44+
)
45+
3846
override fun setParentGroupBlock(block: SqlBlock?) {
3947
super.setParentGroupBlock(block)
4048
indent.indentLevel = IndentType.JOIN

0 commit comments

Comments
 (0)