Refactor SQL Formatter Block Relations and Translate Comments #444
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the SQL formatter's handling of condition/loop directives (
/*%if*/,/*%for*/,/*%else*/,/*%end*/) by introducing a dedicated management system separate from regular group blocks. This change simplifies indent calculation logic and makes the formatter more maintainable.Problem
Previously, condition/loop directives were managed as part of the group block hierarchy, which caused several issues:
Solution
1. Separate Directive Management
conditionOrLoopBlockslist inSqlBlockBuilderto manage directives independently from group blocksgetLastConditionOrLoopBlock(): Get the most recent directivegetLastNotDependOnConditionOrLoopBlock(): Get directives without dependenciesgetNotClosedConditionOrLoopBlock(): Get unclosed directives for proper nesting2. Simplified Parent-Child Relationships
Key Changes
SqlBlockRelationBuilder.kt
setParentLoopConditionDirective()to handle directive relationships independentlysetParentNonLoopConditionDirective()to work with traditional block parentsSqlElConditionLoopCommentBlock.kt
dependsOnBlockproperty to track directive dependencies separately from parent blocksImplementation Notes
Package Reorganization
SqlConditionalExpressionGroupBlock: Relocated fromformatter/block/group/keyword/condition/toformatter/block/group/subgroup/API Changes
openConditionLoopDirectiveparameter to methods that need to check for unclosed condition/loop directive blockscreateOptionsBlock()- Now acceptsopenConditionLoopDirective: SqlElConditionLoopCommentBlock?createExistsBlock()- Internal method updated to handle directive contextBackward Compatibility
SqlConditionalExpressionGroupBlockwill need to be updatedBenefits
Testing
All existing tests pass with the refactored implementation, confirming that formatting behavior remains consistent while the internal structure is improved.
Test Coverage
NestedDirectives_format.sqlConditionalSubquery_format.sqlLoopDirective_format.sqlOrderByGroupWithConditionDirective_format.sqlMigration Notes
This is an internal refactoring with no changes to the public API or formatting output. The changes are:
Future Improvements
This refactoring lays the groundwork for: