Skip to content

Commit cca6dc5

Browse files
committed
Improve SQL block comment formatting and handling for parser level comments
1 parent df06d2d commit cca6dc5

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import com.intellij.formatting.Spacing
2424
import com.intellij.formatting.SpacingBuilder
2525
import com.intellij.formatting.Wrap
2626
import com.intellij.lang.ASTNode
27+
import com.intellij.psi.PsiElement
2728
import com.intellij.psi.PsiWhiteSpace
2829
import com.intellij.psi.formatter.common.AbstractBlock
30+
import com.intellij.psi.util.PsiTreeUtil
31+
import com.intellij.psi.util.elementType
2932
import org.domaframework.doma.intellij.common.util.TypeUtil
3033
import org.domaframework.doma.intellij.formatter.block.comma.SqlCommaBlock
3134
import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock
@@ -262,15 +265,23 @@ open class SqlFileBlock(
262265
child,
263266
createBlockDirectiveCommentSpacingBuilder(),
264267
)
265-
if (tempBlock !is SqlElConditionLoopCommentBlock) {
266-
if (lastGroup is SqlWithQueryGroupBlock || lastGroupFilteredDirective is SqlWithQueryGroupBlock) {
267-
return SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
268+
val commentType = PsiTreeUtil.getChildrenOfType(child.psi, PsiElement::class.java)?.get(1)?.elementType
269+
val defaultBlockComment =
270+
commentType == SqlTypes.EL_PARSER_LEVEL_COMMENT || commentType == SqlTypes.BLOCK_COMMENT_CONTENT ||
271+
child.elementType == SqlTypes.LINE_COMMENT
272+
if (!defaultBlockComment) {
273+
if (tempBlock !is SqlElConditionLoopCommentBlock) {
274+
if (lastGroup is SqlWithQueryGroupBlock || lastGroupFilteredDirective is SqlWithQueryGroupBlock) {
275+
return SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
276+
}
277+
}
278+
return if (lastGroup is SqlWithCommonTableGroupBlock) {
279+
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
280+
} else {
281+
tempBlock
268282
}
269-
}
270-
return if (lastGroup is SqlWithCommonTableGroupBlock) {
271-
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
272283
} else {
273-
tempBlock
284+
return tempBlock
274285
}
275286
}
276287

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/*%! This is Parser Level Comment*/
1+
2+
/*%! This is Parser Level Comment*/
23
select * from table1
34
/*%!This is Parser Level Comment*/
45
where column1 = /*# value1*/
56
/*+
67
* This is Block Comment
78
*/
8-
and column2 = /* value2 */ 'value2' or column3 = /*^ value3 */ 'value3'
9+
and column2 = /* value2 */ 'value2' /** line break */ or column3 = /*^ value3 */ 'value3'

src/test/testData/sql/formatter/BlockCommentParse_format.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ SELECT *
77
* This is Block Comment
88
*/
99
AND column2 = /* value2 */'value2'
10+
/** line break */
1011
OR column3 = /*^ value3 */'value3'

0 commit comments

Comments
 (0)