@@ -67,21 +67,28 @@ open class SqlBlock(
6767 open val childBlocks = mutableListOf<SqlBlock >()
6868 open var prevBlocks = emptyList<SqlBlock >()
6969
70+ companion object {
71+ private const val DEFAULT_INDENT_SIZE = 4
72+ private const val DEFAULT_TEXT_LENGTH_INCREMENT = 1
73+ private val EXCLUDED_FROM_TEXT_LENGTH = setOf (SqlTypes .DOT , SqlTypes .RIGHT_PAREN )
74+ private val SPACING_ONE = Spacing .createSpacing(1 , 1 , 0 , true , 0 )
75+ private val SPACING_ZERO = Spacing .createSpacing(0 , 0 , 0 , true , 0 )
76+ private val SPACING_ONE_NO_KEEP = Spacing .createSpacing(1 , 1 , 0 , false , 0 )
77+ }
78+
7079 fun getChildrenTextLen (): Int = childBlocks.sumOf { child -> calculateChildTextLength(child) }
7180
7281 private fun calculateChildTextLength (child : SqlBlock ): Int {
7382 val nonCommentChildren = child.childBlocks.filterNot { it is SqlDefaultCommentBlock }
7483
75- if (nonCommentChildren.isNotEmpty()) {
76- return child.getChildrenTextLen() + child.getNodeText().length
77- }
78- if (isExcludedFromTextLength(child)) {
79- return 0
84+ return when {
85+ nonCommentChildren.isNotEmpty() -> child.getChildrenTextLen() + child.getNodeText().length
86+ isExcludedFromTextLength(child) -> 0
87+ else -> child.getNodeText().length + DEFAULT_TEXT_LENGTH_INCREMENT
8088 }
81- return child.getNodeText().length + 1
8289 }
8390
84- private fun isExcludedFromTextLength (block : SqlBlock ): Boolean = block.node.elementType in setOf ( SqlTypes . DOT , SqlTypes . RIGHT_PAREN )
91+ private fun isExcludedFromTextLength (block : SqlBlock ): Boolean = block.node.elementType in EXCLUDED_FROM_TEXT_LENGTH
8592
8693 /* *
8794 * Checks if a conditional loop directive is registered before the parent block.
@@ -185,10 +192,8 @@ open class SqlBlock(
185192 open fun isSaveSpace (lastGroup : SqlBlock ? ): Boolean =
186193 when (lastGroup) {
187194 is SqlNewGroupBlock -> shouldSaveSpaceForNewGroup(lastGroup)
188- else -> {
189- shouldSaveSpaceForConditionLoop()
190- }
191- } == true
195+ else -> shouldSaveSpaceForConditionLoop()
196+ }
192197
193198 private fun shouldSaveSpaceForConditionLoop (): Boolean =
194199 isConditionLoopDirectiveRegisteredBeforeParent() ||
@@ -203,15 +208,22 @@ open class SqlBlock(
203208 return false
204209 }
205210
206- return isFollowedByConditionLoop() || isPrecededByConditionLoop (parent)
211+ return hasConditionLoopAround (parent)
207212 }
208213
209214 private fun isNonBreakingKeywordCombination (
210215 parent : SqlNewGroupBlock ,
211216 prevWord : SqlBlock ? ,
212- ): Boolean =
213- SqlKeywordUtil .isSetLineKeyword(getNodeText(), parent.getNodeText()) ||
214- SqlKeywordUtil .isSetLineKeyword(getNodeText(), prevWord?.getNodeText() ? : " " )
217+ ): Boolean {
218+ val currentText = getNodeText()
219+ val parentText = parent.getNodeText()
220+ val prevText = prevWord?.getNodeText() ? : " "
221+
222+ return SqlKeywordUtil .isSetLineKeyword(currentText, parentText) ||
223+ SqlKeywordUtil .isSetLineKeyword(currentText, prevText)
224+ }
225+
226+ private fun hasConditionLoopAround (parent : SqlNewGroupBlock ): Boolean = isFollowedByConditionLoop() || isPrecededByConditionLoop(parent)
215227
216228 private fun isFollowedByConditionLoop (): Boolean = childBlocks.lastOrNull() is SqlElConditionLoopCommentBlock
217229
@@ -271,158 +283,67 @@ open class SqlBlock(
271283 /* *
272284 * Creates a spacing builder specifically for directive block comments.
273285 */
274- protected open fun createBlockDirectiveCommentSpacingBuilder (): SqlCustomSpacingBuilder =
275- SqlCustomSpacingBuilder ()
276- .withSpacing(
277- SqlTypes .BLOCK_COMMENT_START ,
278- SqlTypes .EL_ID_EXPR ,
279- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
280- ).withSpacing(
281- SqlTypes .BLOCK_COMMENT_START ,
282- SqlTypes .EL_PRIMARY_EXPR ,
283- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
284- ).withSpacing(
285- SqlTypes .BLOCK_COMMENT_START ,
286- SqlTypes .EL_STRING ,
287- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
288- ).withSpacing(
289- SqlTypes .BLOCK_COMMENT_START ,
290- SqlTypes .EL_NUMBER ,
291- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
292- ).withSpacing(
293- SqlTypes .BLOCK_COMMENT_START ,
294- SqlTypes .BOOLEAN ,
295- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
296- ).withSpacing(
297- SqlTypes .BLOCK_COMMENT_START ,
298- SqlTypes .EL_NULL ,
299- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
300- ).withSpacing(
301- SqlTypes .BLOCK_COMMENT_START ,
302- SqlTypes .EL_FIELD_ACCESS_EXPR ,
303- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
304- ).withSpacing(
305- SqlTypes .BLOCK_COMMENT_START ,
306- SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR ,
307- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
308- ).withSpacing(
309- SqlTypes .BLOCK_COMMENT_START ,
310- SqlTypes .HASH ,
311- Spacing .createSpacing(0 , 0 , 0 , true , 0 ),
312- ).withSpacing(
313- SqlTypes .HASH ,
314- SqlTypes .EL_ID_EXPR ,
315- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
316- ).withSpacing(
317- SqlTypes .HASH ,
318- SqlTypes .EL_PRIMARY_EXPR ,
319- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
320- ).withSpacing(
321- SqlTypes .HASH ,
322- SqlTypes .EL_STRING ,
323- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
324- ).withSpacing(
325- SqlTypes .HASH ,
326- SqlTypes .EL_NUMBER ,
327- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
328- ).withSpacing(
329- SqlTypes .HASH ,
330- SqlTypes .BOOLEAN ,
331- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
332- ).withSpacing(
333- SqlTypes .HASH ,
334- SqlTypes .EL_NULL ,
335- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
336- ).withSpacing(
337- SqlTypes .HASH ,
338- SqlTypes .EL_FIELD_ACCESS_EXPR ,
339- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
340- ).withSpacing(
341- SqlTypes .HASH ,
342- SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR ,
343- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
344- ).withSpacing(
345- SqlTypes .BLOCK_COMMENT_START ,
346- SqlTypes .CARET ,
347- Spacing .createSpacing(0 , 0 , 0 , true , 0 ),
348- ).withSpacing(
349- SqlTypes .CARET ,
286+ protected open fun createBlockDirectiveCommentSpacingBuilder (): SqlCustomSpacingBuilder {
287+ val builder = SqlCustomSpacingBuilder ()
288+
289+ // Types that need spacing after BLOCK_COMMENT_START
290+ val typesNeedingSpaceAfterStart =
291+ listOf (
350292 SqlTypes .EL_ID_EXPR ,
351- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
352- ).withSpacing(
353- SqlTypes .CARET ,
354293 SqlTypes .EL_PRIMARY_EXPR ,
355- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
356- ).withSpacing(
357- SqlTypes .CARET ,
358294 SqlTypes .EL_STRING ,
359- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
360- ).withSpacing(
361- SqlTypes .CARET ,
362295 SqlTypes .EL_NUMBER ,
363- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
364- ).withSpacing(
365- SqlTypes .CARET ,
366296 SqlTypes .BOOLEAN ,
367- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
368- ).withSpacing(
369- SqlTypes .CARET ,
370297 SqlTypes .EL_NULL ,
371- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
372- ).withSpacing(
373- SqlTypes .CARET ,
374298 SqlTypes .EL_FIELD_ACCESS_EXPR ,
375- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
376- ).withSpacing(
377- SqlTypes .CARET ,
378299 SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR ,
379- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
380- ).withSpacing(
381- SqlTypes .BLOCK_COMMENT_CONTENT ,
382- SqlTypes .BLOCK_COMMENT_END ,
383- Spacing .createSpacing(0 , 0 , 0 , true , 0 ),
384- ).withSpacing(
385- SqlTypes .EL_FIELD_ACCESS_EXPR ,
386- SqlTypes .OTHER ,
387- Spacing .createSpacing(1 , 1 , 0 , false , 0 ),
388- ).withSpacing(
389- SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR ,
390- SqlTypes .OTHER ,
391- Spacing .createSpacing(1 , 1 , 0 , false , 0 ),
392- ).withSpacing(
300+ )
301+
302+ // Types that need spacing before BLOCK_COMMENT_END
303+ val typesNeedingSpaceBeforeEnd =
304+ listOf (
393305 SqlTypes .EL_ID_EXPR ,
394- SqlTypes .BLOCK_COMMENT_END ,
395- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
396- ).withSpacing(
397306 SqlTypes .EL_PRIMARY_EXPR ,
398- SqlTypes .BLOCK_COMMENT_END ,
399- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
400- ).withSpacing(
401307 SqlTypes .STRING ,
402- SqlTypes .BLOCK_COMMENT_END ,
403- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
404- ).withSpacing(
405308 SqlTypes .EL_NUMBER ,
406- SqlTypes .BLOCK_COMMENT_END ,
407- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
408- ).withSpacing(
409309 SqlTypes .EL_NULL ,
410- SqlTypes .BLOCK_COMMENT_END ,
411- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
412- ).withSpacing(
413310 SqlTypes .BOOLEAN ,
414- SqlTypes .BLOCK_COMMENT_END ,
415- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
416- ).withSpacing(
417311 SqlTypes .EL_FIELD_ACCESS_EXPR ,
418- SqlTypes .BLOCK_COMMENT_END ,
419- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
420- ).withSpacing(
421312 SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR ,
422- SqlTypes .BLOCK_COMMENT_END ,
423- Spacing .createSpacing(1 , 1 , 0 , true , 0 ),
424313 )
425314
315+ // Add spacing rules for BLOCK_COMMENT_START
316+ typesNeedingSpaceAfterStart.forEach { type ->
317+ builder.withSpacing(SqlTypes .BLOCK_COMMENT_START , type, SPACING_ONE )
318+ }
319+
320+ // Special cases for BLOCK_COMMENT_START
321+ builder.withSpacing(SqlTypes .BLOCK_COMMENT_START , SqlTypes .HASH , SPACING_ZERO )
322+ builder.withSpacing(SqlTypes .BLOCK_COMMENT_START , SqlTypes .CARET , SPACING_ZERO )
323+
324+ // Add spacing rules for HASH
325+ typesNeedingSpaceAfterStart.forEach { type ->
326+ builder.withSpacing(SqlTypes .HASH , type, SPACING_ONE )
327+ }
328+
329+ // Add spacing rules for CARET
330+ typesNeedingSpaceAfterStart.forEach { type ->
331+ builder.withSpacing(SqlTypes .CARET , type, SPACING_ONE )
332+ }
333+
334+ // Special spacing rules
335+ builder.withSpacing(SqlTypes .BLOCK_COMMENT_CONTENT , SqlTypes .BLOCK_COMMENT_END , SPACING_ZERO )
336+ builder.withSpacing(SqlTypes .EL_FIELD_ACCESS_EXPR , SqlTypes .OTHER , SPACING_ONE_NO_KEEP )
337+ builder.withSpacing(SqlTypes .EL_STATIC_FIELD_ACCESS_EXPR , SqlTypes .OTHER , SPACING_ONE_NO_KEEP )
338+
339+ // Add spacing rules before BLOCK_COMMENT_END
340+ typesNeedingSpaceBeforeEnd.forEach { type ->
341+ builder.withSpacing(type, SqlTypes .BLOCK_COMMENT_END , SPACING_ONE )
342+ }
343+
344+ return builder
345+ }
346+
426347 /* *
427348 * Returns the child indentation for the block.
428349 *
@@ -435,10 +356,6 @@ open class SqlBlock(
435356 Indent .getSpaceIndent(0 )
436357 }
437358
438- companion object {
439- private const val DEFAULT_INDENT_SIZE = 4
440- }
441-
442359 /* *
443360 * Determines whether the block is a leaf node.
444361 *
0 commit comments