Skip to content

Commit 24f87ab

Browse files
committed
Refactor SQL comment block classes to improve structure and indentation handling
1 parent bd4ea51 commit 24f87ab

27 files changed

+105
-130
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import com.intellij.formatting.Wrap
2525
import com.intellij.lang.ASTNode
2626
import com.intellij.psi.formatter.common.AbstractBlock
2727
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
28+
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
29+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2830
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
29-
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
3031
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3132
import org.domaframework.doma.intellij.formatter.util.IndentType
3233
import org.domaframework.doma.intellij.psi.SqlTypes
@@ -123,7 +124,7 @@ open class SqlBlock(
123124
parentBlock?.let { parent ->
124125
if (parent is SqlElConditionLoopCommentBlock) {
125126
val prevBlock =
126-
prevBlocks.lastOrNull { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
127+
prevBlocks.lastOrNull { it !is SqlDefaultCommentBlock }
127128
return prevBlock is SqlElConditionLoopCommentBlock &&
128129
(prevBlock.conditionType.isElse() || prevBlock.conditionType.isEnd()) ||
129130
parent.childBlocks.dropLast(1).isEmpty()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.common.util.TypeUtil
21+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import com.intellij.psi.formatter.common.AbstractBlock
2929
import org.domaframework.doma.intellij.common.util.TypeUtil
3030
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
3131
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
32+
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
33+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElBlockCommentBlock
34+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
3235
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
33-
import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
34-
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
3536
import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock
3637
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
3738
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
@@ -266,7 +267,11 @@ class SqlFileBlock(
266267
commentBlock,
267268
)
268269
} else {
269-
blockBuilder.addCommentBlock(commentBlock)
270+
(commentBlock as? SqlDefaultCommentBlock)?.let {
271+
blockBuilder.addCommentBlock(
272+
commentBlock,
273+
)
274+
}
270275
}
271276
}
272277

@@ -360,7 +365,7 @@ class SqlFileBlock(
360365
)
361366
}
362367

363-
is SqlWordBlock, is SqlOtherBlock, is SqlLineCommentBlock, is SqlBlockCommentBlock -> {
368+
is SqlWordBlock, is SqlOtherBlock, is SqlDefaultCommentBlock -> {
364369
parentSetProcessor.updateGroupBlockParentAndAddGroup(
365370
childBlock,
366371
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package org.domaframework.doma.intellij.formatter.block
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2021
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
21-
import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2323
open class SqlBlockCommentBlock(
2424
node: ASTNode,
2525
context: SqlBlockFormattingContext,
26-
) : SqlCommentBlock(
26+
) : SqlDefaultCommentBlock(
2727
node,
2828
context,
2929
) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class SqlCommentBlock(
4444
super.setParentGroupBlock(lastGroup)
4545
indent.indentLevel = IndentType.NONE
4646
indent.indentLen = createBlockIndentLen()
47-
indent.groupIndentLen = 0
47+
indent.groupIndentLen = createGroupIndentLen()
4848
}
4949

5050
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
@@ -61,4 +61,6 @@ abstract class SqlCommentBlock(
6161
}
6262
return 0
6363
}
64+
65+
override fun createGroupIndentLen(): Int = 0
6466
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter.block.comment
17+
18+
import com.intellij.lang.ASTNode
19+
import com.intellij.psi.formatter.common.AbstractBlock
20+
import com.intellij.psi.util.PsiTreeUtil
21+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22+
import org.domaframework.doma.intellij.formatter.block.SqlFileBlock
23+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
24+
25+
abstract class SqlDefaultCommentBlock(
26+
node: ASTNode,
27+
context: SqlBlockFormattingContext,
28+
) : SqlCommentBlock(node, context) {
29+
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
30+
31+
override fun isLeaf(): Boolean = true
32+
33+
/**
34+
* When the next element block is determined, update the indent to align it with the element below.
35+
*/
36+
fun updateIndentLen(baseBlock: SqlBlock) {
37+
indent.indentLen =
38+
if (parentBlock is SqlFileBlock || isSaveSpace(parentBlock)) {
39+
baseBlock.indent.indentLen
40+
} else {
41+
1
42+
}
43+
}
44+
45+
override fun isSaveSpace(lastGroup: SqlBlock?) = PsiTreeUtil.prevLeaf(node.psi)?.text?.contains("\n") == true
46+
}
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.expr
16+
package org.domaframework.doma.intellij.formatter.block.comment
1717

1818
import com.intellij.formatting.Block
1919
import com.intellij.formatting.Spacing
@@ -25,12 +25,12 @@ import com.intellij.psi.util.elementType
2525
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2626
import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
2727
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
28-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
29-
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
28+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFieldAccessBlock
29+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFunctionCallBlock
30+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElStaticFieldAccessBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
3233
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
33-
import org.domaframework.doma.intellij.formatter.util.IndentType
3434
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
3535
import org.domaframework.doma.intellij.psi.SqlElElseifDirective
3636
import org.domaframework.doma.intellij.psi.SqlElForDirective
@@ -53,20 +53,6 @@ open class SqlElBlockCommentBlock(
5353

5454
val directiveType: SqlElCommentDirectiveType = initDirectiveType()
5555

56-
override val indent =
57-
ElementIndent(
58-
IndentType.NONE,
59-
0,
60-
0,
61-
)
62-
63-
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
64-
super.setParentGroupBlock(lastGroup)
65-
indent.indentLevel = IndentType.NONE
66-
indent.indentLen = createBlockIndentLen()
67-
indent.groupIndentLen = 0
68-
}
69-
7056
private fun initDirectiveType(): SqlElCommentDirectiveType {
7157
val element = this.node.psi
7258
val contentElement = PsiTreeUtil.firstChild(element).nextSibling
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.expr
16+
package org.domaframework.doma.intellij.formatter.block.comment
1717

1818
import com.intellij.formatting.Block
1919
import com.intellij.formatting.Spacing
@@ -29,15 +29,15 @@ import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock
2929
import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
3030
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
3131
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
32-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
33-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
32+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFieldAccessBlock
33+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElFunctionCallBlock
34+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElStaticFieldAccessBlock
3435
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
3536
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
3637
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
3738
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
3839
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
3940
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
40-
import org.domaframework.doma.intellij.formatter.util.IndentType
4141
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
4242
import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
4343
import org.domaframework.doma.intellij.psi.SqlElForDirective
@@ -95,13 +95,6 @@ class SqlElConditionLoopCommentBlock(
9595
return SqlConditionLoopCommentBlockType.UNKNOWN
9696
}
9797

98-
override val indent =
99-
ElementIndent(
100-
IndentType.NONE,
101-
0,
102-
0,
103-
)
104-
10598
/**
10699
* Initially, set a **provisional indentation level** for conditional directives.
107100
*
@@ -110,9 +103,6 @@ class SqlElConditionLoopCommentBlock(
110103
*/
111104
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
112105
super.setParentGroupBlock(lastGroup)
113-
indent.indentLevel = IndentType.NONE
114-
indent.indentLen = createBlockIndentLen()
115-
indent.groupIndentLen = createGroupIndentLen()
116106

117107
childBlocks.forEach { child ->
118108
if (child is SqlElConditionLoopCommentBlock && child.conditionType.isStartDirective()) {
@@ -253,7 +243,7 @@ class SqlElConditionLoopCommentBlock(
253243
}
254244
}
255245
return if (TypeUtil.isExpectedClassType(
256-
SqlRightPatternBlock.NOT_INSERT_SPACE_TYPES,
246+
SqlRightPatternBlock.NOT_INDENT_EXPECTED_TYPES,
257247
parent,
258248
)
259249
) {

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

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,12 @@
1616
package org.domaframework.doma.intellij.formatter.block.comment
1717

1818
import com.intellij.lang.ASTNode
19-
import com.intellij.psi.formatter.common.AbstractBlock
20-
import com.intellij.psi.util.PsiTreeUtil
21-
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
2319
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2420

2521
open class SqlLineCommentBlock(
2622
node: ASTNode,
2723
context: SqlBlockFormattingContext,
28-
) : SqlCommentBlock(
24+
) : SqlDefaultCommentBlock(
2925
node,
3026
context,
31-
) {
32-
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
33-
super.setParentGroupBlock(lastGroup)
34-
indent.indentLen = createBlockIndentLen()
35-
}
36-
37-
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
38-
39-
override fun isLeaf(): Boolean = true
40-
41-
override fun createBlockIndentLen(): Int {
42-
val prevElement = PsiTreeUtil.prevLeaf(node.psi, false)
43-
if (prevElement?.text?.contains("\n") != true &&
44-
prevElement != null &&
45-
PsiTreeUtil.prevLeaf(prevElement) !is SqlLineCommentBlock
46-
) {
47-
return 1
48-
}
49-
parentBlock?.let { parent ->
50-
if (parent is SqlSubQueryGroupBlock) {
51-
if (parent.getChildBlocksDropLast().isEmpty()) {
52-
return 0
53-
}
54-
if (parent.isFirstLineComment) {
55-
return parent.indent.groupIndentLen.minus(2)
56-
}
57-
}
58-
return parent.indent.indentLen
59-
}
60-
return 0
61-
}
62-
63-
override fun isSaveSpace(lastGroup: SqlBlock?) = PsiTreeUtil.prevLeaf(node.psi)?.text?.contains("\n") == true
64-
}
27+
)

0 commit comments

Comments
 (0)