Skip to content

Commit da0b87c

Browse files
committed
LATERAL formatting rule implementation
1 parent b99a60c commit da0b87c

File tree

11 files changed

+56
-20
lines changed

11 files changed

+56
-20
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/formatter/block/group/keyword/SqlJoinGroupBlock.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,14 @@ open class SqlJoinGroupBlock(
6060
?.groupIndentLen
6161
?.plus(1) ?: 1
6262

63+
override fun createGroupIndentLen(): Int =
64+
indent.indentLen
65+
.plus(
66+
topKeywordBlocks
67+
.drop(1)
68+
.filter { it.getNodeText() != "lateral" }
69+
.sumOf { it.getNodeText().length.plus(1) },
70+
).plus(getNodeText().length)
71+
6372
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
6473
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,8 @@ open class SqlKeywordGroupBlock(
8585
if (preChildBlock.indent.indentLevel == this.indent.indentLevel &&
8686
!SqlKeywordUtil.isSetLineKeyword(getNodeText(), preChildBlock.getNodeText())
8787
) {
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-
}
88+
val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length)
89+
return preChildBlock.indent.indentLen.minus(diffPretextLen)
9590
} else {
9691
return createBlockIndentLen(preChildBlock)
9792
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ class SqlSelectQueryGroupBlock(
4242
indent.groupIndentLen = createGroupIndentLen()
4343
}
4444

45-
override fun getBaseIndentLen(
46-
preChildBlock: SqlBlock?,
47-
lastGroup: SqlBlock?,
48-
): Int {
49-
if (parentBlock is SqlSubGroupBlock) {
50-
return parentBlock?.indent?.groupIndentLen
51-
?: createBlockIndentLen(preChildBlock)
52-
}
53-
return createBlockIndentLen(preChildBlock)
54-
}
55-
5645
override fun createGroupIndentLen(): Int {
5746
parentBlock?.let { parent ->
5847
if (parent is SqlSubQueryGroupBlock) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ 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
2123
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2224
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
2325
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -52,7 +54,13 @@ open class SqlSubQueryGroupBlock(
5254
} else if (parent is SqlWithQuerySubGroupBlock) {
5355
parent.indent.groupIndentLen
5456
} else {
55-
parent.indent.groupIndentLen.plus(1)
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))
5664
}
5765
} ?: offset
5866

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class SqlKeywordUtil {
9494
"having",
9595
"limit",
9696
"values",
97-
"lateral",
9897
"returning",
9998
)
10099

@@ -273,6 +272,7 @@ class SqlKeywordUtil {
273272
"first",
274273
"to",
275274
"using",
275+
"lateral",
276276
)
277277

278278
fun isOptionSqlKeyword(keyword: String): Boolean = OPTION_SQL_KEYWORDS.contains(keyword.lowercase())

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class SqlFormatterTest : BasePlatformTestCase() {
5454
formatSqlFile("Select.sql", "Select$formatDataPrefix.sql")
5555
}
5656

57+
fun testSelectFromLateralFormatter() {
58+
formatSqlFile("SelectFromLateral.sql", "SelectFromLateral$formatDataPrefix.sql")
59+
}
60+
61+
fun testSelectJoinLateralFormatter() {
62+
formatSqlFile("SelectJoinLateral.sql", "SelectJoinLateral$formatDataPrefix.sql")
63+
}
64+
5765
fun testCreateTableFormatter() {
5866
formatSqlFile("CreateTable.sql", "CreateTable$formatDataPrefix.sql")
5967
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT u.name, tag
2+
FROM users u , LATERAL ( SELECT *
3+
FROM post WHERE content = 'XXX' ) AS tag , employee
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT u.name
2+
, tag
3+
FROM users u
4+
, LATERAL ( SELECT *
5+
FROM post
6+
WHERE content = 'XXX' ) AS tag
7+
, employee
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT u.user_id,
2+
u.name , o.order_id AS latest_order_id , o.order_date
3+
FROM users u LEFT JOIN lateral ( SELECT *
4+
FROM orders o WHERE o.order_id = u.user_id ORDER BY o.order_date DESC
5+
LIMIT 1 ) o ON true

0 commit comments

Comments
 (0)