Skip to content

Commit 5314405

Browse files
authored
Merge pull request #308 from domaframework/feature/sql-format-support-lateral
Implement Formatting Rules for `LATERAL`
2 parents b99a60c + c356bb2 commit 5314405

32 files changed

+211
-184
lines changed

src/main/java/org/domaframework/doma/intellij/Sql.flex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
6969
"is",
7070
"join",
7171
"key",
72+
"lateral",
7273
"left",
7374
"like",
7475
"limit",

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import org.jetbrains.jps.model.java.JavaResourceRootType
2222
import org.jetbrains.jps.model.java.JavaSourceRootType
2323
import java.util.concurrent.ConcurrentHashMap
2424

25-
val RESOURCES_META_INF_PATH: String
26-
get() = "META-INF"
25+
const val RESOURCES_META_INF_PATH: String = "META-INF"
2726

2827
/**
2928
* A utility for caching directory information on a per-module basis.

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParalle
3232
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
3333
import org.domaframework.doma.intellij.formatter.util.IndentType
3434
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
35-
import org.domaframework.doma.intellij.psi.SqlTypes
3635

3736
open class SqlCommaBlock(
3837
node: ASTNode,
@@ -107,27 +106,14 @@ open class SqlCommaBlock(
107106
}
108107
return parentIndentLen.plus(1)
109108
} else {
110-
var prevLen = 0
111-
parent.childBlocks
112-
.filter { it.node.elementType == SqlTypes.KEYWORD }
113-
.forEach { prev ->
114-
prevLen =
115-
prevLen.plus(
116-
prev
117-
.getNodeText()
118-
.length
119-
.plus(1),
120-
)
121-
}
122-
return parent.indent.groupIndentLen
123-
.plus(1)
109+
return parent.indent.groupIndentLen.plus(1)
124110
}
125111
}
126112
return 1
127113
}
128114

129115
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
130-
val exceptionTypes =
116+
val expectedTypes =
131117
listOf(
132118
SqlInsertColumnGroupBlock::class,
133119
SqlInsertValueGroupBlock::class,
@@ -138,6 +124,6 @@ open class SqlCommaBlock(
138124
SqlWithColumnGroupBlock::class,
139125
SqlKeywordGroupBlock::class,
140126
)
141-
return TypeUtil.isExpectedClassType(exceptionTypes, parentBlock)
127+
return TypeUtil.isExpectedClassType(expectedTypes, parentBlock)
142128
}
143129
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block
1717

18-
import com.intellij.formatting.Indent
1918
import com.intellij.lang.ASTNode
2019
import com.intellij.psi.formatter.common.AbstractBlock
2120
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
@@ -67,13 +66,6 @@ open class SqlKeywordBlock(
6766

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

70-
override fun getIndent(): Indent? {
71-
if (!isAdjustIndentOnEnter() && parentBlock?.indent?.indentLevel == IndentType.SUB) {
72-
return Indent.getIndent(Indent.Type.SPACES, 0, false, true)
73-
}
74-
return Indent.getNoneIndent()
75-
}
76-
7769
override fun createBlockIndentLen(): Int =
7870
when (indentLevel) {
7971
IndentType.TOP -> {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ open class SqlOtherBlock(
4242
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
4343
super.setParentGroupBlock(lastGroup)
4444
indent.indentLevel = IndentType.NONE
45-
indent.indentLen = createIndentLen()
45+
indent.indentLen = 1
4646
indent.groupIndentLen = 0
4747
}
4848

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

51-
private fun createIndentLen(): Int = 1
52-
5351
override fun isLeaf(): Boolean = true
5452
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.domaframework.doma.intellij.formatter.block
1818
import com.intellij.formatting.Alignment
1919
import com.intellij.formatting.Block
2020
import com.intellij.formatting.FormattingMode
21-
import com.intellij.formatting.Indent
2221
import com.intellij.formatting.Spacing
2322
import com.intellij.formatting.SpacingBuilder
2423
import com.intellij.formatting.Wrap
@@ -42,8 +41,6 @@ class SqlWhitespaceBlock(
4241
) {
4342
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
4443

45-
override fun getIndent(): Indent? = Indent.getNoneIndent()
46-
4744
override fun isLeaf(): Boolean = true
4845

4946
override fun getSpacing(

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineGroupBlock.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword
1717

18-
import com.intellij.formatting.Indent
1918
import com.intellij.lang.ASTNode
2019
import com.intellij.psi.formatter.common.AbstractBlock
2120
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -46,13 +45,6 @@ open class SqlInlineGroupBlock(
4645

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

49-
override fun getIndent(): Indent? {
50-
if (!isAdjustIndentOnEnter() && parentBlock?.indent?.indentLevel == IndentType.SUB) {
51-
return Indent.getSpaceIndent(0)
52-
}
53-
return Indent.getNoneIndent()
54-
}
55-
5648
override fun createBlockIndentLen(): Int =
5749
parentBlock?.let {
5850
it.indent.groupIndentLen

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlInlineSecondGroupBlock.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword
1717

18-
import com.intellij.formatting.Indent
1918
import com.intellij.lang.ASTNode
2019
import com.intellij.psi.formatter.common.AbstractBlock
2120
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -48,13 +47,6 @@ open class SqlInlineSecondGroupBlock(
4847

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

51-
override fun getIndent(): Indent? =
52-
if (isAdjustIndentOnEnter()) {
53-
null
54-
} else {
55-
Indent.getSpaceIndent(indent.indentLen)
56-
}
57-
5850
override fun createBlockIndentLen(): Int =
5951
parentBlock?.let {
6052
// TODO:Customize indentation within an inline group

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlJoinGroupBlock.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword
1717

18-
import com.intellij.formatting.Indent
1918
import com.intellij.lang.ASTNode
2019
import com.intellij.psi.formatter.common.AbstractBlock
2120
import org.domaframework.doma.intellij.formatter.block.SqlBlock
@@ -41,24 +40,26 @@ open class SqlJoinGroupBlock(
4140
parentBlock = lastGroup
4241
parentBlock?.childBlocks?.add(this)
4342
indent.indentLevel = IndentType.JOIN
44-
indent.indentLen = createBlockIndentLen(null)
43+
indent.indentLen = createBlockIndentLen()
4544
indent.groupIndentLen = createGroupIndentLen()
4645
}
4746

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

50-
override fun getIndent(): Indent? =
51-
if (isAdjustIndentOnEnter()) {
52-
null
53-
} else {
54-
Indent.getSpaceIndent(indent.indentLen)
55-
}
56-
57-
override fun createBlockIndentLen(preChildBlock: SqlBlock?): Int =
49+
override fun createBlockIndentLen(): Int =
5850
parentBlock
5951
?.indent
6052
?.groupIndentLen
6153
?.plus(1) ?: 1
6254

55+
override fun createGroupIndentLen(): Int =
56+
indent.indentLen
57+
.plus(
58+
topKeywordBlocks
59+
.drop(1)
60+
.filter { it !is SqlLateralGroupBlock }
61+
.sumOf { it.getNodeText().length.plus(1) },
62+
).plus(getNodeText().length)
63+
6364
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
6465
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlKeywordGroupBlock.kt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.block.group.keyword
1717

18-
import com.intellij.formatting.Indent
1918
import com.intellij.lang.ASTNode
2019
import com.intellij.psi.formatter.common.AbstractBlock
20+
import org.domaframework.doma.intellij.common.util.TypeUtil
2121
import org.domaframework.doma.intellij.formatter.block.SqlBlock
22+
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
23+
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
24+
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2225
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2326
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
2427
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -33,9 +36,24 @@ open class SqlKeywordGroupBlock(
3336
context: SqlBlockFormattingContext,
3437
) : SqlNewGroupBlock(node, context) {
3538
val topKeywordBlocks: MutableList<SqlBlock> = mutableListOf(this)
39+
var canAddTopKeyword = true
3640

3741
fun updateTopKeywordBlocks(block: SqlBlock) {
38-
topKeywordBlocks.add(block)
42+
val lastChild =
43+
getChildBlocksDropLast()
44+
.findLast { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
45+
val topKeywordTypes =
46+
listOf(
47+
SqlKeywordBlock::class,
48+
SqlKeywordGroupBlock::class,
49+
)
50+
51+
if (lastChild == null || TypeUtil.isExpectedClassType(topKeywordTypes, lastChild) && canAddTopKeyword) {
52+
topKeywordBlocks.add(block)
53+
} else {
54+
canAddTopKeyword = false
55+
}
56+
3957
indent.groupIndentLen = createGroupIndentLen()
4058
}
4159

@@ -85,27 +103,15 @@ open class SqlKeywordGroupBlock(
85103
if (preChildBlock.indent.indentLevel == this.indent.indentLevel &&
86104
!SqlKeywordUtil.isSetLineKeyword(getNodeText(), preChildBlock.getNodeText())
87105
) {
88-
if (indent.indentLevel == IndentType.SECOND) {
89-
val diffPreBlockTextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
90-
return preChildBlock.indent.indentLen.minus(diffPreBlockTextLen)
91-
} else {
92-
val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
93-
return preChildBlock.indent.indentLen.minus(diffPretextLen)
94-
}
106+
val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
107+
return preChildBlock.indent.indentLen.minus(diffPretextLen)
95108
} else {
96109
return createBlockIndentLen(preChildBlock)
97110
}
98111
}
99112

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

102-
override fun getIndent(): Indent? {
103-
if (!isAdjustIndentOnEnter() && parentBlock?.indent?.indentLevel == IndentType.SUB) {
104-
return Indent.getSpaceIndent(0)
105-
}
106-
return Indent.getNoneIndent()
107-
}
108-
109115
/**
110116
* Adjust the indent position of the subgroup block element itself if it has a comment
111117
*/
@@ -157,10 +163,7 @@ open class SqlKeywordGroupBlock(
157163
return 1
158164
}
159165

160-
override fun createGroupIndentLen(): Int =
161-
indent.indentLen
162-
.plus(topKeywordBlocks.drop(1).sumOf { it.getNodeText().length.plus(1) })
163-
.plus(getNodeText().length)
166+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1))
164167

165168
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
166169
}

0 commit comments

Comments
 (0)