@@ -26,6 +26,7 @@ import com.intellij.lang.ASTNode
2626import com.intellij.psi.PsiWhiteSpace
2727import com.intellij.psi.formatter.common.AbstractBlock
2828import org.domaframework.doma.intellij.formatter.IndentType
29+ import org.domaframework.doma.intellij.formatter.SqlBlockBuilder
2930import org.domaframework.doma.intellij.formatter.SqlCustomSpacingBuilder
3031import org.domaframework.doma.intellij.formatter.SqlKeywordUtil
3132import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
@@ -72,7 +73,7 @@ open class SqlBlock(
7273 0 ,
7374 )
7475
75- private val groupTopNodeIndexHistory = mutableListOf< Pair < Int , SqlBlock >> ()
76+ private val blockBuilder = SqlBlockBuilder ()
7677
7778 protected open val pendingCommentBlocks = mutableListOf<SqlBlock >()
7879
@@ -81,12 +82,12 @@ open class SqlBlock(
8182
8283 var child = node.firstChildNode
8384 var prevNonWhiteSpaceNode: ASTNode ? = null
84- groupTopNodeIndexHistory.add (Pair (0 , this ))
85+ blockBuilder.addGroupTopNodeIndexHistory (Pair (0 , this ))
8586 while (child != null ) {
8687 if (child !is PsiWhiteSpace ) {
8788 val childBlock = getBlock(child)
8889 if (blocks.isNotEmpty() && blocks.last() is SqlWhitespaceBlock ) {
89- val lastGroup = groupTopNodeIndexHistory.lastOrNull ()?.second
90+ val lastGroup = blockBuilder.getLastGroupTopNodeIndexHistory ()?.second
9091 if (isSaveWhiteSpace(childBlock, child, lastGroup)) {
9192 val whiteBlock = blocks.last() as SqlBlock
9293 whiteBlock.parentBlock = lastGroup
@@ -142,25 +143,31 @@ open class SqlBlock(
142143 childBlock : SqlBlock ,
143144 child : ASTNode ,
144145 ) {
145- val lastIndentLevel =
146- groupTopNodeIndexHistory
147- .last()
148- .second.indent.indentLevel
149- val latestGroupTopBlock = groupTopNodeIndexHistory.last().second
146+ val lastGroupBlock = blockBuilder.getLastGroupTopNodeIndexHistory()?.second
147+ val lastIndentLevel = lastGroupBlock?.indent?.indentLevel
148+ if (lastGroupBlock == null || lastIndentLevel == null ) {
149+ setParentGroups(
150+ childBlock,
151+ ) { history ->
152+ return @setParentGroups null
153+ }
154+ return
155+ }
156+
150157 when (childBlock) {
151158 is SqlKeywordGroupBlock -> {
152- if (latestGroupTopBlock .indent.indentLevel == IndentType .SUB ) {
159+ if (lastGroupBlock .indent.indentLevel == IndentType .SUB ) {
153160 setParentGroups(
154161 childBlock,
155162 ) { history ->
156- return @setParentGroups latestGroupTopBlock
163+ return @setParentGroups lastGroupBlock
157164 }
158165 } else if (lastIndentLevel == childBlock.indent.indentLevel) {
159- groupTopNodeIndexHistory.removeLast ()
166+ blockBuilder.removeLastGroupTopNodeIndexHistory ()
160167 setParentGroups(
161168 childBlock,
162169 ) { history ->
163- return @setParentGroups latestGroupTopBlock .parentBlock
170+ return @setParentGroups lastGroupBlock .parentBlock
164171 }
165172 } else if (lastIndentLevel < childBlock.indent.indentLevel) {
166173 setParentGroups(
@@ -194,11 +201,11 @@ open class SqlBlock(
194201 is SqlColumnGroupBlock -> {
195202 when (lastIndentLevel) {
196203 childBlock.indent.indentLevel -> {
197- groupTopNodeIndexHistory.removeLast ()
204+ blockBuilder.removeLastGroupTopNodeIndexHistory ()
198205 setParentGroups(
199206 childBlock,
200207 ) { history ->
201- return @setParentGroups latestGroupTopBlock .parentBlock
208+ return @setParentGroups lastGroupBlock .parentBlock
202209 }
203210 }
204211 else -> {
@@ -223,27 +230,23 @@ open class SqlBlock(
223230 is SqlInlineSecondGroupBlock -> {
224231 if (childBlock.isEndCase) {
225232 val inlineIndex =
226- groupTopNodeIndexHistory.indexOfLast { it.second.indent.indentLevel == IndentType .INLINE }
233+ blockBuilder.getGroupTopNodeIndexByIndentType( IndentType .INLINE )
227234 if (inlineIndex >= 0 ) {
228235 setParentGroups(
229236 childBlock,
230237 ) { history ->
231238 return @setParentGroups history[inlineIndex].second
232239 }
233- groupTopNodeIndexHistory
234- .subList(
235- inlineIndex,
236- groupTopNodeIndexHistory.size,
237- ).clear()
240+ blockBuilder.clearSubListGroupTopNodeIndexHistory(inlineIndex)
238241 }
239242 return
240243 }
241244 if (lastIndentLevel == IndentType .INLINE_SECOND ) {
242- groupTopNodeIndexHistory.removeLast ()
245+ blockBuilder.removeLastGroupTopNodeIndexHistory ()
243246 setParentGroups(
244247 childBlock,
245248 ) { history ->
246- return @setParentGroups latestGroupTopBlock .parentBlock
249+ return @setParentGroups lastGroupBlock .parentBlock
247250 }
248251 return
249252 }
@@ -271,19 +274,14 @@ open class SqlBlock(
271274 }
272275
273276 is SqlRightPatternBlock -> {
274- val leftIndex =
275- groupTopNodeIndexHistory.indexOfLast { it.second.indent.indentLevel == IndentType .SUB }
277+ val leftIndex = blockBuilder.getGroupTopNodeIndexByIndentType(IndentType .SUB )
276278 if (leftIndex >= 0 ) {
277279 setParentGroups(
278280 childBlock,
279281 ) { history ->
280282 return @setParentGroups history[leftIndex].second
281283 }
282- groupTopNodeIndexHistory
283- .subList(
284- leftIndex,
285- groupTopNodeIndexHistory.size,
286- ).clear()
284+ blockBuilder.clearSubListGroupTopNodeIndexHistory(leftIndex)
287285 }
288286 pendingCommentBlocks.clear()
289287 }
@@ -297,13 +295,13 @@ open class SqlBlock(
297295 }
298296
299297 is SqlColumnDefinitionRawGroupBlock -> {
300- if (latestGroupTopBlock is SqlColumnDefinitionRawGroupBlock ) {
301- groupTopNodeIndexHistory.removeLast ()
298+ if (lastGroupBlock is SqlColumnDefinitionRawGroupBlock ) {
299+ blockBuilder.removeLastGroupTopNodeIndexHistory ()
302300 }
303301 setParentGroups(
304302 childBlock,
305303 ) { history ->
306- return @setParentGroups groupTopNodeIndexHistory .last().second
304+ return @setParentGroups history .last().second
307305 }
308306 }
309307
@@ -321,7 +319,7 @@ open class SqlBlock(
321319 childBlock : SqlBlock ,
322320 getParentGroup : (MutableList <Pair <Int , SqlBlock >>) -> SqlBlock ? ,
323321 ) {
324- val parentGroup = getParentGroup(groupTopNodeIndexHistory )
322+ val parentGroup = getParentGroup(blockBuilder.getGroupTopNodeIndexHistory() as MutableList < Pair < Int , SqlBlock >> )
325323 childBlock.setParentGroupBlock(parentGroup)
326324
327325 if ((
@@ -337,12 +335,12 @@ open class SqlBlock(
337335 childBlock is SqlInlineSecondGroupBlock ||
338336 childBlock is SqlColumnDefinitionRawGroupBlock
339337 ) {
340- groupTopNodeIndexHistory.add (Pair (blocks.size - 1 , childBlock))
338+ blockBuilder.addGroupTopNodeIndexHistory (Pair (blocks.size - 1 , childBlock))
341339 }
342340 }
343341
344342 open fun getBlock (child : ASTNode ): SqlBlock {
345- val lastGroup = groupTopNodeIndexHistory.lastOrNull ()?.second
343+ val lastGroup = blockBuilder.getLastGroupTopNodeIndexHistory ()?.second
346344 return when (child.elementType) {
347345 SqlTypes .KEYWORD -> {
348346 return getKeywordBlock(child)
@@ -431,14 +429,15 @@ open class SqlBlock(
431429 // Because we haven't yet set the parent-child relationship of the block,
432430 // the parent group references groupTopNodeIndexHistory.
433431 val indentLevel = SqlKeywordUtil .getIndentType(child.text)
432+ val lastGroupBlock = blockBuilder.getLastGroupTopNodeIndexHistory()?.second
434433 if (indentLevel.isNewLineGroup()) {
435434 if (child.text.lowercase() == " create" ) {
436435 return SqlCreateKeywordGroupBlock (child, wrap, alignment, spacingBuilder)
437436 }
438437 if (indentLevel == IndentType .JOIN ) {
439438 return if (SqlKeywordUtil .isJoinKeyword(child.text)) {
440439 SqlJoinGroupBlock (child, wrap, alignment, spacingBuilder)
441- } else if (groupTopNodeIndexHistory.last().second is SqlJoinGroupBlock ) {
440+ } else if (lastGroupBlock is SqlJoinGroupBlock ) {
442441 SqlKeywordBlock (child, IndentType .ATTACHED , wrap, alignment, spacingBuilder)
443442 } else {
444443 SqlJoinGroupBlock (child, wrap, alignment, spacingBuilder)
@@ -451,11 +450,8 @@ open class SqlBlock(
451450 return SqlKeywordGroupBlock (child, indentLevel, wrap, alignment, spacingBuilder)
452451 }
453452
454- val lastGroup = groupTopNodeIndexHistory.lastOrNull()?.second
455- if (lastGroup is SqlCreateKeywordGroupBlock ) {
456- if (SqlKeywordUtil .isAttachedKeyword(child.text)) {
457- lastGroup.isCreateTable = child.text.lowercase() == " table"
458- }
453+ if (lastGroupBlock is SqlCreateKeywordGroupBlock ) {
454+ lastGroupBlock.setCreateTableGroup(child.text)
459455 return SqlKeywordBlock (child, indentLevel, wrap, alignment, spacingBuilder)
460456 }
461457
0 commit comments