Skip to content

Commit 1fd106b

Browse files
committed
Add Variable Block Comment Indent
1 parent 67f9bf4 commit 1fd106b

File tree

6 files changed

+118
-23
lines changed

6 files changed

+118
-23
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.intellij.formatting.Wrap
2222
import com.intellij.lang.ASTNode
2323
import com.intellij.psi.formatter.common.AbstractBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
25-
import org.domaframework.doma.intellij.psi.SqlTypes
2625

2726
open class SqlLineCommentBlock(
2827
node: ASTNode,
@@ -60,8 +59,4 @@ open class SqlLineCommentBlock(
6059
}
6160
return 1
6261
}
63-
64-
fun isNeedBeforeWhiteSpace(): Boolean =
65-
node.treePrev?.text?.contains("\n") == true ||
66-
node.treePrev?.treePrev?.elementType == SqlTypes.LINE_COMMENT
6762
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ class SqlElAtSignBlock(
4343
override fun getSpacing(
4444
child1: Block?,
4545
child2: Block,
46-
): Spacing? = customSpacingBuilder?.getCustomSpacing(child1, child2) ?: spacingBuilder.getSpacing(this, child1, child2)
46+
): Spacing? =
47+
customSpacingBuilder?.getCustomSpacing(child1, child2) ?: spacingBuilder.getSpacing(
48+
this,
49+
child1,
50+
child2,
51+
)
4752

48-
override fun isLeaf(): Boolean {
49-
TODO("Not yet implemented")
50-
}
53+
override fun isLeaf(): Boolean = true
5154
}

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

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,33 @@ import com.intellij.formatting.Spacing
2121
import com.intellij.formatting.SpacingBuilder
2222
import com.intellij.formatting.Wrap
2323
import com.intellij.lang.ASTNode
24+
import com.intellij.psi.PsiElement
2425
import com.intellij.psi.PsiWhiteSpace
2526
import com.intellij.psi.formatter.common.AbstractBlock
27+
import com.intellij.psi.util.PsiTreeUtil
28+
import com.intellij.psi.util.elementType
29+
import org.domaframework.doma.intellij.formatter.IndentType
2630
import org.domaframework.doma.intellij.formatter.SqlCustomSpacingBuilder
2731
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2832
import org.domaframework.doma.intellij.formatter.block.SqlBlockCommentBlock
2933
import org.domaframework.doma.intellij.formatter.block.SqlCommentBlock
34+
import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock
35+
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
36+
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
37+
import org.domaframework.doma.intellij.psi.SqlElElseifDirective
38+
import org.domaframework.doma.intellij.psi.SqlElForDirective
39+
import org.domaframework.doma.intellij.psi.SqlElIfDirective
3040
import org.domaframework.doma.intellij.psi.SqlTypes
3141

42+
enum class SqlDirectiveType {
43+
IF,
44+
ELSEIF,
45+
ELSE,
46+
FOR,
47+
END,
48+
Variable,
49+
}
50+
3251
class SqlElBlockCommentBlock(
3352
node: ASTNode,
3453
wrap: Wrap?,
@@ -41,13 +60,40 @@ class SqlElBlockCommentBlock(
4160
alignment,
4261
spacingBuilder,
4362
) {
63+
var isConditionLoopBlock = getConditionOrLoopBlock(node)
64+
65+
override val indent =
66+
ElementIndent(
67+
IndentType.NONE,
68+
0,
69+
0,
70+
)
71+
72+
override fun setParentGroupBlock(block: SqlBlock?) {
73+
super.setParentGroupBlock(block)
74+
indent.indentLevel = IndentType.NONE
75+
indent.indentLen = createIndentLen()
76+
indent.groupIndentLen = 0
77+
}
78+
4479
override fun buildChildren(): MutableList<AbstractBlock> {
4580
val blocks = mutableListOf<AbstractBlock>()
4681
var child = node.firstChildNode
4782
while (child != null) {
4883
if (child !is PsiWhiteSpace) {
4984
val block = getBlock(child)
5085
blocks.add(block)
86+
if (!isConditionLoopBlock &&
87+
(
88+
child.elementType == SqlTypes.EL_IF_DIRECTIVE ||
89+
child.elementType == SqlTypes.EL_FOR_DIRECTIVE ||
90+
child.elementType == SqlTypes.EL_ELSEIF_DIRECTIVE ||
91+
child.elementType == SqlTypes.EL_ELSE ||
92+
child.elementType == SqlTypes.EL_END
93+
)
94+
) {
95+
isConditionLoopBlock = true
96+
}
5197
}
5298
child = child.treeNext
5399
}
@@ -56,8 +102,10 @@ class SqlElBlockCommentBlock(
56102

57103
override fun getBlock(child: ASTNode): SqlBlock =
58104
when (child.elementType) {
59-
// is SqlElGeExpr, is SqlElLeExpr, is SqlElGtExpr, is SqlElLtExpr, is SqlElEqExpr, is SqlElNeExpr ->
60-
// SqlOperationBlock(child, wrap, alignment, spacingBuilder)
105+
SqlTypes.GE, SqlTypes.LE, SqlTypes.GT, SqlTypes.LT, SqlTypes.EL_EQ, SqlTypes.EL_NE,
106+
SqlTypes.PLUS, SqlTypes.MINUS, SqlTypes.ASTERISK, SqlTypes.SLASH, SqlTypes.AT_SIGN,
107+
->
108+
SqlOperationBlock(child, wrap, alignment, spacingBuilder)
61109

62110
SqlTypes.EL_FIELD_ACCESS_EXPR ->
63111
SqlElFieldAccessBlock(
@@ -89,9 +137,50 @@ class SqlElBlockCommentBlock(
89137
SqlTypes.BLOCK_COMMENT_CONTENT ->
90138
SqlBlockCommentBlock(child, wrap, alignment, spacingBuilder)
91139

92-
else -> SqlBlock(child, wrap, alignment, customSpacingBuilder, spacingBuilder)
140+
else -> SqlUnknownBlock(child, wrap, alignment, spacingBuilder)
93141
}
94142

143+
private fun getConditionOrLoopBlock(node: ASTNode): Boolean {
144+
val directiveType =
145+
when {
146+
PsiTreeUtil.getChildOfType(node.psi, SqlElIfDirective::class.java) != null -> {
147+
SqlDirectiveType.IF
148+
}
149+
150+
PsiTreeUtil.getChildOfType(node.psi, SqlElElseifDirective::class.java) != null -> {
151+
SqlDirectiveType.ELSEIF
152+
}
153+
154+
PsiTreeUtil.getChildOfType(node.psi, SqlElForDirective::class.java) != null -> {
155+
SqlDirectiveType.FOR
156+
}
157+
158+
PsiTreeUtil.getChildOfType(node.psi, SqlElElseifDirective::class.java) != null -> {
159+
SqlDirectiveType.ELSE
160+
}
161+
162+
PsiTreeUtil.getChildOfType(node.psi, SqlElForDirective::class.java) != null -> {
163+
SqlDirectiveType.END
164+
}
165+
166+
else -> {
167+
val children =
168+
PsiTreeUtil
169+
.getChildrenOfType(node.psi, PsiElement::class.java)
170+
?.firstOrNull { it.elementType == SqlTypes.EL_ELSE || it.elementType == SqlTypes.EL_END }
171+
children?.let {
172+
when (it.elementType) {
173+
SqlTypes.EL_ELSE -> SqlDirectiveType.ELSE
174+
SqlTypes.EL_END -> SqlDirectiveType.END
175+
else -> SqlDirectiveType.Variable
176+
}
177+
} ?: SqlDirectiveType.Variable
178+
}
179+
}
180+
181+
return directiveType != SqlDirectiveType.Variable
182+
}
183+
95184
private fun createFieldAccessSpacingBuilder(): SqlCustomSpacingBuilder =
96185
SqlCustomSpacingBuilder()
97186
.withSpacing(
@@ -143,4 +232,19 @@ class SqlElBlockCommentBlock(
143232
)
144233

145234
override fun isLeaf(): Boolean = false
235+
236+
private fun createIndentLen(): Int {
237+
parentBlock?.let {
238+
if (it is SqlSubQueryGroupBlock) {
239+
if (it.childBlocks.dropLast(1).isEmpty()) {
240+
return 1
241+
}
242+
if (it.isFirstLineComment) {
243+
return it.indent.groupIndentLen.minus(2)
244+
}
245+
}
246+
return 1
247+
}
248+
return 1
249+
}
146250
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ class SqlElClassBlock(
5656

5757
override fun getBlock(child: ASTNode): SqlBlock =
5858
when (child.elementType) {
59-
SqlTypes.EL_IDENTIFIER ->
60-
SqlElIdentifierBlock(child, wrap, alignment, spacingBuilder)
59+
SqlTypes.EL_IDENTIFIER -> SqlElIdentifierBlock(child, wrap, alignment, spacingBuilder)
6160

62-
SqlTypes.DOT ->
63-
SqlElDotBlock(child, wrap, alignment, spacingBuilder)
61+
SqlTypes.DOT -> SqlElDotBlock(child, wrap, alignment, spacingBuilder)
6462

6563
else -> SqlUnknownBlock(child, wrap, alignment, spacingBuilder)
6664
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,5 @@ class SqlElClassRightBlock(
4343
child2: Block,
4444
): Spacing? = spacingBuilder.getSpacing(this, child1, child2)
4545

46-
override fun isLeaf(): Boolean {
47-
TODO("Not yet implemented")
48-
}
46+
override fun isLeaf(): Boolean = true
4947
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SqlElParametersBlock(
3939
) {
4040
override fun getBlock(child: ASTNode): SqlBlock =
4141
when (child.elementType) {
42-
SqlTypes.LEFT_PAREN ->
42+
SqlTypes.LEFT_PAREN, SqlTypes.RIGHT_PAREN ->
4343
SqlElSymbolBlock(child, wrap, alignment, spacingBuilder)
4444

4545
SqlTypes.EL_PRIMARY_EXPR ->
@@ -48,9 +48,6 @@ class SqlElParametersBlock(
4848
SqlTypes.COMMA ->
4949
SqlElCommaBlock(child, wrap, alignment, spacingBuilder)
5050

51-
SqlTypes.RIGHT_PAREN ->
52-
SqlElSymbolBlock(child, wrap, alignment, spacingBuilder)
53-
5451
else -> SqlUnknownBlock(child, wrap, alignment, spacingBuilder)
5552
}
5653

0 commit comments

Comments
 (0)