Skip to content

Commit

Permalink
introduce linebreaks for only for multi-line args
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Dec 20, 2024
1 parent 6df0686 commit 5b7f8d4
Show file tree
Hide file tree
Showing 41 changed files with 211 additions and 421 deletions.
38 changes: 24 additions & 14 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,29 +462,39 @@ set_line_breaks_for_multiline_args <- function(pd) {

has_children <- purrr::some(pd$child, purrr::negate(is.null))
is_function_definition <- pd$token[1L] == "FUNCTION"
has_internal_linebreak <- FALSE

if (has_children && !is_function_definition) {
children <- pd$child
idx_pre_open_brace <- which(pd$token_after == "'{'")
if (length(idx_pre_open_brace)) {
children[idx_pre_open_brace + 1L] <- NULL
}
if (!has_children || is_function_definition) {
return(pd)
}

has_internal_linebreak <- children %>%
purrr::discard(is.null) %>%
purrr::map_lgl(~ sum(.x$newlines, .x$lag_newlines) > 0L) %>%
any()
children <- pd$child

idx_pre_open_brace <- which(pd$token_after == "'{'")
if (length(idx_pre_open_brace)) {
children[idx_pre_open_brace + 1L] <- NULL
}

if (!has_internal_linebreak && sum(pd$newlines, pd$lag_newlines) < 1L) {
args_multiline <- children %>%
purrr::discard(is.null) %>%
purrr::map_lgl(~ sum(.x$newlines, .x$lag_newlines) > 0L)

if (!any(args_multiline)) {
return(pd)
}

idx_comma <- which(pd$token == "','")
idx_comma_has_comment <- which(pd$token[idx_comma + 1L] == "COMMENT")
idx_comma[idx_comma_has_comment] <- idx_comma[idx_comma_has_comment] + 1L
pd[idx_comma + 1L, "lag_newlines"] <- 1L

for (i in seq_along(idx_comma)) {
arg_index <- i + 1L
if (arg_index <= length(args_multiline) && args_multiline[arg_index]) {
target_row <- idx_comma[i] + if (i %in% idx_comma_has_comment) 2L else 1L
if (target_row <= nrow(pd)) {
pd[target_row, "lag_newlines"] <- 1L
}
}
}

pd
}

1 change: 1 addition & 0 deletions styler.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: bbbaaf9d-d7a1-475e-8d3b-3e41589b57c8

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
25 changes: 6 additions & 19 deletions tests/testthat/alignment/cols-with-one-row-out.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
c(
"x",
"z",
"cgjhg",
"thi",
"z"
"x", "z",
"cgjhg", "thi", "z"
)


c(
"x",
"z",
"cgjhg",
"thi",
"z"
"x", "z",
"cgjhg", "thi", "z"
)


c(
"x",
"y",
"z",
"m",
"n",
"o",
"p",
"c",
"d"
"x", "y", "z", "m", "n", "o", "p",
"c", "d"
)
12 changes: 4 additions & 8 deletions tests/testthat/alignment/fun-decs-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ k <- function(x = fish,

# aligned
k <- function(x = flus(we),
aq = x - 22,
k = 22,
ayz = m(jk5),
xfea = 3) {}
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}


# aligned
k <- function(x = flus(we),
aq = x - 22,
k = 22,
ayz = m(jk5),
xfea = 3) {}
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}
Loading

0 comments on commit 5b7f8d4

Please # to comment.