Skip to content

Commit 05441b6

Browse files
all arguments following a braced expression also must go on a new line.
1 parent dac46a8 commit 05441b6

File tree

7 files changed

+156
-45
lines changed

7 files changed

+156
-45
lines changed

R/rules-line-break.R

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
#' Set line break before a curly brace
22
#'
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.
3+
#' Rule:
4+
#' * Principle: Function arguments that consist of a braced expression always
5+
#' need to start on a new line
6+
#' * Exception: [...] unless it's the last argument and all other
7+
#' arguments fit on the line of the function call
8+
#' * Exception: [...] or they are named.
9+
#' * Extension: Also, expressions following on braced expressions also cause a
10+
#' line trigger.
611
#' @keywords internal
712
#' @examples
813
#' \dontrun{
14+
#' tryCatch(
15+
#' {
16+
#' f(8)
17+
#' },
18+
#' error = function(e) NULL
19+
#' )
20+
#' # last-argument case
921
#' testthat("braces braces are cool", {
1022
#' code(to = execute)
1123
#' })
24+
#' call2(
25+
#' x = 2,
26+
#' {
27+
#' code(to = execute)
28+
#' },
29+
#' c = { # this is the named case
30+
#' g(x = 7)
31+
#' }
32+
#' )
33+
#' tryGugus(
34+
#' {
35+
#' g5(k = na)
36+
#' },
37+
#' a + b # line break also here because
38+
#' # proceded by brace expression
39+
#' )
1240
#' }
1341
set_line_break_before_curly_opening <- function(pd) {
14-
line_break_to_set_idx <- which((pd$token_after == "'{'") & (pd$token != "COMMENT"))
42+
line_break_to_set_idx <- which(
43+
(pd$token_after == "'{'") & (pd$token != "COMMENT")
44+
)
45+
1546
line_break_to_set_idx <- setdiff(line_break_to_set_idx, nrow(pd))
1647
if (length(line_break_to_set_idx) > 0) {
1748
is_not_curly_curly <- map_chr(
@@ -36,10 +67,17 @@ set_line_break_before_curly_opening <- function(pd) {
3667
pd$lag_newlines[1 + should_not_be_on_same_line_idx] <- 1L
3768

3869
# 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
70+
if (length(should_not_be_on_same_line_idx) > 0) {
71+
comma_exprs_idx <- which(pd$token == "','")
72+
comma_exprs_idx <- setdiff(comma_exprs_idx, 1 + is_not_curly_curly_idx)
73+
non_comment_after_comma <- map_int(comma_exprs_idx,
74+
next_non_comment,
75+
pd = pd
76+
)
77+
non_comment_after_expr <-
78+
non_comment_after_comma[non_comment_after_comma > should_not_be_on_same_line_idx[1]]
79+
pd$lag_newlines[non_comment_after_comma] <- 1L
80+
}
4381
}
4482
pd
4583
}

man/set_line_break_before_curly_opening.Rd

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

tests/testthat/curly-curly/mixed-out.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ call("test", {
8080
call(
8181
{
8282
1
83-
}, a + b,
83+
},
84+
a + b,
8485
{
8586
33 / f(c)
8687
}

tests/testthat/line_breaks_and_other/braces-fun-calls1-out.R

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

2120
tryCatch(
2221
{
2322
exp(x)
2423
},
25-
error =
26-
function(x) x
24+
error = function(x) x
2725
)
2826

2927
call(

tests/testthat/line_breaks_and_other/braces-fun-calls2-in.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ test(
6969
s(x = sd)
7070
}
7171
)
72+
73+
tetst(
74+
"x",
75+
{
76+
x
77+
}, 1 + +1
78+
)

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

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

tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ test(
7676
s(x = sd)
7777
}
7878
)
79+
80+
tetst(
81+
"x",
82+
{
83+
x
84+
},
85+
1 + +1
86+
)

0 commit comments

Comments
 (0)