Skip to content

Commit 99f3e1f

Browse files
committed
Implement spacing rules within multi-line comment blocks.
1 parent ea92e03 commit 99f3e1f

File tree

9 files changed

+143
-18
lines changed

9 files changed

+143
-18
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
6666
import org.domaframework.doma.intellij.formatter.util.SqlBlockGenerator
6767
import org.domaframework.doma.intellij.psi.SqlTypes
6868

69-
class SqlFileBlock(
69+
open class SqlFileBlock(
7070
node: ASTNode,
7171
wrap: Wrap?,
7272
alignment: Alignment?,
@@ -221,7 +221,7 @@ class SqlFileBlock(
221221
return if (lastGroup is SqlWithCommonTableGroupBlock) {
222222
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
223223
} else {
224-
blockUtil.getBlockCommentBlock(child, createBlockCommentSpacingBuilder())
224+
blockUtil.getBlockCommentBlock(child, createBlockDirectiveCommentSpacingBuilder())
225225
}
226226
}
227227

@@ -416,15 +416,11 @@ class SqlFileBlock(
416416
}
417417

418418
/**
419-
* Creates a spacing builder specifically for block comments.
419+
* Creates a spacing builder specifically for directive block comments.
420420
*/
421-
private fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
421+
protected fun createBlockDirectiveCommentSpacingBuilder(): SqlCustomSpacingBuilder =
422422
SqlCustomSpacingBuilder()
423423
.withSpacing(
424-
SqlTypes.BLOCK_COMMENT_START,
425-
SqlTypes.BLOCK_COMMENT_CONTENT,
426-
Spacing.createSpacing(0, 0, 0, true, 0),
427-
).withSpacing(
428424
SqlTypes.BLOCK_COMMENT_START,
429425
SqlTypes.EL_ID_EXPR,
430426
Spacing.createSpacing(1, 1, 0, true, 0),
@@ -572,10 +568,6 @@ class SqlFileBlock(
572568
SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR,
573569
SqlTypes.BLOCK_COMMENT_END,
574570
Spacing.createSpacing(1, 1, 0, true, 0),
575-
).withSpacing(
576-
SqlTypes.BLOCK_COMMENT_CONTENT,
577-
SqlTypes.BLOCK_COMMENT_END,
578-
Spacing.createSpacing(0, 0, 0, true, 0),
579571
)
580572

581573
/**

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,57 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.comment
1717

18+
import com.intellij.formatting.Block
19+
import com.intellij.formatting.Spacing
1820
import com.intellij.lang.ASTNode
21+
import com.intellij.psi.PsiWhiteSpace
22+
import com.intellij.psi.formatter.common.AbstractBlock
1923
import com.intellij.psi.util.PsiTreeUtil
2024
import org.domaframework.doma.intellij.common.util.StringUtil
2125
import org.domaframework.doma.intellij.formatter.block.SqlBlock
26+
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
27+
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2228
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
29+
import org.domaframework.doma.intellij.psi.SqlTypes
2330

2431
open class SqlBlockCommentBlock(
2532
node: ASTNode,
26-
context: SqlBlockFormattingContext,
33+
private val customSpacingBuilder: SqlCustomSpacingBuilder,
34+
private val context: SqlBlockFormattingContext,
2735
) : SqlDefaultCommentBlock(
2836
node,
2937
context,
3038
) {
39+
override fun buildChildren(): MutableList<AbstractBlock> {
40+
val blocks = mutableListOf<AbstractBlock>()
41+
var child = node.firstChildNode
42+
while (child != null) {
43+
if (child !is PsiWhiteSpace) {
44+
val block = getBlock(child)
45+
blocks.add(block)
46+
}
47+
child = child.treeNext
48+
}
49+
return blocks
50+
}
51+
52+
override fun getBlock(child: ASTNode): SqlBlock {
53+
val elementType = child.elementType
54+
return when (elementType) {
55+
SqlTypes.BLOCK_COMMENT_START -> SqlCommentStartBlock(child, context)
56+
SqlTypes.BLOCK_COMMENT_END -> SqlCommentEndBlock(child, context)
57+
SqlTypes.BLOCK_COMMENT_CONTENT -> SqlCommentContentBlock(child, context)
58+
else -> SqlUnknownBlock(child, context)
59+
}
60+
}
61+
3162
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
3263
PsiTreeUtil.prevLeaf(node.psi)?.text?.contains(StringUtil.LINE_SEPARATE) == true
64+
65+
override fun getSpacing(
66+
child1: Block?,
67+
child2: Block,
68+
): Spacing? =
69+
customSpacingBuilder.getCustomSpacing(child1, child2)
70+
?: SqlCustomSpacingBuilder.normalSpacing
3371
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.domaframework.doma.intellij.formatter.block.comment
2+
3+
import com.intellij.lang.ASTNode
4+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
5+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
6+
7+
class SqlCommentContentBlock(
8+
node: ASTNode,
9+
context: SqlBlockFormattingContext,
10+
) : SqlCommentBlock(node, context) {
11+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
12+
13+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.indentLen ?: 1
14+
15+
override fun createGroupIndentLen(): Int = indent.indentLen
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.domaframework.doma.intellij.formatter.block.comment
2+
3+
import com.intellij.lang.ASTNode
4+
import com.intellij.psi.PsiComment
5+
import com.intellij.psi.util.PsiTreeUtil
6+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
7+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
8+
9+
class SqlCommentEndBlock(
10+
node: ASTNode,
11+
context: SqlBlockFormattingContext,
12+
) : SqlCommentSeparateBlock(node, context) {
13+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
14+
parentBlock?.let { parent ->
15+
val contents =
16+
PsiTreeUtil.getChildOfType<PsiComment>(parent.node.psi, PsiComment::class.java)
17+
return contents != null
18+
}
19+
return false
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.domaframework.doma.intellij.formatter.block.comment
2+
3+
import com.intellij.lang.ASTNode
4+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
5+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
6+
7+
abstract class SqlCommentSeparateBlock(
8+
node: ASTNode,
9+
context: SqlBlockFormattingContext,
10+
) : SqlBlock(
11+
node,
12+
context.wrap,
13+
context.alignment,
14+
context.spacingBuilder,
15+
context.enableFormat,
16+
context.formatMode,
17+
) {
18+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
19+
20+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.indentLen ?: 0
21+
22+
override fun createGroupIndentLen(): Int = indent.indentLen
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.domaframework.doma.intellij.formatter.block.comment
2+
3+
import com.intellij.lang.ASTNode
4+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
5+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
6+
7+
class SqlCommentStartBlock(
8+
node: ASTNode,
9+
context: SqlBlockFormattingContext,
10+
) : SqlCommentSeparateBlock(node, context) {
11+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
12+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ open class SqlElBlockCommentBlock(
108108
createFieldAccessSpacingBuilder(),
109109
)
110110

111+
SqlTypes.BLOCK_COMMENT_START -> SqlCommentStartBlock(child, context)
112+
113+
SqlTypes.BLOCK_COMMENT_END -> SqlCommentEndBlock(child, context)
114+
111115
SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR ->
112116
SqlElStaticFieldAccessBlock(
113117
child,
@@ -121,7 +125,7 @@ open class SqlElBlockCommentBlock(
121125
)
122126

123127
SqlTypes.BLOCK_COMMENT_CONTENT ->
124-
SqlBlockCommentBlock(child, context)
128+
SqlBlockCommentBlock(child, createBlockCommentSpacingBuilder(), context)
125129

126130
else -> SqlUnknownBlock(child, context)
127131
}
@@ -146,6 +150,14 @@ open class SqlElBlockCommentBlock(
146150
Spacing.createSpacing(0, 0, 0, false, 0),
147151
)
148152

153+
protected fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
154+
SqlCustomSpacingBuilder()
155+
.withSpacing(
156+
SqlTypes.BLOCK_COMMENT_START,
157+
SqlTypes.BLOCK_COMMENT_CONTENT,
158+
Spacing.createSpacing(1, 1, 0, false, 0),
159+
)
160+
149161
override fun getSpacing(
150162
child1: Block?,
151163
child2: Block,

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ class SqlElConditionLoopCommentBlock(
159159
context,
160160
)
161161

162-
SqlTypes.BLOCK_COMMENT_CONTENT ->
163-
SqlBlockCommentBlock(child, context)
164-
165162
else -> SqlUnknownBlock(child, context)
166163
}
167164

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockGenerator.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.util
1717

1818
import com.intellij.formatting.Alignment
1919
import com.intellij.formatting.FormattingMode
20+
import com.intellij.formatting.Spacing
2021
import com.intellij.formatting.SpacingBuilder
2122
import com.intellij.formatting.Wrap
2223
import com.intellij.lang.ASTNode
@@ -72,6 +73,7 @@ import org.domaframework.doma.intellij.formatter.handler.NotQueryGroupHandler
7273
import org.domaframework.doma.intellij.formatter.handler.UpdateClauseHandler
7374
import org.domaframework.doma.intellij.formatter.handler.WithClauseHandler
7475
import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
76+
import org.domaframework.doma.intellij.psi.SqlTypes
7577

7678
data class SqlBlockFormattingContext(
7779
val wrap: Wrap?,
@@ -451,7 +453,7 @@ class SqlBlockGenerator(
451453
blockCommentSpacingBuilder: SqlCustomSpacingBuilder?,
452454
): SqlCommentBlock {
453455
if (PsiTreeUtil.getChildOfType(child.psi, PsiComment::class.java) != null) {
454-
return SqlBlockCommentBlock(child, sqlBlockFormattingCtx)
456+
return SqlBlockCommentBlock(child, createBlockCommentSpacingBuilder(), sqlBlockFormattingCtx)
455457
}
456458
if (child.psi is SqlCustomElCommentExpr &&
457459
(child.psi as SqlCustomElCommentExpr).isConditionOrLoopDirective()
@@ -468,4 +470,16 @@ class SqlBlockGenerator(
468470
blockCommentSpacingBuilder,
469471
)
470472
}
473+
474+
private fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
475+
SqlCustomSpacingBuilder()
476+
.withSpacing(
477+
SqlTypes.BLOCK_COMMENT_START,
478+
SqlTypes.BLOCK_COMMENT_CONTENT,
479+
Spacing.createSpacing(0, 0, 0, true, 0),
480+
).withSpacing(
481+
SqlTypes.BLOCK_COMMENT_CONTENT,
482+
SqlTypes.BLOCK_COMMENT_END,
483+
Spacing.createSpacing(0, 0, 0, true, 0),
484+
)
471485
}

0 commit comments

Comments
 (0)