Skip to content

Commit 3cf05ea

Browse files
set line break between %>% and { to 1
1 parent 2e64b07 commit 3cf05ea

File tree

5 files changed

+916
-428
lines changed

5 files changed

+916
-428
lines changed

R/rules-line-breaks.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@
3737
#' a + b # line break also here because
3838
#' # preceded by brace expression
3939
#' )
40+
#'
41+
#' # brace expressions go on new line if part of a pipe, in function call...
42+
#' c(
43+
#' data %>%
44+
#' filter(bar) %>% {
45+
#' cor(.$col1, .$col2, use = "complete.obs")
46+
#' }
47+
#' )
48+
#' # ... or outside
49+
#' data %>%
50+
#' filter(bar) %>%
51+
#' {
52+
#' cor(.$col1, .$col2, use = "complete.obs")
53+
#' }
4054
#' }
4155
set_line_break_before_curly_opening <- function(pd) {
4256
line_break_to_set_idx <- which(
@@ -54,21 +68,25 @@ set_line_break_before_curly_opening <- function(pd) {
5468
# rule not applicable for IF
5569
TRUE, (line_break_to_set_idx + 1L) == last_expr_idx
5670
)
57-
eq_sub_before <- pd$token[line_break_to_set_idx] == "EQ_SUB"
71+
72+
no_line_break_before_curly_idx <- pd$token[line_break_to_set_idx] %in% "EQ_SUB"
5873
linebreak_before_curly <- ifelse(is_function_call(pd),
74+
# if in function call and has pipe, it is not recognized as function call and
75+
# goes to else case
5976
any(pd$lag_newlines[seq2(1, line_break_to_set_idx[1])] > 0),
60-
FALSE
77+
# if not a function call, only break line if it is a pipe followed by {}
78+
pd$token[line_break_to_set_idx] %in% c("SPECIAL-PIPE", "PIPE")
6179
)
6280
# no line break before last brace expression and named brace expression to
6381
should_be_on_same_line <- is_not_curly_curly &
64-
((is_last_expr & !linebreak_before_curly) | eq_sub_before)
82+
((is_last_expr & !linebreak_before_curly) | no_line_break_before_curly_idx)
6583
is_not_curly_curly_idx <- line_break_to_set_idx[should_be_on_same_line]
6684
pd$lag_newlines[1 + is_not_curly_curly_idx] <- 0L
6785

6886

6987
# other cases: line breaks
7088
should_not_be_on_same_line <- is_not_curly_curly &
71-
((!is_last_expr | linebreak_before_curly) & !eq_sub_before)
89+
((!is_last_expr | linebreak_before_curly) & !no_line_break_before_curly_idx)
7290
should_not_be_on_same_line_idx <- line_break_to_set_idx[should_not_be_on_same_line]
7391

7492
pd$lag_newlines[1 + should_not_be_on_same_line_idx] <- 1L

man/set_line_break_before_curly_opening.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,68 @@ b %>%
8989
f() %>% # never move comment to next line as it can be styler: off or nolint
9090
k() %>%
9191
x()
92+
93+
94+
# line break before { inserted inside and outside function calls
95+
c(
96+
data %>%
97+
filter(bar) %>% {
98+
cor(.$col1, .$col2, use = "complete.obs")
99+
}
100+
)
101+
102+
data %>%
103+
filter(bar) %>% {
104+
cor(.$col1, .$col2, use = "complete.obs")
105+
}
106+
107+
# line break before { kept inside and outside function calls
108+
c(
109+
data %>%
110+
filter(bar) %>%
111+
{
112+
cor(.$col1, .$col2, use = "complete.obs")
113+
}
114+
)
115+
116+
data %>%
117+
filter(bar) %>%
118+
{
119+
cor(.$col1, .$col2, use = "complete.obs")
120+
}
121+
122+
# redundant blank lines removed
123+
c(
124+
data %>%
125+
filter(bar) %>%
126+
127+
{
128+
cor(.$col1, .$col2, use = "complete.obs")
129+
}
130+
)
131+
132+
data %>%
133+
filter(bar) %>%
134+
135+
{
136+
cor(.$col1, .$col2, use = "complete.obs")
137+
}
138+
139+
# blank lines kept when around comment
140+
c(
141+
data %>%
142+
filter(bar) %>%
143+
# comment
144+
145+
{
146+
cor(.$col1, .$col2, use = "complete.obs")
147+
}
148+
)
149+
150+
data %>%
151+
filter(bar) %>%
152+
# comment
153+
154+
{
155+
cor(.$col1, .$col2, use = "complete.obs")
156+
}

0 commit comments

Comments
 (0)