Skip to content

Commit 8c0f707

Browse files
line breaks in function calls before named arguments
explicitly find the position (and don't just take the position after the opening parenthesis) at which a line break should be inserted in a multi-line call.
1 parent 1631426 commit 8c0f707

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

R/rules-line_break.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,27 @@ set_line_break_after_opening_if_call_is_multi_line <-
7373
if (!is_multi_line) {
7474
return(pd)
7575
}
76+
break_pos <- find_line_break_position_in_multiline_call(pd)
77+
7678
exception_pos <- c(
7779
which(pd$token %in% except_token_after),
78-
if_else(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA)
80+
if_else(pd$child[[1]]$text[1] %in% except_text_before, break_pos, NA)
7981
)
80-
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
82+
pd$lag_newlines[setdiff(break_pos, exception_pos)] <- 1L
8183
pd
84+
}
85+
86+
87+
#' Find index of the token before which the line should be broken
88+
#'
89+
#' Given a multi-line function call parse table, this function finds the
90+
#' position of the first named argument and breaks returns the index of it.
91+
#' If there is no named argument, the line is broken right after the opening
92+
#' parenthesis.
93+
#' @inheritParams set_line_break_if_call_is_multi_line
94+
find_line_break_position_in_multiline_call <- function(pd) {
95+
candidate <- (which(pd$token == "EQ_SUB") - 1L)[1]
96+
ifelse(is.na(candidate), 3L, candidate)
8297
}
8398

8499

man/find_line_break_position_in_multiline_call.Rd

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

0 commit comments

Comments
 (0)