Skip to content

Commit 8892a79

Browse files
committed
Implement function name formatting rules
1 parent f9639f3 commit 8892a79

31 files changed

+330
-106
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBl
2828
import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock
2929
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
3030
import org.domaframework.doma.intellij.formatter.util.IndentType
31+
import org.domaframework.doma.intellij.psi.SqlTypes
3132

3233
open class SqlBlock(
3334
node: ASTNode,
@@ -59,6 +60,24 @@ open class SqlBlock(
5960
open var parentBlock: SqlBlock? = null
6061
open val childBlocks = mutableListOf<SqlBlock>()
6162

63+
fun getChildrenTextLen(): Int =
64+
childBlocks.sumOf { child ->
65+
val children =
66+
child.childBlocks.filter { it !is SqlLineCommentBlock && it !is SqlBlockCommentBlock }
67+
if (children.isNotEmpty()) {
68+
child
69+
.getChildrenTextLen()
70+
.plus(child.getNodeText().length)
71+
} else if (child.node.elementType == SqlTypes.DOT ||
72+
child.node.elementType == SqlTypes.RIGHT_PAREN
73+
) {
74+
// Since elements do not include surrounding spaces, they should be excluded from the character count.
75+
0
76+
} else {
77+
child.getNodeText().length.plus(1)
78+
}
79+
}
80+
6281
fun getChildBlocksDropLast(
6382
dropIndex: Int = 1,
6483
skipCommentBlock: Boolean = false,

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import org.domaframework.doma.intellij.common.util.TypeUtil
2121
import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock
2222
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
24-
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2524
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock
2625
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock
2726
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
@@ -44,7 +43,6 @@ open class SqlCommaBlock(
4443
node,
4544
context.wrap,
4645
context.alignment,
47-
null,
4846
context.spacingBuilder,
4947
context.enableFormat,
5048
context.formatMode,
@@ -61,6 +59,14 @@ open class SqlCommaBlock(
6159
SqlWithColumnGroupBlock::class,
6260
SqlKeywordGroupBlock::class,
6361
)
62+
63+
private val PARENT_INDENT_SYNC_TYPES =
64+
listOf(
65+
SqlUpdateColumnGroupBlock::class,
66+
SqlInsertColumnGroupBlock::class,
67+
SqlWithColumnGroupBlock::class,
68+
SqlFunctionParamBlock::class,
69+
)
6470
}
6571

6672
override val indent =
@@ -92,14 +98,8 @@ open class SqlCommaBlock(
9298
return 0
9399
}
94100

95-
val parentIndentSyncBlockTypes =
96-
listOf(
97-
SqlUpdateColumnGroupBlock::class,
98-
SqlInsertColumnGroupBlock::class,
99-
SqlWithColumnGroupBlock::class,
100-
)
101101
val parentIndentLen = parent.indent.groupIndentLen
102-
if (TypeUtil.isExpectedClassType(parentIndentSyncBlockTypes, parent)) {
102+
if (TypeUtil.isExpectedClassType(PARENT_INDENT_SYNC_TYPES, parent)) {
103103
return parentIndentLen
104104
}
105105

@@ -113,21 +113,22 @@ open class SqlCommaBlock(
113113
return parentIndentLen.plus(1)
114114
}
115115

116-
if (parent is SqlValuesParamGroupBlock) return 0
116+
val notNewLineTypes =
117+
listOf(
118+
SqlValuesParamGroupBlock::class,
119+
SqlConditionalExpressionGroupBlock::class,
120+
)
121+
if (TypeUtil.isExpectedClassType(notNewLineTypes, parent)) return 0
117122

118123
val grand = parent.parentBlock
119124
grand?.let { grand ->
120-
if (grand is SqlCreateKeywordGroupBlock) {
121-
val grandIndentLen = grand.indent.groupIndentLen
122-
return grandIndentLen.plus(parentIndentLen).minus(1)
123-
}
124-
125125
val grandIndent = grand.indent.indentLen
126126
val groupIndent = parentBlock?.indent?.groupIndentLen ?: 0
127127

128128
if (grand is SqlColumnRawGroupBlock) {
129129
return groupIndent.plus(grandIndent)
130130
}
131+
131132
return groupIndent.plus(grandIndent).minus(1)
132133
}
133134
return parentIndentLen.plus(1)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ open class SqlDataTypeBlock(
2828
node,
2929
context.wrap,
3030
context.alignment,
31-
null,
3231
context.spacingBuilder,
3332
context.enableFormat,
3433
context.formatMode,

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

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTyp
4848
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock
4949
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
5050
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock
51+
import org.domaframework.doma.intellij.formatter.block.other.SqlEscapeBlock
52+
import org.domaframework.doma.intellij.formatter.block.other.SqlOtherBlock
5153
import org.domaframework.doma.intellij.formatter.block.word.SqlFunctionGroupBlock
5254
import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock
5355
import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder
@@ -160,10 +162,18 @@ class SqlFileBlock(
160162
defaultFormatCtx,
161163
)
162164
} else {
163-
SqlOtherBlock(
164-
child,
165-
defaultFormatCtx,
166-
)
165+
val escapeStrings = listOf("\"", "`", "[", "]")
166+
if (escapeStrings.contains(child.text)) {
167+
SqlEscapeBlock(
168+
child,
169+
defaultFormatCtx,
170+
)
171+
} else {
172+
SqlOtherBlock(
173+
child,
174+
defaultFormatCtx,
175+
)
176+
}
167177
}
168178
}
169179

@@ -422,64 +432,77 @@ class SqlFileBlock(
422432
child2: Block,
423433
): Spacing? {
424434
if (isAdjustIndentOnEnter()) return null
435+
val childBlock1: SqlBlock? = child1 as? SqlBlock
436+
val childBlock2: SqlBlock = child2 as SqlBlock
425437

426438
// The end of a line comment element is a newline, so just add a space for the indent.
427-
if (child1 is SqlLineCommentBlock && child2 is SqlBlock) {
428-
return SqlCustomSpacingBuilder().getSpacing(child2)
439+
if (childBlock1 is SqlLineCommentBlock) {
440+
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
429441
}
430442

431-
if (child2 is SqlWithColumnGroupBlock) {
443+
if (childBlock2 is SqlWithColumnGroupBlock) {
432444
return SqlCustomSpacingBuilder.normalSpacing
433445
}
434446

435-
if (child1 is SqlSubGroupBlock && child2 is SqlSubGroupBlock) {
447+
if (childBlock1 is SqlSubGroupBlock && childBlock2 is SqlSubGroupBlock) {
436448
return SqlCustomSpacingBuilder.nonSpacing
437449
}
438450

439451
// Do not leave a space after the comment block of the bind variable
440-
if (child1 is SqlElBlockCommentBlock && child1 !is SqlElConditionLoopCommentBlock && child2 !is SqlCommentBlock) {
452+
if (childBlock1 is SqlElBlockCommentBlock && childBlock1 !is SqlElConditionLoopCommentBlock && childBlock2 !is SqlCommentBlock) {
441453
return SqlCustomSpacingBuilder.nonSpacing
442454
}
443455

444-
if (child2 is SqlElBlockCommentBlock) {
445-
return when (child1) {
456+
if (childBlock2 is SqlElBlockCommentBlock) {
457+
return when (childBlock1) {
446458
is SqlElBlockCommentBlock, is SqlWhitespaceBlock -> {
447-
SqlCustomSpacingBuilder().getSpacing(child2)
459+
SqlCustomSpacingBuilder().getSpacing(childBlock2)
448460
}
449461

450462
else -> SqlCustomSpacingBuilder.normalSpacing
451463
}
452464
}
453465

454-
if (child1 is SqlFunctionParamBlock) {
466+
if (childBlock1?.node?.elementType == SqlTypes.DOT ||
467+
childBlock2.node.elementType == SqlTypes.DOT
468+
) {
455469
return SqlCustomSpacingBuilder.nonSpacing
456470
}
457471

458-
if (child2 is SqlOtherBlock) {
459-
if (child1 is SqlFunctionGroupBlock) {
460-
val reservedWordEscapeSymbolList = listOf("\"", "[", "`")
461-
if (reservedWordEscapeSymbolList.contains(child2.getNodeText())) {
462-
return SqlCustomSpacingBuilder.nonSpacing
463-
}
472+
if (childBlock1 is SqlEscapeBlock) {
473+
return if (!childBlock1.isEndEscape) {
474+
SqlCustomSpacingBuilder.nonSpacing
475+
} else {
476+
SqlCustomSpacingBuilder.normalSpacing
464477
}
465-
return SqlCustomSpacingBuilder().getSpacing(child2)
466478
}
467479

468-
if (child1 is SqlWhitespaceBlock) {
469-
when (child2) {
480+
if (childBlock2 is SqlEscapeBlock) {
481+
if (childBlock2.isEndEscape) {
482+
return SqlCustomSpacingBuilder.nonSpacing
483+
}
484+
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
485+
}
486+
487+
if (childBlock2 is SqlOtherBlock) {
488+
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
489+
}
490+
491+
if (childBlock1 is SqlWhitespaceBlock) {
492+
when (childBlock2) {
470493
is SqlBlockCommentBlock, is SqlLineCommentBlock, is SqlNewGroupBlock -> {
471-
return SqlCustomSpacingBuilder().getSpacing(child2)
494+
return SqlCustomSpacingBuilder().getSpacing(childBlock2)
472495
}
473496
}
474497
}
475498

476-
if (child2 is SqlNewGroupBlock) {
477-
if (child1 is SqlSubGroupBlock && child2.indent.indentLevel == IndentType.ATTACHED) {
499+
if (childBlock2 is SqlNewGroupBlock) {
500+
if (childBlock1 is SqlSubGroupBlock && childBlock2.indent.indentLevel == IndentType.ATTACHED) {
478501
return SqlCustomSpacingBuilder.nonSpacing
479502
}
480-
when (child2) {
503+
when (childBlock2) {
481504
is SqlSubQueryGroupBlock -> {
482-
if (child1 is SqlNewGroupBlock) {
505+
if (childBlock1 is SqlNewGroupBlock) {
483506
return SqlCustomSpacingBuilder.normalSpacing
484507
}
485508
}
@@ -491,31 +514,31 @@ class SqlFileBlock(
491514
}
492515

493516
// Create Table Column Definition Raw Group Block
494-
CreateTableUtil.getColumnDefinitionRawGroupSpacing(child1, child2)?.let { return it }
517+
CreateTableUtil.getColumnDefinitionRawGroupSpacing(childBlock1, childBlock2)?.let { return it }
495518

496-
when (child2) {
519+
when (childBlock2) {
497520
is SqlColumnDefinitionRawGroupBlock ->
498521
SqlCustomSpacingBuilder()
499522
.getSpacingColumnDefinitionRaw(
500-
child2,
523+
childBlock2,
501524
)?.let { return it }
502525

503526
is SqlRightPatternBlock -> return SqlCustomSpacingBuilder().getSpacingRightPattern(
504-
child2,
527+
childBlock2,
505528
)
506529

507530
is SqlColumnBlock ->
508531
SqlCustomSpacingBuilder()
509-
.getSpacingColumnDefinition(child2)
532+
.getSpacingColumnDefinition(childBlock2)
510533
?.let { return it }
511534
}
512535

513-
if (child1 is SqlBlock && (child2 is SqlCommaBlock || child2 is SqlColumnRawGroupBlock)) {
514-
SqlCustomSpacingBuilder().getSpacingWithIndentComma(child1, child2)?.let { return it }
536+
if (childBlock1 is SqlBlock && (childBlock2 is SqlCommaBlock || childBlock2 is SqlColumnRawGroupBlock)) {
537+
SqlCustomSpacingBuilder().getSpacingWithIndentComma(childBlock1, childBlock2)?.let { return it }
515538
}
516539

517-
val spacing: Spacing? = customSpacingBuilder?.getCustomSpacing(child1, child2)
518-
return spacing ?: spacingBuilder.getSpacing(this, child1, child2)
540+
val spacing: Spacing? = customSpacingBuilder?.getCustomSpacing(childBlock1, childBlock2)
541+
return spacing ?: spacingBuilder.getSpacing(this, childBlock1, childBlock2)
519542
}
520543

521544
override fun isLeaf(): Boolean = false

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ open class SqlLiteralBlock(
2727
node,
2828
context.wrap,
2929
context.alignment,
30-
null,
3130
context.spacingBuilder,
3231
context.enableFormat,
3332
context.formatMode,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ open class SqlRightPatternBlock(
4545
node,
4646
context.wrap,
4747
context.alignment,
48-
null,
4948
context.spacingBuilder,
5049
context.enableFormat,
5150
context.formatMode,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class SqlUnknownBlock(
2727
node,
2828
context.wrap,
2929
context.alignment,
30-
null,
3130
context.spacingBuilder,
3231
context.enableFormat,
3332
context.formatMode,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class SqlWhitespaceBlock(
3434
node,
3535
wrap,
3636
alignment,
37-
null,
3837
spacingBuilder,
3938
false,
4039
FormattingMode.ADJUST_INDENT_ON_ENTER,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ abstract class SqlCommentBlock(
2929
node,
3030
context.wrap,
3131
context.alignment,
32-
null,
3332
context.spacingBuilder,
3433
context.enableFormat,
3534
context.formatMode,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class SqlElFieldAccessBlock(
6666
child,
6767
context.wrap,
6868
context.alignment,
69-
customSpacingBuilder,
7069
context.spacingBuilder,
7170
context.enableFormat,
7271
context.formatMode,

0 commit comments

Comments
 (0)