Fix pretty printing of multi-statement closures (issue #494) #498
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.
Issue #494 is related to pretty printing of multi-statement closures, and can be reproduced on
main
by formatting the following input:The pretty printer just spits out the snippet as is. This is because the first break is made elective instead of soft. Note that the closure is formatted correctly if
a in
is removed.The fix that I propose (and have implemented) is forcing the break before the first statement to be
soft
if the closure contains multiple statements. I believe this is reasonable because there is no situation that I know of where Swift Format should format a multi-statement closure as a single-line closure ({ a + 1; a + 2 }
is pretty un-idiomatic).I have added a test for this change that fails on main and succeeds after my change.
This is my first contribution to Swift Format, so please let me know if there's anything I should change about this PR or future PRs (e.g. test style).