Skip to content

Commit 7e63b7e

Browse files
committed
Change Lateral keyword to group block
1 parent da0b87c commit 7e63b7e

File tree

19 files changed

+171
-105
lines changed

19 files changed

+171
-105
lines changed

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/group/keyword/SqlJoinGroupBlock.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class SqlJoinGroupBlock(
4141
parentBlock = lastGroup
4242
parentBlock?.childBlocks?.add(this)
4343
indent.indentLevel = IndentType.JOIN
44-
indent.indentLen = createBlockIndentLen(null)
44+
indent.indentLen = createBlockIndentLen()
4545
indent.groupIndentLen = createGroupIndentLen()
4646
}
4747

@@ -65,7 +65,7 @@ open class SqlJoinGroupBlock(
6565
.plus(
6666
topKeywordBlocks
6767
.drop(1)
68-
.filter { it.getNodeText() != "lateral" }
68+
.filter { it !is SqlLateralGroupBlock }
6969
.sumOf { it.getNodeText().length.plus(1) },
7070
).plus(getNodeText().length)
7171

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword
1818
import com.intellij.formatting.Indent
1919
import com.intellij.lang.ASTNode
2020
import com.intellij.psi.formatter.common.AbstractBlock
21+
import org.domaframework.doma.intellij.common.util.TypeUtil
2122
import org.domaframework.doma.intellij.formatter.block.SqlBlock
23+
import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock
24+
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
25+
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2226
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
2327
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock
2428
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -33,9 +37,24 @@ open class SqlKeywordGroupBlock(
3337
context: SqlBlockFormattingContext,
3438
) : SqlNewGroupBlock(node, context) {
3539
val topKeywordBlocks: MutableList<SqlBlock> = mutableListOf(this)
40+
var canAddTopKeyword = true
3641

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

@@ -152,10 +171,7 @@ open class SqlKeywordGroupBlock(
152171
return 1
153172
}
154173

155-
override fun createGroupIndentLen(): Int =
156-
indent.indentLen
157-
.plus(topKeywordBlocks.drop(1).sumOf { it.getNodeText().length.plus(1) })
158-
.plus(getNodeText().length)
174+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1))
159175

160176
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
161177
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.IndentType
22+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
23+
24+
class SqlLateralGroupBlock(
25+
node: ASTNode,
26+
context: SqlBlockFormattingContext,
27+
) : SqlKeywordGroupBlock(
28+
node,
29+
IndentType.OPTIONS,
30+
context,
31+
) {
32+
var subQueryGroupBlock: SqlSubGroupBlock? = null
33+
34+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
35+
super.setParentGroupBlock(lastGroup)
36+
indent.indentLen = createBlockIndentLen()
37+
indent.groupIndentLen = createGroupIndentLen()
38+
}
39+
40+
override fun createBlockIndentLen(): Int {
41+
parentBlock?.let { parent ->
42+
return parent.indent.groupIndentLen.plus(1)
43+
}
44+
return 0
45+
}
46+
47+
override fun createGroupIndentLen(): Int = indent.indentLen.plus(getNodeText().length)
48+
49+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
50+
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,7 @@ class SqlConditionKeywordGroupBlock(
5656
groupLen.plus(1)
5757
}
5858
} else {
59-
val orBlock =
60-
parentBlock
61-
?.childBlocks
62-
?.dropLast(1)
63-
?.findLast { it is SqlConditionKeywordGroupBlock && it.getNodeText() == "or" }
64-
if (getNodeText() == "and" && orBlock != null) {
65-
groupLen.plus(1)
66-
} else {
67-
return parent.indent.groupIndentLen.minus(getNodeText().length)
68-
}
59+
return parent.indent.groupIndentLen.minus(getNodeText().length)
6960
}
7061
} ?: return 1
7162
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2525
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
2626
import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock
2727
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
28+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
2829
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
2930
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithColumnGroupBlock
@@ -67,6 +68,7 @@ abstract class SqlSubGroupBlock(
6768

6869
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
6970
(lastGroup as? SqlDoGroupBlock)?.doQueryBlock = this
71+
(lastGroup as? SqlLateralGroupBlock)?.subQueryGroupBlock = this
7072
}
7173

7274
override fun addChildBlock(childBlock: SqlBlock) {
@@ -100,14 +102,14 @@ abstract class SqlSubGroupBlock(
100102
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
101103
lastGroup?.let { lastBlock ->
102104
if (lastBlock is SqlJoinQueriesGroupBlock) return true
103-
val exceptionalParentTypes =
105+
val expectedTypes =
104106
listOf(
105107
SqlWithQueryGroupBlock::class,
106108
SqlWithCommonTableGroupBlock::class,
107109
SqlWithColumnGroupBlock::class,
108110
SqlCreateViewGroupBlock::class,
109111
)
110-
return TypeUtil.isExpectedClassType(exceptionalParentTypes, lastBlock.parentBlock)
112+
return TypeUtil.isExpectedClassType(expectedTypes, lastBlock.parentBlock)
111113
}
112114
return false
113115
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package org.domaframework.doma.intellij.formatter.block.group.subgroup
1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock
2020
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21-
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
22-
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2321
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2422
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
2523
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -54,13 +52,7 @@ open class SqlSubQueryGroupBlock(
5452
} else if (parent is SqlWithQuerySubGroupBlock) {
5553
parent.indent.groupIndentLen
5654
} else {
57-
parent.indent.indentLen
58-
.plus(
59-
parent
60-
.getChildBlocksDropLast()
61-
.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
62-
.sumOf { it.getNodeText().length.plus(1) },
63-
).plus(parent.getNodeText().length.plus(1))
55+
parent.indent.groupIndentLen.plus(1)
6456
}
6557
} ?: offset
6658

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRaw
2727
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock
2828
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
2929
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
30+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock
@@ -107,12 +108,12 @@ class SqlSetParentGroupProcessor(
107108
val currentIndentLevel = childBlock.indent.indentLevel
108109
if (currentIndentLevel == IndentType.TOP) {
109110
var parentBlock: SqlBlock? = null
110-
val exceptionalTypes =
111+
val expectedTypes =
111112
listOf(
112113
SqlSubGroupBlock::class,
113114
SqlCreateViewGroupBlock::class,
114115
)
115-
if (isExpectedClassType(exceptionalTypes, lastGroupBlock)) {
116+
if (isExpectedClassType(expectedTypes, lastGroupBlock)) {
116117
parentBlock = lastGroupBlock
117118
} else {
118119
when (childBlock) {
@@ -232,11 +233,11 @@ class SqlSetParentGroupProcessor(
232233
lastGroupBlock: SqlBlock,
233234
childBlock: SqlColumnRawGroupBlock,
234235
) {
235-
val exceptionTypes =
236+
val expectedTypes =
236237
listOf(
237238
SqlColumnRawGroupBlock::class,
238239
)
239-
if (isExpectedClassType(exceptionTypes, lastGroupBlock)) {
240+
if (isExpectedClassType(expectedTypes, lastGroupBlock)) {
240241
blockBuilder.removeLastGroupTopNodeIndexHistory()
241242
}
242243
setParentGroups(
@@ -292,12 +293,12 @@ class SqlSetParentGroupProcessor(
292293
lastGroupBlock: SqlBlock,
293294
childBlock: SqlElConditionLoopCommentBlock,
294295
) {
295-
val exceptionalTypes =
296+
val expectedTypes =
296297
listOf(
297298
SqlCommaBlock::class,
298299
SqlElConditionLoopCommentBlock::class,
299300
)
300-
if (isExpectedClassType(exceptionalTypes, lastGroupBlock)) {
301+
if (isExpectedClassType(expectedTypes, lastGroupBlock)) {
301302
blockBuilder.removeLastGroupTopNodeIndexHistory()
302303
}
303304
setParentGroups(
@@ -370,15 +371,16 @@ class SqlSetParentGroupProcessor(
370371
context.childBlock.setParentGroupBlock(parentGroup)
371372
}
372373

373-
val expectedClassTypes =
374+
val expectedTypes =
374375
listOf(
375376
SqlSubGroupBlock::class,
376377
SqlCreateViewGroupBlock::class,
377378
SqlInlineGroupBlock::class,
378379
SqlInlineSecondGroupBlock::class,
379380
SqlColumnDefinitionRawGroupBlock::class,
381+
SqlLateralGroupBlock::class,
380382
)
381-
if (isNewGroup(context.childBlock, context.blockBuilder) || isExpectedClassType(expectedClassTypes, context.childBlock)) {
383+
if (isNewGroup(context.childBlock, context.blockBuilder) || isExpectedClassType(expectedTypes, context.childBlock)) {
382384
context.blockBuilder.addGroupTopNodeIndexHistory(context.childBlock)
383385
// Set parent-child relationship and indent for preceding comment at beginning of block group
384386
context.blockBuilder.updateCommentBlockIndent(context.childBlock)

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGr
4343
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock
4444
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock
4545
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
46+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlLateralGroupBlock
4647
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlSecondOptionKeywordGroupBlock
4748
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock
4849
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
@@ -129,6 +130,9 @@ class SqlBlockUtil(
129130
return SqlCreateViewGroupBlock(child, sqlBlockFormattingCtx)
130131
}
131132
}
133+
if (keywordText == "lateral") {
134+
return SqlLateralGroupBlock(child, sqlBlockFormattingCtx)
135+
}
132136
}
133137

134138
IndentType.CONFLICT -> {
@@ -276,7 +280,10 @@ class SqlBlockUtil(
276280
}
277281

278282
IndentType.SECOND_OPTION -> {
279-
return if (keywordText == "on" && lastGroupBlock !is SqlJoinGroupBlock) {
283+
return if (keywordText == "on" &&
284+
lastGroupBlock !is SqlJoinGroupBlock &&
285+
lastGroupBlock?.parentBlock !is SqlJoinGroupBlock
286+
) {
280287
SqlConflictClauseBlock(
281288
child,
282289
sqlBlockFormattingCtx,

0 commit comments

Comments
 (0)