Skip to content

Commit 062c45a

Browse files
committed
Add Support Query Group Blocks
1 parent 503b77d commit 062c45a

12 files changed

+576
-4
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.keyword.with.SqlWithQuerySubGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.IndentType
22+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
23+
24+
/**
25+
* Join Queries Keyword Group Block
26+
* [UNION, INTERSECT, EXCEPT]
27+
*/
28+
class SqlJoinQueriesGroupBlock(
29+
node: ASTNode,
30+
context: SqlBlockFormattingContext,
31+
) : SqlKeywordGroupBlock(node, IndentType.TOP, context) {
32+
// TODO Customize offset
33+
val offset = 0
34+
35+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
36+
super.setParentGroupBlock(lastGroup)
37+
indent.indentLen = createBlockIndentLen()
38+
indent.groupIndentLen = createGroupIndentLen()
39+
}
40+
41+
override fun createBlockIndentLen(): Int {
42+
parentBlock?.let { parent ->
43+
if (parent is SqlWithQuerySubGroupBlock) {
44+
return parent.indent.groupIndentLen
45+
}
46+
return parent.indent.groupIndentLen.plus(1)
47+
}
48+
return offset
49+
}
50+
51+
override fun createGroupIndentLen(): Int =
52+
topKeywordBlocks
53+
.sumOf { it.getNodeText().length.plus(1) }
54+
.plus(indent.indentLen)
55+
56+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
57+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.util.IndentType
21+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
22+
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
23+
24+
open class SqlSecondKeywordBlock(
25+
node: ASTNode,
26+
context: SqlBlockFormattingContext,
27+
) : SqlKeywordGroupBlock(node, IndentType.SECOND, context) {
28+
private val offset = 0
29+
30+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
31+
super.setParentGroupBlock(lastGroup)
32+
indent.indentLen = createBlockIndentLen()
33+
indent.groupIndentLen = createGroupIndentLen()
34+
}
35+
36+
override fun createBlockIndentLen(): Int {
37+
parentBlock?.let { parent ->
38+
val groupLen = parent.indent.groupIndentLen
39+
return if (parent.indent.indentLevel == IndentType.FILE) {
40+
offset
41+
} else {
42+
groupLen.minus(this.getNodeText().length)
43+
}
44+
}
45+
return offset
46+
}
47+
48+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
49+
lastGroup?.let {
50+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), lastGroup.getNodeText())
51+
}
52+
return true
53+
}
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.SqlKeywordBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
23+
import org.domaframework.doma.intellij.formatter.util.IndentType
24+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
25+
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
26+
27+
open class SqlSecondOptionKeywordGroupBlock(
28+
node: ASTNode,
29+
context: SqlBlockFormattingContext,
30+
) : SqlKeywordGroupBlock(node, IndentType.SECOND_OPTION, context) {
31+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
32+
super.setParentGroupBlock(lastGroup)
33+
indent.indentLen = createBlockIndentLen()
34+
}
35+
36+
override fun createBlockIndentLen(): Int {
37+
parentBlock?.let { parent ->
38+
val groupLen = parent.indent.groupIndentLen
39+
if (parent.indent.indentLevel == IndentType.FILE) {
40+
return 0
41+
}
42+
val subGroupBlock = parent.parentBlock as? SqlSubGroupBlock
43+
val newIndent =
44+
if (parent is SqlSubQueryGroupBlock) {
45+
groupLen.plus(1)
46+
} else if (parent is SqlKeywordGroupBlock && subGroupBlock != null && subGroupBlock.isFirstLineComment) {
47+
groupLen
48+
} else {
49+
return parent.indent.groupIndentLen
50+
.minus(getNodeText().length)
51+
}
52+
return newIndent
53+
} ?: 1
54+
return 1
55+
}
56+
57+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
58+
val prevKeyword = lastGroup?.childBlocks?.dropLast(1)?.findLast { it is SqlKeywordBlock }
59+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), prevKeyword?.getNodeText() ?: "")
60+
}
61+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.keyword.insert.SqlInsertQueryGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
22+
23+
class SqlValuesGroupBlock(
24+
node: ASTNode,
25+
context: SqlBlockFormattingContext,
26+
) : SqlSecondKeywordBlock(node, context) {
27+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
28+
if (lastGroup is SqlInsertQueryGroupBlock) {
29+
lastGroup.valueKeywordBlock = this
30+
}
31+
}
32+
33+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = parentBlock is SqlInsertQueryGroupBlock
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.domaframework.doma.intellij.formatter.block.group.keyword.top
2+
3+
import com.intellij.lang.ASTNode
4+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
5+
6+
class SqlDeleteQueryGroupBlock(
7+
node: ASTNode,
8+
context: SqlBlockFormattingContext,
9+
) : SqlTopQueryGroupBlock(node, context)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.with
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.SqlBlockFormattingContext
22+
23+
class SqlWithColumnGroupBlock(
24+
node: ASTNode,
25+
context: SqlBlockFormattingContext,
26+
) : SqlSubGroupBlock(node, context) {
27+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
28+
super.setParentGroupBlock(lastGroup)
29+
indent.indentLen = createBlockIndentLen()
30+
indent.groupIndentLen = createGroupIndentLen()
31+
}
32+
33+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
34+
(lastGroup as? SqlWithCommonTableGroupBlock)?.columnGroupBlock = this
35+
}
36+
37+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.minus(1) ?: 0
38+
39+
override fun createGroupIndentLen(): Int {
40+
parentBlock?.let { parent ->
41+
parent.parentBlock?.let { grand ->
42+
val topKeywordLen = grand.getNodeText().length.plus(1)
43+
return grand.childBlocks
44+
.sumOf { it.getNodeText().length.plus(1) }
45+
.plus(topKeywordLen)
46+
.plus(1)
47+
}
48+
}
49+
return 2
50+
}
51+
52+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
53+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.with
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.common.util.TypeUtil
20+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
21+
import org.domaframework.doma.intellij.formatter.block.SqlWordBlock
22+
import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock
23+
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
24+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
25+
26+
class SqlWithCommonTableGroupBlock(
27+
node: ASTNode,
28+
context: SqlBlockFormattingContext,
29+
) : SqlSubGroupBlock(node, context) {
30+
override val offset = 4
31+
32+
var commonTableNameBlock: SqlBlock? = getCommonTableName()
33+
var columnGroupBlock: SqlWithColumnGroupBlock? = null
34+
val optionKeywordBlocks: MutableList<SqlBlock> = mutableListOf()
35+
val queryGroupBlock: MutableList<SqlBlock> = mutableListOf()
36+
var isFirstTable = false
37+
38+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
39+
super.setParentGroupBlock(lastGroup)
40+
indent.indentLen = createBlockIndentLen()
41+
indent.groupIndentLen = createGroupIndentLen()
42+
isFirstTable = findWithQueryChildBlocks() == null
43+
}
44+
45+
private fun getCommonTableName(): SqlBlock? {
46+
if (getNodeText() == ",") return null
47+
val exceptionalTypes =
48+
listOf(
49+
SqlBlockCommentBlock::class,
50+
SqlWordBlock::class,
51+
)
52+
if (TypeUtil.isExpectedClassType(exceptionalTypes, this)) return this
53+
return null
54+
}
55+
56+
private fun findWithQueryChildBlocks(): SqlBlock? {
57+
parentBlock?.let { parent ->
58+
if (parent is SqlWithQueryGroupBlock) {
59+
return parent.childBlocks.dropLast(1).find { it is SqlWithCommonTableGroupBlock }
60+
}
61+
}
62+
return null
63+
}
64+
65+
override fun setParentPropertyBlock(lastGroup: SqlBlock?) {
66+
if (lastGroup is SqlWithQueryGroupBlock) {
67+
lastGroup.commonTableBlocks.add(this)
68+
}
69+
if (lastGroup is SqlWithCommonTableGroupBlock) {
70+
(lastGroup.parentBlock as? SqlWithQueryGroupBlock)?.commonTableBlocks?.add(this)
71+
}
72+
}
73+
74+
override fun createBlockIndentLen(): Int = 0
75+
76+
override fun createGroupIndentLen(): Int {
77+
parentBlock?.let { parent ->
78+
childBlocks.dropLast(1).sumOf { it.getNodeText().length.plus(1) }
79+
}
80+
return offset
81+
}
82+
83+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = !isFirstTable
84+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.with
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.keyword.SqlKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.util.IndentType
22+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
23+
24+
class SqlWithOptionGroupBlock(
25+
node: ASTNode,
26+
context: SqlBlockFormattingContext,
27+
) : SqlKeywordGroupBlock(node, IndentType.SECOND, context) {
28+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
29+
super.setParentGroupBlock(lastGroup)
30+
indent.indentLen = 0
31+
indent.groupIndentLen = getNodeText().length
32+
}
33+
34+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = true
35+
}

0 commit comments

Comments
 (0)