Skip to content

Commit 2ef782d

Browse files
authored
Implement Autobracing (#340)
* 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
1 parent c2f9b54 commit 2ef782d

28 files changed

+3931
-534
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@
22

33
# Development version
44

5+
- Autobracing is a new feature applied to if statements, for loops, while loops,
6+
repeat loops, and function definitions. This feature will automatically add
7+
`{}` around the body of these code elements in certain cases to maximize
8+
readability, consistency, and portability (#225, #334).
9+
10+
For example:
11+
12+
```r
13+
if (condition)
14+
a
15+
16+
# Becomes:
17+
if (condition) {
18+
a
19+
}
20+
```
21+
22+
```r
23+
fn <- function(
24+
a, b
25+
) a + b
26+
27+
# Becomes:
28+
fn <- function(
29+
a,
30+
b
31+
) {
32+
a + b
33+
}
34+
```
35+
36+
Single line if statements and function definitions are still allowed in certain contexts:
37+
38+
```r
39+
list(a = if (is.null(x)) NA else x)
40+
41+
map(xs, function(x) x + 1)
42+
```
43+
544

645
# 0.6.0
746

0 commit comments

Comments
 (0)