Skip to content

Commit b7ad1ff

Browse files
committed
Enhance the implementation of spacing rules within field blocks.
1 parent a67afa5 commit b7ad1ff

File tree

8 files changed

+45
-60
lines changed

8 files changed

+45
-60
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ open class SqlElBlockCommentBlock(
106106
SqlElFieldAccessBlock(
107107
child,
108108
context,
109-
createFieldAccessSpacingBuilder(),
110109
)
111110

112111
SqlTypes.BLOCK_COMMENT_START -> SqlCommentStartBlock(child, context)
@@ -131,26 +130,6 @@ open class SqlElBlockCommentBlock(
131130
else -> SqlUnknownBlock(child, context)
132131
}
133132

134-
private fun createFieldAccessSpacingBuilder(): SqlCustomSpacingBuilder =
135-
SqlCustomSpacingBuilder()
136-
.withSpacing(
137-
SqlTypes.EL_PRIMARY_EXPR,
138-
SqlTypes.DOT,
139-
Spacing.createSpacing(0, 0, 0, false, 0),
140-
).withSpacing(
141-
SqlTypes.DOT,
142-
SqlTypes.EL_IDENTIFIER,
143-
Spacing.createSpacing(0, 0, 0, false, 0),
144-
).withSpacing(
145-
SqlTypes.EL_IDENTIFIER,
146-
SqlTypes.DOT,
147-
Spacing.createSpacing(0, 0, 0, false, 0),
148-
).withSpacing(
149-
SqlTypes.EL_IDENTIFIER,
150-
SqlTypes.EL_PARAMETERS,
151-
Spacing.createSpacing(0, 0, 0, false, 0),
152-
)
153-
154133
protected fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
155134
SqlCustomSpacingBuilder()
156135
.withSpacing(

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class SqlElConditionLoopCommentBlock(
168168
SqlElFieldAccessBlock(
169169
child,
170170
context,
171-
createFieldAccessSpacingBuilder(),
172171
)
173172

174173
SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR ->
@@ -186,26 +185,6 @@ class SqlElConditionLoopCommentBlock(
186185
else -> SqlUnknownBlock(child, context)
187186
}
188187

189-
private fun createFieldAccessSpacingBuilder(): SqlCustomSpacingBuilder =
190-
SqlCustomSpacingBuilder()
191-
.withSpacing(
192-
SqlTypes.EL_PRIMARY_EXPR,
193-
SqlTypes.DOT,
194-
Spacing.createSpacing(0, 0, 0, false, 0),
195-
).withSpacing(
196-
SqlTypes.DOT,
197-
SqlTypes.EL_IDENTIFIER,
198-
Spacing.createSpacing(0, 0, 0, false, 0),
199-
).withSpacing(
200-
SqlTypes.EL_IDENTIFIER,
201-
SqlTypes.DOT,
202-
Spacing.createSpacing(0, 0, 0, false, 0),
203-
).withSpacing(
204-
SqlTypes.EL_IDENTIFIER,
205-
SqlTypes.EL_PARAMETERS,
206-
Spacing.createSpacing(0, 0, 0, false, 0),
207-
)
208-
209188
override fun getSpacing(
210189
child1: Block?,
211190
child2: Block,

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import com.intellij.lang.ASTNode
2121
import com.intellij.psi.PsiWhiteSpace
2222
import com.intellij.psi.formatter.common.AbstractBlock
2323
import org.domaframework.doma.intellij.formatter.block.SqlBlock
24+
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
2425
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2526
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2627
import org.domaframework.doma.intellij.psi.SqlTypes
2728

2829
class SqlElFieldAccessBlock(
2930
node: ASTNode,
3031
private val context: SqlBlockFormattingContext,
31-
private val customSpacingBuilder: SqlCustomSpacingBuilder?,
3232
) : SqlExprBlock(
3333
node,
3434
context,
@@ -48,6 +48,8 @@ class SqlElFieldAccessBlock(
4848

4949
override fun getBlock(child: ASTNode): SqlBlock =
5050
when (child.elementType) {
51+
SqlTypes.EL_IDENTIFIER, SqlTypes.EL_ID_EXPR -> SqlElIdentifierBlock(child, context)
52+
5153
SqlTypes.EL_PRIMARY_EXPR -> {
5254
SqlElPrimaryBlock(child, context)
5355
}
@@ -62,20 +64,13 @@ class SqlElFieldAccessBlock(
6264
SqlElParametersBlock(child, context)
6365

6466
else ->
65-
SqlBlock(
66-
child,
67-
context.wrap,
68-
context.alignment,
69-
context.spacingBuilder,
70-
context.enableFormat,
71-
context.formatMode,
72-
)
67+
SqlUnknownBlock(child, context)
7368
}
7469

7570
override fun getSpacing(
7671
child1: Block?,
7772
child2: Block,
78-
): Spacing? = customSpacingBuilder?.getCustomSpacing(child1, child2) ?: spacingBuilder.getSpacing(this, child1, child2)
73+
): Spacing? = SqlCustomSpacingBuilder.nonSpacing
7974

8075
override fun isLeaf(): Boolean = false
8176
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.expr
1717

18+
import com.intellij.formatting.Block
1819
import com.intellij.formatting.Spacing
1920
import com.intellij.lang.ASTNode
2021
import com.intellij.psi.PsiWhiteSpace
2122
import com.intellij.psi.util.PsiTreeUtil
2223
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2324
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
25+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
2426
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
2527
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2628
import org.domaframework.doma.intellij.psi.SqlElClass
@@ -50,12 +52,20 @@ class SqlElStaticFieldAccessBlock(
5052
SqlTypes.EL_CLASS ->
5153
SqlElClassBlock(child, context, null)
5254

53-
SqlTypes.EL_IDENTIFIER ->
55+
SqlTypes.EL_IDENTIFIER, SqlTypes.EL_ID_EXPR, SqlTypes.EL_PRIMARY_EXPR ->
5456
SqlElIdentifierBlock(child, context)
5557

58+
SqlTypes.EL_PARAMETERS ->
59+
SqlParallelListBlock(child, context)
60+
5661
else -> SqlUnknownBlock(child, context)
5762
}
5863

64+
override fun getSpacing(
65+
child1: Block?,
66+
child2: Block,
67+
): Spacing? = SqlCustomSpacingBuilder.nonSpacing
68+
5969
override fun createSpacingBuilder(): SqlCustomSpacingBuilder =
6070
SqlCustomSpacingBuilder()
6171
.withSpacing(

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.domaframework.doma.intellij.formatter.block.expr
1818
import com.intellij.formatting.Block
1919
import com.intellij.formatting.Spacing
2020
import com.intellij.lang.ASTNode
21+
import com.intellij.psi.PsiWhiteSpace
22+
import com.intellij.psi.formatter.common.AbstractBlock
2123
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2224
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2325

@@ -32,6 +34,19 @@ abstract class SqlExprBlock(
3234
context.enableFormat,
3335
context.formatMode,
3436
) {
37+
override fun buildChildren(): MutableList<AbstractBlock> {
38+
val blocks = mutableListOf<AbstractBlock>()
39+
var child = node.firstChildNode
40+
while (child != null) {
41+
if (child !is PsiWhiteSpace) {
42+
val block = getBlock(child)
43+
blocks.add(block)
44+
}
45+
child = child.treeNext
46+
}
47+
return blocks
48+
}
49+
3550
override fun getSpacing(
3651
child1: Block?,
3752
child2: Block,

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateValueGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SqlUpdateValueGroupBlock(
3232
node,
3333
context,
3434
) {
35-
// TODO:Customize indentation
35+
// TODO Customize indentation
3636
override val offset = 2
3737

3838
override fun setParentGroupBlock(lastGroup: SqlBlock?) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.lang.ASTNode
2121
import com.intellij.psi.formatter.common.AbstractBlock
2222
import org.domaframework.doma.intellij.common.util.TypeUtil
2323
import org.domaframework.doma.intellij.formatter.block.SqlBlock
24+
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
2425
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2526
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2627
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ open class SqlSubQueryGroupBlock(
5656
is SqlJoinGroupBlock -> return parent.indent.groupIndentLen.plus(1)
5757
else -> {
5858
val children = prevChildren?.filter { it !is SqlDefaultCommentBlock }
59-
return children
60-
?.dropLast(1)
61-
?.sumOf { prev ->
59+
// Retrieve the list of child blocks excluding the conditional directive that appears immediately before this block,
60+
// as it is already included as a child block.
61+
val sumChildren =
62+
if (children?.firstOrNull() is SqlElConditionLoopCommentBlock) {
63+
children.drop(1).dropLast(1)
64+
} else {
65+
children?.dropLast(1) ?: emptyList()
66+
}
67+
return sumChildren
68+
.sumOf { prev ->
6269
prev
6370
.getChildrenTextLen()
6471
.plus(prev.getNodeText().length.plus(1))
65-
}?.plus(parent.indent.groupIndentLen)
66-
?.plus(1)
67-
?: offset
72+
}.plus(parent.indent.groupIndentLen)
73+
.plus(1)
6874
}
6975
}
7076
} ?: offset

0 commit comments

Comments
 (0)