Skip to content

Commit 68b621f

Browse files
committed
Add Format Blocks
1 parent e679ca4 commit 68b621f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3453
-15
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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
17+
18+
import com.intellij.formatting.ASTBlock
19+
import com.intellij.formatting.Block
20+
import com.intellij.formatting.Spacing
21+
import com.intellij.psi.tree.IElementType
22+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
23+
import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock
24+
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
25+
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
26+
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
27+
import org.domaframework.doma.intellij.formatter.block.group.SqlCreateKeywordGroupBlock
28+
import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock
29+
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
30+
31+
class SqlCustomSpacingBuilder {
32+
companion object;
33+
34+
private val spacingRules: MutableMap<Pair<IElementType?, IElementType?>?, Spacing?> = HashMap()
35+
36+
fun withSpacing(
37+
left: IElementType?,
38+
right: IElementType?,
39+
spacing: Spacing?,
40+
): SqlCustomSpacingBuilder {
41+
spacingRules.put(Pair(left, right), spacing)
42+
return this
43+
}
44+
45+
fun getSpacing(
46+
child1: Block?,
47+
child2: Block?,
48+
): Spacing? {
49+
if (child1 is ASTBlock && child2 is ASTBlock) {
50+
val type1: IElementType? = child1.node?.elementType
51+
val type2: IElementType? = child2.node?.elementType
52+
val spacing: Spacing? = spacingRules[Pair(type1, type2)]
53+
if (spacing != null) {
54+
return spacing
55+
}
56+
}
57+
if (child1 == null && child2 is ASTBlock) {
58+
val type2: IElementType? = child2.node?.elementType
59+
val spacing: Spacing? = spacingRules[Pair(null, type2)]
60+
if (spacing != null) {
61+
return spacing
62+
}
63+
}
64+
return null
65+
}
66+
67+
fun getSpacingWithIndentComma(
68+
child1: SqlBlock?,
69+
child2: SqlCommaBlock,
70+
): Spacing? {
71+
val indentLen: Int = child2.indent.indentLen
72+
when (child1) {
73+
null -> return Spacing.createSpacing(0, 0, 0, false, 0, 0)
74+
is SqlWhitespaceBlock -> {
75+
val afterNewLine = child1.node.text.substringAfterLast("\n", "")
76+
if (child1.node.text.contains("\n")) {
77+
val currentIndent = afterNewLine.length
78+
val newIndent =
79+
if (currentIndent != indentLen) {
80+
indentLen
81+
} else {
82+
0
83+
}
84+
return Spacing.createSpacing(newIndent, newIndent, 0, false, 0, 0)
85+
}
86+
}
87+
else -> {
88+
return Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
89+
}
90+
}
91+
return null
92+
}
93+
94+
fun getSpacingWithWhiteSpace(
95+
child1: SqlWhitespaceBlock,
96+
child2: SqlNewGroupBlock,
97+
): Spacing? {
98+
child1.node.text.substringAfterLast("\n", "")
99+
return Spacing.createSpacing(
100+
child2.indent.indentLen,
101+
child2.indent.indentLen,
102+
0,
103+
false,
104+
0,
105+
0,
106+
)
107+
return null
108+
}
109+
110+
fun getSpacingColumnRaw(child: SqlColumnDefinitionRawGroupBlock): Spacing? {
111+
val indentLen = child.indent.indentLen
112+
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
113+
}
114+
115+
fun getSpacingColumnRowEndRight(child: SqlRightPatternBlock): Spacing? {
116+
val indentLen = child.indent.indentLen
117+
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
118+
}
119+
120+
/**
121+
* Adjust line breaks and indentation depending on the block indent type
122+
*/
123+
fun getSpacingWithIndentLevel(child: SqlNewGroupBlock): Spacing? {
124+
val parentBlock = child.parentBlock
125+
val indentLen: Int = child.indent.indentLen
126+
127+
return when (child.indent.indentLevel) {
128+
IndentType.TOP -> {
129+
return if (parentBlock?.parentBlock == null) {
130+
Spacing.createSpacing(0, 0, 0, false, 0, 0)
131+
} else if (parentBlock.indent.indentLevel == IndentType.SUB) {
132+
Spacing.createSpacing(0, 0, 0, false, 0, 0)
133+
} else {
134+
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
135+
}
136+
}
137+
138+
IndentType.SECOND -> {
139+
return if (parentBlock is SqlSubQueryGroupBlock) {
140+
Spacing.createSpacing(1, 1, 0, false, 0, 0)
141+
} else if (SqlKeywordUtil.isSetLineKeyword(child.node.text, parentBlock?.node?.text ?: "")) {
142+
null
143+
} else {
144+
Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
145+
}
146+
}
147+
148+
IndentType.SECOND_OPTION -> {
149+
return Spacing.createSpacing(indentLen, indentLen, 1, false, 0, 1)
150+
}
151+
152+
IndentType.SUB -> {
153+
if (parentBlock is SqlCreateKeywordGroupBlock) {
154+
return Spacing.createSpacing(0, 0, 1, false, 0, 1)
155+
}
156+
return null
157+
}
158+
159+
IndentType.INLINE -> {
160+
return Spacing.createSpacing(0, 0, 0, false, 0, 0)
161+
}
162+
163+
IndentType.INLINE_SECOND -> {
164+
parentBlock?.let {
165+
val parentIndentLen = it.indent.groupIndentLen
166+
val parentTextLen = it.node.text.length
167+
val newIndentLen = parentIndentLen.plus(parentTextLen).plus(1)
168+
return Spacing.createSpacing(newIndentLen, newIndentLen, 1, false, 0, 1)
169+
}
170+
return Spacing.createSpacing(0, 0, 1, false, 0, 1)
171+
}
172+
173+
else -> {
174+
return null
175+
}
176+
}
177+
return null
178+
}
179+
}

0 commit comments

Comments
 (0)