fix(editor): keep nested indentation for set operations in subqueries and CTEs (#1698)#1710
Merged
Merged
Conversation
7e6ba04 to
0c9ffeb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1698.
Problem
The SQL formatter mangled a
UNION(orINTERSECT/EXCEPT) inside a derived table or CTE: it collapsed the nested indentation, put blank lines around the set-operation keyword, and appended the closing)directly to the last innerSELECT(...current) AS final).Root cause
indentwas a bare counter, andhandleSetOperationassumed set operations are always top-level: it didindent = 0andclauseStack.removeAll()on everyUNION/INTERSECT/EXCEPT. Inside a subquery that destroyed the.subqueryframe, so the nextSELECTformatted at indent 0 and the closing)fell throughhandleCloseParen's block branch into the bare-append fallback.Fix
Refactor to a nesting-derived model rather than patch the one symptom:
indentis now a computed property equal to the number of open block frames (subquery / CREATE TABLE body). Every manualindent += 1 / -= 1 / = 0is gone, so indentation can't be clobbered by clause handling.handleSetOperationpops only the current SELECT's clause frames back to the enclosing block, keeps the block frame, emits the keyword at the real nesting indent, and uses blank-line separation only at the top level (single newlines when nested).selectColumnIndentStack) is cleared on;, fixing a stack imbalance.MINUSroutes through the same path.For the reported query the formatter now indents the derived table one level, aligns both
UNIONblocks, and puts) AS finalon its own line.Tests
13 new cases in
SQLFormatterServiceTests: UNION / UNION ALL / INTERSECT / EXCEPT in a derived table, UNION in a CTE, UNION in a nested subquery, chained top-level unions, top-level INTERSECT, INSERT ... SELECT, window-frameROWS BETWEENstays inline, and two idempotency checks. The full suite (49 tests, including every pre-existing case:union,subqueryInFrom,cte,createTable,windowFunction) passes with no regressions.Notes
subqueryInFrom/ctetests, not the 4-space style in the issue's "expected" block.swiftlint --strictclean on the formatter source.ON CONFLICT/ON DUPLICATE KEY UPDATEbodies, and MSSQL[bracket]identifiers (conflicts with Postgres array subscripts).