Skip to content

Commit 377d573

Browse files
Merge branch 'main' into patch-4
2 parents 854edfe + fe6ae64 commit 377d573

File tree

3 files changed

+108
-14
lines changed

3 files changed

+108
-14
lines changed

R/rules-line-breaks.R

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,49 @@ style_line_break_around_curly <- function(strict, pd) {
207207
#' @keywords internal
208208
#' @seealso style_text_without_curly_curly
209209
set_line_break_around_curly_curly <- function(pd) {
210-
if (is_curly_expr(pd)) {
211-
# none after {
212-
opening_before <- (pd$token == "'{'") &
213-
(pd$token_before == "'{'" | pd$token_after == "'{'")
214-
215-
# none before }
216-
closing_before <- (pd$token == "'}'") &
217-
(pd$token_after == "'}'" | pd$token_before == "'}'")
218-
if (any(opening_before) && any(closing_before)) {
219-
pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT"
210+
if (!is_curly_expr(pd)) {
211+
return(pd)
212+
}
213+
214+
pd %>%
215+
set_line_break_around_outer_curly_curly() %>%
216+
set_line_break_around_inner_curly_curly()
217+
}
218+
219+
set_line_break_around_outer_curly_curly <- function(pd) {
220+
# none after {
221+
opening_before <- (pd$token == "'{'") & (pd$token_after == "'{'")
222+
# none before }
223+
closing_before <- (pd$token == "'}'") & (pd$token_before == "'}'")
224+
225+
if (any(opening_before) && any(closing_before)) {
226+
pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT"
227+
pd$lag_newlines[pos_opening_idx] <- 0L
228+
if (any(pos_opening_idx)) {
229+
# if line is broken with opening `{`, also break it with closing
230+
pd$lag_newlines[closing_before & pd$token_before != "COMMENT"] <- 0L
231+
}
232+
}
233+
234+
pd
235+
}
236+
237+
set_line_break_around_inner_curly_curly <- function(pd) {
238+
# none before {
239+
opening_before <- (pd$token == "'{'") & (pd$token_before == "'{'")
240+
# none after }
241+
closing_before <- (pd$token == "'}'") & (pd$token_after == "'}'")
242+
243+
if (any(opening_before) && any(closing_before)) {
244+
pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT"
245+
can_remove_line_break_closing <- closing_before & pd$token_before != "COMMENT"
246+
if (any(pos_opening_idx) && any(can_remove_line_break_closing)) {
220247
pd$lag_newlines[pos_opening_idx] <- 0L
221-
if (any(pos_opening_idx)) {
222-
# if line is broken with opening `{`, also break it with closing
223-
pd$lag_newlines[closing_before & pd$token_after != "COMMENT"] <- 0L
224-
}
248+
# if line is broken with opening `{`, also break it with closing
249+
pd$lag_newlines[can_remove_line_break_closing] <- 0L
225250
}
226251
}
252+
227253
pd
228254
}
229255

tests/testthat/curly-curly/mixed-in.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,43 @@ call({{
112112
{{
113113
#
114114
}}
115+
116+
117+
call({
118+
{
119+
1
120+
}
121+
})
122+
123+
call({
124+
{
125+
1 #
126+
}
127+
})
128+
129+
call({
130+
{ #
131+
1
132+
}
133+
})
134+
135+
call({
136+
{
137+
1
138+
} #
139+
})
140+
141+
142+
call({
143+
{
144+
1
145+
}
146+
} #
147+
)
148+
149+
call({ #
150+
{
151+
1
152+
}
153+
}
154+
)

tests/testthat/curly-curly/mixed-out.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,31 @@ call({{
111111
{{
112112
#
113113
}}
114+
115+
116+
call({{ 1 }})
117+
118+
call({{
119+
1 #
120+
}})
121+
122+
call({{ #
123+
1
124+
}})
125+
126+
call({
127+
{
128+
1
129+
} #
130+
})
131+
132+
133+
call(
134+
{{ 1 }} #
135+
)
136+
137+
call({ #
138+
{
139+
1
140+
}
141+
})

0 commit comments

Comments
 (0)