Skip to content

Commit 81fdc4d

Browse files
committed
Indentation of comment blocks and subgroup blocks
1 parent 932a1fc commit 81fdc4d

23 files changed

+464
-157
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,45 @@
1616
package org.domaframework.doma.intellij.formatter
1717

1818
import org.domaframework.doma.intellij.formatter.block.SqlBlock
19-
import kotlin.text.clear
19+
import org.domaframework.doma.intellij.formatter.block.group.SqlSubGroupBlock
2020

2121
open class SqlBlockBuilder {
2222
private val groupTopNodeIndexHistory = mutableListOf<Pair<Int, SqlBlock>>()
2323

24+
private val commentBlocks = mutableListOf<SqlBlock>()
25+
2426
fun getGroupTopNodeIndexHistory(): List<Pair<Int, SqlBlock>> = groupTopNodeIndexHistory
2527

2628
fun addGroupTopNodeIndexHistory(block: Pair<Int, SqlBlock>) {
2729
groupTopNodeIndexHistory.add(block)
2830
}
2931

32+
fun addCommentBlock(block: SqlBlock) {
33+
commentBlocks.add(block)
34+
}
35+
36+
fun updateCommentBlockIndent(baseIndent: SqlBlock) {
37+
if (commentBlocks.isNotEmpty()) {
38+
var index = 0
39+
commentBlocks.forEach { block ->
40+
val indentLen =
41+
if (index == 0 &&
42+
baseIndent.parentBlock is SqlSubGroupBlock &&
43+
baseIndent.parentBlock?.childBlocks?.size == 1
44+
) {
45+
1
46+
} else {
47+
baseIndent.indent.indentLen
48+
}
49+
block.indent.indentLevel = IndentType.NONE
50+
block.indent.indentLen = indentLen
51+
block.indent.groupIndentLen = 0
52+
index++
53+
}
54+
commentBlocks.clear()
55+
}
56+
}
57+
3058
fun getLastGroupTopNodeIndexHistory(): Pair<Int, SqlBlock>? = groupTopNodeIndexHistory.lastOrNull()
3159

3260
fun removeLastGroupTopNodeIndexHistory() {

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlCustomSpacingBuilder.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import org.domaframework.doma.intellij.formatter.block.group.SqlSubGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
3131

3232
class SqlCustomSpacingBuilder {
33-
companion object;
33+
companion object {
34+
val normalSpacing: Spacing = Spacing.createSpacing(1, 1, 0, false, 0, 0)
35+
val nonSpacing: Spacing = Spacing.createSpacing(0, 0, 0, false, 0, 0)
36+
}
3437

3538
private val spacingRules: MutableMap<Pair<IElementType?, IElementType?>?, Spacing?> = HashMap()
3639

@@ -127,17 +130,17 @@ class SqlCustomSpacingBuilder {
127130
return when (child.indent.indentLevel) {
128131
IndentType.TOP -> {
129132
return if (parentBlock?.parentBlock == null) {
130-
Spacing.createSpacing(0, 0, 0, false, 0, 0)
133+
nonSpacing
131134
} else if (parentBlock is SqlSubGroupBlock) {
132-
Spacing.createSpacing(0, 0, 0, false, 0, 0)
135+
nonSpacing
133136
} else {
134137
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
135138
}
136139
}
137140

138141
IndentType.SECOND -> {
139142
return if (parentBlock is SqlSubQueryGroupBlock) {
140-
Spacing.createSpacing(1, 1, 0, false, 0, 0)
143+
normalSpacing
141144
} else if (SqlKeywordUtil.isSetLineKeyword(child.node.text, parentBlock?.node?.text ?: "")) {
142145
null
143146
} else {
@@ -157,7 +160,7 @@ class SqlCustomSpacingBuilder {
157160
}
158161

159162
IndentType.INLINE -> {
160-
return Spacing.createSpacing(1, 1, 0, false, 0, 0)
163+
return normalSpacing
161164
}
162165

163166
IndentType.INLINE_SECOND -> {

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.intellij.formatting.FormattingContext
2020
import com.intellij.formatting.FormattingModel
2121
import com.intellij.formatting.FormattingModelBuilder
2222
import com.intellij.formatting.FormattingModelProvider
23-
import com.intellij.formatting.Spacing
2423
import com.intellij.formatting.SpacingBuilder
2524
import com.intellij.formatting.Wrap
2625
import com.intellij.formatting.WrapType
@@ -32,9 +31,6 @@ import org.domaframework.doma.intellij.setting.SqlLanguage
3231

3332
@Suppress("ktlint:standard:no-consecutive-comments")
3433
class SqlFormattingModelBuilder : FormattingModelBuilder {
35-
val breakLineSpacing: Spacing? = Spacing.createSpacing(0, 0, 1, true, 0, 1)
36-
val normalSpacing: Spacing? = Spacing.createSpacing(1, 1, 0, true, 0, 0)
37-
3834
override fun createModel(formattingContext: FormattingContext): FormattingModel {
3935
val codeStyleSettings = formattingContext.codeStyleSettings
4036
return FormattingModelProvider
@@ -57,7 +53,9 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
5753
.spacing(0, 0, 0, false, 0)
5854
.after(SqlTypes.COMMA)
5955
.spacing(1, 1, 0, false, 0)
60-
.around(SqlTypes.BLOCK_COMMENT)
56+
.before(SqlTypes.LINE_COMMENT)
57+
.spacing(1, 1, 0, false, 0)
58+
.before(SqlTypes.BLOCK_COMMENT)
6159
.spacing(0, 0, 1, false, 0)
6260
.before(SqlTypes.KEYWORD)
6361
.spacing(1, 1, 0, false, 0)
@@ -81,41 +79,41 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
8179
.withSpacing(
8280
TokenType.WHITE_SPACE,
8381
SqlTypes.KEYWORD,
84-
Spacing.createSpacing(0, 0, 1, true, 0, 1),
82+
SqlCustomSpacingBuilder.nonSpacing,
8583
).withSpacing(
8684
SqlTypes.WORD,
8785
TokenType.WHITE_SPACE,
88-
Spacing.createSpacing(0, 0, 0, true, 0, 0),
86+
SqlCustomSpacingBuilder.nonSpacing,
87+
).withSpacing(
88+
SqlTypes.WORD,
89+
SqlTypes.LEFT_PAREN,
90+
SqlCustomSpacingBuilder.nonSpacing,
8991
).withSpacing(
9092
SqlTypes.LEFT_PAREN,
9193
SqlTypes.WORD,
92-
Spacing.createSpacing(0, 0, 0, true, 0, 0),
94+
SqlCustomSpacingBuilder.nonSpacing,
9395
).withSpacing(
9496
SqlTypes.WORD,
9597
SqlTypes.RIGHT_PAREN,
96-
Spacing.createSpacing(0, 0, 0, true, 0, 0),
98+
SqlCustomSpacingBuilder.nonSpacing,
9799
).withSpacing(
98100
SqlTypes.OTHER,
99101
TokenType.WHITE_SPACE,
100-
Spacing.createSpacing(0, 0, 0, true, 0, 0),
102+
SqlCustomSpacingBuilder.nonSpacing,
101103
).withSpacing(
102104
SqlTypes.ASTERISK,
103105
TokenType.WHITE_SPACE,
104-
Spacing.createSpacing(0, 0, 0, true, 0, 0),
105-
).withSpacing(
106-
SqlTypes.OTHER,
107-
SqlTypes.LINE_COMMENT,
108-
breakLineSpacing,
106+
SqlCustomSpacingBuilder.nonSpacing,
109107
)
110108
// Table And Column Rules
111109
// WORD And OTHER Rules
112110
.withSpacing(
113111
SqlTypes.WORD,
114112
SqlTypes.WORD,
115-
normalSpacing,
113+
SqlCustomSpacingBuilder.normalSpacing,
116114
).withSpacing(
117115
SqlTypes.OTHER,
118116
SqlTypes.OTHER,
119-
normalSpacing,
117+
SqlCustomSpacingBuilder.normalSpacing,
120118
)
121119
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlKeywordUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ enum class IndentType(
2424
SECOND(2, true),
2525
JOIN(3, true),
2626
SECOND_OPTION(4, true),
27-
TIRD(5, true),
28-
ATTACHED(6, true),
27+
TIRD(5),
28+
ATTACHED(6),
2929
INLINE_SECOND(8, true),
3030
COLUMN(9, true),
3131
SUB(90),

0 commit comments

Comments
 (0)