Skip to content

Commit 1b2573e

Browse files
authored
Merge pull request #152 from lorenzwalthert/no_reindention_calls
- No line break after `switch()` and friends (#152).
2 parents 54e9b24 + 319a165 commit 1b2573e

18 files changed

+141
-25
lines changed

R/rules-line_break.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ add_line_break_after_pipe <- function(pd) {
5252

5353
#' Set line break for multi-line function calls
5454
#' @param pd A parse table.
55-
#' @param except_token A character vector with tokens before "'('" that do not
55+
#' @param except_token_after A character vector with tokens after "'('" that do
56+
#' not cause a line break after "'('".
57+
#' @param except_text_before A character vector with texts before "'('" that do
58+
#' not cause a line break after "'('".
5659
#' @name set_line_break_if_call_is_multi_line
5760
#' @importFrom rlang seq2
5861
NULL
@@ -61,12 +64,17 @@ NULL
6164
#' opening parenthesis.
6265
set_line_break_after_opening_if_call_is_multi_line <-
6366
function(pd,
64-
except_token = NULL) {
67+
except_token_after = NULL,
68+
except_text_before = NULL) {
6569
if (!is_function_call(pd)) return(pd)
6670
npd <- nrow(pd)
6771
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
6872
if (!is_multi_line) return(pd)
69-
exception_pos <- which(pd$token %in% except_token)
73+
exception_pos <- c(
74+
which(pd$token %in% except_token_after),
75+
ifelse(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA)
76+
)
77+
pd$lag_newlines[3] <- 0L
7078
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
7179
pd
7280
}

R/style_guides.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ tidyverse_style <- function(scope = "tokens",
7575
add_line_break_before_curly_closing,
7676
partial(
7777
set_line_break_after_opening_if_call_is_multi_line,
78-
except_token = "COMMENT"
78+
except_token_after = "COMMENT",
79+
except_text_before = c("switch", "ifelse", "if_else")
7980
),
8081
set_line_break_before_closing_if_call_is_multi_line,
8182
remove_line_break_in_empty_fun_call,

man/set_line_break_if_call_is_multi_line.Rd

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

tests/testthat/indention_operators/eq_assign-out.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
switch(
2-
engine,
1+
switch(engine,
32
pdftex = {
43
if (any) {
54
x
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
call(
2+
2,
3+
3
4+
)
5+
6+
switch(abc,
7+
wei9
8+
)
9+
10+
switch(abc,
11+
wei9
12+
)
13+
14+
if_else(a,
15+
c, v
16+
)
17+
18+
ifelse(x,
19+
y, z
20+
)

tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-in_tree

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
call(
2+
2,
3+
3
4+
)
5+
6+
switch(abc,
7+
wei9
8+
)
9+
10+
switch(abc,
11+
wei9
12+
)
13+
14+
if_else(a,
15+
c, v
16+
)
17+
18+
ifelse(x,
19+
y, z
20+
)

0 commit comments

Comments
 (0)