Skip to content

Commit 6db6eff

Browse files
Merge pull request #1195 from r-lib/f1032-remove-blank-lines-after-and-before-parens
Remove blank lines after opening and before closing braces
2 parents 4256522 + f9d4ab4 commit 6db6eff

28 files changed

+291
-24
lines changed

.github/workflows/check-full.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ jobs:
2525
# use 4.1 to check with rtools40's older compiler
2626
- { os: windows-latest, r: "4.1" }
2727

28-
- { os: ubuntu-latest, r: "devel", http-user-agent: "release" }
29-
- { os: ubuntu-latest, r: "devel", locale: "en_US" }
28+
- { os: ubuntu-latest, r: "devel", locale: "en_US", http-user-agent: "release" }
3029
#- { os: ubuntu-latest, r: "release", locale: "zh_CN" }
30+
- { os: ubuntu-latest, r: "release" }
3131
- { os: ubuntu-latest, r: "oldrel-1" }
3232
- { os: ubuntu-latest, r: "oldrel-2" }
3333
- { os: ubuntu-latest, r: "oldrel-3" }

.github/workflows/pre-commit.yaml

+2-7
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ jobs:
2424
- uses: actions/checkout@v4
2525
with:
2626
fetch-depth: 0
27-
- name: Install system dependencies
28-
if: runner.os == 'Linux'
29-
run: |
30-
# your system installation code here
31-
# sudo apt-get install -y libcurl4-openssl-dev
3227
- name: Set up Python
33-
uses: actions/setup-python@v5
28+
uses: actions/setup-python@v5.1.0
3429
with:
3530
python-version: "3.9"
3631
architecture: "x64"
3732
- name: Run pre-commit
3833
uses: pre-commit/[email protected]
39-
env:
34+
env:
4035
SKIP: pkgdown
4136
- name: Commit files
4237
if: failure() && startsWith(github.ref, 'refs/heads')

R/rules-line-breaks.R

+23-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ remove_line_breaks_in_fun_dec <- function(pd) {
244244
pd
245245
}
246246

247-
#'
247+
248248
add_line_break_after_pipe <- function(pd) {
249249
is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE")
250250
pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1L] <- 1L
@@ -417,3 +417,25 @@ set_line_break_after_ggplot2_plus <- function(pd) {
417417
}
418418
pd
419419
}
420+
421+
422+
remove_empty_lines_after_opening_and_before_closing_braces <- function(pd) {
423+
opening_braces <- c("'('", "'['", "LBB")
424+
closing_braces <- c("')'", "']'")
425+
426+
paren_after <- pd$token %in% opening_braces
427+
if (any(paren_after)) {
428+
pd$lag_newlines[
429+
lag(pd$token %in% opening_braces) & pd$lag_newlines > 1L
430+
] <- 1L
431+
}
432+
433+
paren_before <- pd$token %in% closing_braces
434+
if (any(paren_before)) {
435+
pd$lag_newlines[
436+
pd$token %in% closing_braces & pd$lag_newlines > 1L
437+
] <- 1L
438+
}
439+
440+
pd
441+
}

R/rules-spaces.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ remove_space_after_unary_pm_nested <- function(pd) {
131131
}
132132

133133
remove_space_before_opening_paren <- function(pd_flat) {
134-
paren_after <- pd_flat$token %in% c("'('", "'['", "LBB")
134+
opening_braces <- c("'('", "'['", "LBB")
135+
paren_after <- pd_flat$token %in% opening_braces
135136
if (!any(paren_after)) {
136137
return(pd_flat)
137138
}
@@ -141,7 +142,8 @@ remove_space_before_opening_paren <- function(pd_flat) {
141142
}
142143

143144
remove_space_after_opening_paren <- function(pd_flat) {
144-
paren_after <- pd_flat$token %in% c("'('", "'['", "LBB")
145+
opening_braces <- c("'('", "'['", "LBB")
146+
paren_after <- pd_flat$token %in% opening_braces
145147
if (!any(paren_after)) {
146148
return(pd_flat)
147149
}
@@ -150,7 +152,8 @@ remove_space_after_opening_paren <- function(pd_flat) {
150152
}
151153

152154
remove_space_before_closing_paren <- function(pd_flat) {
153-
paren_after <- pd_flat$token %in% c("')'", "']'")
155+
closing_braces <- c("')'", "']'")
156+
paren_after <- pd_flat$token %in% closing_braces
154157
if (!any(paren_after)) {
155158
return(pd_flat)
156159
}

R/style-guides.R

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ tidyverse_style <- function(scope = "tokens",
130130

131131
line_break_manipulators <- if ("line_breaks" %in% scope) {
132132
list(
133+
remove_empty_lines_after_opening_and_before_closing_braces =
134+
remove_empty_lines_after_opening_and_before_closing_braces,
133135
set_line_break_around_comma_and_or = set_line_break_around_comma_and_or,
134136
set_line_break_after_assignment = set_line_break_after_assignment,
135137
set_line_break_before_curly_opening = set_line_break_before_curly_opening,

tests/testthat/indention_curly_brackets/multi_line_curly_only-in.R

+10
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22
{1 + 3}
33
{2 + sin(pi)}
44
}
5+
6+
{
7+
8+
# some additions
9+
{1 + 3}
10+
{2 + sin(pi)}
11+
12+
# nothing to see here
13+
14+
}

tests/testthat/indention_curly_brackets/multi_line_curly_only-out.R

+12
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@
66
2 + sin(pi)
77
}
88
}
9+
10+
{
11+
# some additions
12+
{
13+
1 + 3
14+
}
15+
{
16+
2 + sin(pi)
17+
}
18+
19+
# nothing to see here
20+
}

tests/testthat/indention_curly_brackets/multi_line_curly_while_for_if_fun-in.R

+15
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@ a <- function(x, y, z) {
88
x[i] +1
99
}
1010
}
11+
12+
13+
if (
14+
15+
require("logspline") &&
16+
require("rstanarm")
17+
18+
19+
20+
) {
21+
22+
NULL
23+
24+
25+
}

tests/testthat/indention_curly_brackets/multi_line_curly_while_for_if_fun-out.R

+8
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ a <- function(x, y, z) {
88
x[i] + 1
99
}
1010
}
11+
12+
13+
if (
14+
require("logspline") &&
15+
require("rstanarm")
16+
) {
17+
NULL
18+
}

tests/testthat/indention_round_brackets/arithmetic_start-in.R

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@
33
3 + 4
44
)
55
)
6+
7+
(
8+
9+
1 +
10+
2 + (
11+
# the space below is intentional
12+
13+
3 + 4
14+
# but the one here isn't
15+
16+
17+
)
18+
)

tests/testthat/indention_round_brackets/arithmetic_start-out.R

+10
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@
33
3 + 4
44
)
55
)
6+
7+
(
8+
1 +
9+
2 + (
10+
# the space below is intentional
11+
12+
3 + 4
13+
# but the one here isn't
14+
)
15+
)

tests/testthat/indention_round_brackets/multi_line-random-in.R

+13
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ call3(1, 2, 22),
77
),
88
144
99
)
10+
11+
call(
12+
13+
1,
14+
call2(
15+
2, 3,
16+
call3(1, 2, 22),
17+
5
18+
19+
),
20+
144
21+
22+
)

tests/testthat/indention_round_brackets/multi_line-random-out.R

+10
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ call(
77
),
88
144
99
)
10+
11+
call(
12+
1,
13+
call2(
14+
2, 3,
15+
call3(1, 2, 22),
16+
5
17+
),
18+
144
19+
)

tests/testthat/indention_square_brackets/square_brackets_double_line_break-in.R

+23
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,26 @@ a[[
2020
2
2121
] #
2222
]
23+
24+
25+
a[[
26+
27+
2
28+
29+
30+
]]
31+
32+
33+
a[[
34+
35+
# this comment shouldn't mess
36+
1, c(
37+
38+
1, 2
39+
40+
# neither should this one
41+
42+
)
43+
44+
45+
]]

tests/testthat/indention_square_brackets/square_brackets_double_line_break-out.R

+15
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ a[[
2020
2
2121
] #
2222
]
23+
24+
25+
a[[
26+
2
27+
]]
28+
29+
30+
a[[
31+
# this comment shouldn't mess
32+
1, c(
33+
1, 2
34+
35+
# neither should this one
36+
)
37+
]]

tests/testthat/indention_square_brackets/square_brackets_line_break-in.R

+41
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ fac[
1919
]
2020

2121
fac[, `:`(a = c)
22+
]
23+
24+
fac[
25+
26+
, `:`(a = c)
27+
28+
29+
2230
]
2331

2432
x[a ==3 |
@@ -44,3 +52,36 @@ x[a ==3 &&
4452

4553
x[a ==3 &
4654
b == v,]
55+
56+
x[
57+
58+
# comments above
59+
a ==3 &
60+
b == v,
61+
# or below shouldn't be an issue
62+
63+
64+
]
65+
66+
67+
x[
68+
69+
a,
70+
b
71+
72+
73+
]
74+
75+
x[
76+
77+
# this comment shouldn't be an issue
78+
1, c(
79+
80+
1, 2
81+
82+
# neither should this one
83+
84+
)
85+
86+
87+
]

tests/testthat/indention_square_brackets/square_brackets_line_break-out.R

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ fac[
2525

2626
fac[, `:`(a = c)]
2727

28+
fac[
29+
, `:`(a = c)
30+
]
31+
2832
x[a == 3 |
2933
b == v, ]
3034

@@ -48,3 +52,25 @@ x[a == 3 &&
4852

4953
x[a == 3 &
5054
b == v, ]
55+
56+
x[
57+
# comments above
58+
a == 3 &
59+
b == v,
60+
# or below shouldn't be an issue
61+
]
62+
63+
64+
x[
65+
a,
66+
b
67+
]
68+
69+
x[
70+
# this comment shouldn't be an issue
71+
1, c(
72+
1, 2
73+
74+
# neither should this one
75+
)
76+
]

tests/testthat/line_breaks_and_other/assignment-in.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ x <-
44

55
x <- 3
66

7-
# FIXME: edge case not working for R < 3.6: Problem: most likely, comment is
8-
# not moved to the right nest with relocate_eq_assign.
7+
98
x <-
109
# the culprit
1110

tests/testthat/line_breaks_and_other/assignment-out.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ x <-
44

55
x <- 3
66

7-
# FIXME: edge case not working for R < 3.6: Problem: most likely, comment is
8-
# not moved to the right nest with relocate_eq_assign.
7+
98
x <-
109
# the culprit
1110

0 commit comments

Comments
 (0)