Skip to content

Commit 34b2e5f

Browse files
committed
Refactor properties and method parameters.
1 parent cc560c6 commit 34b2e5f

File tree

11 files changed

+87
-35
lines changed

11 files changed

+87
-35
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/util/TypeUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object TypeUtil {
102102
*/
103103
fun isExpectedClassType(
104104
expectedClasses: List<KClass<*>>,
105-
childBlock: SqlBlock,
105+
childBlock: SqlBlock?,
106106
): Boolean =
107107
expectedClasses.any { clazz ->
108108
clazz.isInstance(childBlock)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.column
16+
package org.domaframework.doma.intellij.formatter.block
1717

1818
import com.intellij.lang.ASTNode
1919
import com.intellij.psi.formatter.common.AbstractBlock

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ open class SqlOtherBlock(
3939
0,
4040
)
4141

42-
override val isNeedWhiteSpace: Boolean = false
43-
4442
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
4543
super.setParentGroupBlock(lastGroup)
4644
indent.indentLevel = IndentType.NONE

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class SqlTableBlock(
2828
node,
2929
context,
3030
) {
31-
override val isNeedWhiteSpace = false
32-
3331
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
3432
super.setParentGroupBlock(lastGroup)
3533
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class SqlExprBlock(
3434
context.formatMode,
3535
) {
3636
override fun getSpacing(
37-
p0: Block?,
38-
p1: Block,
37+
child1: Block?,
38+
child2: Block,
3939
): Spacing? = null
4040
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ open class SqlKeywordGroupBlock(
7575

7676
open fun getBaseIndentLen(
7777
preChildBlock: SqlBlock?,
78-
block: SqlBlock?,
78+
lastGroup: SqlBlock?,
7979
): Int {
80-
if (block == null) {
80+
if (lastGroup == null) {
8181
return createBlockIndentLen(preChildBlock)
8282
}
8383
if (preChildBlock == null) return createBlockIndentLen(preChildBlock)
@@ -130,17 +130,16 @@ open class SqlKeywordGroupBlock(
130130
open fun createBlockIndentLen(preChildBlock: SqlBlock?): Int {
131131
when (indentLevel) {
132132
IndentType.TOP -> {
133-
parentBlock?.let {
134-
val groupLen = it.indent.groupIndentLen
133+
parentBlock?.let { parent ->
135134
if (SqlKeywordUtil.isSetLineKeyword(getNodeText(), preChildBlock?.getNodeText() ?: "")) {
136135
val prevBlockIndent = preChildBlock?.indent?.indentLen ?: 0
137136
val prevBlockLen = preChildBlock?.getNodeText()?.length ?: 0
138137
return prevBlockIndent.plus(prevBlockLen).plus(1)
139138
}
140-
return if (it.indent.indentLevel == IndentType.FILE) {
139+
return if (parent.indent.indentLevel == IndentType.FILE) {
141140
0
142141
} else {
143-
groupLen
142+
parent.indent.groupIndentLen
144143
}
145144
} ?: return 0
146145
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ abstract class SqlSubGroupBlock(
7979
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
8080

8181
override fun getSpacing(
82-
p0: Block?,
83-
p1: Block,
82+
child1: Block?,
83+
child2: Block,
8484
): Spacing? = null
8585

8686
override fun isLeaf(): Boolean = true

src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.formatting.Block
2020
import com.intellij.formatting.Spacing
2121
import com.intellij.psi.tree.IElementType
2222
import org.domaframework.doma.intellij.formatter.block.SqlBlock
23+
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2324
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock
@@ -29,7 +30,6 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlU
2930
import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock
3031
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock
32-
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlRightPatternBlock
3333

3434
class SqlCustomSpacingBuilder {
3535
companion object {

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

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,79 @@ class SqlSetParentGroupProcessor(
9696
blockBuilder,
9797
)
9898

99+
val currentIndentLevel = childBlock.indent.indentLevel
100+
if (currentIndentLevel == IndentType.TOP) {
101+
var parentBlock: SqlBlock? = null
102+
val exceptionalTypes =
103+
listOf(
104+
SqlSubGroupBlock::class,
105+
SqlCreateViewGroupBlock::class,
106+
)
107+
if (isExpectedClassType(exceptionalTypes, lastGroupBlock)) {
108+
parentBlock = lastGroupBlock
109+
} else {
110+
when (childBlock) {
111+
is SqlUpdateQueryGroupBlock -> {
112+
UpdateClauseUtil
113+
.getParentGroupBlock(blockBuilder, childBlock)
114+
?.let { parentBlock = it }
115+
}
116+
117+
else -> {
118+
val topKeywordIndex =
119+
blockBuilder.getGroupTopNodeIndex { block ->
120+
block.indent.indentLevel == IndentType.TOP
121+
}
122+
val subGroupIndex =
123+
blockBuilder.getGroupTopNodeIndex { block ->
124+
block is SqlSubGroupBlock
125+
}
126+
127+
var deleteIndex = topKeywordIndex
128+
if (topKeywordIndex >= 0 && subGroupIndex < 0) {
129+
val copyParentBlock =
130+
blockBuilder.getGroupTopNodeIndexHistory()[topKeywordIndex]
131+
parentBlock = copyParentBlock.parentBlock
132+
} else if (topKeywordIndex > subGroupIndex) {
133+
val copyParentBlock =
134+
blockBuilder.getGroupTopNodeIndexHistory()[subGroupIndex]
135+
parentBlock = copyParentBlock
136+
}
137+
if (deleteIndex >= 0) {
138+
blockBuilder.clearSubListGroupTopNodeIndexHistory(deleteIndex)
139+
}
140+
}
141+
}
142+
}
143+
setParentGroups(context) { history ->
144+
return@setParentGroups parentBlock
145+
}
146+
return
147+
}
148+
99149
if (lastGroupBlock.indent.indentLevel == IndentType.SUB) {
100150
setParentGroups(context) { history ->
101151
return@setParentGroups lastGroupBlock
102152
}
103-
} else if (lastIndentLevel == childBlock.indent.indentLevel) {
153+
} else if (lastIndentLevel == currentIndentLevel) {
104154
blockBuilder.removeLastGroupTopNodeIndexHistory()
105155
updateGroupBlockLastGroupParentAddGroup(
106156
lastGroupBlock,
107157
childBlock,
108158
)
109-
} else if (lastIndentLevel < childBlock.indent.indentLevel) {
159+
} else if (lastIndentLevel < currentIndentLevel) {
160+
updateGroupBlockParentAndAddGroup(
161+
childBlock,
162+
)
163+
} else if (lastIndentLevel == IndentType.JOIN &&
164+
SqlKeywordUtil.isSecondOptionKeyword(childBlock.getNodeText())
165+
) {
166+
// left,right < inner,outer < join
110167
updateGroupBlockParentAndAddGroup(
111168
childBlock,
112169
)
170+
return
113171
} else {
114-
if (lastIndentLevel == IndentType.JOIN &&
115-
SqlKeywordUtil.Companion.isSecondOptionKeyword(childBlock.getNodeText())
116-
) {
117-
// left,right < inner,outer < join
118-
updateGroupBlockParentAndAddGroup(
119-
childBlock,
120-
)
121-
return
122-
}
123-
124172
setParentGroups(context) { history ->
125173
return@setParentGroups history
126174
.lastOrNull { it.indent.indentLevel < childBlock.indent.indentLevel }
@@ -152,7 +200,11 @@ class SqlSetParentGroupProcessor(
152200
lastGroupBlock: SqlBlock,
153201
childBlock: SqlColumnRawGroupBlock,
154202
) {
155-
if (lastGroupBlock is SqlColumnRawGroupBlock) {
203+
val exceptionTypes =
204+
listOf(
205+
SqlColumnRawGroupBlock::class,
206+
)
207+
if (isExpectedClassType(exceptionTypes, lastGroupBlock)) {
156208
blockBuilder.removeLastGroupTopNodeIndexHistory()
157209
}
158210
updateGroupBlockParentAndAddGroup(childBlock)
@@ -200,7 +252,12 @@ class SqlSetParentGroupProcessor(
200252
lastGroupBlock: SqlBlock,
201253
childBlock: SqlElConditionLoopCommentBlock,
202254
) {
203-
if (lastGroupBlock is SqlCommaBlock || lastGroupBlock is SqlElConditionLoopCommentBlock) {
255+
val exceptionalTypes =
256+
listOf(
257+
SqlCommaBlock::class,
258+
SqlElConditionLoopCommentBlock::class,
259+
)
260+
if (isExpectedClassType(exceptionalTypes, lastGroupBlock)) {
204261
blockBuilder.removeLastGroupTopNodeIndexHistory()
205262
}
206263
setParentGroups(
@@ -251,7 +308,7 @@ class SqlSetParentGroupProcessor(
251308
val parentGroup =
252309
getParentGroup(context.blockBuilder.getGroupTopNodeIndexHistory() as MutableList<SqlBlock>)
253310

254-
// // The parent block for SqlElConditionLoopCommentBlock will be set later
311+
// The parent block for SqlElConditionLoopCommentBlock will be set later
255312
if (context.childBlock !is SqlElConditionLoopCommentBlock ||
256313
context.childBlock.conditionType.isEnd()
257314
) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package org.domaframework.doma.intellij.formatter.util
1717

1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20-
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlValuesGroupBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
@@ -32,7 +32,7 @@ object InsertClauseUtil {
3232
if (lastGroup is SqlInsertQueryGroupBlock) {
3333
return SqlInsertColumnGroupBlock(child, sqlBlockFormattingCtx)
3434
}
35-
if (lastGroup is SqlKeywordGroupBlock && lastGroup.getNodeText() == "values") {
35+
if (lastGroup is SqlValuesGroupBlock) {
3636
return SqlInsertValueGroupBlock(child, sqlBlockFormattingCtx)
3737
}
3838
return null

0 commit comments

Comments
 (0)