Autobrace for loops, while loops, repeat loops, and function definitions#344
Merged
DavisVaughan merged 16 commits intoMay 29, 2025
Merged
Conversation
dcf415e to
257755a
Compare
…ping
Allowing you to put a line break here `map(xs, function(x)<here>x + 1)`, which then causes autobracing to kick in, and now gives you the "middle variant" that you probably want of
```
map(xs, function(x) {
x + 1
})
```
3deaee5 to
54ed8f7
Compare
DavisVaughan
commented
May 16, 2025
lionel-
approved these changes
May 29, 2025
lionel-
approved these changes
May 29, 2025
DavisVaughan
added a commit
that referenced
this pull request
May 29, 2025
* First draft of if statement autobracing * Correctly recurse into `consequence` when handling non-enclosed comments * Handle nested if statements in `consequence` * Add failing test * Don't pass pre-computed `braced_expression` value through to nested ifs in `consequence` They should get to compute their own value, because the user could have validly written a one liner if statement inside a braced if to begin with, and we want to be consistent with that. * Add one more test seen in data.table * More test cases * Another test case * Another test * Remove handling of comments not enclosed by the if statement This causes too many issues with idempotence and verbatim handling, even though it would be quite nice * Prefer generating leading comments * Fix a typo buglet * Only allow one liner if statements in specific contexts * Add symmetrical support for function parameters, to mirror call arguments * Refine end of line comments before `else` * Remove context awareness from one liner computation It seems like it is actually hard to get this right, as there are many other places where one liners feel "pretty good" and expanding them out causes more churn than it is worth. * Add additional tests from trying it on dplyr * Implement `SyntaxPosition` for `Value` vs `Effect` positioning And use in if statement handling as a big improvement on context awareness! * Tweak outdated comments * Autobrace for loops, while loops, repeat loops, and function definitions (#344) * Add autobracing for `for_statement` * Add autobracing for `repeat_statement` * Add autobracing for `while_statement` * Add autobracing for `function_definition` * In `for_statement`, push onto the `body` again * In `while_statement`, push onto the `body` again * In `repeat_statement`, push onto the `body` again * Add documentation on autobracing * CHANGELOG bullet * Mention synergy with persistent line breaks * Declare that any `RFunctionDefinition` can benefit from argument grouping Allowing you to put a line break here `map(xs, function(x)<here>x + 1)`, which then causes autobracing to kick in, and now gives you the "middle variant" that you probably want of ``` map(xs, function(x) { x + 1 }) ``` * Rework if statement section with value vs effect position * Tweak CHANGELOG * Portability is for multiline if statements * Consistency with if statements * Make effect/value comparison clearer * Tweak a comment
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.
Branched from #340
Added autobracing to:
These are significantly less complicated than if/else handling because we don't have to worry about nesting (like
if (cond) 1 else if (cond) 2 else 3).We unconditionally autobrace the body of all loop types. As discussed, we don't see a use case for 1 line loops that would be more clear without braces.
Function definitions are allowed on one line. Braces will be added if any part of the function definition spans multiple lines (the parameters or the body).
The comment handling of all of these types needed a bit of an overhaul, so there's a decent amount of churn there. We more comprehensively handle all possible comment positions now, and I've added a slew of new comment related tests to ensure we don't break anything in the future.
I've added the CHANGELOG bullet for this feature here, along with a new
Autobracingsection in the documentation.Addition of "most flat" variant
The most subtle but also most impactful change here is that I've had to touch
call_arguments.rs.Consider the following two map() calls:
Previously, we would only consider a function definition eligible for the "best fitting" algorithm if its body already had
{. So only the second would be considered as a candidate for "best fitting". The first would have chosen between a call with no breaks at all, or a call that is completely expanded.Best fitting in theory chooses between 3 layouts:
Because we required
{, this actually immediately ruled out the most flat variant. I've always had the intention of adding it back in when we needed it, but up until now it was in a branch that wasunreachable!().We now need to consider the most flat variant, because we've changed the function definition rule from "only do best fitting if it has
{in the body" to "always try best fitting". That means for something like this:we now decide between:
this also allows the cool "code reflow" move of putting a line break here:
to get
Even before autobracing, I think it was actually a mistake that we required
{as a prerequisite for trying the best fitting algorithm. Like, this doesn't have{and probably should have gone through the best fitting algorithm so it could select the middle variant (which is the form you see here):This will now get autobraced to