Skip to content

Commit 4b2bee9

Browse files
authored
Merge pull request #440 from domaframework/fix/sql-format-comment-block
Fix: Unnecessary Space in Parser-Level Comments
2 parents ab94184 + 1a3f085 commit 4b2bee9

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
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

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,41 @@ class SqlFormatPreProcessor : PreFormatProcessor {
116116
}
117117
}
118118

119+
trimLeadingWhitespaceAtFileHead(document, source, rangeToReformat)
119120
docManager.commitDocument(document)
120121

121122
return ProcessResult(document, rangeToReformat.grown(visitor.replaces.size))
122123
}
123124

125+
private fun trimLeadingWhitespaceAtFileHead(
126+
document: Document,
127+
source: PsiFile,
128+
rangeToReformat: TextRange,
129+
) {
130+
if (rangeToReformat.startOffset != 0) return
131+
if (isInjectedSqlFile(source)) return
132+
133+
val chars = document.charsSequence
134+
if (chars.isEmpty()) return
135+
136+
var start = 0
137+
// Save Bom
138+
if (chars[0] == '\uFEFF') start = 1
139+
140+
var i = start
141+
while (i < chars.length) {
142+
val c = chars[i]
143+
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
144+
i++
145+
} else {
146+
break
147+
}
148+
}
149+
if (i > start) {
150+
document.deleteString(start, i)
151+
}
152+
}
153+
124154
private fun processNonWhiteSpaceElement(
125155
current: PsiElement,
126156
document: Document,
@@ -145,7 +175,7 @@ class SqlFormatPreProcessor : PreFormatProcessor {
145175
else -> getUpperText(current)
146176
}
147177

148-
return if (isFirstElementInFile(current)) getUpperText(current) else newKeyword
178+
return newKeyword
149179
}
150180

151181
private fun processKeyword(current: PsiElement): String {
@@ -164,11 +194,6 @@ class SqlFormatPreProcessor : PreFormatProcessor {
164194
getNewLineString(PsiTreeUtil.prevLeaf(current), getUpperText(current))
165195
}
166196

167-
private fun isFirstElementInFile(current: PsiElement): Boolean {
168-
val prev = PsiTreeUtil.prevLeaf(current)
169-
return prev == null || PsiTreeUtil.prevLeaf(prev) == null
170-
}
171-
172197
private fun removeSpacesAroundNewline(
173198
document: Document,
174199
element: PsiWhiteSpace,
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)