Skip to content

Commit

Permalink
Merge pull request #629 from lorenzwalthert/issue-627
Browse files Browse the repository at this point in the history
- No blank line in function calls for strict = TRUE (#629).
  • Loading branch information
lorenzwalthert committed Apr 11, 2020
2 parents 9a42a40 + 720981e commit 1d299f2
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 8 deletions.
17 changes: 13 additions & 4 deletions R/rules-line-break.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ set_line_break_before_curly_opening <- function(pd) {
}


set_line_break_around_comma <- function(pd) {
set_line_break_around_comma <- function(pd, strict) {
comma_with_line_break_that_can_be_removed_before <-
(pd$token == "','") &
(pd$lag_newlines > 0) &
(pd$token_before != "COMMENT") &
(lag(pd$token) != "'['")

pd$lag_newlines[comma_with_line_break_that_can_be_removed_before] <- 0L
pd$lag_newlines[lag(comma_with_line_break_that_can_be_removed_before)] <- 1L
pd
Expand Down Expand Up @@ -267,9 +268,17 @@ set_line_break_before_closing_call <- function(pd, except_token_before) {

#' @rdname set_line_break_if_call_is_multi_line
#' @keywords internal
remove_line_break_in_empty_fun_call <- function(pd) {
if (is_function_call(pd) && nrow(pd) == 3) {
pd$lag_newlines[3] <- 0L
remove_line_break_in_fun_call <- function(pd, strict) {
if (is_function_call(pd)) {
# no blank lines within function calls
if (strict) {
pd$lag_newlines[lag(pd$token == "','") & pd$lag_newlines > 1] <- 1L

pd$lag_newlines[lag(pd$token == "COMMENT") & pd$lag_newlines > 0] <- 1L
}
if (nrow(pd) == 3) {
pd$lag_newlines[3] <- 0L
}
}
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 @@ -139,7 +139,7 @@ tidyverse_style <- function(scope = "tokens",
except_token_before = "COMMENT"
)
},
remove_line_break_in_empty_fun_call,
purrr::partial(remove_line_break_in_fun_call, strict = strict),
add_line_break_after_pipe = if (strict) add_line_break_after_pipe,
set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus
)
Expand Down
4 changes: 2 additions & 2 deletions man/set_line_break_if_call_is_multi_line.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/alignment/named-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ call(
# algorithm: aligned. human: aligned.
call(
x = 1, n = 33, z = "333",

xy = 2,
)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-non-strict-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
call(


1
)

call(
# comment

1
)

call(
x = 2,
1,

"w"
)
31 changes: 31 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-non-strict-in_tree

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

18 changes: 18 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-non-strict-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
call(


1
)

call(
# comment

1
)

call(
x = 2,
1,

"w"
)
18 changes: 18 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-strict-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
call(


1
)

call(
# comment

1
)

call(
x = 2,
1,

"w"
)
31 changes: 31 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-strict-in_tree

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

14 changes: 14 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-strict-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
call(
1
)

call(
# comment
1
)

call(
x = 2,
1,
"w"
)
13 changes: 13 additions & 0 deletions tests/testthat/test-line_breaks_fun_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ test_that("line breaks work in general", {
), NA)
})

test_that("blank lines in function calls are removed for strict = TRUE", {
expect_warning(test_collection("line_breaks_fun_call",
"blank-strict",
transformer = style_text
), NA)

expect_warning(test_collection("line_breaks_fun_call",
"blank-non-strict",
transformer = style_text, strict = FALSE
), NA)
})


test_that("line breaks are not applied with non-strict", {
expect_warning(test_collection("line_breaks_fun_call",
"token_dependent_complex_non_strict",
Expand Down

0 comments on commit 1d299f2

Please sign in to comment.