Skip to content

Commit a7002e8

Browse files
committed
Adjusted indentation and removed trailing spaces for each query type
1 parent 1fd106b commit a7002e8

18 files changed

+388
-168
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fun getExtension(type: String): String =
3737
* Does it match the Dao file type condition?
3838
*/
3939
fun isJavaOrKotlinFileType(daoFile: PsiFile): Boolean {
40-
if (daoFile.virtualFile == null) return false
4140
val fileType = FileTypeManager.getInstance().getFileTypeByFile(daoFile.virtualFile)
4241
return when (fileType.name) {
4342
"JAVA", "Kotlin" -> true

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/DirectiveHandler.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ open class DirectiveHandler(
3333
): Boolean {
3434
val prev = it.prevLeaf()
3535
return (
36-
prev != null &&
37-
prev.text == symbol ||
36+
prev?.text == symbol ||
3837
it.text.startsWith(symbol)
3938
)
4039
}

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

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlColumnBlock
2424
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2525
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
2626
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
27-
import org.domaframework.doma.intellij.formatter.block.group.SqlCreateKeywordGroupBlock
2827
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
29-
import org.domaframework.doma.intellij.formatter.block.group.SqlSubGroupBlock
30-
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
3128

3229
class SqlCustomSpacingBuilder {
3330
companion object {
@@ -119,64 +116,4 @@ class SqlCustomSpacingBuilder {
119116
val indentLen = child.indent.indentLen
120117
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
121118
}
122-
123-
/**
124-
* Adjust line breaks and indentation depending on the block indent type
125-
*/
126-
fun getSpacingWithIndentLevel(child: SqlNewGroupBlock): Spacing? {
127-
val parentBlock = child.parentBlock
128-
val indentLen: Int = child.indent.indentLen
129-
130-
return when (child.indent.indentLevel) {
131-
IndentType.TOP -> {
132-
return if (parentBlock?.parentBlock == null) {
133-
nonSpacing
134-
} else if (parentBlock is SqlSubGroupBlock) {
135-
nonSpacing
136-
} else {
137-
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
138-
}
139-
}
140-
141-
IndentType.SECOND -> {
142-
return if (parentBlock is SqlSubQueryGroupBlock) {
143-
normalSpacing
144-
} else if (SqlKeywordUtil.isSetLineKeyword(child.node.text, parentBlock?.node?.text ?: "")) {
145-
null
146-
} else {
147-
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
148-
}
149-
}
150-
151-
IndentType.SECOND_OPTION -> {
152-
return Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
153-
}
154-
155-
IndentType.SUB -> {
156-
if (parentBlock is SqlCreateKeywordGroupBlock) {
157-
return Spacing.createSpacing(0, 0, 1, false, 0, 1)
158-
}
159-
return null
160-
}
161-
162-
IndentType.INLINE -> {
163-
return normalSpacing
164-
}
165-
166-
IndentType.INLINE_SECOND -> {
167-
parentBlock?.let {
168-
val parentIndentLen = it.indent.groupIndentLen
169-
val parentTextLen = it.node.text.length
170-
val newIndentLen = parentIndentLen.plus(parentTextLen).plus(1)
171-
return Spacing.createSpacing(newIndentLen, newIndentLen, 1, false, 0, 1)
172-
}
173-
return Spacing.createSpacing(0, 0, 1, false, 0, 1)
174-
}
175-
176-
else -> {
177-
return null
178-
}
179-
}
180-
return null
181-
}
182119
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,6 @@ class SqlFormatPreProcessor : PreFormatProcessor {
5959
var index = keywordList.size
6060
var keywordIndex = replaceKeywordList.size
6161

62-
// Add a newline to the end of the file
63-
val documentLastElement = visitor.lastElement
64-
val documentLastRange = visitor.lastElement?.textRange
65-
if (documentLastElement != null && documentLastRange != null && documentLastRange.endOffset <= rangeToReformat.endOffset) {
66-
if (documentLastElement is PsiWhiteSpace) {
67-
val textStart = documentLastElement.startOffset
68-
val textEnd = documentLastElement.endOffset
69-
document.replaceString(textStart, textEnd, " \n")
70-
} else if (documentLastElement.elementType == SqlTypes.LINE_COMMENT) {
71-
val textStart = documentLastElement.startOffset
72-
val textEnd = documentLastElement.endOffset
73-
document.replaceString(textStart, textEnd, "${documentLastElement.text} \n")
74-
} else {
75-
val textEnd = documentLastElement.endOffset
76-
document.insertString(textEnd, " \n")
77-
}
78-
}
79-
8062
visitor.replaces.asReversed().forEach {
8163
val createQueryType = getCreateQueryGroup(keywordList, index)
8264
val textRangeStart = it.startOffset
@@ -95,6 +77,14 @@ class SqlFormatPreProcessor : PreFormatProcessor {
9577
newKeyword =
9678
if (createQueryType == CreateQueryType.TABLE) {
9779
getNewLineString(it.prevSibling, getUpperText(it))
80+
} else if (keywordIndex > 0) {
81+
if (replaceKeywordList[keywordIndex - 1].text.lowercase() == "insert" ||
82+
replaceKeywordList[keywordIndex - 1].text.lowercase() == "into"
83+
) {
84+
getNewLineString(it.prevSibling, getUpperText(it))
85+
} else {
86+
getUpperText(it)
87+
}
9888
} else {
9989
getUpperText(it)
10090
}
@@ -125,6 +115,8 @@ class SqlFormatPreProcessor : PreFormatProcessor {
125115
removeSpacesAroundNewline(document, it.textRange)
126116
} else if (isSubGroupFirstElement(nextElement)) {
127117
document.deleteString(textRangeStart, textRangeEnd)
118+
} else if (isCreateViewAs(replaceKeywordList[keywordIndex], createQueryType)) {
119+
removeSpacesAroundNewline(document, it.textRange)
128120
} else {
129121
val isNewLineGroup = SqlKeywordUtil.getIndentType(nextElement.text ?: "").isNewLineGroup()
130122
val isSetLineKeyword =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class SqlKeywordUtil {
8787
"group",
8888
"having",
8989
"limit",
90+
"values",
9091
)
9192

9293
fun isSecondKeyword(keyword: String): Boolean = SECOND_KEYWORD.contains(keyword.lowercase())
@@ -133,6 +134,7 @@ class SqlKeywordUtil {
133134
private val ATTACHED_KEYWORD =
134135
setOf(
135136
"distinct",
137+
"into",
136138
"table",
137139
"index",
138140
"database",
@@ -236,7 +238,6 @@ class SqlKeywordUtil {
236238
"like",
237239
"offset",
238240
"then",
239-
"values",
240241
"in",
241242
)
242243

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.domaframework.doma.intellij.formatter
2+
3+
import com.intellij.openapi.application.ApplicationManager
4+
import com.intellij.openapi.command.WriteCommandAction
5+
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.util.TextRange
7+
import com.intellij.psi.PsiDocumentManager
8+
import com.intellij.psi.PsiElement
9+
import com.intellij.psi.PsiFile
10+
import com.intellij.psi.codeStyle.CodeStyleSettings
11+
import com.intellij.psi.impl.source.codeStyle.PostFormatProcessor
12+
13+
class SqlPostProcessor : PostFormatProcessor {
14+
override fun processElement(
15+
source: PsiElement,
16+
settings: CodeStyleSettings,
17+
): PsiElement = source
18+
19+
override fun processText(
20+
source: PsiFile,
21+
rangeToReformat: TextRange,
22+
settings: CodeStyleSettings,
23+
): TextRange {
24+
val project: Project = source.project
25+
val document = PsiDocumentManager.getInstance(project).getDocument(source) ?: return rangeToReformat
26+
27+
val originalText = document.text
28+
val withoutTrailingSpaces = originalText.replace(Regex(" +(\r?\n)"), "$1")
29+
val finalText = withoutTrailingSpaces.trimEnd() + " \n"
30+
31+
if (originalText == finalText) {
32+
return rangeToReformat
33+
}
34+
35+
ApplicationManager.getApplication().invokeAndWait {
36+
WriteCommandAction.runWriteCommandAction(project) {
37+
document.setText(finalText)
38+
PsiDocumentManager.getInstance(project).commitDocument(document)
39+
}
40+
}
41+
return TextRange(0, finalText.length)
42+
}
43+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import org.domaframework.doma.intellij.formatter.block.group.SqlCreateKeywordGro
4242
import org.domaframework.doma.intellij.formatter.block.group.SqlDataTypeParamBlock
4343
import org.domaframework.doma.intellij.formatter.block.group.SqlInlineGroupBlock
4444
import org.domaframework.doma.intellij.formatter.block.group.SqlInlineSecondGroupBlock
45+
import org.domaframework.doma.intellij.formatter.block.group.SqlInsertColumnGroupBlock
46+
import org.domaframework.doma.intellij.formatter.block.group.SqlInsertKeywordGroupBlock
4547
import org.domaframework.doma.intellij.formatter.block.group.SqlJoinGroupBlock
4648
import org.domaframework.doma.intellij.formatter.block.group.SqlKeywordGroupBlock
4749
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
@@ -113,9 +115,9 @@ open class SqlBlock(
113115
blocks.removeLast()
114116
}
115117
}
116-
setRightSpace(blocks.lastOrNull() as SqlBlock?)
117118
prevNonWhiteSpaceNode = child
118119
updateSearchKeywordLevelHistory(childBlock, child)
120+
setRightSpace(childBlock)
119121
blocks.add(childBlock)
120122
if (childBlock is SqlCommentBlock) {
121123
blockBuilder.addCommentBlock(childBlock)
@@ -147,6 +149,7 @@ open class SqlBlock(
147149
): Boolean =
148150
isNewLineGroupBlock(childBlock, child, lastGroup) ||
149151
childBlock.node.elementType == SqlTypes.COMMA ||
152+
childBlock is SqlInsertColumnGroupBlock ||
150153
childBlock is SqlColumnDefinitionRawGroupBlock ||
151154
childBlock is SqlColumnDefinitionGroupBlock ||
152155
(childBlock is SqlRightPatternBlock && childBlock.isNeedBeforeWhiteSpace(lastGroup)) ||
@@ -159,8 +162,8 @@ open class SqlBlock(
159162
) ||
160163
(childBlock is SqlElBlockCommentBlock && childBlock.isConditionLoopBlock)
161164

162-
private fun setRightSpace(lastBlock: SqlBlock?) {
163-
val rightBlock = lastBlock as? SqlRightPatternBlock
165+
private fun setRightSpace(currentBlock: SqlBlock?) {
166+
val rightBlock = currentBlock as? SqlRightPatternBlock
164167
rightBlock?.enableLastRight()
165168
}
166169

@@ -465,6 +468,8 @@ open class SqlBlock(
465468
SqlColumnDefinitionGroupBlock(child, wrap, alignment, spacingBuilder)
466469
} else if (lastGroup is SqlColumnDefinitionRawGroupBlock) {
467470
SqlDataTypeParamBlock(child, wrap, alignment, spacingBuilder)
471+
} else if (lastGroup is SqlInsertKeywordGroupBlock) {
472+
SqlInsertColumnGroupBlock(child, wrap, alignment, spacingBuilder)
468473
} else {
469474
SqlSubQueryGroupBlock(child, wrap, alignment, spacingBuilder)
470475
}
@@ -596,6 +601,9 @@ open class SqlBlock(
596601
if (child.text.lowercase() == "create") {
597602
return SqlCreateKeywordGroupBlock(child, wrap, alignment, spacingBuilder)
598603
}
604+
if (child.text.lowercase() == "insert") {
605+
return SqlInsertKeywordGroupBlock(child, wrap, alignment, spacingBuilder)
606+
}
599607
return SqlKeywordGroupBlock(child, indentLevel, wrap, alignment, spacingBuilder)
600608
}
601609

@@ -674,7 +682,6 @@ open class SqlBlock(
674682
child2: Block,
675683
): Spacing? {
676684
if (!isEnableFormat()) return null
677-
678685
// The end of a line comment element is a newline, so just add a space for the indent.
679686
if (child1 is SqlLineCommentBlock) {
680687
if (child2 is SqlBlock) {
@@ -748,7 +755,7 @@ open class SqlBlock(
748755
?.let { return it }
749756
child2.parentBlock is SqlDataTypeParamBlock -> SqlCustomSpacingBuilder.nonSpacing
750757

751-
child2.lastRight -> SqlCustomSpacingBuilder.normalSpacing
758+
child2.preSpaceRight -> SqlCustomSpacingBuilder.normalSpacing
752759
else -> SqlCustomSpacingBuilder.nonSpacing
753760
}
754761
}

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ import com.intellij.formatting.SpacingBuilder
2121
import com.intellij.formatting.Wrap
2222
import com.intellij.lang.ASTNode
2323
import com.intellij.psi.formatter.common.AbstractBlock
24+
import fleet.util.plus
2425
import org.domaframework.doma.intellij.formatter.IndentType
25-
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
26+
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.SqlCreateKeywordGroupBlock
28+
import org.domaframework.doma.intellij.formatter.block.group.SqlInsertKeywordGroupBlock
29+
import org.domaframework.doma.intellij.formatter.block.group.SqlSubGroupBlock
2630

2731
open class SqlCommaBlock(
2832
node: ASTNode,
@@ -48,18 +52,29 @@ open class SqlCommaBlock(
4852
override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen)
4953

5054
private fun createIndentLen(): Int {
51-
parentBlock?.let {
52-
if (it is SqlSubQueryGroupBlock) {
53-
val parentIndentLen = it.indent.groupIndentLen
54-
val grand = it.parentBlock
55-
if (grand != null && grand.node.text.lowercase() == "create") {
56-
val grandIndentLen = grand.indent.groupIndentLen
57-
return grandIndentLen.plus(parentIndentLen).minus(1)
55+
parentBlock?.let { parent ->
56+
if (parent is SqlSubGroupBlock) {
57+
val parentIndentLen = parent.indent.groupIndentLen
58+
val grand = parent.parentBlock
59+
grand?.let { grand ->
60+
if (grand is SqlCreateKeywordGroupBlock) {
61+
val grandIndentLen = grand.indent.groupIndentLen
62+
return grandIndentLen.plus(parentIndentLen).minus(1)
63+
}
64+
if (grand is SqlInsertKeywordGroupBlock) {
65+
return parentIndentLen
66+
}
67+
if (grand is SqlColumnGroupBlock) {
68+
val grandIndentLen = grand.indent.groupIndentLen
69+
var prevTextLen = 1
70+
parent.prevChildren?.dropLast(1)?.forEach { prev -> prevTextLen = prevTextLen.plus(prev.node.text.length) }
71+
return grandIndentLen.plus(prevTextLen).plus(1)
72+
}
5873
}
5974
return parentIndentLen
6075
} else {
61-
val parentLen = it.node.text.length
62-
return it.indent.groupIndentLen.plus(parentLen.plus(1))
76+
val parentLen = parent.node.text.length
77+
return parent.indent.groupIndentLen.plus(parentLen.plus(1))
6378
}
6479
}
6580
return 1

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
@@ -50,7 +50,7 @@ open class SqlKeywordBlock(
5050
indent.indentLevel = indentLevel
5151
indent.indentLen = createIndentLen()
5252
indent.groupIndentLen = indent.indentLen.plus(node.text.length)
53-
updateParentIndentLen()
53+
// updateParentIndentLen()
5454
}
5555

5656
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import com.intellij.psi.formatter.common.AbstractBlock
2323
import org.domaframework.doma.intellij.formatter.IndentType
2424
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionGroupBlock
2525
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
26+
import org.domaframework.doma.intellij.formatter.block.group.SqlInsertColumnGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.SqlInsertKeywordGroupBlock
28+
import org.domaframework.doma.intellij.psi.SqlTypes
2629

2730
/**
2831
* Parent is always a subclass of a subgroup
@@ -39,15 +42,28 @@ open class SqlRightPatternBlock(
3942
null,
4043
spacingBuilder,
4144
) {
42-
var lastRight = false
45+
var preSpaceRight = false
4346

4447
fun enableLastRight() {
45-
parentBlock?.parentBlock?.let {
46-
lastRight = it.indent.indentLevel <= IndentType.SECOND ||
47-
it.indent.indentLevel == IndentType.JOIN
48-
return
48+
parentBlock?.let {
49+
if (it.node.treePrev.elementType == SqlTypes.WORD) {
50+
preSpaceRight = false
51+
return
52+
}
53+
if (it is SqlInsertColumnGroupBlock) {
54+
preSpaceRight = false
55+
return
56+
}
57+
it.parentBlock?.let {
58+
preSpaceRight = (
59+
it.indent.indentLevel <= IndentType.SECOND &&
60+
it.parentBlock !is SqlInsertKeywordGroupBlock
61+
) ||
62+
it.indent.indentLevel == IndentType.JOIN
63+
return
64+
}
4965
}
50-
lastRight = false
66+
preSpaceRight = false
5167
}
5268

5369
override val indent =

0 commit comments

Comments
 (0)