Skip to content

Commit d8dbc62

Browse files
committed
Change column row indentation to fixed in Create Table
1 parent 81fdc4d commit d8dbc62

File tree

6 files changed

+89
-64
lines changed

6 files changed

+89
-64
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +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.SqlDataTypeBlock
23+
import org.domaframework.doma.intellij.formatter.block.SqlColumnBlock
2424
import org.domaframework.doma.intellij.formatter.block.SqlRightPatternBlock
2525
import org.domaframework.doma.intellij.formatter.block.SqlWhitespaceBlock
2626
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
@@ -105,13 +105,13 @@ class SqlCustomSpacingBuilder {
105105
0,
106106
)
107107

108-
fun getSpacingDataType(child: SqlDataTypeBlock): Spacing? {
108+
fun getSpacingColumnDefinition(child: SqlColumnBlock): Spacing? {
109109
val indentLen = child.createIndentLen()
110110
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
111111
}
112112

113113
fun getSpacingColumnDefinitionRaw(child: SqlColumnDefinitionRawGroupBlock): Spacing? {
114-
val indentLen = child.indent.indentLen
114+
val indentLen = child.createIndentLen()
115115
return Spacing.createSpacing(indentLen, indentLen, 0, false, 0, 0)
116116
}
117117

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ open class SqlBlock(
105105
blocks.removeLast()
106106
}
107107
}
108-
setRightSpace(blocks.lastOrNull() as SqlBlock?, childBlock)
108+
setRightSpace(blocks.lastOrNull() as SqlBlock?)
109109
prevNonWhiteSpaceNode = child
110110
updateSearchKeywordLevelHistory(childBlock, child)
111111
blocks.add(childBlock)
@@ -151,10 +151,7 @@ open class SqlBlock(
151151
child.treePrev.text.contains("\n")
152152
)
153153

154-
private fun setRightSpace(
155-
lastBlock: SqlBlock?,
156-
currentBlock: SqlBlock,
157-
) {
154+
private fun setRightSpace(lastBlock: SqlBlock?) {
158155
val rightBlock = lastBlock as? SqlRightPatternBlock
159156
rightBlock?.enableLastRight()
160157
}
@@ -331,13 +328,13 @@ open class SqlBlock(
331328
}
332329
}
333330

334-
is SqlWordBlock, is SqlOtherBlock, is SqlLineCommentBlock, is SqlBlockCommentBlock -> {
331+
is SqlColumnBlock -> {
335332
setParentGroups(
336333
childBlock,
337334
) { history ->
338335
val parentGroupBlock = history.last().second
339336
if (parentGroupBlock is SqlColumnDefinitionRawGroupBlock &&
340-
parentGroupBlock.columnName == ","
337+
parentGroupBlock.columnName != ","
341338
) {
342339
parentGroupBlock.columnName = childBlock.node.text
343340
val columnDefinition = parentGroupBlock.parentBlock as? SqlColumnDefinitionGroupBlock
@@ -349,6 +346,25 @@ open class SqlBlock(
349346
}
350347
}
351348

349+
is SqlColumnDefinitionRawGroupBlock -> {
350+
if (lastGroupBlock is SqlColumnDefinitionRawGroupBlock) {
351+
blockBuilder.removeLastGroupTopNodeIndexHistory()
352+
}
353+
setParentGroups(
354+
childBlock,
355+
) { history ->
356+
return@setParentGroups history.last().second
357+
}
358+
}
359+
360+
is SqlWordBlock, is SqlOtherBlock, is SqlLineCommentBlock, is SqlBlockCommentBlock -> {
361+
setParentGroups(
362+
childBlock,
363+
) { history ->
364+
return@setParentGroups history.last().second
365+
}
366+
}
367+
352368
is SqlSubQueryGroupBlock -> {
353369
setParentGroups(
354370
childBlock,
@@ -389,17 +405,6 @@ open class SqlBlock(
389405
}
390406
}
391407

392-
is SqlColumnDefinitionRawGroupBlock -> {
393-
if (lastGroupBlock is SqlColumnDefinitionRawGroupBlock) {
394-
blockBuilder.removeLastGroupTopNodeIndexHistory()
395-
}
396-
setParentGroups(
397-
childBlock,
398-
) { history ->
399-
return@setParentGroups history.last().second
400-
}
401-
}
402-
403408
is SqlDataTypeBlock -> {
404409
setParentGroups(
405410
childBlock,
@@ -506,6 +511,20 @@ open class SqlBlock(
506511
)
507512
}
508513

514+
is SqlColumnDefinitionRawGroupBlock -> {
515+
if (lastGroup.childBlocks.isEmpty()) {
516+
lastGroup.columnName = child.text
517+
SqlColumnBlock(
518+
child,
519+
wrap,
520+
alignment,
521+
spacingBuilder,
522+
)
523+
} else {
524+
SqlWordBlock(child, wrap, alignment, spacingBuilder)
525+
}
526+
}
527+
509528
else -> SqlWordBlock(child, wrap, alignment, spacingBuilder)
510529
}
511530
}
@@ -705,6 +724,7 @@ open class SqlBlock(
705724
SqlCustomSpacingBuilder()
706725
.getSpacingColumnDefinitionRawEndRight(child2)
707726
?.let { return it }
727+
child2.parentBlock is SqlDataTypeParamBlock -> SqlCustomSpacingBuilder.nonSpacing
708728

709729
child2.lastRight -> SqlCustomSpacingBuilder.normalSpacing
710730
else -> SqlCustomSpacingBuilder.nonSpacing
@@ -719,8 +739,8 @@ open class SqlBlock(
719739
return SqlCustomSpacingBuilder.nonSpacing
720740
}
721741

722-
if (child2 is SqlDataTypeBlock) {
723-
SqlCustomSpacingBuilder().getSpacingDataType(child2)?.let { return it }
742+
if (child2 is SqlColumnBlock) {
743+
SqlCustomSpacingBuilder().getSpacingColumnDefinition(child2)?.let { return it }
724744
}
725745

726746
val spacing: Spacing? = customSpacingBuilder?.getCustomSpacing(child1, child2)

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.intellij.formatting.Wrap
2121
import com.intellij.lang.ASTNode
2222
import com.intellij.psi.formatter.common.AbstractBlock
2323
import org.domaframework.doma.intellij.formatter.IndentType
24-
import org.domaframework.doma.intellij.formatter.block.group.SqlSubQueryGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionGroupBlock
2525

2626
class SqlColumnBlock(
2727
node: ASTNode,
@@ -44,19 +44,22 @@ class SqlColumnBlock(
4444
override fun setParentGroupBlock(block: SqlBlock?) {
4545
super.setParentGroupBlock(block)
4646
indent.indentLevel = IndentType.NONE
47-
indent.indentLen = createIndentLen()
47+
// Calculate right justification space during indentation after getting all column rows
48+
indent.indentLen = 1
4849
indent.groupIndentLen = 0
4950
}
5051

5152
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
5253

53-
private fun createIndentLen(): Int =
54+
fun createIndentLen(): Int {
5455
parentBlock?.let {
55-
if (it is SqlSubQueryGroupBlock) {
56-
return@let it.indent.groupIndentLen
57-
}
58-
it.indent.groupIndentLen
59-
.plus(it.node.text.length)
60-
.plus(1)
61-
} ?: 1
56+
val parentGroupDefinition = it.parentBlock as? SqlColumnDefinitionGroupBlock
57+
if (parentGroupDefinition == null) return 1
58+
59+
val groupMaxAlimentLen = parentGroupDefinition.alignmentColumnName.length
60+
val diffColumnName = groupMaxAlimentLen.minus(node.text.length)
61+
return diffColumnName.plus(1)
62+
}
63+
return 1
64+
}
6265
}

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import com.intellij.formatting.Wrap
2222
import com.intellij.lang.ASTNode
2323
import com.intellij.psi.formatter.common.AbstractBlock
2424
import org.domaframework.doma.intellij.formatter.IndentType
25-
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionGroupBlock
26-
import org.domaframework.doma.intellij.formatter.block.group.SqlColumnDefinitionRawGroupBlock
2725

2826
open class SqlDataTypeBlock(
2927
node: ASTNode,
@@ -47,30 +45,11 @@ open class SqlDataTypeBlock(
4745
override fun setParentGroupBlock(block: SqlBlock?) {
4846
super.setParentGroupBlock(block)
4947
indent.indentLevel = IndentType.NONE
50-
// Calculate right justification space during indentation after getting all column rows
51-
indent.indentLen = 0
48+
indent.indentLen = 1
5249
indent.groupIndentLen = indent.indentLen
5350
}
5451

5552
override fun buildChildren(): MutableList<AbstractBlock> = mutableListOf()
5653

5754
override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen)
58-
59-
fun createIndentLen(): Int {
60-
parentBlock?.let {
61-
when (it) {
62-
is SqlColumnDefinitionRawGroupBlock -> {
63-
val groupBlock = it.parentBlock as? SqlColumnDefinitionGroupBlock
64-
val groupAlimentLen = groupBlock?.alignmentColumnName?.length ?: 1
65-
val rawColumnNameLen = it.columnName.length
66-
val newSpaces = groupAlimentLen.minus(rawColumnNameLen).plus(1)
67-
return newSpaces
68-
}
69-
else -> {
70-
return 1
71-
}
72-
}
73-
}
74-
return 1
75-
}
7655
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class SqlColumnDefinitionGroupBlock(
3939
) {
4040
var alignmentColumnName = ""
4141

42+
// TODO:Customize indentation
43+
val offset = 2
44+
4245
override fun setParentGroupBlock(block: SqlBlock?) {
4346
super.setParentGroupBlock(block)
4447
indent.indentLen = createIndentLen()
@@ -49,5 +52,5 @@ class SqlColumnDefinitionGroupBlock(
4952

5053
override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen)
5154

52-
override fun createIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
55+
override fun createIndentLen(): Int = offset
5356
}

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class SqlColumnDefinitionRawGroupBlock(
4040
null,
4141
spacingBuilder,
4242
) {
43+
// TODO:Customize indentation within an inline group
44+
val defaultOffset = 5
45+
val isFirstColumnRaw = node.elementType != SqlTypes.COMMA
46+
4347
var columnName = node.text
4448

4549
override fun setParentGroupBlock(block: SqlBlock?) {
@@ -52,14 +56,30 @@ class SqlColumnDefinitionRawGroupBlock(
5256

5357
override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen)
5458

55-
private fun createIndentLen(): Int =
59+
/**
60+
* Right-justify the longest column name in the column definition.
61+
*/
62+
fun createIndentLen(): Int {
63+
if (!isFirstColumnRaw) return defaultOffset
64+
5665
parentBlock?.let {
57-
val parentIndentLen = it.indent.groupIndentLen
58-
val baseIndent = parentIndentLen.plus(parentIndentLen)
59-
when (node.elementType) {
60-
SqlTypes.COMMA -> baseIndent.minus(1)
61-
SqlTypes.WORD -> baseIndent.plus(1)
62-
else -> baseIndent
66+
return when (it) {
67+
is SqlColumnDefinitionGroupBlock -> {
68+
getColumnRawNewIndent(it)
69+
}
70+
71+
else -> {
72+
1
73+
}
6374
}
64-
} ?: 1
75+
}
76+
return 1
77+
}
78+
79+
private fun getColumnRawNewIndent(groupRawBlock: SqlColumnDefinitionGroupBlock): Int {
80+
val groupMaxAlimentLen = groupRawBlock.alignmentColumnName.length
81+
val diffColumnName = groupMaxAlimentLen.minus(columnName.length)
82+
val newSpaces = defaultOffset.plus(diffColumnName)
83+
return newSpaces.plus(2)
84+
}
6585
}

0 commit comments

Comments
 (0)