Skip to content

Commit 4eab34c

Browse files
committed
Fixed indentation and space breaking
1 parent bbe3094 commit 4eab34c

32 files changed

+782
-124
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package org.domaframework.doma.intellij.formatter
1717

1818
import org.domaframework.doma.intellij.formatter.block.SqlBlock
19-
import org.domaframework.doma.intellij.formatter.block.group.SqlSubGroupBlock
19+
import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2021

2122
open class SqlBlockBuilder {
2223
private val groupTopNodeIndexHistory = mutableListOf<Pair<Int, SqlBlock>>()
@@ -25,6 +26,8 @@ open class SqlBlockBuilder {
2526

2627
fun getGroupTopNodeIndexHistory(): List<Pair<Int, SqlBlock>> = groupTopNodeIndexHistory
2728

29+
fun getLastGroup(): SqlBlock? = groupTopNodeIndexHistory.lastOrNull()?.second
30+
2831
fun addGroupTopNodeIndexHistory(block: Pair<Int, SqlBlock>) {
2932
groupTopNodeIndexHistory.add(block)
3033
}
@@ -37,19 +40,21 @@ open class SqlBlockBuilder {
3740
if (commentBlocks.isNotEmpty()) {
3841
var index = 0
3942
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++
43+
if (block !is SqlElBlockCommentBlock) {
44+
val indentLen =
45+
if (index == 0 &&
46+
baseIndent.parentBlock is SqlSubGroupBlock &&
47+
baseIndent.parentBlock?.childBlocks?.size == 1
48+
) {
49+
1
50+
} else {
51+
baseIndent.indent.indentLen
52+
}
53+
block.indent.indentLevel = IndentType.NONE
54+
block.indent.indentLen = indentLen
55+
block.indent.groupIndentLen = 0
56+
index++
57+
}
5358
}
5459
commentBlocks.clear()
5560
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.intellij.formatting.Spacing
2121
import com.intellij.psi.tree.IElementType
2222
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2323
import org.domaframework.doma.intellij.formatter.block.SqlColumnBlock
24-
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2524
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
2625
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
2726
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
@@ -111,9 +110,4 @@ class SqlCustomSpacingBuilder {
111110
val indentLen = child.createBlockIndentLen()
112111
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
113112
}
114-
115-
fun getSpacingColumnDefinitionRawEndRight(child: SqlRightPatternBlock): Spacing? {
116-
val indentLen = child.indent.indentLen
117-
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
118-
}
119113
}

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import com.intellij.psi.impl.source.codeStyle.PreFormatProcessor
2828
import com.intellij.psi.util.PsiTreeUtil
2929
import com.intellij.psi.util.elementType
3030
import com.intellij.psi.util.prevLeafs
31+
import org.domaframework.doma.intellij.extension.expr.isConditionOrLoopDirective
3132
import org.domaframework.doma.intellij.psi.SqlBlockComment
33+
import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
3234
import org.domaframework.doma.intellij.psi.SqlTypes
3335
import org.domaframework.doma.intellij.setting.SqlLanguage
3436
import org.domaframework.doma.intellij.state.DomaToolsFunctionEnableSettings
@@ -90,14 +92,14 @@ class SqlFormatPreProcessor : PreFormatProcessor {
9092
}
9193

9294
SqlTypes.RIGHT_PAREN -> {
93-
newKeyword = getRightPatternNewText(it, newKeyword, createQueryType)
95+
newKeyword = getRightPatternNewText(it, newKeyword, replaceKeywordList[keywordIndex - 1], createQueryType)
9496
}
9597

9698
SqlTypes.WORD -> {
9799
newKeyword = getWordNewText(it, newKeyword, createQueryType)
98100
}
99101

100-
SqlTypes.COMMA -> {
102+
SqlTypes.COMMA, SqlTypes.BLOCK_COMMENT, SqlTypes.OTHER -> {
101103
newKeyword = getNewLineString(it.prevSibling, getUpperText(it))
102104
}
103105
}
@@ -192,25 +194,29 @@ class SqlFormatPreProcessor : PreFormatProcessor {
192194

193195
private fun getRightPatternNewText(
194196
element: PsiElement,
195-
newKeyword: String,
197+
keyword: String,
198+
nextKeyword: PsiElement,
196199
createQueryType: CreateQueryType,
197200
): String {
198-
var newKeyword1 = newKeyword
199-
val prefixElements =
200-
getElementsBeforeKeyword(element.prevLeafs.toList()) { it.elementType == SqlTypes.LEFT_PAREN }
201-
val containsColumnRaw =
202-
prefixElements.findLast { isColumnDefinedRawElementType(it) } != null
203-
newKeyword1 =
204-
if (createQueryType == CreateQueryType.TABLE) {
201+
var newKeyword = keyword
202+
val elementText = element.text
203+
if (createQueryType == CreateQueryType.TABLE) {
204+
val prefixElements =
205+
getElementsBeforeKeyword(element.prevLeafs.toList()) { it.elementType == SqlTypes.LEFT_PAREN }
206+
val containsColumnRaw =
207+
prefixElements.findLast { isColumnDefinedRawElementType(it) } != null
208+
newKeyword =
205209
if (containsColumnRaw) {
206-
getNewLineString(element.prevSibling, getUpperText(element))
210+
getNewLineString(element.prevSibling, elementText)
207211
} else {
208-
getUpperText(element)
212+
elementText
209213
}
210-
} else {
211-
getUpperText(element)
212-
}
213-
return newKeyword1
214+
} else if (nextKeyword.text.lowercase() == "set") {
215+
newKeyword = getNewLineString(element.prevSibling, elementText)
216+
} else {
217+
newKeyword = elementText
218+
}
219+
return newKeyword
214220
}
215221

216222
private fun getWordNewText(
@@ -329,6 +335,23 @@ private class SqlFormatVisitor : PsiRecursiveElementVisitor() {
329335
SqlTypes.KEYWORD, SqlTypes.COMMA, SqlTypes.LEFT_PAREN, SqlTypes.RIGHT_PAREN, SqlTypes.WORD -> {
330336
replaces.add(element)
331337
}
338+
SqlTypes.OTHER -> {
339+
if (element.text == "=") {
340+
val updateSetKeyword =
341+
replaces
342+
.lastOrNull { it.elementType == SqlTypes.KEYWORD }
343+
if (updateSetKeyword?.text?.lowercase() == "set") {
344+
replaces.add(element)
345+
}
346+
}
347+
}
348+
SqlTypes.BLOCK_COMMENT ->
349+
if (
350+
element is SqlCustomElCommentExpr &&
351+
element.isConditionOrLoopDirective()
352+
) {
353+
replaces.add(element)
354+
}
332355
}
333356
}
334357
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ class SqlFormattingModelBuilder : FormattingModelBuilder {
109109
SqlTypes.WORD,
110110
SqlTypes.RIGHT_PAREN,
111111
SqlCustomSpacingBuilder.nonSpacing,
112-
).withSpacing(
113-
SqlTypes.OTHER,
114-
TokenType.WHITE_SPACE,
115-
SqlCustomSpacingBuilder.nonSpacing,
116112
).withSpacing(
117113
SqlTypes.ASTERISK,
118114
TokenType.WHITE_SPACE,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class SqlKeywordUtil {
8181

8282
private val SECOND_KEYWORD =
8383
setOf(
84+
"set",
8485
"from",
8586
"where",
8687
"order",

0 commit comments

Comments
 (0)