Skip to content

Commit 0ea6d50

Browse files
Merge pull request #569 from lorenzwalthert/issue-544
- Break line in ggplot2 calls (#544).
2 parents 460b737 + fd28e14 commit 0ea6d50

File tree

7 files changed

+426
-2
lines changed

7 files changed

+426
-2
lines changed

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# styler 1.2.0.9000
23

34
## Breaking changes
@@ -23,8 +24,10 @@
2324

2425
## Minor changes and fixes
2526

27+
* lines are now broken after `+` in `ggplot2` calls for `strict = TRUE` (#569).
28+
2629
* `style_file()` and friends now strip `./` in file paths returned invisibly,
27-
i.e. `./script.R` becomes `script.R`.
30+
i.e. `./script.R` becomes `script.R` (#568).
2831

2932
* function documentation now contains many more linebreaks due to roxygen2
3033
update to version 7.0.1 (#566).

R/rules-line-break.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,28 @@ remove_line_break_in_empty_fun_call <- function(pd) {
273273
}
274274
pd
275275
}
276+
277+
278+
set_linebreak_after_ggplot2_plus <- function(pd) {
279+
is_plus_raw <- pd$token == "'+'"
280+
if (any(is_plus_raw)) {
281+
first_plus <- which(is_plus_raw)[1]
282+
next_non_comment <- next_non_comment(pd, first_plus)
283+
is_plus_or_comment_after_plus_before_fun_call <-
284+
lag(is_plus_raw, next_non_comment - first_plus - 1, default = FALSE) &
285+
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
286+
if (any(is_plus_or_comment_after_plus_before_fun_call)) {
287+
gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1]]
288+
if (!is.null(gg_call) && gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot") {
289+
plus_without_comment_after <- setdiff(
290+
which(is_plus_raw),
291+
which(lead(pd$token == "COMMENT"))
292+
)
293+
294+
pd$lag_newlines[plus_without_comment_after + 1] <- 1L
295+
}
296+
}
297+
298+
}
299+
pd
300+
}

R/style-guides.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ tidyverse_style <- function(scope = "tokens",
137137
)
138138
},
139139
remove_line_break_in_empty_fun_call,
140-
add_line_break_after_pipe = if (strict) add_line_break_after_pipe
140+
add_line_break_after_pipe = if (strict) add_line_break_after_pipe,
141+
set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus
141142
)
142143
}
143144

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# don't remove line break
2+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
3+
geom_point()
4+
5+
6+
# add when unmasked
7+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + geom_point()
8+
9+
10+
# add when masked
11+
ggplot2::ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + geom_point()
12+
13+
# add when masked
14+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + ggplot2::geom_point()
15+
16+
# add when comment
17+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + # comment
18+
ggplot2::geom_point() + g()
19+
20+
21+
# add when comment
22+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
23+
ggplot2::geom_point() + g() # comment
24+
25+
# add when comment
26+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + ggplot2::geom_point() + g() # comment
27+
28+
29+
# add when comment
30+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
31+
ggplot2::geom_point() + g() + geom_oint() # comment

tests/testthat/line_breaks_and_other/ggplot2-in_tree

Lines changed: 319 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# don't remove line break
2+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
3+
geom_point()
4+
5+
6+
# add when unmasked
7+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
8+
geom_point()
9+
10+
11+
# add when masked
12+
ggplot2::ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
13+
geom_point()
14+
15+
# add when masked
16+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
17+
ggplot2::geom_point()
18+
19+
# add when comment
20+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + # comment
21+
ggplot2::geom_point() +
22+
g()
23+
24+
25+
# add when comment
26+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
27+
ggplot2::geom_point() +
28+
g() # comment
29+
30+
# add when comment
31+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
32+
ggplot2::geom_point() +
33+
g() # comment
34+
35+
36+
# add when comment
37+
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
38+
ggplot2::geom_point() +
39+
g() +
40+
geom_oint() # comment

tests/testthat/test-line_breaks_and_other.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ test_that("line break before comma is removed and placed after comma ", {
3636
expect_warning(test_collection("line_breaks_and_other", "pipe-line",
3737
transformer = style_text), NA)
3838
})
39+
40+
test_that("line break added for ggplot2 call", {
41+
expect_warning(test_collection("line_breaks_and_other", "ggplot2",
42+
transformer = style_text), NA)
43+
})

0 commit comments

Comments
 (0)