Skip to content

Commit fa9d9f4

Browse files
Merge pull request #1235 from r-lib/f-single-indent
Adapt to single indent semantics in style guide
2 parents d50fc86 + ff785ea commit fa9d9f4

File tree

8 files changed

+80
-66
lines changed

8 files changed

+80
-66
lines changed

R/rules-indention.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,38 @@ indent_braces <- function(pd, indent_by) {
1515
#'
1616
#' Necessary for consistent indention of the function declaration header.
1717
#' @param pd A parse table.
18-
#' @inheritParams is_double_indent_function_declaration
18+
#' @inheritParams is_single_indent_function_declaration
1919
#' @seealso set_unindention_child update_indention_ref_fun_dec
2020
#' @keywords internal
2121
unindent_fun_dec <- function(pd, indent_by = 2L) {
2222
if (is_function_declaration(pd)) {
2323
idx_closing_brace <- which(pd$token == "')'")
2424
fun_dec_head <- seq2(2L, idx_closing_brace)
25-
if (is_double_indent_function_declaration(pd, indent_by = indent_by)) {
26-
pd$indent[fun_dec_head] <- 2L * indent_by
25+
if (is_single_indent_function_declaration(pd, indent_by = indent_by)) {
26+
pd$indent[fun_dec_head] <- indent_by
27+
pd$indent[idx_closing_brace] <- 0L
2728
} else {
2829
pd$indent[fun_dec_head] <- 0L
2930
}
3031
}
3132
pd
3233
}
3334

34-
#' Is the function declaration double indented?
35+
#' Is the function declaration single indented?
3536
#'
3637
#' Assumes you already checked if it's a function with
37-
#' `is_function_declaration`. It is double indented if the first token
38+
#' `is_function_declaration`. It is single indented if the first token
3839
#' after the first line break that is a `"SYMBOL_FORMALS"`.
3940
#' @param pd A parse table.
4041
#' @inheritParams tidyverse_style
4142
#' @keywords internal
42-
is_double_indent_function_declaration <- function(pd, indent_by = 2L) {
43+
is_single_indent_function_declaration <- function(pd, indent_by = 2L) {
4344
head_pd <- vec_slice(pd, -nrow(pd))
4445
line_break_in_header <- which(head_pd$lag_newlines > 0L & head_pd$token == "SYMBOL_FORMALS")
4546
if (length(line_break_in_header) > 0L) {
4647
# indent results from applying the rules, spaces is the initial spaces
4748
# (which is indention if a newline is ahead)
49+
# The 2L factor is kept to convert double indent to single indent
4850
pd$spaces[line_break_in_header[1L] - 1L] <= 2L * indent_by
4951
} else {
5052
FALSE
@@ -132,7 +134,7 @@ NULL
132134
#'
133135
#' @keywords internal
134136
update_indention_ref_fun_dec <- function(pd_nested) {
135-
if (is_function_declaration(pd_nested) && !is_double_indent_function_declaration(pd_nested)) {
137+
if (is_function_declaration(pd_nested) && !is_single_indent_function_declaration(pd_nested)) {
136138
seq <- seq2(3L, nrow(pd_nested) - 2L)
137139
pd_nested$indention_ref_pos_id[seq] <- pd_nested$pos_id[2L]
138140
}

R/rules-line-breaks.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,17 @@ remove_line_break_before_round_closing_after_curly <- function(pd) {
236236

237237
remove_line_breaks_in_fun_dec <- function(pd) {
238238
if (is_function_declaration(pd)) {
239-
is_double_indention <- is_double_indent_function_declaration(pd)
239+
is_single_indention <- is_single_indent_function_declaration(pd)
240240
round_after <- (
241241
pd$token == "')'" | pd$token_before == "'('"
242242
) &
243243
pd$token_before != "COMMENT"
244244
pd$lag_newlines[pd$lag_newlines > 1L] <- 1L
245-
pd$lag_newlines[round_after] <- 0L
246-
if (is_double_indention) {
245+
if (is_single_indention) {
247246
pd$lag_newlines[lag(pd$token == "'('")] <- 1L
247+
pd$lag_newlines[round_after] <- 1L
248+
} else {
249+
pd$lag_newlines[round_after] <- 0L
248250
}
249251
}
250252
pd

man/is_double_indent_function_declaration.Rd renamed to man/is_single_indent_function_declaration.Rd

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

tests/testthat/fun_dec/line_break_fun_dec-out.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ a <- function(x,
4040
}
4141

4242
a <- function(
43-
#
44-
x,
45-
y) {
43+
#
44+
x,
45+
y
46+
) {
4647
x - 1
4748
}

tests/testthat/indention_operators/eq_formals_complex_indention-out.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
function(a =
2-
33,
3-
b
4-
) {}
2+
33,
3+
b
4+
) {}
55

66
function(a =
7-
33,
8-
b) {}
7+
33,
8+
b) {}
99

1010
function(a,
11-
b,
12-
c
13-
) {}
11+
b,
12+
c
13+
) {}
1414

1515
function(a,
16-
b,
17-
c) {}
16+
b,
17+
c) {}
1818

1919
function(ss,
20-
a =
21-
3,
22-
er =
23-
4
24-
) {}
20+
a =
21+
3,
22+
er =
23+
4
24+
) {}
2525

2626
function(a =
2727
b,

tests/testthat/indention_operators/eq_formals_complex_tokens-out.R

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
function(
2-
a =
3-
33,
4-
b) {}
2+
a =
3+
33,
4+
b
5+
) {}
56

67
function(
7-
a =
8-
33,
9-
b) {}
8+
a =
9+
33,
10+
b
11+
) {}
1012

1113
function(
12-
a,
13-
b,
14-
c) {}
14+
a,
15+
b,
16+
c
17+
) {}
1518

1619
function(
17-
a,
18-
b,
19-
c) {}
20+
a,
21+
b,
22+
c
23+
) {}
2024

2125
function(
22-
ss,
23-
a =
24-
3,
25-
er =
26-
4) {}
26+
ss,
27+
a =
28+
3,
29+
er =
30+
4
31+
) {}
2732

2833
function(a =
2934
b,

tests/testthat/indention_operators/eq_sub_complex_indention-out.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ b
1010
# multiple nested levels
1111
{
1212
v <- function(x =
13-
122,
14-
y) {
13+
122,
14+
y) {
1515
}
1616
}
1717

tests/testthat/unindention/mixed-double-out.R

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,33 @@ function(x,
3737

3838
# double
3939
function(
40-
x,
41-
y) {
40+
x,
41+
y
42+
) {
4243
1
4344
}
4445

4546

4647
function(
47-
x,
48-
y,
49-
k) {
48+
x,
49+
y,
50+
k
51+
) {
5052
1
5153
}
5254

5355

5456
function(
55-
x,
56-
y) {
57+
x,
58+
y
59+
) {
5760
1
5861
}
5962

6063

6164
function(
62-
x, y) {
65+
x, y
66+
) {
6367
1
6468
}
6569

@@ -72,23 +76,23 @@ function(x,
7276

7377
# last brace
7478
function(
75-
x, y) {
79+
x, y) {
7680
NULL
7781
}
7882

7983
function(
80-
x, y) {
84+
x, y) {
8185
NULL
8286
}
8387

8488
function(
85-
x,
86-
y) {
89+
x,
90+
y) {
8791
NULL
8892
}
8993

9094
function(
91-
x,
92-
y) {
95+
x,
96+
y) {
9397
NULL
9498
}

0 commit comments

Comments
 (0)