Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No blank line in function calls for strict = TRUE #629

Merged
merged 4 commits into from
Apr 11, 2020
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
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# styler 1.3.2.9000 (Development)

## Major changes


## Minor chnages and fixes

- overhaul pgkdown site: Add search (#623), group function in Reference (#625).
- always strip trailing spaces and make cache insensitive to it (#626).
- typos in documentation (#618, #614).



# styler 1.3.2

Release upon request by the CRAN team.
Expand Down
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
1 change: 1 addition & 0 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ make_transformer <- function(transformers,
assert_transformers(transformers)

function(text) {
text <- trimws(text, which = "right")
should_use_cache <- cache_is_activated()

if (should_use_cache) {
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
4 changes: 2 additions & 2 deletions tests/testthat/test-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ test_that("gives warning markers are not correct", {
)))
})

test_that("trailing spaces are stripped when checking marker, but not written back", {
test_that("trailing spaces are stripped when checking marker and written back", {
expect_equal(
style_text(c(
"# styler: off ",
"1+1",
"# styler: on "
)) %>%
as.character(),
c("# styler: off ", "1+1", "# styler: on")
c("# styler: off", "1+1", "# styler: on")
)
})

Expand Down