@@ -26,11 +26,12 @@ import com.intellij.formatting.Wrap
2626import com.intellij.lang.ASTNode
2727import com.intellij.psi.PsiWhiteSpace
2828import com.intellij.psi.formatter.common.AbstractBlock
29- import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
29+ import org.domaframework.doma.intellij.common.util.TypeUtil
3030import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
31+ import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
32+ import org.domaframework.doma.intellij.formatter.block.comment.SqlElBlockCommentBlock
33+ import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
3134import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
32- import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
33- import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
3435import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock
3536import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
3637import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
@@ -50,15 +51,17 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGrou
5051import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
5152import org.domaframework.doma.intellij.formatter.block.other.SqlEscapeBlock
5253import org.domaframework.doma.intellij.formatter.block.other.SqlOtherBlock
54+ import org.domaframework.doma.intellij.formatter.block.word.SqlAliasBlock
5355import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
56+ import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
5457import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
5558import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
5659import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
60+ import org.domaframework.doma.intellij.formatter.handler.CreateClauseHandler
5761import org.domaframework.doma.intellij.formatter.processor.SqlSetParentGroupProcessor
58- import org.domaframework.doma.intellij.formatter.util.CreateTableUtil
5962import org.domaframework.doma.intellij.formatter.util.IndentType
6063import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
61- import org.domaframework.doma.intellij.formatter.util.SqlBlockUtil
64+ import org.domaframework.doma.intellij.formatter.util.SqlBlockGenerator
6265import org.domaframework.doma.intellij.psi.SqlTypes
6366
6467class SqlFileBlock (
@@ -77,11 +80,22 @@ class SqlFileBlock(
7780 enableFormat,
7881 formatMode,
7982 ) {
83+ override val indent =
84+ ElementIndent (
85+ IndentType .FILE ,
86+ 0 ,
87+ 0 ,
88+ )
89+
90+ override fun setParentGroupBlock (lastGroup : SqlBlock ? ) {
91+ super .setParentGroupBlock(null )
92+ }
93+
8094 private val blocks = mutableListOf<AbstractBlock >()
8195
8296 private val blockBuilder = SqlBlockBuilder ()
8397 private val parentSetProcessor = SqlSetParentGroupProcessor (blockBuilder)
84- private val blockUtil = SqlBlockUtil (this , isEnableFormat(), formatMode)
98+ private val blockUtil = SqlBlockGenerator (this , isEnableFormat(), formatMode)
8599
86100 private val pendingCommentBlocks = mutableListOf<SqlBlock >()
87101
@@ -251,7 +265,11 @@ class SqlFileBlock(
251265 commentBlock,
252266 )
253267 } else {
254- blockBuilder.addCommentBlock(commentBlock)
268+ (commentBlock as ? SqlDefaultCommentBlock )?.let {
269+ blockBuilder.addCommentBlock(
270+ commentBlock,
271+ )
272+ }
255273 }
256274 }
257275
@@ -295,6 +313,8 @@ class SqlFileBlock(
295313 return
296314 }
297315
316+ if (childBlock is SqlDefaultCommentBlock ) return
317+
298318 when (childBlock) {
299319 is SqlKeywordGroupBlock -> {
300320 parentSetProcessor.updateKeywordGroupBlockParentAndAddGroup(
@@ -345,7 +365,7 @@ class SqlFileBlock(
345365 )
346366 }
347367
348- is SqlWordBlock , is SqlOtherBlock , is SqlLineCommentBlock , is SqlBlockCommentBlock -> {
368+ is SqlWordBlock , is SqlOtherBlock -> {
349369 parentSetProcessor.updateGroupBlockParentAndAddGroup(
350370 childBlock,
351371 )
@@ -440,6 +460,25 @@ class SqlFileBlock(
440460 return SqlCustomSpacingBuilder ().getSpacing(childBlock2)
441461 }
442462
463+ if (childBlock1 is SqlWhitespaceBlock && childBlock2.parentBlock is SqlElConditionLoopCommentBlock ) {
464+ val child1 = childBlock2.parentBlock as SqlElConditionLoopCommentBlock
465+ SqlCustomSpacingBuilder ()
466+ .getSpacingElDirectiveComment(child1, childBlock2)
467+ ?.let { return it }
468+ }
469+
470+ if (childBlock1 is SqlElBlockCommentBlock && childBlock2 !is SqlRightPatternBlock ) {
471+ SqlCustomSpacingBuilder ()
472+ .getSpacingElDirectiveComment(childBlock1, childBlock2)
473+ ?.let { return it }
474+ }
475+
476+ if (childBlock2 is SqlRightPatternBlock ) {
477+ return SqlCustomSpacingBuilder ().getSpacingRightPattern(
478+ childBlock2,
479+ )
480+ }
481+
443482 if (childBlock2 is SqlWithColumnGroupBlock ) {
444483 return SqlCustomSpacingBuilder .normalSpacing
445484 }
@@ -454,8 +493,15 @@ class SqlFileBlock(
454493 }
455494
456495 if (childBlock2 is SqlElBlockCommentBlock ) {
496+ if (TypeUtil .isExpectedClassType(
497+ SqlRightPatternBlock .NOT_INDENT_EXPECTED_TYPES ,
498+ childBlock1,
499+ )
500+ ) {
501+ return SqlCustomSpacingBuilder .nonSpacing
502+ }
457503 return when (childBlock1) {
458- is SqlElBlockCommentBlock , is SqlWhitespaceBlock -> {
504+ is SqlWhitespaceBlock -> {
459505 SqlCustomSpacingBuilder ().getSpacing(childBlock2)
460506 }
461507
@@ -488,14 +534,6 @@ class SqlFileBlock(
488534 return SqlCustomSpacingBuilder ().getSpacing(childBlock2)
489535 }
490536
491- if (childBlock1 is SqlWhitespaceBlock ) {
492- when (childBlock2) {
493- is SqlBlockCommentBlock , is SqlLineCommentBlock , is SqlNewGroupBlock -> {
494- return SqlCustomSpacingBuilder ().getSpacing(childBlock2)
495- }
496- }
497- }
498-
499537 if (childBlock2 is SqlNewGroupBlock ) {
500538 if (childBlock1 is SqlSubGroupBlock && childBlock2.indent.indentLevel == IndentType .ATTACHED ) {
501539 return SqlCustomSpacingBuilder .nonSpacing
@@ -509,12 +547,14 @@ class SqlFileBlock(
509547
510548 is SqlDataTypeParamBlock , is SqlFunctionParamBlock -> return SqlCustomSpacingBuilder .nonSpacing
511549
512- else -> return SqlCustomSpacingBuilder .normalSpacing
550+ // else -> return SqlCustomSpacingBuilder.normalSpacing
513551 }
514552 }
515553
516554 // Create Table Column Definition Raw Group Block
517- CreateTableUtil .getColumnDefinitionRawGroupSpacing(childBlock1, childBlock2)?.let { return it }
555+ CreateClauseHandler
556+ .getColumnDefinitionRawGroupSpacing(childBlock1, childBlock2)
557+ ?.let { return it }
518558
519559 when (childBlock2) {
520560 is SqlColumnDefinitionRawGroupBlock ->
@@ -523,18 +563,32 @@ class SqlFileBlock(
523563 childBlock2,
524564 )?.let { return it }
525565
526- is SqlRightPatternBlock -> return SqlCustomSpacingBuilder ().getSpacingRightPattern(
527- childBlock2,
528- )
529-
530566 is SqlColumnBlock ->
531567 SqlCustomSpacingBuilder ()
532568 .getSpacingColumnDefinition(childBlock2)
533569 ?.let { return it }
534570 }
535571
536572 if (childBlock1 is SqlBlock && (childBlock2 is SqlCommaBlock || childBlock2 is SqlColumnRawGroupBlock )) {
537- SqlCustomSpacingBuilder ().getSpacingWithIndentComma(childBlock1, childBlock2)?.let { return it }
573+ SqlCustomSpacingBuilder ()
574+ .getSpacingWithIndentComma(childBlock1, childBlock2)
575+ ?.let { return it }
576+ }
577+
578+ // First apply spacing logic for blocks under specific conditions,
579+ // then execute the general spacing logic for post-line-break blocks at the end.
580+ if (childBlock1 is SqlWhitespaceBlock ) {
581+ return when (childBlock2) {
582+ is SqlDefaultCommentBlock , is SqlNewGroupBlock -> {
583+ SqlCustomSpacingBuilder ().getSpacing(childBlock2)
584+ }
585+
586+ else -> SqlCustomSpacingBuilder ().getSpacing(childBlock2)
587+ }
588+ }
589+
590+ if (childBlock1 is SqlTableBlock || childBlock1 is SqlAliasBlock ) {
591+ return SqlCustomSpacingBuilder .normalSpacing
538592 }
539593
540594 val spacing: Spacing ? = customSpacingBuilder?.getCustomSpacing(childBlock1, childBlock2)
0 commit comments