Skip to content

Commit d033162

Browse files
committed
Add Implemented indentation for CASE-END blocks
1 parent 7a9e45b commit d033162

25 files changed

+525
-164
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class SqlCustomSpacingBuilder {
6868
child1: SqlBlock?,
6969
child2: SqlCommaBlock,
7070
): Spacing? {
71-
val indentLen: Int = child2.indentLen
71+
val indentLen: Int = child2.indent.indentLen
7272
when (child1) {
7373
null -> return Spacing.createSpacing(0, 0, 0, false, 0, 0)
7474
is SqlWhitespaceBlock -> {
@@ -98,14 +98,14 @@ class SqlCustomSpacingBuilder {
9898
val postNewLine = child1.node.text.substringAfterLast("\n", "")
9999
val newIndent =
100100
if (child1.node.text.endsWith("\n") || postNewLine.isEmpty()) {
101-
child2.indentLen
101+
child2.indent.indentLen
102102
} else {
103-
child2.indentLen - postNewLine.length
103+
child2.indent.indentLen - postNewLine.length
104104
}
105105
println("InNewLine limeSpace:${postNewLine.length},Spacing:${child2.node.text}, $newIndent")
106106
return Spacing.createSpacing(
107-
child2.indentLen,
108-
child2.indentLen,
107+
child2.indent.indentLen,
108+
child2.indent.indentLen,
109109
0,
110110
false,
111111
0,
@@ -115,12 +115,12 @@ class SqlCustomSpacingBuilder {
115115
}
116116

117117
fun getSpacingColumnRaw(child: SqlColumnDefinitionRawGroupBlock): Spacing? {
118-
val indentLen = child.indentLen
118+
val indentLen = child.indent.indentLen
119119
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
120120
}
121121

122122
fun getSpacingColumnRowEndRight(child: SqlRightPatternBlock): Spacing? {
123-
val indentLen = child.indentLen
123+
val indentLen = child.indent.indentLen
124124
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
125125
}
126126

@@ -129,13 +129,13 @@ class SqlCustomSpacingBuilder {
129129
*/
130130
fun getSpacingWithIndentLevel(child: SqlNewGroupBlock): Spacing? {
131131
val parentBlock = child.parentBlock
132-
val indentLen: Int = child.indentLen
132+
val indentLen: Int = child.indent.indentLen
133133

134-
return when (child.indentLevel) {
134+
return when (child.indent.indentLevel) {
135135
IndentType.TOP -> {
136136
return if (parentBlock?.parentBlock == null) {
137137
Spacing.createSpacing(0, 0, 0, false, 0, 0)
138-
} else if (parentBlock.indentLevel == IndentType.SUB) {
138+
} else if (parentBlock.indent.indentLevel == IndentType.SUB) {
139139
Spacing.createSpacing(0, 0, 0, false, 0, 0)
140140
} else {
141141
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
@@ -163,9 +163,13 @@ class SqlCustomSpacingBuilder {
163163
return null
164164
}
165165

166+
IndentType.INLINE -> {
167+
return Spacing.createSpacing(0, 0, 0, false, 0, 0)
168+
}
169+
166170
IndentType.INLINE_SECOND -> {
167171
parentBlock?.let {
168-
val parentIndentLen = it.groupIndent
172+
val parentIndentLen = it.indent.groupIndentLen
169173
val parentTextLen = it.node.text.length
170174
val newIndentLen = parentIndentLen.plus(parentTextLen).plus(1)
171175
return Spacing.createSpacing(newIndentLen, newIndentLen, 1, false, 0, 1)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class SqlFormatPreProcessor : PreFormatProcessor {
8282
keywordIndex--
8383
newKeyword =
8484
if (checkKeywordPrevElement(index, it) &&
85-
SqlKeywordUtil.getIndentType(it.text).isNewLineGroup()
85+
SqlKeywordUtil.getIndentType(it.text).isNewLineGroup() ||
86+
it.text.lowercase() == "end"
8687
) {
8788
if (SqlKeywordUtil.isSetLineKeyword(
8889
it.text,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
6868
.spacing(0, 0, 0, false, 0)
6969
.around(SqlTypes.WORD)
7070
.spacing(1, 1, 0, false, 0)
71+
.before(SqlTypes.RIGHT_PAREN)
72+
.spacing(0, 0, 0, false, 0)
73+
.around(SqlTypes.PLUS)
74+
.spacing(1, 1, 0, false, 0)
75+
.around(SqlTypes.MINUS)
76+
.spacing(1, 1, 0, false, 0)
77+
.around(SqlTypes.ASTERISK)
78+
.spacing(1, 1, 0, false, 0)
7179

7280
private fun createCustomSpacingBuilder(): SqlCustomSpacingBuilder =
7381
SqlCustomSpacingBuilder()
@@ -79,6 +87,14 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
7987
SqlTypes.WORD,
8088
TokenType.WHITE_SPACE,
8189
Spacing.createSpacing(0, 0, 0, true, 0, 0),
90+
).withSpacing(
91+
SqlTypes.LEFT_PAREN,
92+
SqlTypes.WORD,
93+
Spacing.createSpacing(0, 0, 0, true, 0, 0),
94+
).withSpacing(
95+
SqlTypes.WORD,
96+
SqlTypes.RIGHT_PAREN,
97+
Spacing.createSpacing(0, 0, 0, true, 0, 0),
8298
).withSpacing(
8399
SqlTypes.OTHER,
84100
TokenType.WHITE_SPACE,

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ enum class IndentType(
2424
SECOND(2, true),
2525
JOIN(3, true),
2626
SECOND_OPTION(4, true),
27-
SUB(5, true),
28-
TIRD(6),
29-
ATTACHED(7),
30-
INLINE(8, true),
31-
INLINE_SECOND(9, true),
32-
COLUMN(10),
33-
ATTRIBUTE(11),
34-
LITERAL(12),
35-
OPTIONS(13),
36-
COMMA(14),
37-
COLUMN_ROW(15),
27+
TIRD(5),
28+
ATTACHED(6),
29+
INLINE_SECOND(8, true),
30+
COLUMN(9),
31+
SUB(90, true),
32+
ATTRIBUTE(91),
33+
LITERAL(92),
34+
OPTIONS(93),
35+
COMMA(94),
36+
COLUMN_ROW(95),
37+
INLINE(96, true),
3838
NONE(99),
3939
;
4040

@@ -192,7 +192,6 @@ class SqlKeywordUtil {
192192
setOf(
193193
"if",
194194
"case",
195-
"end",
196195
)
197196

198197
fun isInlineParentSqlKeyword(keyword: String): Boolean = INLINE_PARENT_SQL_KEYWORDS.contains(keyword.lowercase())
@@ -201,6 +200,7 @@ class SqlKeywordUtil {
201200
setOf(
202201
"when",
203202
"else",
203+
"end",
204204
)
205205

206206
fun isInlineSqlKeyword(keyword: String): Boolean = INLINE_SQL_KEYWORDS.contains(keyword.lowercase())

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

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ open class SqlBlock(
5050
parentBlock?.childBlocks?.add(this)
5151
}
5252

53-
open val indentLevel = IndentType.FILE
54-
open var indentLen = 0
55-
open var groupIndent = 0
53+
open val indent: ElementIndent =
54+
ElementIndent(
55+
IndentType.FILE,
56+
0,
57+
0,
58+
)
5659

5760
private val groupTopNodeIndexHistory = mutableListOf<Pair<Int, SqlBlock>>()
5861

@@ -108,13 +111,13 @@ open class SqlBlock(
108111
lastGroup: SqlBlock?,
109112
): Boolean =
110113
(
111-
childBlock.indentLevel.isNewLineGroup() &&
114+
childBlock.indent.indentLevel.isNewLineGroup() &&
112115
!SqlKeywordUtil.isSetLineKeyword(
113116
child.text,
114117
lastGroup?.node?.text ?: "",
115118
)
116119
) ||
117-
childBlock.indentLevel == IndentType.COMMA ||
120+
childBlock.node.elementType == SqlTypes.COMMA ||
118121
childBlock is SqlColumnDefinitionRawGroupBlock ||
119122
childBlock is SqlColumnDefinitionGroupBlock ||
120123
(
@@ -129,46 +132,76 @@ open class SqlBlock(
129132
childBlock: SqlBlock,
130133
child: ASTNode,
131134
) {
132-
val lastIndentLevel = groupTopNodeIndexHistory.last().second.indentLevel
135+
val lastIndentLevel =
136+
groupTopNodeIndexHistory
137+
.last()
138+
.second.indent.indentLevel
133139
val latestGroupTopBlock = groupTopNodeIndexHistory.last().second
134140
when (childBlock) {
135141
is SqlKeywordGroupBlock -> {
136-
if (latestGroupTopBlock.indentLevel == IndentType.SUB) {
142+
if (latestGroupTopBlock.indent.indentLevel == IndentType.SUB) {
137143
setParentGroups(
138144
childBlock,
139145
) { history ->
140146
return@setParentGroups latestGroupTopBlock
141147
}
142-
} else if (lastIndentLevel == childBlock.indentLevel) {
148+
} else if (lastIndentLevel == childBlock.indent.indentLevel) {
143149
groupTopNodeIndexHistory.removeLast()
144150
setParentGroups(
145151
childBlock,
146152
) { history ->
147153
return@setParentGroups latestGroupTopBlock.parentBlock
148154
}
149-
} else if (lastIndentLevel < childBlock.indentLevel) {
155+
} else if (lastIndentLevel < childBlock.indent.indentLevel) {
150156
setParentGroups(
151157
childBlock,
152158
) { history ->
153159
return@setParentGroups history.last().second
154160
}
155161
} else {
156-
if (lastIndentLevel == IndentType.JOIN && SqlKeywordUtil.isSecondOptionKeyword(child.text)) {
162+
if (lastIndentLevel == IndentType.JOIN &&
163+
SqlKeywordUtil.isSecondOptionKeyword(child.text)
164+
) {
165+
// left,right < inner,outer < join
157166
setParentGroups(
158167
childBlock,
159168
) { history ->
160169
return@setParentGroups history.last().second
161170
}
162-
} else {
163-
setParentGroups(
164-
childBlock,
165-
) { history ->
166-
return@setParentGroups history.lastOrNull { it.second.indentLevel < childBlock.indentLevel }?.second
167-
}
171+
return
172+
}
173+
174+
setParentGroups(
175+
childBlock,
176+
) { history ->
177+
return@setParentGroups history
178+
.lastOrNull { it.second.indent.indentLevel < childBlock.indent.indentLevel }
179+
?.second
168180
}
169181
}
170182
}
171183

184+
is SqlInlineGroupBlock -> {
185+
// case-end
186+
setParentGroups(
187+
childBlock,
188+
) { history ->
189+
return@setParentGroups history.last().second
190+
}
191+
}
192+
193+
is SqlInlineSecondGroupBlock -> {
194+
groupTopNodeIndexHistory.removeLast()
195+
setParentGroups(
196+
childBlock,
197+
) { history ->
198+
return@setParentGroups latestGroupTopBlock.parentBlock
199+
}
200+
if (childBlock.isEndCase) {
201+
groupTopNodeIndexHistory.removeLast()
202+
}
203+
}
204+
172205
is SqlWordBlock, is SqlOtherBlock, is SqlLineCommentBlock, is SqlBlockCommentBlock -> {
173206
setParentGroups(
174207
childBlock,
@@ -187,7 +220,7 @@ open class SqlBlock(
187220

188221
is SqlRightPatternBlock -> {
189222
val leftIndex =
190-
groupTopNodeIndexHistory.indexOfLast { it.second.indentLevel == IndentType.SUB }
223+
groupTopNodeIndexHistory.indexOfLast { it.second.indent.indentLevel == IndentType.SUB }
191224
if (leftIndex >= 0) {
192225
setParentGroups(
193226
childBlock,
@@ -241,13 +274,15 @@ open class SqlBlock(
241274

242275
if ((
243276
childBlock is SqlNewGroupBlock &&
244-
childBlock.indentLevel.isNewLineGroup() &&
277+
childBlock.indent.indentLevel.isNewLineGroup() &&
245278
!SqlKeywordUtil.isSetLineKeyword(
246279
childBlock.node.text,
247280
parentGroup?.node?.text ?: "",
248281
)
249282
) ||
250283
childBlock is SqlSubGroupBlock ||
284+
childBlock is SqlInlineGroupBlock ||
285+
childBlock is SqlInlineSecondGroupBlock ||
251286
childBlock is SqlColumnDefinitionRawGroupBlock
252287
) {
253288
groupTopNodeIndexHistory.add(Pair(blocks.size - 1, childBlock))
@@ -262,7 +297,7 @@ open class SqlBlock(
262297
}
263298

264299
SqlTypes.LEFT_PAREN -> {
265-
if (lastGroup is SqlCreateKeywordGroupBlock) {
300+
if (lastGroup is SqlCreateKeywordGroupBlock && lastGroup.isCreateTable) {
266301
SqlColumnDefinitionGroupBlock(child, wrap, alignment, spacingBuilder)
267302
} else if (lastGroup is SqlColumnDefinitionRawGroupBlock) {
268303
SqlDataTypeParamBlock(child, wrap, alignment, spacingBuilder)
@@ -280,7 +315,7 @@ open class SqlBlock(
280315
return SqlRightPatternBlock(child, wrap, alignment, spacingBuilder)
281316

282317
SqlTypes.COMMA -> {
283-
when (lastGroup) {
318+
return when (lastGroup) {
284319
is SqlColumnDefinitionGroupBlock, is SqlColumnDefinitionRawGroupBlock ->
285320
SqlColumnDefinitionRawGroupBlock(
286321
child,
@@ -305,14 +340,6 @@ open class SqlBlock(
305340
spacingBuilder,
306341
)
307342

308-
SqlKeywordUtil.isBeforeColumnKeyword(lastGroup.node.text) ->
309-
SqlColumnBlock(
310-
child,
311-
wrap,
312-
alignment,
313-
spacingBuilder,
314-
)
315-
316343
else -> SqlWordBlock(child, wrap, alignment, spacingBuilder)
317344
}
318345
}
@@ -347,6 +374,8 @@ open class SqlBlock(
347374
}
348375

349376
private fun getKeywordBlock(child: ASTNode): SqlBlock {
377+
// Because we haven't yet set the parent-child relationship of the block,
378+
// the parent group references groupTopNodeIndexHistory.
350379
val indentLevel = SqlKeywordUtil.getIndentType(child.text)
351380
if (indentLevel.isNewLineGroup()) {
352381
if (child.text.lowercase() == "create") {
@@ -361,9 +390,25 @@ open class SqlBlock(
361390
SqlJoinGroupBlock(child, wrap, alignment, spacingBuilder)
362391
}
363392
}
393+
if (indentLevel == IndentType.INLINE_SECOND) {
394+
return SqlInlineSecondGroupBlock(child, wrap, alignment, spacingBuilder)
395+
}
364396

365397
return SqlKeywordGroupBlock(child, indentLevel, wrap, alignment, spacingBuilder)
366398
}
399+
400+
val lastGroup = groupTopNodeIndexHistory.lastOrNull()?.second
401+
if (lastGroup is SqlCreateKeywordGroupBlock) {
402+
if (SqlKeywordUtil.isAttachedKeyword(child.text)) {
403+
lastGroup.isCreateTable = child.text.lowercase() == "table"
404+
}
405+
return SqlKeywordBlock(child, indentLevel, wrap, alignment, spacingBuilder)
406+
}
407+
408+
if (indentLevel == IndentType.INLINE) {
409+
return SqlInlineGroupBlock(child, wrap, alignment, spacingBuilder)
410+
}
411+
367412
return SqlKeywordBlock(child, indentLevel, wrap, alignment, spacingBuilder)
368413
}
369414

@@ -439,7 +484,7 @@ open class SqlBlock(
439484
?.let {
440485
val indent =
441486
when (it) {
442-
is SqlKeywordGroupBlock -> Indent.getSpaceIndent(it.indentLen)
487+
is SqlKeywordGroupBlock -> Indent.getSpaceIndent(it.indent.indentLen)
443488
else -> childIndent ?: Indent.getNoneIndent()
444489
}
445490
return ChildAttributes(indent, null)
@@ -450,4 +495,10 @@ open class SqlBlock(
450495
override fun getChildIndent(): Indent? = Indent.getSpaceIndent(4)
451496

452497
override fun isLeaf(): Boolean = myNode.firstChildNode == null
498+
499+
data class ElementIndent(
500+
var indentLevel: IndentType,
501+
var indentLen: Int,
502+
var groupIndentLen: Int,
503+
)
453504
}

0 commit comments

Comments
 (0)