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

Break line in ggplot2 calls #569

Merged
merged 4 commits into from
Dec 18, 2019
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
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# styler 1.2.0.9000

## Breaking changes
Expand All @@ -23,8 +24,10 @@

## Minor changes and fixes

* lines are now broken after `+` in `ggplot2` calls for `strict = TRUE` (#569).

* `style_file()` and friends now strip `./` in file paths returned invisibly,
i.e. `./script.R` becomes `script.R`.
i.e. `./script.R` becomes `script.R` (#568).

* function documentation now contains many more linebreaks due to roxygen2
update to version 7.0.1 (#566).
Expand Down
25 changes: 25 additions & 0 deletions R/rules-line-break.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,28 @@ remove_line_break_in_empty_fun_call <- function(pd) {
}
pd
}


set_linebreak_after_ggplot2_plus <- function(pd) {
is_plus_raw <- pd$token == "'+'"
if (any(is_plus_raw)) {
first_plus <- which(is_plus_raw)[1]
next_non_comment <- next_non_comment(pd, first_plus)
is_plus_or_comment_after_plus_before_fun_call <-
lag(is_plus_raw, next_non_comment - first_plus - 1, default = FALSE) &
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
if (any(is_plus_or_comment_after_plus_before_fun_call)) {
gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1]]
if (!is.null(gg_call) && gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot") {
plus_without_comment_after <- setdiff(
which(is_plus_raw),
which(lead(pd$token == "COMMENT"))
)

pd$lag_newlines[plus_without_comment_after + 1] <- 1L
}
}

}
pd
}
3 changes: 2 additions & 1 deletion R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ tidyverse_style <- function(scope = "tokens",
)
},
remove_line_break_in_empty_fun_call,
add_line_break_after_pipe = if (strict) add_line_break_after_pipe
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
31 changes: 31 additions & 0 deletions tests/testthat/line_breaks_and_other/ggplot2-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# don't remove line break
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
geom_point()


# add when unmasked
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + geom_point()


# add when masked
ggplot2::ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + geom_point()

# add when masked
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + ggplot2::geom_point()

# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + # comment
ggplot2::geom_point() + g()


# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() + g() # comment

# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + ggplot2::geom_point() + g() # comment


# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() + g() + geom_oint() # comment
319 changes: 319 additions & 0 deletions tests/testthat/line_breaks_and_other/ggplot2-in_tree

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions tests/testthat/line_breaks_and_other/ggplot2-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# don't remove line break
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
geom_point()


# add when unmasked
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
geom_point()


# add when masked
ggplot2::ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
geom_point()

# add when masked
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point()

# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + # comment
ggplot2::geom_point() +
g()


# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() +
g() # comment

# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() +
g() # comment


# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() +
g() +
geom_oint() # comment
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 @@ -36,3 +36,8 @@ 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)
})

test_that("line break added for ggplot2 call", {
expect_warning(test_collection("line_breaks_and_other", "ggplot2",
transformer = style_text), NA)
})