|
1 | | -# A { should never go on its own line |
2 | | -remove_line_break_before_curly_opening <- function(pd) { |
3 | | - rm_break_idx <- which((pd$token_after == "'{'") & (pd$token != "COMMENT")) |
4 | | - rm_break_idx <- setdiff(rm_break_idx, nrow(pd)) |
5 | | - if (length(rm_break_idx) > 0) { |
| 1 | +#' Set line break before a curly brace |
| 2 | +#' |
| 3 | +#' Rule: |
| 4 | +#' * Principle: Function arguments that consist of a braced expression always |
| 5 | +#' need to start on a new line |
| 6 | +#' * Exception: [...] unless it's the last argument and all other |
| 7 | +#' arguments fit on the line of the function call |
| 8 | +#' * Exception: [...] or they are named. |
| 9 | +#' * Extension: Also, expressions following on braced expressions also cause a |
| 10 | +#' line trigger. |
| 11 | +#' @keywords internal |
| 12 | +#' @examples |
| 13 | +#' \dontrun{ |
| 14 | +#' tryCatch( |
| 15 | +#' { |
| 16 | +#' f(8) |
| 17 | +#' }, |
| 18 | +#' error = function(e) NULL |
| 19 | +#' ) |
| 20 | +#' # last-argument case |
| 21 | +#' testthat("braces braces are cool", { |
| 22 | +#' code(to = execute) |
| 23 | +#' }) |
| 24 | +#' call2( |
| 25 | +#' x = 2, |
| 26 | +#' { |
| 27 | +#' code(to = execute) |
| 28 | +#' }, |
| 29 | +#' c = { # this is the named case |
| 30 | +#' g(x = 7) |
| 31 | +#' } |
| 32 | +#' ) |
| 33 | +#' tryGugus( |
| 34 | +#' { |
| 35 | +#' g5(k = na) |
| 36 | +#' }, |
| 37 | +#' a + b # line break also here because |
| 38 | +#' # proceded by brace expression |
| 39 | +#' ) |
| 40 | +#' } |
| 41 | +set_line_break_before_curly_opening <- function(pd) { |
| 42 | + line_break_to_set_idx <- which( |
| 43 | + (pd$token_after == "'{'") & (pd$token != "COMMENT") |
| 44 | + ) |
| 45 | + |
| 46 | + line_break_to_set_idx <- setdiff(line_break_to_set_idx, nrow(pd)) |
| 47 | + if (length(line_break_to_set_idx) > 0) { |
6 | 48 | is_not_curly_curly <- map_chr( |
7 | | - rm_break_idx + 1L, |
| 49 | + line_break_to_set_idx + 1L, |
8 | 50 | ~ next_terminal(pd[.x, ], vars = "token_after")$token_after |
9 | 51 | ) != "'{'" |
10 | | - is_not_curly_curly_idx <- rm_break_idx[is_not_curly_curly] |
| 52 | + last_expr_idx <- max(which(pd$token == "expr")) |
| 53 | + is_last_expr <- ifelse(pd$token[1] == "IF", |
| 54 | + # rule not applicable for IF |
| 55 | + TRUE, (line_break_to_set_idx + 1L) == last_expr_idx |
| 56 | + ) |
| 57 | + eq_sub_before <- pd$token[line_break_to_set_idx] == "EQ_SUB" |
| 58 | + # no line break before last brace expression and named brace expression to |
| 59 | + should_be_on_same_line <- is_not_curly_curly & (is_last_expr | eq_sub_before) |
| 60 | + is_not_curly_curly_idx <- line_break_to_set_idx[should_be_on_same_line] |
11 | 61 | pd$lag_newlines[1 + is_not_curly_curly_idx] <- 0L |
| 62 | + |
| 63 | + # other cases: line breaks |
| 64 | + should_not_be_on_same_line <- is_not_curly_curly & (!is_last_expr & !eq_sub_before) |
| 65 | + should_not_be_on_same_line_idx <- line_break_to_set_idx[should_not_be_on_same_line] |
| 66 | + |
| 67 | + pd$lag_newlines[1 + should_not_be_on_same_line_idx] <- 1L |
| 68 | + |
| 69 | + # non-curly expressions after curly expressions must have line breaks |
| 70 | + if (length(should_not_be_on_same_line_idx) > 0) { |
| 71 | + comma_exprs_idx <- which(pd$token == "','") |
| 72 | + comma_exprs_idx <- setdiff(comma_exprs_idx, 1 + is_not_curly_curly_idx) |
| 73 | + non_comment_after_comma <- map_int(comma_exprs_idx, |
| 74 | + next_non_comment, |
| 75 | + pd = pd |
| 76 | + ) |
| 77 | + non_comment_after_expr <- |
| 78 | + non_comment_after_comma[non_comment_after_comma > should_not_be_on_same_line_idx[1]] |
| 79 | + pd$lag_newlines[non_comment_after_comma] <- 1L |
| 80 | + } |
12 | 81 | } |
13 | 82 | pd |
14 | 83 | } |
15 | 84 |
|
| 85 | + |
16 | 86 | set_line_break_around_comma <- function(pd) { |
17 | 87 | comma_with_line_break_that_can_be_removed_before <- |
18 | 88 | (pd$token == "','") & |
|
0 commit comments