@@ -65,23 +65,21 @@ open class SqlBlock(
6565 open val childBlocks = mutableListOf<SqlBlock >()
6666 open var prevBlocks = emptyList<SqlBlock >()
6767
68- fun getChildrenTextLen (): Int =
69- childBlocks.sumOf { child ->
70- val children =
71- child.childBlocks.filter { it !is SqlDefaultCommentBlock }
72- if (children.isNotEmpty()) {
73- child
74- .getChildrenTextLen()
75- .plus(child.getNodeText().length)
76- } else if (child.node.elementType == SqlTypes .DOT ||
77- child.node.elementType == SqlTypes .RIGHT_PAREN
78- ) {
79- // Since elements do not include surrounding spaces, they should be excluded from the character count.
80- 0
81- } else {
82- child.getNodeText().length.plus(1 )
83- }
68+ fun getChildrenTextLen (): Int = childBlocks.sumOf { child -> calculateChildTextLength(child) }
69+
70+ private fun calculateChildTextLength (child : SqlBlock ): Int {
71+ val nonCommentChildren = child.childBlocks.filterNot { it is SqlDefaultCommentBlock }
72+
73+ if (nonCommentChildren.isNotEmpty()) {
74+ return child.getChildrenTextLen() + child.getNodeText().length
8475 }
76+ if (isExcludedFromTextLength(child)) {
77+ return 0
78+ }
79+ return child.getNodeText().length + 1
80+ }
81+
82+ private fun isExcludedFromTextLength (block : SqlBlock ): Boolean = block.node.elementType in setOf (SqlTypes .DOT , SqlTypes .RIGHT_PAREN )
8583
8684 fun getChildBlocksDropLast (
8785 dropIndex : Int = 1,
@@ -122,34 +120,48 @@ open class SqlBlock(
122120
123121 fun isEnableFormat (): Boolean = enableFormat
124122
125- open fun isSaveSpace (lastGroup : SqlBlock ? ): Boolean {
123+ open fun isSaveSpace (lastGroup : SqlBlock ? ): Boolean =
126124 parentBlock?.let { parent ->
127- if (parent is SqlElConditionLoopCommentBlock ) {
128- val prevBlock =
129- prevBlocks.lastOrNull { it !is SqlDefaultCommentBlock }
130- return prevBlock is SqlElConditionLoopCommentBlock &&
131- (prevBlock.conditionType.isElse() || prevBlock.conditionType.isEnd()) ||
132- parent.childBlocks.dropLast(1 ).isEmpty()
133- }
134- // Checks for non-breaking keyword combinations, ignoring comment blocks
135- if (parent is SqlNewGroupBlock ) {
136- val prevWord = prevBlocks.lastOrNull { it !is SqlCommentBlock }
137- if (SqlKeywordUtil .isSetLineKeyword(getNodeText(), parent.getNodeText()) ||
138- SqlKeywordUtil .isSetLineKeyword(getNodeText(), prevWord?.getNodeText() ? : " " )
139- ) {
140- return false
141- }
142- // Breaks a line if it is a child of itself or preceded by a condition/loop directive
143- return childBlocks.lastOrNull() is SqlElConditionLoopCommentBlock ||
144- (
145- prevBlocks.lastOrNull() is SqlElConditionLoopCommentBlock &&
146- prevBlocks
147- .last()
148- .node.psi.startOffset > parent.node.psi.startOffset
149- )
125+ when (parent) {
126+ is SqlElConditionLoopCommentBlock -> shouldSaveSpaceForConditionLoop(parent)
127+ is SqlNewGroupBlock -> shouldSaveSpaceForNewGroup(parent)
128+ else -> false
150129 }
130+ } == true
131+
132+ private fun shouldSaveSpaceForConditionLoop (parent : SqlElConditionLoopCommentBlock ): Boolean {
133+ val prevBlock = prevBlocks.lastOrNull { it !is SqlDefaultCommentBlock }
134+ val isPrevBlockElseOrEnd =
135+ prevBlock is SqlElConditionLoopCommentBlock &&
136+ (prevBlock.conditionType.isElse() || prevBlock.conditionType.isEnd())
137+ val hasNoChildrenExceptLast = parent.childBlocks.dropLast(1 ).isEmpty()
138+
139+ return isPrevBlockElseOrEnd || hasNoChildrenExceptLast
140+ }
141+
142+ private fun shouldSaveSpaceForNewGroup (parent : SqlNewGroupBlock ): Boolean {
143+ val prevWord = prevBlocks.lastOrNull { it !is SqlCommentBlock }
144+
145+ if (isNonBreakingKeywordCombination(parent, prevWord)) {
146+ return false
151147 }
152- return false
148+
149+ return isFollowedByConditionLoop() || isPrecededByConditionLoop(parent)
150+ }
151+
152+ private fun isNonBreakingKeywordCombination (
153+ parent : SqlNewGroupBlock ,
154+ prevWord : SqlBlock ? ,
155+ ): Boolean =
156+ SqlKeywordUtil .isSetLineKeyword(getNodeText(), parent.getNodeText()) ||
157+ SqlKeywordUtil .isSetLineKeyword(getNodeText(), prevWord?.getNodeText() ? : " " )
158+
159+ private fun isFollowedByConditionLoop (): Boolean = childBlocks.lastOrNull() is SqlElConditionLoopCommentBlock
160+
161+ private fun isPrecededByConditionLoop (parent : SqlNewGroupBlock ): Boolean {
162+ val lastPrevBlock = prevBlocks.lastOrNull()
163+ return lastPrevBlock is SqlElConditionLoopCommentBlock &&
164+ lastPrevBlock.node.psi.startOffset > parent.node.psi.startOffset
153165 }
154166
155167 /* *
@@ -193,11 +205,15 @@ open class SqlBlock(
193205 */
194206 override fun getChildIndent (): Indent ? =
195207 if (isEnableFormat()) {
196- Indent .getSpaceIndent(4 )
208+ Indent .getSpaceIndent(DEFAULT_INDENT_SIZE )
197209 } else {
198210 Indent .getSpaceIndent(0 )
199211 }
200212
213+ companion object {
214+ private const val DEFAULT_INDENT_SIZE = 4
215+ }
216+
201217 /* *
202218 * Determines whether the block is a leaf node.
203219 */
0 commit comments