Skip to content

Commit 193f570

Browse files
committed
Fixed the indent adjustment method for normal comments
1 parent 7a24889 commit 193f570

File tree

12 files changed

+80
-42
lines changed

12 files changed

+80
-42
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ import com.intellij.formatting.SpacingBuilder
2424
import com.intellij.formatting.Wrap
2525
import com.intellij.lang.ASTNode
2626
import com.intellij.psi.formatter.common.AbstractBlock
27-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
2827
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2928
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
3029
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
31-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
3230
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
3331
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3432
import org.domaframework.doma.intellij.formatter.util.IndentType
3533
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
3634
import org.domaframework.doma.intellij.psi.SqlTypes
35+
import org.jetbrains.kotlin.psi.psiUtil.startOffset
3736

3837
open class SqlBlock(
3938
node: ASTNode,
@@ -69,7 +68,7 @@ open class SqlBlock(
6968
fun getChildrenTextLen(): Int =
7069
childBlocks.sumOf { child ->
7170
val children =
72-
child.childBlocks.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
71+
child.childBlocks.filter { it !is SqlDefaultCommentBlock }
7372
if (children.isNotEmpty()) {
7473
child
7574
.getChildrenTextLen()
@@ -90,7 +89,7 @@ open class SqlBlock(
9089
): List<SqlBlock> {
9190
val children = childBlocks.dropLast(dropIndex)
9291
if (skipCommentBlock) {
93-
return children.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
92+
return children.filter { it !is SqlDefaultCommentBlock }
9493
}
9594
return children
9695
}
@@ -132,15 +131,22 @@ open class SqlBlock(
132131
(prevBlock.conditionType.isElse() || prevBlock.conditionType.isEnd()) ||
133132
parent.childBlocks.dropLast(1).isEmpty()
134133
}
134+
// Checks for non-breaking keyword combinations, ignoring comment blocks
135135
if (parent is SqlNewGroupBlock) {
136136
val prevWord = prevBlocks.lastOrNull { it !is SqlCommentBlock }
137137
if (SqlKeywordUtil.isSetLineKeyword(getNodeText(), parent.getNodeText()) ||
138138
SqlKeywordUtil.isSetLineKeyword(getNodeText(), prevWord?.getNodeText() ?: "")
139139
) {
140140
return false
141141
}
142+
// Breaks a line if it is a child of itself or preceded by a condition/loop directive
142143
return childBlocks.lastOrNull() is SqlElConditionLoopCommentBlock ||
143-
prevBlocks.lastOrNull() is SqlElConditionLoopCommentBlock
144+
(
145+
prevBlocks.lastOrNull() is SqlElConditionLoopCommentBlock &&
146+
prevBlocks
147+
.last()
148+
.node.psi.startOffset > parent.node.psi.startOffset
149+
)
144150
}
145151
}
146152
return false

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGrou
5151
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
5252
import org.domaframework.doma.intellij.formatter.block.other.SqlEscapeBlock
5353
import org.domaframework.doma.intellij.formatter.block.other.SqlOtherBlock
54+
import org.domaframework.doma.intellij.formatter.block.word.SqlAliasBlock
5455
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
56+
import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock
5557
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
5658
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
5759
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
@@ -314,6 +316,8 @@ class SqlFileBlock(
314316
return
315317
}
316318

319+
if (childBlock is SqlDefaultCommentBlock) return
320+
317321
when (childBlock) {
318322
is SqlKeywordGroupBlock -> {
319323
parentSetProcessor.updateKeywordGroupBlockParentAndAddGroup(
@@ -364,7 +368,7 @@ class SqlFileBlock(
364368
)
365369
}
366370

367-
is SqlWordBlock, is SqlOtherBlock, is SqlDefaultCommentBlock -> {
371+
is SqlWordBlock, is SqlOtherBlock -> {
368372
parentSetProcessor.updateGroupBlockParentAndAddGroup(
369373
childBlock,
370374
)
@@ -546,7 +550,7 @@ class SqlFileBlock(
546550

547551
is SqlDataTypeParamBlock, is SqlFunctionParamBlock -> return SqlCustomSpacingBuilder.nonSpacing
548552

549-
else -> return SqlCustomSpacingBuilder.normalSpacing
553+
// else -> return SqlCustomSpacingBuilder.normalSpacing
550554
}
551555
}
552556

@@ -586,6 +590,10 @@ class SqlFileBlock(
586590
}
587591
}
588592

593+
if (childBlock1 is SqlTableBlock || childBlock1 is SqlAliasBlock) {
594+
return SqlCustomSpacingBuilder.normalSpacing
595+
}
596+
589597
val spacing: Spacing? = customSpacingBuilder?.getCustomSpacing(childBlock1, childBlock2)
590598
return spacing ?: spacingBuilder.getSpacing(this, childBlock1, childBlock2)
591599
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ open class SqlRightPatternBlock(
110110
INDENT_EXPECTED_TYPES,
111111
parent,
112112
) ||
113-
parent.childBlocks.firstOrNull() is SqlValuesGroupBlock
113+
parent.childBlocks.any { it is SqlValuesGroupBlock }
114114
) {
115115
preSpaceRight = true
116116
return
@@ -179,7 +179,7 @@ open class SqlRightPatternBlock(
179179
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
180180
parentBlock?.let { parent ->
181181
if (isExpectedClassType(NEW_LINE_EXPECTED_TYPES, parent) ||
182-
parent.childBlocks.firstOrNull() is SqlValuesGroupBlock
182+
parent.childBlocks.any { it is SqlValuesGroupBlock }
183183
) {
184184
lineBreakAndSpacingType =
185185
if (preSpaceRight) {

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,42 @@ import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import com.intellij.psi.util.PsiTreeUtil
2121
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22-
import org.domaframework.doma.intellij.formatter.block.SqlFileBlock
22+
import org.domaframework.doma.intellij.formatter.util.IndentType
2323
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2424

2525
abstract class SqlDefaultCommentBlock(
2626
node: ASTNode,
2727
context: SqlBlockFormattingContext,
2828
) : SqlCommentBlock(node, context) {
29+
/**
30+
* If this block is the last element, the indentation update process is not called,
31+
* so set the default indentation to 1.
32+
*/
33+
override val indent =
34+
ElementIndent(
35+
IndentType.NONE,
36+
1,
37+
1,
38+
)
39+
2940
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
3041

3142
override fun isLeaf(): Boolean = true
3243

3344
/**
34-
* When the next element block is determined, update the indent to align it with the element below.
45+
* When the next element block is determined,
46+
* update the indent to align it with the element below.
3547
*/
36-
fun updateIndentLen(baseBlock: SqlBlock) {
48+
fun updateIndentLen(
49+
baseBlock: SqlBlock,
50+
groupBlockCount: Int,
51+
) {
3752
indent.indentLen =
38-
if (parentBlock is SqlFileBlock || isSaveSpace(parentBlock)) {
53+
if (isSaveSpace(parentBlock)) {
3954
baseBlock.indent.indentLen
55+
} else if (groupBlockCount <= 2) {
56+
// If it is the top of the file, the indent number should be 0.
57+
0
4058
} else {
4159
1
4260
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import com.intellij.psi.util.elementType
2525
import org.domaframework.doma.intellij.common.util.TypeUtil
2626
import org.domaframework.doma.intellij.extension.expr.isConditionOrLoopDirective
2727
import org.domaframework.doma.intellij.formatter.block.SqlBlock
28-
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
2928
import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
3029
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
3130
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
@@ -204,7 +203,7 @@ class SqlElConditionLoopCommentBlock(
204203
if (lastGroup is SqlSubGroupBlock) {
205204
return lastGroup.childBlocks.dropLast(1).isNotEmpty()
206205
}
207-
return lastGroup?.childBlocks?.any { it !is SqlKeywordBlock && it !is SqlKeywordGroupBlock } == true
206+
return true
208207
}
209208

210209
/**
@@ -275,7 +274,7 @@ class SqlElConditionLoopCommentBlock(
275274
val conditionLoopDirectives: List<SqlElConditionLoopCommentBlock> =
276275
parent
277276
.childBlocks
278-
.mapNotNull { it as? SqlElConditionLoopCommentBlock }
277+
.filterIsInstance<SqlElConditionLoopCommentBlock>()
279278
.filter { it.conditionEnd == null }
280279
val startDirectives =
281280
conditionLoopDirectives.count { it.conditionType.isStartDirective() }

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ package org.domaframework.doma.intellij.formatter.block.group.subgroup
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2121
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
22-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2322
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
2423
import org.domaframework.doma.intellij.formatter.util.IndentType
2524
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -65,9 +64,8 @@ class SqlFunctionParamBlock(
6564

6665
val children =
6766
prevChildren?.dropLast(1)?.filter {
68-
it !is SqlLineCommentBlock &&
69-
it !is SqlBlockCommentBlock &&
70-
it.node != SqlTypes.DOT
67+
it !is SqlDefaultCommentBlock
68+
it.node != SqlTypes.DOT
7169
}
7270
children?.let { prevList ->
7371
return prevList
@@ -89,8 +87,7 @@ class SqlFunctionParamBlock(
8987

9088
val prevChildrenDropLast =
9189
prevChildren?.dropLast(1)?.filter {
92-
it !is SqlLineCommentBlock &&
93-
it !is SqlBlockCommentBlock &&
90+
it !is SqlDefaultCommentBlock &&
9491
it.node.elementType != SqlTypes.DOT
9592
}
9693
?: emptyList()

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ abstract class SqlSubGroupBlock(
102102

103103
override fun isLeaf(): Boolean = true
104104

105-
open fun endGroup() {}
106-
107105
override fun createBlockIndentLen(): Int {
108106
parentBlock?.let { parent ->
109107
if (parent is SqlElConditionLoopCommentBlock) return parent.indent.groupIndentLen

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ package org.domaframework.doma.intellij.formatter.block.group.subgroup
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
21+
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2222
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
23-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2423
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock
2524
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2625
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
@@ -56,7 +55,7 @@ open class SqlSubQueryGroupBlock(
5655
is SqlJoinQueriesGroupBlock -> return parent.indent.indentLen
5756
is SqlJoinGroupBlock -> return parent.indent.groupIndentLen.plus(1)
5857
else -> {
59-
val children = prevChildren?.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
58+
val children = prevChildren?.filter { it !is SqlDefaultCommentBlock }
6059
return children
6160
?.dropLast(1)
6261
?.sumOf { prev ->

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ package org.domaframework.doma.intellij.formatter.block.word
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
21-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
20+
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2221
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
2322
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2423
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
@@ -40,7 +39,7 @@ class SqlFunctionGroupBlock(
4039

4140
override fun createBlockIndentLen(): Int {
4241
parentBlock?.let { parent ->
43-
val children = prevChildren.dropLast(1).filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
42+
val children = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
4443
val prevBlocksLength =
4544
children
4645
.sumOf { prev ->

src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlBlockBuilder.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ package org.domaframework.doma.intellij.formatter.builder
1717

1818
import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
2120
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2221
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
23-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2422
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2523
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2624

2725
open class SqlBlockBuilder {
2826
private val updateDirectiveParentTypes =
2927
listOf(
30-
SqlLineCommentBlock::class,
31-
SqlBlockCommentBlock::class,
28+
SqlDefaultCommentBlock::class,
3229
)
3330

3431
private val originalConditionLoopDirectiveParentType =
@@ -58,11 +55,14 @@ open class SqlBlockBuilder {
5855
* but the indentation is aligned with the next block.
5956
*/
6057
fun updateCommentBlockIndent(nextBlock: SqlBlock) {
61-
if (commentBlocks.isNotEmpty()) {
58+
if (commentBlocks.isNotEmpty() &&
59+
nextBlock.parentBlock != null ||
60+
groupTopNodeIndexHistory.size <= 2
61+
) {
6262
var index = 0
6363
commentBlocks
6464
.forEach { block ->
65-
block.updateIndentLen(nextBlock)
65+
block.updateIndentLen(nextBlock, groupTopNodeIndexHistory.size)
6666
index++
6767
}
6868
commentBlocks.clear()

0 commit comments

Comments
 (0)