Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions R/rules-line-break.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ remove_line_break_before_round_closing_fun_dec <- function(pd) {

#' @importFrom rlang seq2
add_line_break_after_pipe <- function(pd) {
is_special <- pd$token == c("SPECIAL-PIPE") & pd$token_after != "COMMENT"
if (any(pd$lag_newlines != 0L)) {
pd$lag_newlines[lag(is_special)] <- 1L
is_pipe <- pd$token == c("SPECIAL-PIPE") & pd$token_after != "COMMENT"
if (sum(is_pipe) > 1 &&
!(next_terminal(pd, vars = "token_before")$token_before %in% c("'('", "EQ_SUB", "','"))) {
pd$lag_newlines[lag(is_pipe)] <- 1L
}
pd
}
Expand Down
2 changes: 1 addition & 1 deletion R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ tidyverse_style <- function(scope = "tokens",
)
},
remove_line_break_in_empty_fun_call,
add_line_break_after_pipe
add_line_break_after_pipe = if (strict) add_line_break_after_pipe
)
}

Expand Down
8 changes: 2 additions & 6 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,10 @@ parse_transform_serialize_r <- function(text, transformers, warn_empty = TRUE) {
#' @importFrom purrr flatten
#' @keywords internal
apply_transformers <- function(pd_nested, transformers) {
transformed_line_breaks <- pre_visit(
pd_nested,
c(transformers$initialize, transformers$line_break)
)

transformed_updated_multi_line <- post_visit(
transformed_line_breaks,
c(set_multi_line, update_newlines)
pd_nested,
c(transformers$initialize, transformers$line_break, set_multi_line, update_newlines)
)

transformed_all <- pre_visit(
Expand Down
85 changes: 85 additions & 0 deletions tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
c(a %>% b)

c(a %>% b())

c(a + b %>% c)

c(
a %>% b)

c(a %>% b()
)

c(a %>% b() # 33
)

c(
a + b %>% c
)

c(
a + b %>%
c)

c(a + b %>%
c)

c(
a + b %>% # 654
c
)

c( # rr
a + b %>%
c
)

c(
a +
b %>% c
)

c(a +
b %>% c
)

a %>% b(
)

a %>% b(
) %>% q

a %>%
b()

a %>% b() %>% c

# short pipes < 2 can stay on one line
a %>% b()

fun(x,
a %>% b)

fun(x,
gg = a %>% b,
tt %>% q)

fun(x, gg = a %>% b, tt %>% q)

z = a %>% b()

fun( s = g(x),
gg = a(n == 2) %>% b,
tt %>% q(r = 3))

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x %>%

c(), y = 2)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x %>%
c())


{a %>% c +1}
402 changes: 402 additions & 0 deletions tests/testthat/line_breaks_and_other/pipe-line-breaks-in_tree

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
c(a %>% b())

c(a %>% b())

c(a + b %>% c())

c(
a %>% b()
)

c(a %>% b())

c(
a %>% b() # 33
)

c(
a + b %>% c()
)

c(
a + b %>%
c()
)

c(a + b %>%
c())

c(
a + b %>% # 654
c()
)

c( # rr
a + b %>%
c()
)

c(
a +
b %>% c()
)

c(a +
b %>% c())

a %>% b()

a %>%
b() %>%
q()

a %>%
b()

a %>%
b() %>%
c()

# short pipes < 2 can stay on one line
a %>% b()

fun(
x,
a %>% b()
)

fun(x,
gg = a %>% b(),
tt %>% q()
)

fun(x, gg = a %>% b(), tt %>% q())

z <- a %>% b()

fun(
s = g(x),
gg = a(n == 2) %>% b(),
tt %>% q(r = 3)
)

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x %>%

c(), y = 2)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x %>%
c())


{
a %>% c() + 1
}
5 changes: 5 additions & 0 deletions tests/testthat/test-line_breaks_and_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ test_that("line break before comma is removed and placed after comma ", {
expect_warning(test_collection("line_breaks_and_other", "comma",
transformer = style_text), NA)
})

test_that("line break before comma is removed and placed after comma ", {
expect_warning(test_collection("line_breaks_and_other", "pipe-line",
transformer = style_text), NA)
})
6 changes: 0 additions & 6 deletions tests/testthat/token_adding_removing/mixed_token-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ a; b ;c;d
'text with "quotes"'


# no linebreak after special if they fit in a line
a %>% b() %>%c()

# linebreak after special
a %>%
b() %>%c()

# adding brackets in pipes
a %>%
Expand Down
66 changes: 17 additions & 49 deletions tests/testthat/token_adding_removing/mixed_token-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions tests/testthat/token_adding_removing/mixed_token-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ d
'text with "quotes"'


# no linebreak after special if they fit in a line
a %>% b() %>% c()

# linebreak after special
a %>%
b() %>%
c()

# adding brackets in pipes
a %>%
Expand Down