@@ -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