Skip to content

Commit f74502f

Browse files
Merge pull request #1022 from r-lib/issue-1004
Leave comments on line after `{`
2 parents ff5c8ab + 4a847f5 commit f74502f

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

R/rules-line-breaks.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ style_line_break_around_curly <- function(strict, pd) {
156156
opening_before <- (pd$token == "'{'")
157157
to_break <- lag(opening_before, default = FALSE) | closing_before
158158
len_to_break <- sum(to_break)
159-
pd$lag_newlines[to_break] <- ifelse(rep(strict, len_to_break),
160-
1L,
161-
pmax(1L, pd$lag_newlines[to_break])
159+
pd$lag_newlines[to_break] <- ifelse(
160+
pd$token[to_break] == "COMMENT",
161+
pmin(1L, pd$lag_newlines[to_break]),
162+
if (strict) 1L else pmax(1L, pd$lag_newlines[to_break])
162163
)
163164
} else {
164165
is_else <- pd$token == "ELSE"

tests/testthat/indention_multiple/overall-out.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ a <- function(x) {
88
22 + 1
99
))
1010
if (x > 10) {
11-
for (x in 22) {
12-
# FIXME in operator only to be surrounded by one space. What about %in%?
11+
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
1312
prin(x)
1413
}
1514
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
X_and_F_symbol_linter <- function() { # nolint: object_name.
2+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
X_and_F_symbol_linter <- function() { # nolint: object_name.
2+
}

tests/testthat/test-cache-high-level-api.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ test_that("no speedup when tranformer changes", {
137137
first <- system.time(style_text(text, transformers = t1))
138138
t1 <- tidyverse_style(indent_by = 4)
139139
second <- system.time(style_text(text, transformers = t1))
140-
expect_false(first["elapsed"] / 1.3 > second["elapsed"])
140+
expect_true(first["elapsed"] < 1.3 * second["elapsed"])
141141
})
142142

143143

tests/testthat/test-line_breaks_and_other.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ test_that("line is correctly broken around = ", {
6868
transformer = style_text
6969
), NA)
7070
})
71+
72+
test_that("comments are not moved down after {", {
73+
expect_warning(test_collection("line_breaks_and_other", "comment-around-curly",
74+
transformer = style_text
75+
), NA)
76+
})

0 commit comments

Comments
 (0)