Skip to content

Commit 5868c21

Browse files
Merge pull request #149 from lorenzwalthert/no_reindention_calls
Don't reindent function calls and break line correctly for multi-line calls (#149).
2 parents db6d4b1 + ae7c748 commit 5868c21

23 files changed

+263
-124
lines changed

R/rules-line_break.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,51 @@ remove_line_break_before_round_closing <- function(pd) {
4242
pd
4343
}
4444

45+
#' @importFrom rlang seq2
4546
add_line_break_after_pipe <- function(pd) {
4647
is_special <- pd$token %in% c("SPECIAL-PIPE")
4748
pd$lag_newlines[lag(is_special)] <- 1L
4849
pd
4950
}
51+
52+
53+
#' Set line break for multi-line function calls
54+
#' @param pd A parse table.
55+
#' @param except_token A character vector with tokens before "'('" that do not
56+
#' @name set_line_break_if_call_is_multi_line
57+
#' @importFrom rlang seq2
58+
NULL
59+
60+
#' @describeIn set_line_break_if_call_is_multi_line Sets line break after
61+
#' opening parenthesis.
62+
set_line_break_after_opening_if_call_is_multi_line <-
63+
function(pd,
64+
except_token = NULL) {
65+
if (!is_function_call(pd)) return(pd)
66+
npd <- nrow(pd)
67+
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
68+
if (!is_multi_line) return(pd)
69+
exception_pos <- which(pd$token %in% except_token)
70+
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
71+
pd
72+
}
73+
74+
75+
#' @describeIn set_line_break_if_call_is_multi_line Sets line break before
76+
#' closing parenthesis.
77+
set_line_break_before_closing_if_call_is_multi_line <- function(pd) {
78+
if (!is_function_call(pd)) return(pd)
79+
npd <- nrow(pd)
80+
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
81+
if (!is_multi_line) return(pd)
82+
pd$lag_newlines[npd] <- 1L
83+
pd
84+
}
85+
86+
#' @rdname set_line_break_if_call_is_multi_line
87+
remove_line_break_in_empty_fun_call <- function(pd) {
88+
if (is_function_call(pd) && nrow(pd) == 3) {
89+
pd$lag_newlines[3] <- 0L
90+
}
91+
pd
92+
}

R/rules-spacing.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ remove_space_around_colons <- function(pd_flat) {
215215
}
216216

217217
#' Set space between EQ_SUB and "','"
218+
#' @param pd A parse table.
218219
set_space_between_eq_sub_and_comma <- function(pd) {
219220
op_before <- which(pd$token == "EQ_SUB" & lead(pd$token == "','"))
220221
pd$spaces[op_before] <- 1L

R/style_guides.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ tidyverse_style <- function(scope = "tokens",
7373
add_line_break_afer_curly_opening,
7474
if (strict) set_line_break_before_curly_closing else
7575
add_line_break_before_curly_closing,
76+
partial(
77+
set_line_break_after_opening_if_call_is_multi_line,
78+
except_token = "COMMENT"
79+
),
80+
set_line_break_before_closing_if_call_is_multi_line,
81+
remove_line_break_in_empty_fun_call,
7682
add_line_break_after_pipe
7783
)
7884

@@ -87,7 +93,7 @@ tidyverse_style <- function(scope = "tokens",
8793
indention_modifier <-
8894
c(
8995
update_indention_ref_fun_dec,
90-
update_indention_ref_fun_call
96+
NULL
9197
)
9298

9399
create_style_guide(

man/set_line_break_if_call_is_multi_line.Rd

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

man/set_space_between_eq_sub_and_comma.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/indention_curly_brackets/multi_line_curly_round_spacing-in.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
b<-function(x ){
22
x <- c(1,
33
2+ 3,
4-
sin(pi) ) # FIXME add tidyverse-comliant rule to break after '('
4+
sin(pi) )
55

66
if(x > 10){
77
return("done")

tests/testthat/indention_curly_brackets/multi_line_curly_round_spacing-in_tree

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

tests/testthat/indention_curly_brackets/multi_line_curly_round_spacing-out.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
b <- function(x) {
2-
x <- c(1,
3-
2 + 3,
4-
sin(pi)) # FIXME add tidyverse-comliant rule to break after '('
2+
x <- c(
3+
1,
4+
2 + 3,
5+
sin(pi)
6+
)
57

68
if (x > 10) {
79
return("done")

tests/testthat/indention_multiple/curly_and_round-out.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ test_that("this is a text", {
66
{
77
call(
88
12, 1 + 1,
9-
26)
9+
26
10+
)
1011
}
1112
}))
1213

tests/testthat/indention_multiple/edge_mixed-out.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
(({
44
{
55
{
6-
c(99,
6+
c(
7+
99,
78
1 + 1, {
89
"within that suff"
9-
})
10+
}
11+
)
1012
}
1113
}
1214
}))

0 commit comments

Comments
 (0)