Skip to content

Commit d4abea3

Browse files
Merge pull request #318 from lorenzwalthert/named-arguments
- Named arguments and line breaks (#318).
2 parents 1631426 + 3b84c6c commit d4abea3

File tree

6 files changed

+196
-2
lines changed

6 files changed

+196
-2
lines changed

R/rules-line_break.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,27 @@ set_line_break_after_opening_if_call_is_multi_line <-
7373
if (!is_multi_line) {
7474
return(pd)
7575
}
76+
break_pos <- find_line_break_position_in_multiline_call(pd)
77+
7678
exception_pos <- c(
7779
which(pd$token %in% except_token_after),
78-
if_else(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA)
80+
if_else(pd$child[[1]]$text[1] %in% except_text_before, break_pos, NA)
7981
)
80-
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
82+
pd$lag_newlines[setdiff(break_pos, exception_pos)] <- 1L
8183
pd
84+
}
85+
86+
87+
#' Find index of the token before which the line should be broken
88+
#'
89+
#' Given a multi-line function call parse table, this function finds the
90+
#' position of the first named argument and breaks returns the index of it.
91+
#' If there is no named argument, the line is broken right after the opening
92+
#' parenthesis.
93+
#' @inheritParams set_line_break_if_call_is_multi_line
94+
find_line_break_position_in_multiline_call <- function(pd) {
95+
candidate <- (which(pd$token == "EQ_SUB") - 1L)[1]
96+
ifelse(is.na(candidate), 3L, candidate)
8297
}
8398

8499

man/find_line_break_position_in_multiline_call.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
call(3,
2+
b = 2, c
3+
)
4+
5+
gs(3, b = 2,
6+
c)
7+
8+
call(3, b = 2, c)
9+
10+
map(data, fun,
11+
x = 3, z = 33)
12+
13+
map2(dat1, data2, fun, x, y,
14+
z)
15+
16+
map2(dat1, data2, fun, x = 1, y = 2,
17+
z
18+
)

tests/testthat/line_breaks_fun_call/named_arguments-in_tree

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
call(3,
2+
b = 2, c
3+
)
4+
5+
gs(3,
6+
b = 2,
7+
c
8+
)
9+
10+
call(3, b = 2, c)
11+
12+
map(data, fun,
13+
x = 3, z = 33
14+
)
15+
16+
map2(
17+
dat1, data2, fun, x, y,
18+
z
19+
)
20+
21+
map2(dat1, data2, fun,
22+
x = 1, y = 2,
23+
z
24+
)

tests/testthat/test-line_breaks_fun_call.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ test_that("line breaks work with exceptions", {
2929
"switch_ifelse",
3030
transformer = style_text), NA)
3131
})
32+
33+
test_that("line breaks work with exceptions", {
34+
expect_warning(test_collection("line_breaks_fun_call",
35+
"named_arguments",
36+
transformer = style_text), NA)
37+
})

0 commit comments

Comments
 (0)