Skip to content

Autobrace for loops, while loops, repeat loops, and function definitions#344

Merged
DavisVaughan merged 16 commits into
feature/if-else-bracingfrom
feature/remaining-autobracing
May 29, 2025
Merged

Autobrace for loops, while loops, repeat loops, and function definitions#344
DavisVaughan merged 16 commits into
feature/if-else-bracingfrom
feature/remaining-autobracing

Conversation

@DavisVaughan

@DavisVaughan DavisVaughan commented May 14, 2025

Copy link
Copy Markdown
Collaborator

Branched from #340

Added autobracing to:

  • for loops
  • while loops
  • repeat loops
  • function definitions

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 Autobracing section 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:

map(xs, function(x) x + 1)

map(xs, function(x) {
  x + 1
})

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:

  • Most flat variant
  • Middle variant (the special form you see above)
  • Most expanded variant (fully expanded function call)

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 was unreachable!().

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:

map(xs, function(x) x + something_really_really_long_here_exceeding_line_length)

we now decide between:

# most flat
map(xs, function(x) x + something_really_really_long_here_exceeding_line_length)

# middle variant
map(xs, function(x) {
  x + something_really_really_long_here_exceeding_line_length
})

# most expanded (may or may not autobrace depending on line length)
map(
  xs,
  function(x) {
    x + something_really_really_long_here_exceeding_line_length
  }
)

this also allows the cool "code reflow" move of putting a line break here:

map(xs, function(x) <here>x + 1)

to get

map(xs, function(x) {
  x + 1
})

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):

map(xs, function(x) 
  x + y + z + this_other_long_thing
)

This will now get autobraced to

map(xs, function(x) {
  x + y + z + this_other_long_thing
})

@DavisVaughan DavisVaughan force-pushed the feature/remaining-autobracing branch from dcf415e to 257755a Compare May 15, 2025 19:29
@DavisVaughan DavisVaughan force-pushed the feature/remaining-autobracing branch from 3deaee5 to 54ed8f7 Compare May 16, 2025 13:31
Comment thread docs/formatter.qmd
@DavisVaughan DavisVaughan requested a review from lionel- May 16, 2025 14:58
Comment thread docs/formatter.qmd
Comment thread docs/formatter.qmd
Comment thread docs/formatter.qmd
@DavisVaughan DavisVaughan merged commit a26918a into feature/if-else-bracing May 29, 2025
4 checks passed
@DavisVaughan DavisVaughan deleted the feature/remaining-autobracing branch May 29, 2025 16:09
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants