Skip to content

Commit dac46a8

Browse files
extend rules for non-brace expressions
1 parent 67bd978 commit dac46a8

File tree

8 files changed

+398
-5
lines changed

8 files changed

+398
-5
lines changed

R/rules-line-break.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
# A { should never go on its own line
2-
remove_line_break_before_curly_opening <- function(pd) {
1+
#' Set line break before a curly brace
2+
#'
3+
#' Rule: Function arguments that consist of a braced expression always need to
4+
#' start on a new line, unless it's the last argument and all other arguments
5+
#' fit on the line of the function call or they are named.
6+
#' @keywords internal
7+
#' @examples
8+
#' \dontrun{
9+
#' testthat("braces braces are cool", {
10+
#' code(to = execute)
11+
#' })
12+
#' }
13+
set_line_break_before_curly_opening <- function(pd) {
314
line_break_to_set_idx <- which((pd$token_after == "'{'") & (pd$token != "COMMENT"))
415
line_break_to_set_idx <- setdiff(line_break_to_set_idx, nrow(pd))
516
if (length(line_break_to_set_idx) > 0) {
@@ -13,18 +24,27 @@ remove_line_break_before_curly_opening <- function(pd) {
1324
TRUE, (line_break_to_set_idx + 1L) == last_expr_idx
1425
)
1526
eq_sub_before <- pd$token[line_break_to_set_idx] == "EQ_SUB"
27+
# no line break before last brace expression and named brace expression to
1628
should_be_on_same_line <- is_not_curly_curly & (is_last_expr | eq_sub_before)
1729
is_not_curly_curly_idx <- line_break_to_set_idx[should_be_on_same_line]
1830
pd$lag_newlines[1 + is_not_curly_curly_idx] <- 0L
1931

32+
# other cases: line breaks
2033
should_not_be_on_same_line <- is_not_curly_curly & (!is_last_expr & !eq_sub_before)
2134
should_not_be_on_same_line_idx <- line_break_to_set_idx[should_not_be_on_same_line]
2235

2336
pd$lag_newlines[1 + should_not_be_on_same_line_idx] <- 1L
37+
38+
# non-curly expressions after curly expressions must have line breaks
39+
exprs_idx <- which(pd$token == "expr")
40+
exprs_after_last_expr_with_line_break_idx <-
41+
exprs_idx[exprs_idx > should_not_be_on_same_line_idx[1] + 1L]
42+
pd$lag_newlines[exprs_after_last_expr_with_line_break_idx] <- 1L
2443
}
2544
pd
2645
}
2746

47+
2848
set_line_break_around_comma <- function(pd) {
2949
comma_with_line_break_that_can_be_removed_before <-
3050
(pd$token == "','") &

R/style-guides.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ tidyverse_style <- function(scope = "tokens",
114114
line_break_manipulators <- if (scope >= "line_breaks") {
115115
lst(
116116
set_line_break_around_comma,
117-
remove_line_break_before_curly_opening,
117+
set_line_break_before_curly_opening,
118118
remove_line_break_before_round_closing_after_curly =
119119
if (strict) remove_line_break_before_round_closing_after_curly,
120120
remove_line_break_before_round_closing_fun_dec =

tests/testthat/line_breaks_and_other/braces-fun-calls-out.R renamed to tests/testthat/line_breaks_and_other/braces-fun-calls1-out.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ tryCatch(
1414
{
1515
exp(x)
1616
},
17-
error = function(x) x
17+
error =
18+
function(x) x
1819
)
1920

2021
tryCatch(
2122
{
2223
exp(x)
2324
},
24-
error = function(x) x
25+
error =
26+
function(x) x
2527
)
2628

2729
call(
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
test(
2+
"x",
3+
{
4+
5+
}, a + b, {
6+
s(x = sd)
7+
}
8+
)
9+
10+
test(
11+
"x", {
12+
13+
}, a + b, {
14+
s(x = sd)
15+
}
16+
)
17+
18+
test(
19+
"x",
20+
{
21+
22+
},
23+
a + b, {
24+
s(x = sd)
25+
}
26+
)
27+
28+
29+
test(
30+
"x",
31+
{
32+
33+
},
34+
a + b,
35+
{
36+
s(x = sd)
37+
}
38+
)
39+
40+
test(
41+
"x",
42+
{
43+
44+
}, # h
45+
a + b, {
46+
s(x = sd)
47+
}
48+
)
49+
50+
test(
51+
"x",
52+
{
53+
54+
}, # h
55+
a + b,
56+
# k
57+
{
58+
s(x = sd)
59+
}
60+
)
61+
62+
test(
63+
"x",
64+
{
65+
66+
},
67+
a + b, # k
68+
{
69+
s(x = sd)
70+
}
71+
)

tests/testthat/line_breaks_and_other/braces-fun-calls2-in_tree

Lines changed: 222 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)