diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index b3b758db3..c71b81c23 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -25,7 +25,9 @@ jobs: - uses: actions/checkout@v5 with: fetch-depth: 0 - + - name: Setup system dependencies + run: | + sudo apt install cmake libcurl4-openssl-dev libnode-dev pandoc - name: Set up Python uses: actions/setup-python@v6.0.0 with: diff --git a/DESCRIPTION b/DESCRIPTION index c8d2d32f6..3c24cef7e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: styler Title: Non-Invasive Pretty Printing of R Code -Version: 1.11.0 +Version: 1.11.0.9001 Authors@R: c(person(given = "Kirill", family = "Müller", @@ -53,7 +53,6 @@ Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace", "collate", if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet" else { warning("Please install r-lib/pkgapi to make sure the file API is kept up to date"); NULL})) -RoxygenNote: 7.3.3 Language: en-US Config/testthat/edition: 3 Config/testthat/parallel: true @@ -107,3 +106,4 @@ Collate: 'vertical.R' 'visit.R' 'zzz.R' +Config/roxygen2/version: 8.0.0 diff --git a/R/addins.R b/R/addins.R index ab832ca52..ad5fed7fb 100644 --- a/R/addins.R +++ b/R/addins.R @@ -45,12 +45,12 @@ NULL - #' @keywords internal style_active_file <- function() { communicate_addins_style_transformers() context <- get_rstudio_context() - transformer <- make_transformer(get_addins_style_transformer(), + transformer <- make_transformer( + get_addins_style_transformer(), include_roxygen_examples = TRUE, base_indention = 0L, warn_empty = is_plain_r_file(context$path) @@ -124,7 +124,9 @@ style_selection <- function() { communicate_addins_style_transformers() context <- get_rstudio_context() text <- context$selection[[1L]]$text - if (!any(nzchar(text))) abort("No code selected") + if (!any(nzchar(text))) { + abort("No code selected") + } out <- style_text( text, transformers = get_addins_style_transformer(), @@ -132,10 +134,13 @@ style_selection <- function() { ) rstudioapi::modifyRange( context$selection[[1L]]$range, - paste(c( - out, - if (context$selection[[1L]]$range$end[2L] == 1L) "" - ), collapse = "\n"), + paste( + c( + out, + if (context$selection[[1L]]$range$end[2L] == 1L) "" + ), + collapse = "\n" + ), id = context$id ) if (save_after_styling_is_active() && context$path != "") { @@ -169,7 +174,9 @@ set_style_transformers <- function() { error = function(e) { abort(paste0( "The selected style transformers \"", - new_style, "\" is not valid: ", e$message + new_style, + "\" is not valid: ", + e$message )) } ) diff --git a/R/communicate.R b/R/communicate.R index 6cd00d385..7c99c55e7 100644 --- a/R/communicate.R +++ b/R/communicate.R @@ -7,9 +7,10 @@ #' @inheritParams parse_tree_must_be_identical #' @keywords internal communicate_warning <- function(changed, transformers) { - if (any(changed, na.rm = TRUE) && - !parse_tree_must_be_identical(transformers) && - !getOption("styler.quiet", FALSE) + if ( + any(changed, na.rm = TRUE) && + !parse_tree_must_be_identical(transformers) && + !getOption("styler.quiet", FALSE) ) { cat("Please review the changes carefully!", fill = TRUE) } @@ -26,17 +27,24 @@ communicate_summary <- function(changed, ruler_width) { cli::cat_rule(width = max(40L, ruler_width)) cat("Status\tCount\tLegend \n") cli::cat_bullet( - "\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.", + "\t", + sum(!changed, na.rm = TRUE), + "\tFile unchanged.", bullet = "tick", bullet_col = "green" ) cli::cat_bullet( - "\t", sum(changed, na.rm = TRUE), "\tFile changed.", + "\t", + sum(changed, na.rm = TRUE), + "\tFile changed.", bullet = "info", bullet_col = "cyan" ) cli::cat_bullet( - bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error.", + bullet = "cross", + "\t", + sum(is.na(changed)), + "\tStyling threw an error.", bullet_col = "red" ) cli::cat_rule(width = max(40L, ruler_width)) diff --git a/R/detect-alignment-utils.R b/R/detect-alignment-utils.R index 8e7965c19..d87015982 100644 --- a/R/detect-alignment-utils.R +++ b/R/detect-alignment-utils.R @@ -4,8 +4,10 @@ #' to be the last token in that case. #' @inheritParams alignment_drop_comments #' @keywords internal -alignment_ensure_no_closing_brace <- function(pd_by_line, - last_line_droped_early) { +alignment_ensure_no_closing_brace <- function( + pd_by_line, + last_line_droped_early +) { if (last_line_droped_early) { return(pd_by_line) } @@ -15,7 +17,10 @@ alignment_ensure_no_closing_brace <- function(pd_by_line, pd_by_line[-length(pd_by_line)] } else { # only drop last elment of last line - pd_by_line[[length(pd_by_line)]] <- vec_slice(last, seq2(1L, nrow(last) - 1L)) + pd_by_line[[length(pd_by_line)]] <- vec_slice( + last, + seq2(1L, nrow(last) - 1L) + ) pd_by_line } } @@ -59,7 +64,10 @@ alignment_drop_comments <- function(pd_by_line) { alignment_drop_last_expr <- function(pds_by_line) { # TODO could be skipped if we know it's not a function dec pd_last_line <- pds_by_line[[length(pds_by_line)]] - last_two_lines <- pd_last_line$token[c(nrow(pd_last_line) - 1L, nrow(pd_last_line))] + last_two_lines <- pd_last_line$token[c( + nrow(pd_last_line) - 1L, + nrow(pd_last_line) + )] if (identical(last_two_lines, c("')'", "expr"))) { pd_last_line <- vec_slice(pd_last_line, -nrow(pd_last_line)) } @@ -110,7 +118,10 @@ alignment_col1_all_named <- function(relevant_pd_by_line) { } x$token[3L] == "expr" && any(c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") == x$token[1L]) && - any(c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") == x$token[2L]) + any( + c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") == + x$token[2L] + ) }) %>% all() } @@ -151,13 +162,20 @@ alignment_serialize_line <- function(relevant_pd_by_line, column) { #' @inheritParams alignment_drop_comments #' @keywords internal alignment_serialize <- function(pd_sub) { - out <- Map(function(terminal, text, child, spaces, newlines) { - if (terminal) { - paste0(text, rep_char(" ", spaces)) - } else { - paste0(alignment_serialize(child), rep_char(" ", spaces)) - } - }, pd_sub$terminal, pd_sub$text, pd_sub$child, pd_sub$spaces, pd_sub$newlines) + out <- Map( + function(terminal, text, child, spaces, newlines) { + if (terminal) { + paste0(text, rep_char(" ", spaces)) + } else { + paste0(alignment_serialize(child), rep_char(" ", spaces)) + } + }, + pd_sub$terminal, + pd_sub$text, + pd_sub$child, + pd_sub$spaces, + pd_sub$newlines + ) if (anyNA(out)) { return(NA) } diff --git a/R/detect-alignment.R b/R/detect-alignment.R index bf8ffbe7c..a03012ae5 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -72,14 +72,16 @@ token_is_on_aligned_line <- function(pd_flat) { return(FALSE) } has_correct_spacing_around_comma <- map_lgl( - pd_by_line, alignment_has_correct_spacing_around_comma + pd_by_line, + alignment_has_correct_spacing_around_comma ) if (!all(has_correct_spacing_around_comma)) { return(FALSE) } has_correct_spacing_around_eq_sub <- map_lgl( - pd_by_line, alignment_has_correct_spacing_around_eq_sub + pd_by_line, + alignment_has_correct_spacing_around_eq_sub ) if (!all(has_correct_spacing_around_eq_sub)) { diff --git a/R/expr-is.R b/R/expr-is.R index cbbecadbf..2e8df6b94 100644 --- a/R/expr-is.R +++ b/R/expr-is.R @@ -157,7 +157,10 @@ is_subset_expr <- function(pd) { #' @keywords internal is_shebang <- function(pd) { is_first_comment <- pd$pos_id == 1L - is_first_comment[is_first_comment] <- startsWith(pd$text[is_first_comment], "#!") + is_first_comment[is_first_comment] <- startsWith( + pd$text[is_first_comment], + "#!" + ) is_first_comment } diff --git a/R/indent.R b/R/indent.R index b6e12285b..55dee32df 100644 --- a/R/indent.R +++ b/R/indent.R @@ -39,28 +39,29 @@ indent_without_paren_if_else <- function(pd, indent_by) { } needs_indention_now <- pd$lag_newlines[ next_non_comment(pd, which(pd$token == "')'")) - ] > 0L + ] > + 0L if (needs_indention_now) { pd$indent[expr_after_if] <- indent_by } - else_idx <- which(pd$token == "ELSE") - if (length(else_idx) == 0L) { + els_idx <- which(pd$token == "ELSE") + if (length(els_idx) == 0L) { return(pd) } - expr_after_else_idx <- next_non_comment(pd, else_idx) - has_else_without_curly_or_else_chid <- - any(pd$token == "ELSE") && - pd$child[[expr_after_else_idx]]$token[1L] != "'{'" && - pd$child[[expr_after_else_idx]]$token[1L] != "IF" + expr_after_else_idx <- next_non_comment(pd, els_idx) + has_else_without_curly_or_else_chid <- any(pd$token == "ELSE") && + pd$child[[expr_after_else_idx]]$token[1L] != "'{'" && + pd$child[[expr_after_else_idx]]$token[1L] != "IF" needs_indention_now <- pd$lag_newlines[ next_non_comment(pd, which(pd$token == "ELSE")) - ] > 0L + ] > + 0L if (has_else_without_curly_or_else_chid && needs_indention_now) { - pd$indent[seq(else_idx + 1L, nrow(pd))] <- indent_by + pd$indent[seq(els_idx + 1L, nrow(pd))] <- indent_by } pd } @@ -96,12 +97,12 @@ indent_without_paren_if_else <- function(pd, indent_by) { #' @examples #' styler:::parse_text("a[1]") #' styler:::parse_text("a[[1\n]]") -compute_indent_indices <- function(pd, - token_opening, - token_closing = NULL) { +compute_indent_indices <- function(pd, token_opening, token_closing = NULL) { npd <- nrow(pd) potential_triggers <- which(pd$token %in% token_opening) - needs_indention <- needs_indention(pd, potential_triggers, + needs_indention <- needs_indention( + pd, + potential_triggers, other_trigger_tokens = c("EQ_SUB", "EQ_FORMALS") ) trigger <- potential_triggers[needs_indention][1L] @@ -128,11 +129,16 @@ compute_indent_indices <- function(pd, #' tokens in `pd`. #' @inheritParams needs_indention_one #' @keywords internal -needs_indention <- function(pd, - potential_triggers_pos, - other_trigger_tokens = NULL) { - map_lgl(potential_triggers_pos, needs_indention_one, - pd = pd, other_trigger_tokens = other_trigger_tokens +needs_indention <- function( + pd, + potential_triggers_pos, + other_trigger_tokens = NULL +) { + map_lgl( + potential_triggers_pos, + needs_indention_one, + pd = pd, + other_trigger_tokens = other_trigger_tokens ) } @@ -168,15 +174,18 @@ needs_indention <- function(pd, #' "call(named = c,", #' "named = b)" #' ), strict = FALSE) -needs_indention_one <- function(pd, - potential_trigger_pos, - other_trigger_tokens) { +needs_indention_one <- function( + pd, + potential_trigger_pos, + other_trigger_tokens +) { before_first_break <- which(pd$lag_newlines > 0L)[1L] - 1L if (is.na(before_first_break)) { return(FALSE) } row_idx_between_trigger_and_line_break <- seq2( - potential_trigger_pos, before_first_break + potential_trigger_pos, + before_first_break ) multi_line_token <- pd_is_multi_line( vec_slice(pd, row_idx_between_trigger_and_line_break) @@ -186,10 +195,10 @@ needs_indention_one <- function(pd, potential_trigger_pos ) - other_trigger_on_same_line <- ( - pd$token[remaining_row_idx_between_trigger_and_line_break] %in% - other_trigger_tokens - ) + other_trigger_on_same_line <- (pd$token[ + remaining_row_idx_between_trigger_and_line_break + ] %in% + other_trigger_tokens) line_break_after_other_trigger <- pd$lag_newlines[remaining_row_idx_between_trigger_and_line_break + 1L] > 0L @@ -200,7 +209,6 @@ needs_indention_one <- function(pd, } - #' Set the multi-line column #' #' Sets the column `multi_line` in `pd` by checking row-wise whether any child diff --git a/R/initialize.R b/R/initialize.R index 87b0a1e62..e0aaf1b30 100644 --- a/R/initialize.R +++ b/R/initialize.R @@ -25,7 +25,6 @@ default_style_guide_attributes <- function(pd_flat) { } - #' Initialize attributes #' #' @name initialize_attributes @@ -46,9 +45,14 @@ initialize_newlines <- function(pd_flat) { #' @describeIn initialize_attributes Initializes `spaces`. #' @keywords internal initialize_spaces <- function(pd_flat) { - pd_flat$col3 <- lead(pd_flat$col1, default = utils::tail(pd_flat$col2, 1L) + 1L) - pd_flat$col2_nl <- ifelse(pd_flat$newlines > 0L, - rep(0L, nrow(pd_flat)), pd_flat$col2 + pd_flat$col3 <- lead( + pd_flat$col1, + default = utils::tail(pd_flat$col2, 1L) + 1L + ) + pd_flat$col2_nl <- ifelse( + pd_flat$newlines > 0L, + rep(0L, nrow(pd_flat)), + pd_flat$col2 ) pd_flat$spaces <- pd_flat$col3 - pd_flat$col2_nl - 1L pd_flat$col3 <- pd_flat$col2_nl <- NULL @@ -64,10 +68,7 @@ remove_attributes <- function(pd_flat, attributes) { #' @keywords internal initialize_multi_line <- function(pd_flat) { nrow <- nrow(pd_flat) - pd_flat$multi_line <- ifelse(pd_flat$terminal, - rep(0L, nrow), - rep(NA, nrow) - ) + pd_flat$multi_line <- ifelse(pd_flat$terminal, rep(0L, nrow), rep(NA, nrow)) pd_flat } diff --git a/R/io.R b/R/io.R index a0d8c2168..eca4bb375 100644 --- a/R/io.R +++ b/R/io.R @@ -31,10 +31,13 @@ transform_utf8_one <- function(path, fun, dry) { identical_content <- identical(file_with_info$text, new) identical <- identical_content && !file_with_info$missing_EOF_line_break if (!identical) { + # fmt: skip switch(dry, fail = rlang::abort( paste0( - "File `", path, "` would be modified by styler and argument dry", + "File `", + path, + "` would be modified by styler and argument dry", " is set to 'fail'." ), class = "dryError" @@ -104,7 +107,9 @@ read_utf8_bare <- function(con, warn = TRUE) { if (n > 0L) { stop( c( - "The file ", con, " is not encoded in UTF-8. ", + "The file ", + con, + " is not encoded in UTF-8. ", "These lines contain invalid UTF-8 characters: " ), toString(c(utils::head(i), if (n > 6L) "...")), diff --git a/R/nest.R b/R/nest.R index f6e26d210..4e287fc09 100644 --- a/R/nest.R +++ b/R/nest.R @@ -13,9 +13,11 @@ #' writeLines(code) #' compute_parse_data_nested(code) #' @export -compute_parse_data_nested <- function(text, - transformers = tidyverse_style(), - more_specs = NULL) { +compute_parse_data_nested <- function( + text, + transformers = tidyverse_style(), + more_specs = NULL +) { parse_data <- text_to_flat_pd(text, transformers, more_specs = more_specs) env_add_stylerignore(parse_data) parse_data$child <- rep(list(NULL), length(parse_data$text)) @@ -95,9 +97,18 @@ add_cache_block <- function(pd_nested) { #' @keywords internal shallowify <- function(pd) { if (cache_is_activated()) { - order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)) + order <- order( + pd$line1, + pd$col1, + -pd$line2, + -pd$col2, + as.integer(pd$terminal) + ) pd_parent_first <- vec_slice(pd, order) - pd_parent_first_split <- vec_split(pd_parent_first, cumsum(pd_parent_first$parent == 0L)) + pd_parent_first_split <- vec_split( + pd_parent_first, + cumsum(pd_parent_first$parent == 0L) + ) pos_ids_to_keep <- pd_parent_first_split[[2L]] %>% map(find_pos_id_to_keep) %>% unlist(use.names = FALSE) @@ -285,7 +296,8 @@ add_attributes_caching <- function(pd_flat, transformers, more_specs) { is_parent <- pd_flat$parent == 0L pd_flat$is_cached[is_parent] <- map_lgl( pd_flat$text[pd_flat$parent == 0L], - is_cached, transformers, + is_cached, + transformers, more_specs = more_specs ) is_comment <- pd_flat$token == "COMMENT" diff --git a/R/nested-to-tree.R b/R/nested-to-tree.R index 90787fecd..c9ec59a14 100644 --- a/R/nested-to-tree.R +++ b/R/nested-to-tree.R @@ -11,8 +11,10 @@ create_tree <- function(text, structure_only = FALSE) { create_tree_from_pd_with_default_style_attributes(structure_only) } -create_tree_from_pd_with_default_style_attributes <- function(pd, - structure_only = FALSE) { +create_tree_from_pd_with_default_style_attributes <- function( + pd, + structure_only = FALSE +) { pd %>% create_node_from_nested_root(structure_only) %>% # don't use `styler_df()` here; `vctrs::data_frame()` only accepts a vector, not a object @@ -76,10 +78,15 @@ create_node_info <- function(pd_nested, structure_only) { return(seq2(1L, nrow(pd_nested))) } paste0( - pd_nested$token, ": ", - pd_nested$short, " [", - pd_nested$lag_newlines, "/", - pd_nested$spaces, "] {", - pd_nested$pos_id, "}" + pd_nested$token, + ": ", + pd_nested$short, + " [", + pd_nested$lag_newlines, + "/", + pd_nested$spaces, + "] {", + pd_nested$pos_id, + "}" ) } diff --git a/R/parse.R b/R/parse.R index d4bacd1c4..29a677f7c 100644 --- a/R/parse.R +++ b/R/parse.R @@ -48,7 +48,10 @@ has_crlf_as_first_line_sep <- function(message, initial_text) { if (length(split) > 1L && split[1L] == "") { start_char <- as.numeric(split[3L]) offending_line <- initial_text[as.integer(split[2L])] - if (!is.na(offending_line) && substr(offending_line, start_char, start_char + 1L) == "\r\n") { + if ( + !is.na(offending_line) && + substr(offending_line, start_char, start_char + 1L) == "\r\n" + ) { return(TRUE) } } @@ -138,7 +141,8 @@ ensure_correct_txt <- function(pd, text) { is_parent_of_problematic_string <- pd$id %in% problematic_text$parent is_unaffected_token <- !magrittr::or( - is_problematic_text, is_parent_of_problematic_string + is_problematic_text, + is_parent_of_problematic_string ) pd_with_all_text <- get_parse_data(text, include_text = TRUE) @@ -147,7 +151,9 @@ ensure_correct_txt <- function(pd, text) { pd_with_all_text[is_parent_of_problematic_string, parent_cols_for_merge] problematic_text$text <- NULL problematic_text$short <- NULL - new_text <- merge(problematic_text, parent_of_problematic_text, + new_text <- merge( + problematic_text, + parent_of_problematic_text, by.x = "parent", by.y = "id", suffixes = c("", "parent") @@ -155,9 +161,12 @@ ensure_correct_txt <- function(pd, text) { styler_df() if (!lines_and_cols_match(new_text)) { - abort(paste( - "Error in styler:::ensure_correct_txt()." - ), .internal = TRUE) + abort( + paste( + "Error in styler:::ensure_correct_txt()." + ), + .internal = TRUE + ) } names_to_keep <- setdiff( names(new_text), diff --git a/R/reindent.R b/R/reindent.R index fa78aa840..da62b7f20 100644 --- a/R/reindent.R +++ b/R/reindent.R @@ -85,10 +85,12 @@ find_tokens_to_update <- function(flattened_pd, target_token) { #' @return A flattened parse table with indention set to `target_indention` for #' the tokens that match `regex.` #' @keywords internal -set_regex_indention <- function(flattened_pd, - pattern, - target_indention = 0L, - comments_only = TRUE) { +set_regex_indention <- function( + flattened_pd, + pattern, + target_indention = 0L, + comments_only = TRUE +) { if (comments_only) { cond <- which( (flattened_pd$token == "COMMENT") & (flattened_pd$lag_newlines > 0L) diff --git a/R/relevel.R b/R/relevel.R index c7e5c95cc..d430435a1 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -25,7 +25,15 @@ flatten_operators <- function(pd_nested) { #' @keywords internal flatten_operators_one <- function(pd_nested) { pd_token_left <- c(special_token, "PIPE", math_token, "'$'") - pd_token_right <- c(special_token, "PIPE", "LEFT_ASSIGN", "EQ_ASSIGN", "'+'", "'-'", "'~'") + pd_token_right <- c( + special_token, + "PIPE", + "LEFT_ASSIGN", + "EQ_ASSIGN", + "'+'", + "'-'", + "'~'" + ) pd_nested %>% flatten_pd(pd_token_left, left = TRUE) %>% flatten_pd(pd_token_right, left = FALSE) @@ -94,7 +102,8 @@ bind_with_child <- function(pd_nested, pos) { #' @keywords internal wrap_expr_in_expr <- function(pd) { create_tokens( - "expr", "", + "expr", + "", pos_ids = create_pos_ids(pd, 1L, after = FALSE), child = pd, terminal = FALSE, diff --git a/R/roxygen-examples-add-remove.R b/R/roxygen-examples-add-remove.R index 3a7ecb40b..dbc8fafe7 100644 --- a/R/roxygen-examples-add-remove.R +++ b/R/roxygen-examples-add-remove.R @@ -5,10 +5,15 @@ #' remove_dont_mask <- function(roxygen) { mask <- c( - 1L, 2L, if (roxygen[3L] == "\n") 3L, last(which(roxygen == "}")) - ) %>% sort() + 1L, + 2L, + if (roxygen[3L] == "\n") 3L, + last(which(roxygen == "}")) + ) %>% + sort() list( - code = roxygen[-mask], mask = paste(roxygen[seq2(1L, 2L)], collapse = "") + code = roxygen[-mask], + mask = paste(roxygen[seq2(1L, 2L)], collapse = "") ) } diff --git a/R/roxygen-examples-parse.R b/R/roxygen-examples-parse.R index a428acae4..3488851b5 100644 --- a/R/roxygen-examples-parse.R +++ b/R/roxygen-examples-parse.R @@ -131,13 +131,17 @@ roxygen_remove_extra_brace <- function(parsed) { #' @keywords internal emulate_rd <- function(roxygen) { example_type <- gsub( - "^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "examples\\2", roxygen[1L] + "^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", + "examples\\2", + roxygen[1L] ) if (needs_rd_emulation(roxygen)) { roxygen <- c( "#' Example", gsub( - "^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "#' @examples \\4", roxygen + "^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", + "#' @examples \\4", + roxygen ), "x <- 1" ) diff --git a/R/roxygen-examples.R b/R/roxygen-examples.R index 588006df8..2b9300a25 100644 --- a/R/roxygen-examples.R +++ b/R/roxygen-examples.R @@ -10,8 +10,10 @@ style_roxygen_code_example <- function(example, transformers, base_indention) { example <- vec_split(example, cumsum(grepl("^#' *@examples", example))) purrr::map( - example[[2L]], style_roxygen_code_example_one, - transformers = transformers, base_indention = base_indention + example[[2L]], + style_roxygen_code_example_one, + transformers = transformers, + base_indention = base_indention ) %>% flatten_chr() } @@ -21,15 +23,22 @@ style_roxygen_code_example <- function(example, transformers, base_indention) { #' @param example_one A character vector, one element per line, that contains in #' total at most one example tag. #' @keywords internal -style_roxygen_code_example_one <- function(example_one, - transformers, - base_indention) { +style_roxygen_code_example_one <- function( + example_one, + transformers, + base_indention +) { # Workaround for imperfect parsing of roxygen2 examples example_one <- example_one[example_one != ""] bare <- parse_roxygen(example_one) - one_dont <- vec_split(bare$text, factor(cumsum(bare$text %in% dont_keywords()))) - unmasked <- map(one_dont[[2L]], style_roxygen_code_example_segment, + one_dont <- vec_split( + bare$text, + factor(cumsum(bare$text %in% dont_keywords())) + ) + unmasked <- map( + one_dont[[2L]], + style_roxygen_code_example_segment, transformers = transformers, base_indention = base_indention ) %>% @@ -66,9 +75,11 @@ style_roxygen_code_example_one <- function(example_one, #' @inheritParams parse_transform_serialize_r #' @inheritSection parse_transform_serialize_roxygen Hierarchy #' @keywords internal -style_roxygen_code_example_segment <- function(one_dont, - transformers, - base_indention) { +style_roxygen_code_example_segment <- function( + one_dont, + transformers, + base_indention +) { if (length(one_dont) < 1L) { return(character()) } else if (identical(one_dont, "\n")) { @@ -76,9 +87,12 @@ style_roxygen_code_example_segment <- function(one_dont, } dont_seqs <- find_dont_seqs(one_dont) split_segments <- split_roxygen_segments(one_dont, unlist(dont_seqs)) - is_dont <- seq2(1L, length(split_segments$separated)) %in% split_segments$selectors + is_dont <- seq2(1L, length(split_segments$separated)) %in% + split_segments$selectors - map2(split_segments$separated, is_dont, + map2( + split_segments$separated, + is_dont, style_roxygen_example_snippet, transformers = transformers, base_indention = base_indention @@ -94,10 +108,12 @@ style_roxygen_code_example_segment <- function(one_dont, #' @inheritParams parse_transform_serialize_r #' @inheritSection parse_transform_serialize_roxygen Hierarchy #' @keywords internal -style_roxygen_example_snippet <- function(code_snippet, - transformers, - is_dont, - base_indention) { +style_roxygen_example_snippet <- function( + code_snippet, + transformers, + is_dont, + base_indention +) { if (is_dont) { decomposed <- remove_dont_mask(code_snippet) code_snippet <- decomposed$code @@ -110,7 +126,8 @@ style_roxygen_example_snippet <- function(code_snippet, cache_is_active <- cache_is_activated() is_cached <- is_cached( - code_snippet, transformers, + code_snippet, + transformers, cache_more_specs( include_roxygen_examples = TRUE, base_indention = base_indention @@ -127,13 +144,18 @@ style_roxygen_example_snippet <- function(code_snippet, ) } - code_snippet <- ensure_last_n_empty(code_snippet, n = as.integer(append_empty)) + code_snippet <- ensure_last_n_empty( + code_snippet, + n = as.integer(append_empty) + ) if (!is_cached && cache_is_active) { cache_write( - code_snippet, transformers, + code_snippet, + transformers, cache_more_specs( - include_roxygen_examples = TRUE, base_indention = base_indention + include_roxygen_examples = TRUE, + base_indention = base_indention ) ) } diff --git a/R/rules-indention.R b/R/rules-indention.R index 65e83785e..ce0c92f49 100644 --- a/R/rules-indention.R +++ b/R/rules-indention.R @@ -76,22 +76,23 @@ is_single_indent_function_declaration <- function(pd, indent_by = 2L) { pd$spaces[first_nl_formal - 1L] <= 2L * indent_by } - #' @describeIn update_indention Indents *all* tokens after `token` - including #' the last token. #' @keywords internal -indent_op <- function(pd, - indent_by, - token = c( - math_token, - logical_token, - special_token, - "PIPE", - "LEFT_ASSIGN", - "EQ_ASSIGN", - "'$'", - "'~'" - )) { +indent_op <- function( + pd, + indent_by, + token = c( + math_token, + logical_token, + special_token, + "PIPE", + "LEFT_ASSIGN", + "EQ_ASSIGN", + "'$'", + "'~'" + ) +) { indent_indices <- compute_indent_indices(pd, token) pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by pd @@ -103,9 +104,7 @@ indent_op <- function(pd, #' multiple times in a parse table. #' occurs is not indented (see[compute_indent_indices()]) #' @keywords internal -indent_eq_sub <- function(pd, - indent_by, - token = c("EQ_SUB", "EQ_FORMALS")) { +indent_eq_sub <- function(pd, indent_by, token = c("EQ_SUB", "EQ_FORMALS")) { eq_sub <- pd$token %in% token if (!any(eq_sub)) { return(pd) @@ -156,7 +155,10 @@ NULL #' #' @keywords internal update_indention_reference_function_declaration <- function(pd_nested) { - if (is_function_declaration(pd_nested) && !is_single_indent_function_declaration(pd_nested)) { + if ( + is_function_declaration(pd_nested) && + !is_single_indent_function_declaration(pd_nested) + ) { seq <- seq2(3L, nrow(pd_nested) - 2L) pd_nested$indention_ref_pos_id[seq] <- pd_nested$pos_id[2L] } diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index 673bcab41..b6131af46 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -61,7 +61,8 @@ set_line_break_before_curly_opening <- function(pd) { is_not_curly_curly <- map_chr( line_break_to_set_idx + 1L, ~ next_terminal(vec_slice(pd, .x), vars = "token_after")$token_after - ) != "'{'" + ) != + "'{'" last_expr_idx <- max(which(pd$token == "expr")) is_last_expr <- if (any(c("IF", "WHILE") == pd$token[1L])) { # rule not applicable for if and while @@ -69,8 +70,11 @@ set_line_break_before_curly_opening <- function(pd) { } else { (line_break_to_set_idx + 1L) == last_expr_idx } - no_line_break_before_curly_idx <- any(pd$token[line_break_to_set_idx] == "EQ_SUB") - linebreak_before_curly <- ifelse(is_function_call(pd), + no_line_break_before_curly_idx <- any( + pd$token[line_break_to_set_idx] == "EQ_SUB" + ) + linebreak_before_curly <- ifelse( + is_function_call(pd), # if in function call and has pipe, it is not recognized as function call # and goes to else case any(pd$lag_newlines[seq2(1L, line_break_to_set_idx[1L])] > 0L), @@ -79,26 +83,22 @@ set_line_break_before_curly_opening <- function(pd) { ) # no line break before last brace expression and named brace expression to should_be_on_same_line <- is_not_curly_curly & - ( - (is_last_expr & !linebreak_before_curly) | - no_line_break_before_curly_idx - ) + ((is_last_expr & !linebreak_before_curly) | + no_line_break_before_curly_idx) is_not_curly_curly_idx <- line_break_to_set_idx[should_be_on_same_line] pd$lag_newlines[1L + is_not_curly_curly_idx] <- 0L - # other cases: line breaks should_not_be_on_same_line <- is_not_curly_curly & - ( - (!is_last_expr | linebreak_before_curly) & - !no_line_break_before_curly_idx - ) + ((!is_last_expr | linebreak_before_curly) & + !no_line_break_before_curly_idx) should_not_be_on_same_line_idx <- line_break_to_set_idx[ should_not_be_on_same_line ] if (is_function_declaration(pd)) { should_not_be_on_same_line_idx <- setdiff( - 1L + should_not_be_on_same_line_idx, nrow(pd) + 1L + should_not_be_on_same_line_idx, + nrow(pd) ) } else { should_not_be_on_same_line_idx <- 1L + should_not_be_on_same_line_idx @@ -109,7 +109,8 @@ set_line_break_before_curly_opening <- function(pd) { if (length(should_not_be_on_same_line_idx) > 0L) { comma_exprs_idx <- which(pd$token == "','") comma_exprs_idx <- setdiff(comma_exprs_idx, 1L + is_not_curly_curly_idx) - non_comment_after_comma <- map_int(comma_exprs_idx, + non_comment_after_comma <- map_int( + comma_exprs_idx, next_non_comment, pd = pd ) @@ -122,11 +123,10 @@ set_line_break_before_curly_opening <- function(pd) { set_line_break_around_comma_and_or <- function(pd, strict) { ops <- c("','", "AND", "OR", "AND2", "OR2") - comma_with_line_break_that_can_be_removed_before <- - (pd$token %in% ops) & - (pd$lag_newlines > 0L) & - (pd$token_before != "COMMENT") & - !(lag(pd$token) %in% subset_token_opening) + comma_with_line_break_that_can_be_removed_before <- (pd$token %in% ops) & + (pd$lag_newlines > 0L) & + (pd$token_before != "COMMENT") & + !(lag(pd$token) %in% subset_token_opening) pd$lag_newlines[comma_with_line_break_that_can_be_removed_before] <- 0L pd$lag_newlines[lag(comma_with_line_break_that_can_be_removed_before)] <- 1L @@ -223,7 +223,8 @@ set_line_break_around_outer_curly_curly <- function(pd) { closing_before <- (pd$token == "'}'") & (pd$token_before == "'}'") if (any(opening_before) && any(closing_before)) { - pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT" + pos_opening_idx <- lag(opening_before, default = FALSE) & + pd$token != "COMMENT" pd$lag_newlines[pos_opening_idx] <- 0L if (any(pos_opening_idx)) { # if line is broken with opening `{`, also break it with closing @@ -241,8 +242,10 @@ set_line_break_around_inner_curly_curly <- function(pd) { closing_before <- (pd$token == "'}'") & (pd$token_after == "'}'") if (any(opening_before) && any(closing_before)) { - pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT" - can_remove_line_break_closing <- closing_before & pd$token_before != "COMMENT" + pos_opening_idx <- lag(opening_before, default = FALSE) & + pd$token != "COMMENT" + can_remove_line_break_closing <- closing_before & + pd$token_before != "COMMENT" if (any(pos_opening_idx) && any(can_remove_line_break_closing)) { pd$lag_newlines[pos_opening_idx] <- 0L # if line is broken with opening `{`, also break it with closing @@ -263,9 +266,7 @@ remove_line_break_before_round_closing_after_curly <- function(pd) { remove_line_breaks_in_function_declaration <- function(pd) { if (is_function_declaration(pd)) { is_single_indention <- is_single_indent_function_declaration(pd) - round_after <- ( - pd$token == "')'" | pd$token_before == "'('" - ) & + round_after <- (pd$token == "')'" | pd$token_before == "'('") & pd$token_before != "COMMENT" pd$lag_newlines[pd$lag_newlines > 1L] <- 1L if (is_single_indention) { @@ -283,15 +284,19 @@ add_line_break_after_pipe <- function(pd) { is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE") pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1L] <- 1L - if (sum(is_pipe & pd$token_after != "COMMENT") > 1L && - !(next_terminal(pd, vars = "token_before")$token_before %in% c("'('", "EQ_SUB", "','"))) { + if ( + sum(is_pipe & pd$token_after != "COMMENT") > 1L && + !(next_terminal(pd, vars = "token_before")$token_before %in% + c("'('", "EQ_SUB", "','")) + ) { pd$lag_newlines[lag(is_pipe) & pd$token != "COMMENT"] <- 1L } pd } set_line_break_after_assignment <- function(pd) { - is_assignment <- lag(pd$token, default = FALSE) %in% c("LEFT_ASSIGN", "EQ_ASSIGN") + is_assignment <- lag(pd$token, default = FALSE) %in% + c("LEFT_ASSIGN", "EQ_ASSIGN") if (any(is_assignment)) { pd$lag_newlines[is_assignment] <- pmin(1L, pd$lag_newlines[is_assignment]) } @@ -329,16 +334,19 @@ NULL #' line. Governed by `force_text_before`. #' #' @keywords internal -set_line_break_after_opening_if_call_is_multi_line <- function(pd, - except_token_after = NULL, - except_text_before = NULL, - force_text_before = NULL) { +set_line_break_after_opening_if_call_is_multi_line <- function( + pd, + except_token_after = NULL, + except_text_before = NULL, + force_text_before = NULL +) { if (!is_function_call(pd) && !is_subset_expr(pd)) { return(pd) } idx <- length(pd$child[[1L]]$text) - has_force_text_before <- - pd$child[[1L]]$text[idx] %in% force_text_before && pd$child[[1L]]$token[idx] == "SYMBOL_FUNCTION_CALL" + has_force_text_before <- pd$child[[1L]]$text[idx] %in% + force_text_before && + pd$child[[1L]]$token[idx] == "SYMBOL_FUNCTION_CALL" if (has_force_text_before) { break_pos <- c( which(lag(pd$token %in% c("','", "COMMENT"))), @@ -436,12 +444,21 @@ set_line_break_after_ggplot2_plus <- function(pd) { if (any(is_plus_raw)) { first_plus <- which(is_plus_raw)[1L] next_non_comment <- next_non_comment(pd, first_plus) - is_plus_or_comment_after_plus_before_fun_call <- - lag(is_plus_raw, next_non_comment - first_plus - 1L, default = FALSE) & - (pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE") + is_plus_or_comment_after_plus_before_fun_call <- lag( + is_plus_raw, + next_non_comment - first_plus - 1L, + default = FALSE + ) & + (pd$token_after == "SYMBOL_FUNCTION_CALL" | + pd$token_after == "SYMBOL_PACKAGE") if (any(is_plus_or_comment_after_plus_before_fun_call, na.rm = TRUE)) { gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1L]] - if (!is.null(gg_call) && isTRUE(gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot")) { + if ( + !is.null(gg_call) && + isTRUE( + gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot" + ) + ) { plus_without_comment_after <- setdiff( which(is_plus_raw), which(lead(pd$token == "COMMENT")) @@ -477,7 +494,10 @@ remove_empty_lines_after_opening_and_before_closing_braces <- function(pd) { } -set_line_breaks_between_top_level_exprs <- function(pd, allowed_blank_lines = 2L) { +set_line_breaks_between_top_level_exprs <- function( + pd, + allowed_blank_lines = 2L +) { pd$lag_newlines <- pmin(pd$lag_newlines, allowed_blank_lines + 1L) pd } diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 57cc8dfb8..2bebd0559 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -20,10 +20,8 @@ set_space_around_operator <- function(pd_flat, strict) { sum_lag_newlines <- sum(pd_flat$lag_newlines) if ( !getOption("styler.ignore_alignment", FALSE) && - ( - (is_function_call(pd_flat) && sum_lag_newlines > 2L) || - (is_function_declaration(pd_flat) && sum_lag_newlines > 1L) - ) && + ((is_function_call(pd_flat) && sum_lag_newlines > 2L) || + (is_function_declaration(pd_flat) && sum_lag_newlines > 1L)) && any(pd_flat$token %in% c("EQ_SUB", "','", "EQ_FORMALS")) ) { is_on_aligned_line <- token_is_on_aligned_line(pd_flat) @@ -31,13 +29,17 @@ set_space_around_operator <- function(pd_flat, strict) { is_on_aligned_line <- FALSE } # operator - must_have_space_before <- op_before & (pd_flat$newlines == 0L) & !is_on_aligned_line + must_have_space_before <- op_before & + (pd_flat$newlines == 0L) & + !is_on_aligned_line pd_flat$spaces[must_have_space_before] <- if (strict) { 1L } else { pmax(pd_flat$spaces[must_have_space_before], 1L) } - must_have_space_after <- op_after & (pd_flat$newlines == 0L) & !is_on_aligned_line + must_have_space_after <- op_after & + (pd_flat$newlines == 0L) & + !is_on_aligned_line pd_flat$spaces[must_have_space_after] <- if (strict) { 1L } else { @@ -61,14 +63,20 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) { pd_flat <- style_space_around_token( pd_flat, - strict = TRUE, tokens = zero, level_before = 0L, level_after = 0L + strict = TRUE, + tokens = zero, + level_before = 0L, + level_after = 0L ) } if (any(pd_flat$token %in% one)) { pd_flat <- style_space_around_token( pd_flat, - strict = strict, tokens = one, level_before = 1L, level_after = 1L + strict = strict, + tokens = one, + level_before = 1L, + level_after = 1L ) } pd_flat @@ -85,11 +93,13 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) { #' should be inserted around the `tokens` on the left and right position #' respectively. #' @keywords internal -style_space_around_token <- function(pd_flat, - strict, - tokens, - level_before, - level_after = level_before) { +style_space_around_token <- function( + pd_flat, + strict, + tokens, + level_before, + level_after = level_before +) { op_after <- pd_flat$token %in% tokens op_before <- lead(op_after, default = FALSE) idx_before <- op_before & (pd_flat$newlines == 0L) @@ -106,15 +116,21 @@ style_space_around_token <- function(pd_flat, style_space_around_tilde <- function(pd_flat, strict) { if (is_symmetric_tilde_expr(pd_flat)) { - pd_flat <- style_space_around_token(pd_flat, - strict, "'~'", - level_before = 1L, level_after = 1L + pd_flat <- style_space_around_token( + pd_flat, + strict, + "'~'", + level_before = 1L, + level_after = 1L ) } if (is_asymmetric_tilde_expr(pd_flat)) { - pd_flat <- style_space_around_token(pd_flat, - strict = TRUE, "'~'", level_before = 1L, + pd_flat <- style_space_around_token( + pd_flat, + strict = TRUE, + "'~'", + level_before = 1L, level_after = as.integer(nrow(pd_flat$child[[2L]]) > 1L) ) } @@ -356,8 +372,12 @@ remove_space_after_function_declaration <- function(pd_flat) { } remove_space_around_colons <- function(pd_flat) { - one_two_or_three_col_after <- pd_flat$token %in% c("':'", "NS_GET_INT", "NS_GET") - one_two_or_three_col_before <- lead(one_two_or_three_col_after, default = FALSE) + one_two_or_three_col_after <- pd_flat$token %in% + c("':'", "NS_GET_INT", "NS_GET") + one_two_or_three_col_before <- lead( + one_two_or_three_col_after, + default = FALSE + ) col_around <- one_two_or_three_col_before | one_two_or_three_col_after diff --git a/R/rules-tokens.R b/R/rules-tokens.R index ed3d3b361..185532ef9 100644 --- a/R/rules-tokens.R +++ b/R/rules-tokens.R @@ -51,7 +51,10 @@ add_brackets_in_pipe_one <- function(pd, pos) { block = NA, is_cached = FALSE ) - pd$child[[next_non_comment]] <- vec_rbind(pd$child[[next_non_comment]], new_pd) %>% + pd$child[[next_non_comment]] <- vec_rbind( + pd$child[[next_non_comment]], + new_pd + ) %>% arrange_pos_id() } pd @@ -64,18 +67,24 @@ add_brackets_in_pipe_one <- function(pd, pos) { #' @param indent_by The amount of spaces used to indent an expression in curly #' braces. Used for unindention. #' @keywords internal -wrap_if_else_while_for_function_multi_line_in_curly <- function(pd, indent_by = 2L) { +wrap_if_else_while_for_function_multi_line_in_curly <- function( + pd, + indent_by = 2L +) { key_token <- NULL if (is_for_expr(pd)) { key_token <- "forcond" - } else if (is_conditional_expr(pd) || is_while_expr(pd) || is_function_declaration(pd)) { + } else if ( + is_conditional_expr(pd) || is_while_expr(pd) || is_function_declaration(pd) + ) { key_token <- "')'" } if (length(key_token) > 0L) { pd <- pd %>% - wrap_multiline_curly(indent_by, + wrap_multiline_curly( + indent_by, key_token = key_token, space_after = as.integer(contains_else_expr(pd)) ) @@ -96,20 +105,29 @@ wrap_if_else_while_for_function_multi_line_in_curly <- function(pd, indent_by = #' @keywords internal wrap_multiline_curly <- function(pd, indent_by, key_token, space_after = 1L) { to_be_wrapped_expr_with_child <- next_non_comment( - pd, which(pd$token == key_token)[1L] + pd, + which(pd$token == key_token)[1L] ) - next_terminal <- next_terminal(vec_slice(pd, to_be_wrapped_expr_with_child))$text - requires_braces <- if_for_while_part_requires_braces(pd, key_token) && !any(pd$stylerignore) + next_terminal <- next_terminal(vec_slice( + pd, + to_be_wrapped_expr_with_child + ))$text + requires_braces <- if_for_while_part_requires_braces(pd, key_token) && + !any(pd$stylerignore) if (requires_braces || next_terminal == "return") { closing_brace_ind <- which(pd$token == key_token)[1L] pd$spaces[closing_brace_ind] <- 1L all_to_be_wrapped_ind <- seq2( - closing_brace_ind + 1L, to_be_wrapped_expr_with_child + closing_brace_ind + 1L, + to_be_wrapped_expr_with_child ) pd <- wrap_subexpr_in_curly( - pd, all_to_be_wrapped_ind, indent_by, space_after + pd, + all_to_be_wrapped_ind, + indent_by, + space_after ) if (nrow(pd) > 5L) pd$lag_newlines[6L] <- 0L @@ -124,17 +142,22 @@ wrap_multiline_curly <- function(pd, indent_by, key_token, space_after = 1L) { #' @inheritParams wrap_multiline_curly #' @keywords internal wrap_else_multiline_curly <- function(pd, indent_by = 2L, space_after = 0L) { - if (contains_else_expr(pd) && - pd_is_multi_line(pd) && - contains_else_expr_that_needs_braces(pd) && - !any(pd$stylerignore) && - pd$token_before[1L] != "SPECIAL-PIPE") { + if ( + contains_else_expr(pd) && + pd_is_multi_line(pd) && + contains_else_expr_that_needs_braces(pd) && + !any(pd$stylerignore) && + pd$token_before[1L] != "SPECIAL-PIPE" + ) { else_idx <- which(pd$token == "ELSE") pd$spaces[else_idx] <- 1L all_to_be_wrapped_ind <- seq2(else_idx + 1L, nrow(pd)) pd <- wrap_subexpr_in_curly( - pd, all_to_be_wrapped_ind, indent_by, space_after + pd, + all_to_be_wrapped_ind, + indent_by, + space_after ) } pd @@ -148,10 +171,12 @@ wrap_else_multiline_curly <- function(pd, indent_by = 2L, space_after = 0L) { #' into a new expression. #' @inheritParams wrap_expr_in_curly #' @keywords internal -wrap_subexpr_in_curly <- function(pd, - ind_to_be_wrapped, - indent_by, - space_after) { +wrap_subexpr_in_curly <- function( + pd, + ind_to_be_wrapped, + indent_by, + space_after +) { to_be_wrapped_starts_with_comment <- pd$token[ind_to_be_wrapped[1L]] == "COMMENT" new_expr <- wrap_expr_in_curly( diff --git a/R/serialize.R b/R/serialize.R index 89bd0e730..db9ebebef 100644 --- a/R/serialize.R +++ b/R/serialize.R @@ -5,7 +5,10 @@ #' @param indent_character The character that is used for indention. We strongly #' advise for using spaces as indention characters. #' @keywords internal -serialize_parse_data_flattened <- function(flattened_pd, indent_character = "") { +serialize_parse_data_flattened <- function( + flattened_pd, + indent_character = "" +) { flattened_pd <- apply_stylerignore(flattened_pd) flattened_pd$lag_newlines[1L] <- 0L # resolve start_line elsewhere with( diff --git a/R/set-assert-args.R b/R/set-assert-args.R index 374f49c94..d78514194 100644 --- a/R/set-assert-args.R +++ b/R/set-assert-args.R @@ -16,7 +16,8 @@ assert_transformers <- function(transformers) { } message <- paste( "Style guides without a name and a version field are", - action, "\nIf you are a user: Open an issue on", + action, + "\nIf you are a user: Open an issue on", "https://github.com/r-lib/styler and provide a reproducible example", "of this error. \nIf you are a developer:", "When you create a style guide with {.fn styler::create_style_guide}, the", @@ -84,7 +85,9 @@ assert_tokens <- function(tokens) { invalid_tokens <- tokens[!(tokens %in% lookup_tokens()$token)] if (length(invalid_tokens) > 0L) { abort(paste( - "Token(s)", toString(invalid_tokens), "are invalid.", + "Token(s)", + toString(invalid_tokens), + "are invalid.", "You can lookup all valid tokens and their text", "with styler:::lookup_tokens(). Make sure you supply the values of", "the column 'token', not 'text'." diff --git a/R/style-guides.R b/R/style-guides.R index 6a5366c52..ff4e65d31 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -62,24 +62,26 @@ NULL #' # styling line breaks only without spaces #' style_text(c("ab <- 3", "a =3"), strict = TRUE, scope = I(c("line_breaks", "tokens"))) #' @export -tidyverse_style <- function(scope = "tokens", - strict = TRUE, - indent_by = 2L, - start_comments_with_one_space = FALSE, - reindention = tidyverse_reindention(), - math_token_spacing = tidyverse_math_token_spacing()) { +tidyverse_style <- function( + scope = "tokens", + strict = TRUE, + indent_by = 2L, + start_comments_with_one_space = FALSE, + reindention = tidyverse_reindention(), + math_token_spacing = tidyverse_math_token_spacing() +) { args <- as.list(environment()) scope <- scope_normalize(scope) indent_character <- " " - indention_manipulators <- if ("indention" %in% scope) { list( indent_braces = partial(indent_braces, indent_by = indent_by), unindent_function_declaration = unindent_function_declaration, indent_op = partial(indent_op, indent_by = indent_by), indent_eq_sub = partial(indent_eq_sub, indent_by = indent_by), - indent_without_paren = partial(indent_without_paren, + indent_without_paren = partial( + indent_without_paren, indent_by = indent_by ), update_indention_reference_function_declaration = update_indention_reference_function_declaration @@ -94,7 +96,8 @@ tidyverse_style <- function(scope = "tokens", add_space_after_for_if_while = add_space_after_for_if_while, remove_space_before_comma = remove_space_before_comma, style_space_around_math_token = partial( - style_space_around_math_token, strict, + style_space_around_math_token, + strict, math_token_spacing$zero, math_token_spacing$one ), @@ -102,7 +105,8 @@ tidyverse_style <- function(scope = "tokens", style_space_around_tilde, strict = strict ), - spacing_around_op = purrr::partial(set_space_around_operator, + spacing_around_op = purrr::partial( + set_space_around_operator, strict = strict ), remove_space_after_opening_paren = remove_space_after_opening_paren, @@ -111,7 +115,8 @@ tidyverse_style <- function(scope = "tokens", remove_space_around_dollar = remove_space_around_dollar, remove_space_after_function_declaration = remove_space_after_function_declaration, remove_space_around_colons = remove_space_around_colons, - start_comments_with_space = partial(start_comments_with_space, + start_comments_with_space = partial( + start_comments_with_space, force_one = start_comments_with_one_space ), remove_space_after_unary_plus_minus_nested = remove_space_after_unary_plus_minus_nested, @@ -130,17 +135,21 @@ tidyverse_style <- function(scope = "tokens", line_break_manipulators <- if ("line_breaks" %in% scope) { list( + # fmt: skip remove_empty_lines_after_opening_and_before_closing_braces = remove_empty_lines_after_opening_and_before_closing_braces, set_line_break_around_comma_and_or = set_line_break_around_comma_and_or, set_line_break_after_assignment = set_line_break_after_assignment, set_line_break_before_curly_opening = set_line_break_before_curly_opening, - remove_line_break_before_round_closing_after_curly = - if (strict) remove_line_break_before_round_closing_after_curly, - remove_line_breaks_in_function_declaration = - if (strict) remove_line_breaks_in_function_declaration, - set_line_breaks_between_top_level_exprs = - if (strict) set_line_breaks_between_top_level_exprs, + remove_line_break_before_round_closing_after_curly = if (strict) { + remove_line_break_before_round_closing_after_curly + }, + remove_line_breaks_in_function_declaration = if (strict) { + remove_line_breaks_in_function_declaration + }, + set_line_breaks_between_top_level_exprs = if (strict) { + set_line_breaks_between_top_level_exprs + }, style_line_break_around_curly = partial( style_line_break_around_curly, strict @@ -159,8 +168,7 @@ tidyverse_style <- function(scope = "tokens", set_line_break_after_opening_if_call_is_multi_line, except_token_after = "COMMENT", # don't modify line break here - except_text_before = c("ifelse", "if_else"), - force_text_before = "switch" # force line break after first token + except_text_before = c("ifelse", "if_else") ) }, remove_line_break_in_fun_call = purrr::partial( @@ -180,13 +188,12 @@ tidyverse_style <- function(scope = "tokens", force_assignment_op = force_assignment_op, resolve_semicolon = resolve_semicolon, add_brackets_in_pipe = add_brackets_in_pipe, - wrap_if_else_while_for_function_multi_line_in_curly = - if (strict) { - purrr::partial( - wrap_if_else_while_for_function_multi_line_in_curly, - indent_by = indent_by - ) - } + wrap_if_else_while_for_function_multi_line_in_curly = if (strict) { + purrr::partial( + wrap_if_else_while_for_function_multi_line_in_curly, + indent_by = indent_by + ) + } ) } @@ -232,12 +239,16 @@ tidyverse_style <- function(scope = "tokens", add_brackets_in_pipe = c("SPECIAL-PIPE", "PIPE"), force_assignment_op = "EQ_ASSIGN", wrap_if_else_while_for_function_multi_line_in_curly = c( - "IF", "WHILE", "FOR", "FUNCTION" + "IF", + "WHILE", + "FOR", + "FUNCTION" ) ) ) style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib" + # fmt: skip create_style_guide( # transformer functions initialize = default_style_guide_attributes, @@ -320,18 +331,20 @@ tidyverse_style <- function(scope = "tokens", #' style = set_line_break_before_curly_opening_style #' ) #' @export -create_style_guide <- function(initialize = default_style_guide_attributes, - line_break = NULL, - space = NULL, - token = NULL, - indention = NULL, - use_raw_indention = FALSE, - reindention = tidyverse_reindention(), - style_guide_name = NULL, - style_guide_version = NULL, - more_specs_style_guide = NULL, - transformers_drop = specify_transformers_drop(), - indent_character = " ") { +create_style_guide <- function( + initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention(), + style_guide_name = NULL, + style_guide_version = NULL, + more_specs_style_guide = NULL, + transformers_drop = specify_transformers_drop(), + indent_character = " " +) { list( # transformer functions initialize = list(initialize = initialize), @@ -405,12 +418,16 @@ create_style_guide <- function(initialize = default_style_guide_attributes, #' # not, not via `is.null()` and we can use the `is.null()` check to see if #' # this scope was initially required by the user. #' @export -specify_transformers_drop <- function(spaces = NULL, - indention = NULL, - line_breaks = NULL, - tokens = NULL) { +specify_transformers_drop <- function( + spaces = NULL, + indention = NULL, + line_breaks = NULL, + tokens = NULL +) { list( - space = spaces, indention = indention, line_break = line_breaks, + space = spaces, + indention = indention, + line_break = line_breaks, token = tokens ) } @@ -438,9 +455,11 @@ NULL #' regex_pattern = "xyz", indention = 4, comments_only = FALSE #' )) #' @export -specify_reindention <- function(regex_pattern = NULL, - indention = 0L, - comments_only = TRUE) { +specify_reindention <- function( + regex_pattern = NULL, + indention = 0L, + comments_only = TRUE +) { list( regex_pattern = regex_pattern, indention = indention, @@ -456,7 +475,9 @@ specify_reindention <- function(regex_pattern = NULL, #' @export tidyverse_reindention <- function() { specify_reindention( - regex_pattern = NULL, indention = 0L, comments_only = TRUE + regex_pattern = NULL, + indention = 0L, + comments_only = TRUE ) } @@ -516,8 +537,7 @@ NULL #' spacing. #' @export specify_math_token_spacing <- - function(zero = "'^'", - one = c("'+'", "'-'", "'*'", "'/'")) { + function(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'")) { assert_tokens(c(one, zero)) list( one = setdiff(c(math_token, one), zero), diff --git a/R/styler-package.R b/R/styler-package.R index c307a9284..581aeffbe 100644 --- a/R/styler-package.R +++ b/R/styler-package.R @@ -31,11 +31,22 @@ NULL utils::globalVariables(c( ".", - "pd", "pd_nested", "pd_flat", "flattened_pd", - "line1", "line2", "col1", "col2", "parent", - "terminal", "text", "short", - "spaces", "lag_spaces", - "newlines", "lag_newlines", + "pd", + "pd_nested", + "pd_flat", + "flattened_pd", + "line1", + "line2", + "col1", + "col2", + "parent", + "terminal", + "text", + "short", + "spaces", + "lag_spaces", + "newlines", + "lag_newlines", "pos_id", NULL )) diff --git a/R/stylerignore.R b/R/stylerignore.R index 64fc05337..c06e21bb7 100644 --- a/R/stylerignore.R +++ b/R/stylerignore.R @@ -20,13 +20,15 @@ env_add_stylerignore <- function(pd_flat) { # this means the block can contain cached and uncached expressions. pd_flat_temp <- vec_slice(pd_flat, pd_flat$terminal) %>% default_style_guide_attributes() - is_stylerignore_switchpoint <- pd_flat_temp$stylerignore != lag( - pd_flat_temp$stylerignore, - default = pd_flat_temp$stylerignore[1L] - ) + is_stylerignore_switchpoint <- pd_flat_temp$stylerignore != + lag( + pd_flat_temp$stylerignore, + default = pd_flat_temp$stylerignore[1L] + ) pos_id_split <- vec_split( - pd_flat_temp$pos_id, cumsum(is_stylerignore_switchpoint) + pd_flat_temp$pos_id, + cumsum(is_stylerignore_switchpoint) ) pd_flat_temp$first_pos_id_in_segment <- pos_id_split[[2L]] %>% @@ -59,8 +61,10 @@ env_add_stylerignore <- function(pd_flat) { add_stylerignore <- function(pd_flat) { parse_text <- trimws(pd_flat$text) start_candidate <- grepl( - option_read("styler.ignore_start"), parse_text - ) & pd_flat$token == "COMMENT" + option_read("styler.ignore_start"), + parse_text + ) & + pd_flat$token == "COMMENT" pd_flat$stylerignore <- rep(FALSE, length(start_candidate)) env_current$any_stylerignore <- any(start_candidate) if (!env_current$any_stylerignore) { @@ -74,7 +78,8 @@ add_stylerignore <- function(pd_flat) { pd_flat$token == "COMMENT" ) pd_flat$indicator_off <- cumsum_start + cumsum_stop - is_invalid <- cumsum_start - cumsum_stop < 0L | cumsum_start - cumsum_stop > 1L + is_invalid <- cumsum_start - cumsum_stop < 0L | + cumsum_start - cumsum_stop > 1L if (any(is_invalid)) { cli::cli_warn(c( "Invalid stylerignore sequences found, potentially ignoring some of the \\ @@ -112,21 +117,28 @@ apply_stylerignore <- function(flattened_pd) { } env_current$stylerignore$pos_id_ <- env_current$stylerignore$pos_id colnames_required_apply_stylerignore <- c( - "pos_id_", "lag_newlines", "lag_spaces", "text", "first_pos_id_in_segment" + "pos_id_", + "lag_newlines", + "lag_spaces", + "text", + "first_pos_id_in_segment" ) # cannot rely on flattened_pd$text == option_read("styler.ignore_start") # because if the marker logic is not correct (twice off in a row), we'll # get it wrong. to_ignore <- flattened_pd$stylerignore - not_first <- flattened_pd$stylerignore == lag( - flattened_pd$stylerignore, - default = FALSE - ) + not_first <- flattened_pd$stylerignore == + lag( + flattened_pd$stylerignore, + default = FALSE + ) flattened_pd <- merge( vec_slice(flattened_pd, !(to_ignore & not_first)), env_current$stylerignore[, colnames_required_apply_stylerignore], - by.x = "pos_id", by.y = "first_pos_id_in_segment", all.x = TRUE, + by.x = "pos_id", + by.y = "first_pos_id_in_segment", + all.x = TRUE, sort = FALSE ) @@ -150,11 +162,14 @@ apply_stylerignore <- function(flattened_pd) { #' consolidate. #' @inheritParams apply_stylerignore #' @keywords internal -stylerignore_consolidate_col <- function(flattened_pd, - col, - col_x = paste0(col, ".x"), - col_y = paste0(col, ".y")) { - flattened_pd[[col]] <- ifelse(is.na(flattened_pd[[col_y]]), +stylerignore_consolidate_col <- function( + flattened_pd, + col, + col_x = paste0(col, ".x"), + col_y = paste0(col, ".y") +) { + flattened_pd[[col]] <- ifelse( + is.na(flattened_pd[[col_y]]), flattened_pd[[col_x]], flattened_pd[[col_y]] ) diff --git a/R/testing-mocks.R b/R/testing-mocks.R index cc52625fb..2f1e0e7b2 100644 --- a/R/testing-mocks.R +++ b/R/testing-mocks.R @@ -16,11 +16,13 @@ #' styler:::style_text("rlang::list2({{ x }} := 3)") #' @keywords internal #' @seealso set_line_break_around_curly_curly -style_text_without_curly_curly <- function(text, - ..., - style = tidyverse_style, - transformers = style(...), - include_roxygen_examples = TRUE) { +style_text_without_curly_curly <- function( + text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE +) { dots <- list(...) if ("strict" %in% names(dots)) { strict <- dots$strict @@ -28,8 +30,11 @@ style_text_without_curly_curly <- function(text, strict <- TRUE } transformers$line_break$set_line_break_around_curly_curly <- NULL - style_text(text, ..., - style = NULL, transformers = transformers, + style_text( + text, + ..., + style = NULL, + transformers = transformers, include_roxygen_examples = include_roxygen_examples ) } diff --git a/R/testing.R b/R/testing.R index bce9f8b3c..f15bc4b4e 100644 --- a/R/testing.R +++ b/R/testing.R @@ -17,11 +17,14 @@ #' function, every *-out.R file has just one in file. #' @inheritParams transform_and_check #' @keywords internal -test_collection <- function(test, sub_test = NULL, - dry = "off", - write_tree = FALSE, - transformer, - ...) { +test_collection <- function( + test, + sub_test = NULL, + dry = "off", + write_tree = FALSE, + transformer, + ... +) { path <- rprojroot::find_testthat_root_file(test) pattern <- paste0( @@ -35,7 +38,9 @@ test_collection <- function(test, sub_test = NULL, full.names = FALSE ) - if (length(in_names) < 1L) abort("no items to check") + if (length(in_names) < 1L) { + abort("no items to check") + } out_names <- construct_out(in_names) @@ -51,7 +56,8 @@ test_collection <- function(test, sub_test = NULL, out_trees <- file.path(tempdir(), construct_tree(in_names)) } - pwalk(list(in_items, out_items, in_names, out_names, out_trees), + pwalk( + list(in_items, out_items, in_names, out_names, out_trees), transform_and_check, transformer = transformer, dry = dry, @@ -98,17 +104,28 @@ construct_tree <- function(in_paths, suffix = "_tree") { #' @param out_tree Name of tree file if written out. #' @inheritParams transform_utf8 #' @keywords internal -transform_and_check <- function(in_item, out_item, - in_name = in_item, out_name = out_item, - transformer, dry, - write_tree = FALSE, - out_tree = "_tree", ...) { - if (write_tree) check_installed("data.tree") +transform_and_check <- function( + in_item, + out_item, + in_name = in_item, + out_name = out_item, + transformer, + dry, + write_tree = FALSE, + out_tree = "_tree", + ... +) { + if (write_tree) { + check_installed("data.tree") + } read_in <- read_utf8_bare(in_item) if (write_tree) { create_tree(read_in) %>% - utils::write.table(out_tree, - col.names = FALSE, row.names = FALSE, quote = FALSE, + utils::write.table( + out_tree, + col.names = FALSE, + row.names = FALSE, + quote = FALSE, fileEncoding = "UTF-8" ) } @@ -117,7 +134,9 @@ transform_and_check <- function(in_item, out_item, unclass() if (!file.exists(out_item)) { warn(paste( - "File", out_item, "does not exist. Creating it from transformation." + "File", + out_item, + "does not exist. Creating it from transformation." )) file.create(out_item) } @@ -168,7 +187,8 @@ style_empty <- function(text, base_indention = 0L) { indent_character = " ", NULL ) - transformed_text <- parse_transform_serialize_r(text, + transformed_text <- parse_transform_serialize_r( + text, transformers = transformers, base_indention = base_indention ) @@ -178,6 +198,7 @@ style_empty <- function(text, base_indention = 0L) { #' @describeIn test_transformer Transformations for indention based on operators #' @keywords internal style_op <- function(text, base_indention = 0L) { + # fmt: skip transformers <- list( # transformer functions initialize = default_style_guide_attributes, @@ -191,7 +212,8 @@ style_op <- function(text, base_indention = 0L) { NULL ) - transformed_text <- parse_transform_serialize_r(text, + transformed_text <- parse_transform_serialize_r( + text, transformers = transformers, base_indention = base_indention ) @@ -231,15 +253,25 @@ copy_to_tempdir <- function(path_perm = testthat_file()) { #' A scalar indicating the relative difference of the second compared to the #' first run. #' @keywords internal -n_times_faster_with_cache <- function(x1, x2 = x1, ..., - fun = styler::style_text, - n = 3L, - clear = "always") { +n_times_faster_with_cache <- function( + x1, + x2 = x1, + ..., + fun = styler::style_text, + n = 3L, + clear = "always" +) { rlang::arg_match0(clear, c("always", "final", "never", "all but last")) - out <- purrr::map(1L:n, n_times_faster_bench, - x1 = x1, x2 = x2, fun = fun, - ..., n = n, clear = clear + out <- purrr::map( + 1L:n, + n_times_faster_bench, + x1 = x1, + x2 = x2, + fun = fun, + ..., + n = n, + clear = clear ) out <- out %>% purrr::map_dbl( @@ -279,8 +311,7 @@ activate_testthat_cache <- purrr::partial(cache_activate, "testthat") #' @details #' * make styler quiet. #' @keywords internal -local_test_setup <- function(cache = FALSE, - .local_envir = parent.frame()) { +local_test_setup <- function(cache = FALSE, .local_envir = parent.frame()) { current_cache <- cache_info(format = "tabular") withr::local_options( list(styler.quiet = TRUE, R.cache.rootPath = tempfile()), @@ -317,17 +348,22 @@ test_transformers_drop <- function(transformers) { names(transformers) ) - purrr::walk2(transformers$transformers_drop, transformers[scopes], function(x, y) { - # all x must be in y. select the x that are not in y - diff <- setdiff(names(x), names(y)) - if (length(diff) > 0L) { - rlang::abort(paste( - "transformers_drop specifies exclusion rules for transformers that ", - "are not in the style guilde. Please add the rule to the style guide ", - "or remove the dropping rules:", toString(diff) - )) + purrr::walk2( + transformers$transformers_drop, + transformers[scopes], + function(x, y) { + # all x must be in y. select the x that are not in y + diff <- setdiff(names(x), names(y)) + if (length(diff) > 0L) { + rlang::abort(paste( + "transformers_drop specifies exclusion rules for transformers that ", + "are not in the style guilde. Please add the rule to the style guide ", + "or remove the dropping rules:", + toString(diff) + )) + } } - }) + ) } diff --git a/R/token-create.R b/R/token-create.R index d793b89f8..2c7bb4ad2 100644 --- a/R/token-create.R +++ b/R/token-create.R @@ -24,20 +24,22 @@ #' @param is_cached Whether the token is cached already. #' @family token creators #' @keywords internal -create_tokens <- function(tokens, - texts, - lag_newlines = 0L, - spaces = 0L, - pos_ids, - token_before = NA, - token_after = NA, - indention_ref_pos_ids = NA, - indents, - terminal = TRUE, - child = NULL, - stylerignore, - block = NA, - is_cached = FALSE) { +create_tokens <- function( + tokens, + texts, + lag_newlines = 0L, + spaces = 0L, + pos_ids, + token_before = NA, + token_after = NA, + indention_ref_pos_ids = NA, + indents, + terminal = TRUE, + child = NULL, + stylerignore, + block = NA, + is_cached = FALSE +) { len_text <- length(texts) new_styler_df( list( @@ -84,8 +86,10 @@ create_pos_ids <- function(pd, pos, by = 0.1, after = FALSE, n = 1L) { -1L } first <- find_start_pos_id(pd, pos, by, direction, after) - new_ids <- seq(first, - to = first + direction * (n - 1L) * by, by = by * direction + new_ids <- seq( + first, + to = first + direction * (n - 1L) * by, + by = by * direction ) validate_new_pos_ids(new_ids, after) new_ids @@ -101,12 +105,14 @@ create_pos_ids <- function(pd, pos, by = 0.1, after = FALSE, n = 1L) { #' nests. #' @inheritParams create_pos_ids #' @keywords internal -find_start_pos_id <- function(pd, - pos, - by, - direction, - after, - candidates = NULL) { +find_start_pos_id <- function( + pd, + pos, + by, + direction, + after, + candidates = NULL +) { candidates <- append(candidates, pd$pos_id[pos]) if (is.null(pd$child[[pos]])) { start_pos_idx <- if (after) { @@ -134,7 +140,6 @@ find_start_pos_id <- function(pd, } - #' Validate sequence of new position ids #' #' Ids created with `after = TRUE` can have `pos_id` values between x.0 and @@ -166,9 +171,11 @@ validate_new_pos_ids <- function(new_ids, after) { #' curly brace and before the closing curly brace. #' @param space_after How many spaces should be inserted after the closing brace. #' @keywords internal -wrap_expr_in_curly <- function(pd, - stretch_out = c(FALSE, FALSE), - space_after = 1L) { +wrap_expr_in_curly <- function( + pd, + stretch_out = c(FALSE, FALSE), + space_after = 1L +) { if (is_curly_expr(pd)) { return(pd) } @@ -176,7 +183,9 @@ wrap_expr_in_curly <- function(pd, pd$lag_newlines[1L] <- 1L } - opening <- create_tokens("'{'", "{", + opening <- create_tokens( + "'{'", + "{", pos_ids = create_pos_ids(pd, 1L, after = FALSE), spaces = 1L - as.integer(stretch_out[1L]), stylerignore = pd$stylerignore[1L], @@ -184,8 +193,10 @@ wrap_expr_in_curly <- function(pd, ) closing <- create_tokens( - "'}'", "}", - spaces = space_after, lag_newlines = as.integer(stretch_out[2L]), + "'}'", + "}", + spaces = space_after, + lag_newlines = as.integer(stretch_out[2L]), pos_ids = create_pos_ids(pd, nrow(pd), after = TRUE), stylerignore = pd$stylerignore[1L], indents = pd$indent[1L] diff --git a/R/token-define.R b/R/token-define.R index 64813fa1d..4c65886e0 100644 --- a/R/token-define.R +++ b/R/token-define.R @@ -1,4 +1,5 @@ # styler: off +# fmt: skip token <- rbind.data.frame( c("&", "logical", "AND"), c("&&", "logical", "AND2"), @@ -65,7 +66,9 @@ op_token <- c( logical_token, left_assignment_token, right_assignment_token, - "EQ_SUB", "ELSE", "IN", + "EQ_SUB", + "ELSE", + "IN", "EQ_FORMALS" ) diff --git a/R/transform-block.R b/R/transform-block.R index ba6ccd5f5..15e79e125 100644 --- a/R/transform-block.R +++ b/R/transform-block.R @@ -22,10 +22,12 @@ #' paste0(styler:::add_spaces(3), style_text(text_in), sep = "") #' ) #' @keywords internal -parse_transform_serialize_r_block <- function(pd_nested, - start_line, - transformers, - base_indention) { +parse_transform_serialize_r_block <- function( + pd_nested, + start_line, + transformers, + base_indention +) { if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) { transformed_pd <- apply_transformers(pd_nested, transformers) flattened_pd <- @@ -40,7 +42,10 @@ parse_transform_serialize_r_block <- function(pd_nested, ) is_on_newline <- flattened_pd$lag_newlines > 0L is_on_newline[1L] <- TRUE - flattened_pd$lag_spaces[is_on_newline] <- flattened_pd$lag_spaces[is_on_newline] + base_indention + flattened_pd$lag_spaces[is_on_newline] <- flattened_pd$lag_spaces[ + is_on_newline + ] + + base_indention serialized_transformed_text <- serialize_parse_data_flattened( flattened_pd, indent_character = transformers$indent_character @@ -92,7 +97,8 @@ parse_transform_serialize_r_block <- function(pd_nested, #' @param pd A top-level nest. #' @keywords internal cache_find_block <- function(pd) { - first_after_cache_state_switch <- pd$is_cached != lag(pd$is_cached, default = !pd$is_cached[1L]) + first_after_cache_state_switch <- pd$is_cached != + lag(pd$is_cached, default = !pd$is_cached[1L]) not_first_on_line <- find_blank_lines_to_next_expr(pd) == 0L invalid_turning_point_idx <- which( diff --git a/R/transform-code.R b/R/transform-code.R index 929ba8182..c7371ea9a 100644 --- a/R/transform-code.R +++ b/R/transform-code.R @@ -12,14 +12,18 @@ transform_code <- function(path, fun, ..., dry) { if (is_plain_r_file(path) || is_rprofile_file(path)) { transform_utf8(path, fun = fun, ..., dry = dry) } else if (is_rmd_file(path) || is_qmd_file(path)) { - transform_utf8(path, + transform_utf8( + path, fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"), - ..., dry = dry + ..., + dry = dry ) } else if (is_rnw_file(path)) { - transform_utf8(path, + transform_utf8( + path, fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"), - ..., dry = dry + ..., + dry = dry ) } else { cli::cli_abort("{.path {path}} is not a qmd, R, Rmd, or Rnw file.") @@ -40,7 +44,9 @@ transform_mixed <- function(lines, transformer_fun, filetype) { rlang::abort("{knitr} is required to process vignette files (Rmd, Rnw)") } chunks <- separate_chunks(lines, filetype) - chunks$r_chunks <- map(chunks$r_chunks, transform_mixed_non_empty, + chunks$r_chunks <- map( + chunks$r_chunks, + transform_mixed_non_empty, transformer_fun = transformer_fun ) map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0L))), c) %>% @@ -70,11 +76,14 @@ separate_chunks <- function(lines, filetype) { r_raw_chunks <- identify_raw_chunks(lines, filetype = filetype) r_chunks <- map2( - r_raw_chunks$starts, r_raw_chunks$ends, ~ lines[seq2(.x + 1L, .y - 1L)] + r_raw_chunks$starts, + r_raw_chunks$ends, + ~ lines[seq2(.x + 1L, .y - 1L)] ) text_chunks <- map2( - c(1L, r_raw_chunks$ends), c(r_raw_chunks$starts, length(lines)), + c(1L, r_raw_chunks$ends), + c(r_raw_chunks$starts, length(lines)), ~ lines[seq2(.x, .y)] ) list(r_chunks = r_chunks, text_chunks = text_chunks) @@ -93,9 +102,11 @@ separate_chunks <- function(lines, filetype) { #' @param engine_pattern A regular expression that must match the engine name. #' @keywords internal -identify_raw_chunks <- function(lines, - filetype, - engine_pattern = get_engine_pattern()) { +identify_raw_chunks <- function( + lines, + filetype, + engine_pattern = get_engine_pattern() +) { pattern <- get_knitr_pattern(filetype) if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) { abort("Unrecognized chunk pattern!") @@ -105,7 +116,8 @@ identify_raw_chunks <- function(lines, starts <- grep( "^[\t >]*```+\\s*\\{((r|webr-r|webr)( *[ ,].*)?)\\}\\s*$", lines, - perl = TRUE, ignore.case = TRUE + perl = TRUE, + ignore.case = TRUE ) ends <- grep("^[\t >]*```+\\s*$", lines, perl = TRUE) ends <- purrr::imap_int(starts, ~ ends[which(ends > .x)[1L]]) %>% @@ -121,8 +133,12 @@ identify_raw_chunks <- function(lines, } } - purrr::map2(starts, ends, finalize_raw_chunks, - filetype = filetype, lines = lines + purrr::map2( + starts, + ends, + finalize_raw_chunks, + filetype = filetype, + lines = lines ) %>% purrr::compact() %>% purrr::transpose() @@ -171,6 +187,7 @@ get_engine_pattern <- function() { #' @inheritParams separate_chunks #' @keywords internal get_knitr_pattern <- function(filetype) { + # fmt: skip switch(filetype, Rnw = knitr::all_patterns[["rnw"]], Rmd = knitr::all_patterns[["md"]] diff --git a/R/transform-files.R b/R/transform-files.R index 28dc79c50..c3eae60f3 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -11,13 +11,17 @@ #' styling whether or not it was actually changed (or would be changed when #' `dry` is not "off"). #' @keywords internal -transform_files <- function(files, - transformers, - include_roxygen_examples, - base_indention, - dry) { +transform_files <- function( + files, + transformers, + include_roxygen_examples, + base_indention, + dry +) { transformer <- make_transformer( - transformers, include_roxygen_examples, base_indention + transformers, + include_roxygen_examples, + base_indention ) max_char <- min(max(nchar(files), 0L), getOption("width")) len_files <- length(files) @@ -25,8 +29,12 @@ transform_files <- function(files, cat("Styling ", len_files, " files:\n") } - changed <- map_lgl(files, transform_file, - fun = transformer, max_char_path = max_char, dry = dry + changed <- map_lgl( + files, + transform_file, + fun = transformer, + max_char_path = max_char, + dry = dry ) communicate_summary(changed, max_char) communicate_warning(changed, transformers) @@ -45,21 +53,24 @@ transform_files <- function(files, #' @inheritParams transform_code #' @param ... Further arguments passed to [transform_utf8()]. #' @keywords internal -transform_file <- function(path, - fun, - max_char_path, - message_before = "", - message_after = " [DONE]", - message_after_if_changed = " *", - ..., - dry) { +transform_file <- function( + path, + fun, + max_char_path, + message_before = "", + message_after = " [DONE]", + message_after_if_changed = " *", + ..., + dry +) { char_after_path <- nchar(message_before) + nchar(path) + 1L max_char_after_message_path <- nchar(message_before) + max_char_path + 1L n_spaces_before_message_after <- max_char_after_message_path - char_after_path if (!getOption("styler.quiet", FALSE)) { cat( - message_before, path, + message_before, + path, rep_char(" ", max(0L, n_spaces_before_message_after)), append = FALSE ) @@ -93,10 +104,12 @@ transform_file <- function(path, #' examples. #' @inheritParams parse_transform_serialize_r #' @keywords internal -make_transformer <- function(transformers, - include_roxygen_examples, - base_indention, - warn_empty = TRUE) { +make_transformer <- function( + transformers, + include_roxygen_examples, + base_indention, + warn_empty = TRUE +) { force(transformers) assert_transformers(transformers) @@ -106,7 +119,8 @@ make_transformer <- function(transformers, if (should_use_cache) { use_cache <- is_cached( - text, transformers, + text, + transformers, cache_more_specs( include_roxygen_examples = include_roxygen_examples, base_indention = base_indention @@ -137,7 +151,8 @@ make_transformer <- function(transformers, if (should_use_cache) { cache_write( - transformed_code, transformers, + transformed_code, + transformers, cache_more_specs(include_roxygen_examples, base_indention) ) } @@ -172,9 +187,11 @@ make_transformer <- function(transformers, #' we style them in [style_roxygen_example_snippet()] using #' [parse_transform_serialize_r()]. #' @keywords internal -parse_transform_serialize_roxygen <- function(text, - transformers, - base_indention) { +parse_transform_serialize_roxygen <- function( + text, + transformers, + base_indention +) { roxygen_seqs <- identify_start_to_stop_of_roxygen_examples_from_text(text) if (length(roxygen_seqs) < 1L) { return(text) @@ -187,7 +204,9 @@ parse_transform_serialize_roxygen <- function(text, )) } split_segments <- split_roxygen_segments(text, unlist(roxygen_seqs)) - map_at(split_segments$separated, split_segments$selectors, + map_at( + split_segments$separated, + split_segments$selectors, style_roxygen_code_example, transformers = transformers, base_indention = base_indention @@ -239,13 +258,16 @@ split_roxygen_segments <- function(text, roxygen_examples) { #' @seealso [parse_transform_serialize_roxygen()] #' @keywords internal -parse_transform_serialize_r <- function(text, - transformers, - base_indention, - warn_empty = TRUE, - is_roxygen_code_example = FALSE) { +parse_transform_serialize_r <- function( + text, + transformers, + base_indention, + warn_empty = TRUE, + is_roxygen_code_example = FALSE +) { more_specs <- cache_more_specs( - include_roxygen_examples = TRUE, base_indention = base_indention + include_roxygen_examples = TRUE, + base_indention = base_indention ) text <- assert_text(text) @@ -275,7 +297,6 @@ parse_transform_serialize_r <- function(text, pd_blank[[i]] } - text_out[[i]] <- parse_transform_serialize_r_block( pd_split[[i]], start_line = start_line, @@ -287,7 +308,8 @@ parse_transform_serialize_r <- function(text, text_out <- unlist(text_out, use.names = FALSE) verify_roundtrip( - text, text_out, + text, + text_out, parsable_only = !parse_tree_must_be_identical(transformers) ) @@ -360,7 +382,9 @@ apply_transformers <- function(pd_nested, transformers) { transformed_updated_multi_line <- post_visit( pd_nested, c( - transformers$initialize, transformers$line_break, set_multi_line, + transformers$initialize, + transformers$line_break, + set_multi_line, if (length(transformers$line_break) != 0L) update_newlines ) ) @@ -381,7 +405,6 @@ apply_transformers <- function(pd_nested, transformers) { } - #' Check whether a round trip verification can be carried out #' #' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]), @@ -418,10 +441,13 @@ verify_roundtrip <- function(old_text, new_text, parsable_only = FALSE) { rlang::try_fetch( parse_safely(new_text), error = function(e) { - rlang::abort(paste0( - "Styling resulted in code that isn't parsable. This should not ", - "happen." - ), .internal = TRUE) + rlang::abort( + paste0( + "Styling resulted in code that isn't parsable. This should not ", + "happen." + ), + .internal = TRUE + ) } ) } else if (!expressions_are_identical(old_text, new_text)) { diff --git a/R/ui-caching.R b/R/ui-caching.R index 6713569e5..4c2f56163 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -109,11 +109,19 @@ cache_info <- function(cache_name = NULL, format = "both") { if (any(c("lucid", "both") == format)) { cat( - "Size:\t\t", tbl$size, " bytes (", tbl$n, " cached expressions)", - "\nLast modified:\t", as.character(tbl$last_modified), - "\nCreated:\t", as.character(tbl$created), - "\nLocation:\t", path_cache, - "\nActivated:\t", tbl$activated, + "Size:\t\t", + tbl$size, + " bytes (", + tbl$n, + " cached expressions)", + "\nLast modified:\t", + as.character(tbl$last_modified), + "\nCreated:\t", + as.character(tbl$created), + "\nLocation:\t", + path_cache, + "\nActivated:\t", + tbl$activated, "\n", sep = "" ) @@ -135,15 +143,20 @@ cache_info <- function(cache_name = NULL, format = "both") { #' #' @family cache managers #' @export -cache_activate <- function(cache_name = NULL, - verbose = !getOption("styler.quiet", FALSE)) { +cache_activate <- function( + cache_name = NULL, + verbose = !getOption("styler.quiet", FALSE) +) { options(styler.cache_name = cache_name %||% styler_version) path <- cache_find_path(cache_name) if (verbose) { cat( - "Using cache ", cache_get_name(), " at ", - path, ".\n", + "Using cache ", + cache_get_name(), + " at ", + path, + ".\n", sep = "" ) } diff --git a/R/ui-styling.R b/R/ui-styling.R index ac48da5eb..9d1e1e325 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -64,23 +64,35 @@ #' # don't write back and fail if input is not already styled #' style_pkg("/path/to/pkg/", dry = "fail") #' @export -style_pkg <- function(pkg = ".", - ..., - style = tidyverse_style, - transformers = style(...), - filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "qmd"), - exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R"), - exclude_dirs = c("packrat", "renv"), - include_roxygen_examples = TRUE, - base_indention = 0L, - dry = "off") { +style_pkg <- function( + pkg = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "qmd"), + exclude_files = c( + "R/RcppExports\\.R", + "R/cpp11\\.R", + "R/import-standalone.*\\.R" + ), + exclude_dirs = c("packrat", "renv"), + include_roxygen_examples = TRUE, + base_indention = 0L, + dry = "off" +) { pkg_root <- rprojroot::find_package_root_file(path = pkg) - changed <- withr::with_dir(pkg_root, prettify_pkg( - transformers, - filetype, exclude_files, exclude_dirs, include_roxygen_examples, - base_indention, - dry - )) + changed <- withr::with_dir( + pkg_root, + prettify_pkg( + transformers, + filetype, + exclude_files, + exclude_dirs, + include_roxygen_examples, + base_indention, + dry + ) + ) invisible(changed) } @@ -98,17 +110,23 @@ style_pkg <- function(pkg = ".", #' [style_dir()] and as these directories are anyways not styled. #' @inheritParams transform_files #' @keywords internal -prettify_pkg <- function(transformers, - filetype, - exclude_files, - exclude_dirs, - include_roxygen_examples, - base_indention, - dry) { +prettify_pkg <- function( + transformers, + filetype, + exclude_files, + exclude_dirs, + include_roxygen_examples, + base_indention, + dry +) { filetype_ <- set_and_assert_arg_filetype(filetype) r_files <- rprofile_files <- vignette_files <- readme <- NULL all_files <- list.files(".", recursive = TRUE, all.files = TRUE) - exclude_files <- grep(paste(exclude_files, collapse = "|"), all_files, value = TRUE) + exclude_files <- grep( + paste(exclude_files, collapse = "|"), + all_files, + value = TRUE + ) exclude_files <- set_arg_paths(exclude_files) exclude_files <- c( exclude_files, @@ -123,12 +141,14 @@ prettify_pkg <- function(transformers, if ("\\.rprofile" %in% filetype_) { rprofile_files <- dir_without_.( - path = ".", pattern = "^\\.rprofile$" + path = ".", + pattern = "^\\.rprofile$" ) } if ("\\.rmd" %in% filetype_) { vignette_files <- dir_without_.( - path = "vignettes", pattern = "\\.rmd$" + path = "vignettes", + pattern = "\\.rmd$" ) readme <- dir_without_.( path = ".", @@ -140,13 +160,15 @@ prettify_pkg <- function(transformers, vignette_files <- append( vignette_files, dir_without_.( - path = "vignettes", pattern = "\\.rmarkdown$" + path = "vignettes", + pattern = "\\.rmarkdown$" ) ) readme <- append( readme, dir_without_.( - path = ".", pattern = "^readme\\.rmarkdown$" + path = ".", + pattern = "^readme\\.rmarkdown$" ) ) } @@ -155,7 +177,8 @@ prettify_pkg <- function(transformers, vignette_files <- append( vignette_files, dir_without_.( - path = "vignettes", pattern = "\\.rnw$" + path = "vignettes", + pattern = "\\.rnw$" ) ) } @@ -174,7 +197,8 @@ prettify_pkg <- function(transformers, c(r_files, rprofile_files, vignette_files, readme), exclude_files ) - transform_files(files, + transform_files( + files, transformers = transformers, include_roxygen_examples = include_roxygen_examples, base_indention = base_indention, @@ -207,13 +231,16 @@ prettify_pkg <- function(transformers, #' # opt out with I() to only style specific levels #' style_text("a%>%b; a", scope = I("tokens")) #' @export -style_text <- function(text, - ..., - style = tidyverse_style, - transformers = style(...), - include_roxygen_examples = TRUE, - base_indention = 0L) { - transformer <- make_transformer(transformers, +style_text <- function( + text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE, + base_indention = 0L +) { + transformer <- make_transformer( + transformers, include_roxygen_examples = include_roxygen_examples, base_indention = base_indention ) @@ -246,22 +273,30 @@ style_text <- function(text, #' style_dir(style = tidyverse_style, strict = TRUE) #' style_dir(transformers = tidyverse_style(strict = TRUE)) #' @export -style_dir <- function(path = ".", - ..., - style = tidyverse_style, - transformers = style(...), - filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), - recursive = TRUE, - exclude_files = NULL, - exclude_dirs = c("packrat", "renv"), - include_roxygen_examples = TRUE, - base_indention = 0L, - dry = "off") { +style_dir <- function( + path = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), + recursive = TRUE, + exclude_files = NULL, + exclude_dirs = c("packrat", "renv"), + include_roxygen_examples = TRUE, + base_indention = 0L, + dry = "off" +) { changed <- withr::with_dir( - path, prettify_any( + path, + prettify_any( transformers, - filetype, recursive, exclude_files, exclude_dirs, - include_roxygen_examples, base_indention, dry + filetype, + recursive, + exclude_files, + exclude_dirs, + include_roxygen_examples, + base_indention, + dry ) ) invisible(changed) @@ -275,21 +310,26 @@ style_dir <- function(path = ".", #' @param recursive A logical value indicating whether or not files in #' subdirectories should be styled as well. #' @keywords internal -prettify_any <- function(transformers, - filetype, - recursive, - exclude_files, - exclude_dirs, - include_roxygen_examples, - base_indention = 0L, - dry) { +prettify_any <- function( + transformers, + filetype, + recursive, + exclude_files, + exclude_dirs, + include_roxygen_examples, + base_indention = 0L, + dry +) { exclude_files <- set_arg_paths(exclude_files) exclude_dirs <- exclude_dirs %>% list.dirs(recursive = TRUE, full.names = TRUE) %>% set_arg_paths() files_root <- dir( - path = ".", pattern = map_filetype_to_pattern(filetype), - ignore.case = TRUE, recursive = FALSE, all.files = TRUE + path = ".", + pattern = map_filetype_to_pattern(filetype), + ignore.case = TRUE, + recursive = FALSE, + all.files = TRUE ) if (recursive) { files_other <- list.dirs(full.names = FALSE, recursive = TRUE) %>% @@ -344,15 +384,18 @@ prettify_any <- function(transformers, #' unlink(file) #' @family stylers #' @export -style_file <- function(path, - ..., - style = tidyverse_style, - transformers = style(...), - include_roxygen_examples = TRUE, - base_indention = 0L, - dry = "off") { +style_file <- function( + path, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE, + base_indention = 0L, + dry = "off" +) { path <- set_arg_paths(path) - changed <- transform_files(path, + changed <- transform_files( + path, transformers = transformers, include_roxygen_examples = include_roxygen_examples, base_indention = base_indention, diff --git a/R/unindent.R b/R/unindent.R index 24cf1f2d5..83e56313b 100644 --- a/R/unindent.R +++ b/R/unindent.R @@ -27,7 +27,8 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) { non_candidates <- vec_slice(pd, -cand_ind) - candidates$child <- map(candidates$child, + candidates$child <- map( + candidates$child, unindent_child, unindent_by = abs(pd$indent[closing] - pd$indent[closing - 1L]) ) diff --git a/R/utils-cache.R b/R/utils-cache.R index 605975876..6f7945f19 100644 --- a/R/utils-cache.R +++ b/R/utils-cache.R @@ -22,10 +22,12 @@ hash_standardize <- function(text) { #' @param cache_dir The caching directory relative to the `.Rcache` root to #' look for a cached value. #' @keywords internal -is_cached <- function(text, - transformers, - more_specs, - cache_dir = get_cache_dir()) { +is_cached <- function( + text, + transformers, + more_specs, + cache_dir = get_cache_dir() +) { R.cache::generateCache( key = cache_make_key(text, transformers, more_specs), dirs = cache_dir @@ -153,9 +155,7 @@ cache_is_activated <- function(cache_name = NULL) { #' @param text A character vector with one or more expressions. #' @inheritParams cache_write #' @keywords internal -cache_by_expression <- function(text, - transformers, - more_specs) { +cache_by_expression <- function(text, transformers, more_specs) { expressions <- parse(text = text, keep.source = TRUE) %>% utils::getParseData(includeText = TRUE) if (env_current$any_stylerignore) { @@ -171,9 +171,15 @@ cache_by_expression <- function(text, # was removed via parse, same as it is in cache_by_expression) and add the # base indention. map( - expressions[expressions$parent == 0L & expressions$token != "COMMENT" & !expressions$stylerignore, "text"], + expressions[ + expressions$parent == 0L & + expressions$token != "COMMENT" & + !expressions$stylerignore, + "text" + ], cache_write, - transformers = transformers, more_specs + transformers = transformers, + more_specs ) } @@ -190,7 +196,10 @@ cache_write <- function(text, transformers, more_specs) { file.create() } -styler_version <- unlist(unname(read.dcf("DESCRIPTION")[, "Version"]), use.names = FALSE) +styler_version <- unlist( + unname(read.dcf("DESCRIPTION")[, "Version"]), + use.names = FALSE +) cache_get_name <- function() { getOption("styler.cache_name") @@ -212,8 +221,7 @@ get_cache_dir <- function(cache_name = cache_get_name()) { #' Syntactic sugar for creating more specs. This is useful when we want to add #' more arguments (because we can search for this function in the source code). #' @keywords internal -cache_more_specs <- function(include_roxygen_examples, - base_indention) { +cache_more_specs <- function(include_roxygen_examples, base_indention) { list( include_roxygen_examples = include_roxygen_examples, base_indention = base_indention, diff --git a/R/utils-navigate-nest.R b/R/utils-navigate-nest.R index 9aeccf109..16eb7a1b1 100644 --- a/R/utils-navigate-nest.R +++ b/R/utils-navigate-nest.R @@ -63,10 +63,12 @@ previous_non_comment <- function(pd, pos) { #' styler:::next_terminal(pd) #' } #' ) -next_terminal <- function(pd, - stack = FALSE, - vars = c("pos_id", "token", "text"), - tokens_exclude = NULL) { +next_terminal <- function( + pd, + stack = FALSE, + vars = c("pos_id", "token", "text"), + tokens_exclude = NULL +) { pd$position <- seq2(1L, nrow(pd)) pd <- vec_slice(pd, !(pd$token %in% tokens_exclude)) if (pd$terminal[1L]) { @@ -74,7 +76,9 @@ next_terminal <- function(pd, } else { current <- next_terminal( pd$child[[1L]], - stack = stack, vars = vars, tokens_exclude = tokens_exclude + stack = stack, + vars = vars, + tokens_exclude = tokens_exclude ) if (stack) { vec_rbind(pd[1L, c("position", vars)], current) diff --git a/R/utils.R b/R/utils.R index 4329915e0..b36414f1d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -70,11 +70,14 @@ re_match <- function(text, pattern) { #' @keywords internal convert_newlines_to_linebreaks <- function(text) { split <- strsplit(text, "\n", fixed = TRUE) - map(split, ~ if (identical(.x, character(0L))) { - "" - } else { - .x - }) %>% + map( + split, + ~ if (identical(.x, character(0L))) { + "" + } else { + .x + } + ) %>% unlist(use.names = FALSE) } diff --git a/R/vertical.R b/R/vertical.R index 8062185a1..e8376876b 100644 --- a/R/vertical.R +++ b/R/vertical.R @@ -17,9 +17,12 @@ construct_vertical <- function(x) { #' `prettycode::highlight()`. #' @param style Passed to `prettycode::highlight()`. #' @export -print.vertical <- function(x, ..., - colored = getOption("styler.colored_print.vertical"), - style = prettycode::default_style()) { +print.vertical <- function( + x, + ..., + colored = getOption("styler.colored_print.vertical"), + style = prettycode::default_style() +) { if (colored) { if (is_installed("prettycode")) { x <- prettycode::highlight(x, style = style) diff --git a/R/visit.R b/R/visit.R index 6fd0d5a97..9935981c7 100644 --- a/R/visit.R +++ b/R/visit.R @@ -120,19 +120,23 @@ visit_one <- function(pd_flat, funs) { #' @inherit context_towards_terminals #' @seealso context_towards_terminals visitors #' @keywords internal -context_to_terminals <- function(pd_nested, - outer_lag_newlines, - outer_indent, - outer_spaces, - outer_indention_refs) { +context_to_terminals <- function( + pd_nested, + outer_lag_newlines, + outer_indent, + outer_spaces, + outer_indention_refs +) { if (is.null(pd_nested)) { return() } pd_transformed <- context_towards_terminals( pd_nested, - outer_lag_newlines, outer_indent, - outer_spaces, outer_indention_refs + outer_lag_newlines, + outer_indent, + outer_spaces, + outer_indention_refs ) pd_transformed$child <- pmap( @@ -163,16 +167,19 @@ context_to_terminals <- function(pd_nested, #' @return An updated parse table. #' @seealso context_to_terminals #' @keywords internal -context_towards_terminals <- function(pd_nested, - outer_lag_newlines, - outer_indent, - outer_spaces, - outer_indention_refs) { - pd_nested$indent <- pd_nested$indent + ifelse( - is.na(pd_nested$indention_ref_pos_id), - outer_indent, - 0L - ) +context_towards_terminals <- function( + pd_nested, + outer_lag_newlines, + outer_indent, + outer_spaces, + outer_indention_refs +) { + pd_nested$indent <- pd_nested$indent + + ifelse( + is.na(pd_nested$indention_ref_pos_id), + outer_indent, + 0L + ) ref_pos_id_is_na <- !is.na(pd_nested$indention_ref_pos_id) pd_nested$indention_ref_pos_id[!ref_pos_id_is_na] <- outer_indention_refs pd_nested$lag_newlines[1L] <- pd_nested$lag_newlines[1L] + outer_lag_newlines @@ -263,7 +270,8 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) { #' @keywords internal choose_indention <- function(flattened_pd, use_raw_indention) { if (!use_raw_indention) { - flattened_pd$lag_spaces <- ifelse(flattened_pd$lag_newlines > 0L, + flattened_pd$lag_spaces <- ifelse( + flattened_pd$lag_newlines > 0L, flattened_pd$indent, flattened_pd$lag_spaces ) diff --git a/R/zzz.R b/R/zzz.R index 65bfd3e16..0bef99522 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -16,7 +16,9 @@ styler.test_dir_writable = TRUE ) toset <- !(names(op.styler) %in% names(op)) - if (any(toset)) options(op.styler[toset]) + if (any(toset)) { + options(op.styler[toset]) + } rlang::try_fetch( { ask_to_switch_to_non_default_cache_root() @@ -41,12 +43,16 @@ delete_if_cache_directory <- function(path) { if (getRversion() < package_version("4.0.0")) { return(FALSE) } - designated_cache_path <- normalizePath(tools::R_user_dir("R.cache", which = "cache"), mustWork = FALSE) + designated_cache_path <- normalizePath( + tools::R_user_dir("R.cache", which = "cache"), + mustWork = FALSE + ) is_in_tools_cache <- startsWith(path, designated_cache_path) temp_dir <- normalizePath(dirname(tempdir())) is_in_generic_cache <- startsWith(path, temp_dir) if (is_in_tools_cache || is_in_generic_cache) { - all_files <- list.files(path, + all_files <- list.files( + path, full.names = TRUE, recursive = TRUE, all.files = FALSE @@ -61,7 +67,9 @@ delete_if_cache_directory <- function(path) { ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) { - if (ask && stats::runif(1L) > 0.9 && is.null(getOption("styler.cache_root"))) { + if ( + ask && stats::runif(1L) > 0.9 && is.null(getOption("styler.cache_root")) + ) { ask_to_switch_to_non_default_cache_root_impl() options(styler.cache_root = "styler") } @@ -79,7 +87,8 @@ remove_old_cache_files <- function() { path_version_specific <- R.cache::getCachePath(c("styler", styler_version)) all_cached <- list.files( path_version_specific, - full.names = TRUE, recursive = TRUE + full.names = TRUE, + recursive = TRUE ) date_boundary <- Sys.time() - as.difftime(6L, units = "days") file.remove( diff --git a/man/alignment_drop_last_expr.Rd b/man/alignment_drop_last_expr.Rd index f67092d76..269b04993 100644 --- a/man/alignment_drop_last_expr.Rd +++ b/man/alignment_drop_last_expr.Rd @@ -12,7 +12,7 @@ an if, while or for statement or a function call. We don't call about that part, in fact it's important to remove it for alignment. See 'Examples'. } \examples{ -\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (FALSE) withAutoprint(\{ # examplesIf} call( x = 12, y = 3, diff --git a/man/cache_activate.Rd b/man/cache_activate.Rd index 5187937a0..9a4080afb 100644 --- a/man/cache_activate.Rd +++ b/man/cache_activate.Rd @@ -22,9 +22,9 @@ Helper functions to control the behavior of caching. Simple wrappers around \code{\link[base:options]{base::options()}}. } \seealso{ -Other cache managers: -\code{\link{cache_clear}()}, -\code{\link{cache_info}()}, +Other cache managers: +\code{\link[=cache_clear]{cache_clear()}}, +\code{\link[=cache_info]{cache_info()}}, \code{\link{caching}} } \concept{cache managers} diff --git a/man/cache_clear.Rd b/man/cache_clear.Rd index 3a97a3bff..cb99941d7 100644 --- a/man/cache_clear.Rd +++ b/man/cache_clear.Rd @@ -24,9 +24,9 @@ Each version of styler has its own cache by default, because styling is potentially different with different versions of styler. } \seealso{ -Other cache managers: -\code{\link{cache_activate}()}, -\code{\link{cache_info}()}, +Other cache managers: +\code{\link[=cache_activate]{cache_activate()}}, +\code{\link[=cache_info]{cache_info()}}, \code{\link{caching}} } \concept{cache managers} diff --git a/man/cache_info.Rd b/man/cache_info.Rd index 363336316..381bbaa94 100644 --- a/man/cache_info.Rd +++ b/man/cache_info.Rd @@ -22,9 +22,9 @@ empty file of size 0 bytes for every cached expression. The inode is excluded from this displayed size but negligible. } \seealso{ -Other cache managers: -\code{\link{cache_activate}()}, -\code{\link{cache_clear}()}, +Other cache managers: +\code{\link[=cache_activate]{cache_activate()}}, +\code{\link[=cache_clear]{cache_clear()}}, \code{\link{caching}} } \concept{cache managers} diff --git a/man/caching.Rd b/man/caching.Rd index 59a7db6cb..61f1c691d 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -67,9 +67,9 @@ This can often be set in config files of CI/CD tools, e.g. see the } \seealso{ -Other cache managers: -\code{\link{cache_activate}()}, -\code{\link{cache_clear}()}, -\code{\link{cache_info}()} +Other cache managers: +\code{\link[=cache_activate]{cache_activate()}}, +\code{\link[=cache_clear]{cache_clear()}}, +\code{\link[=cache_info]{cache_info()}} } \concept{cache managers} diff --git a/man/create_pos_ids.Rd b/man/create_pos_ids.Rd index 4498e19f0..ba72b2909 100644 --- a/man/create_pos_ids.Rd +++ b/man/create_pos_ids.Rd @@ -27,9 +27,9 @@ create one. The validation is done with \code{\link[=validate_new_pos_ids]{valid Create valid pos_ids if possible } \seealso{ -Other token creators: -\code{\link{create_tokens}()}, -\code{\link{validate_new_pos_ids}()} +Other token creators: +\code{\link[=create_tokens]{create_tokens()}}, +\code{\link[=validate_new_pos_ids]{validate_new_pos_ids()}} } \concept{token creators} \keyword{internal} diff --git a/man/create_tokens.Rd b/man/create_tokens.Rd index 24ef302e3..5a922d9c8 100644 --- a/man/create_tokens.Rd +++ b/man/create_tokens.Rd @@ -61,9 +61,9 @@ styler. Must take value from token before, can't have a default.} Creates a terminal token represented as (a row of) a parse table. } \seealso{ -Other token creators: -\code{\link{create_pos_ids}()}, -\code{\link{validate_new_pos_ids}()} +Other token creators: +\code{\link[=create_pos_ids]{create_pos_ids()}}, +\code{\link[=validate_new_pos_ids]{validate_new_pos_ids()}} } \concept{token creators} \keyword{internal} diff --git a/man/dir_without_..Rd b/man/dir_without_..Rd index 61c560c8e..67edec39e 100644 --- a/man/dir_without_..Rd +++ b/man/dir_without_..Rd @@ -9,7 +9,7 @@ dir_without_.(path, recursive = TRUE, ...) \arguments{ \item{path}{A path.} -\item{...}{Passed to \code{\link[base:list.files]{base::dir()}}.} +\item{...}{Passed to \code{\link[base:dir]{base::dir()}}.} } \description{ When using \code{dir()}, you can set \code{full.names = FALSE}, but then you can only diff --git a/man/env_current.Rd b/man/env_current.Rd index bdd407371..cddb9e51f 100644 --- a/man/env_current.Rd +++ b/man/env_current.Rd @@ -1,12 +1,8 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/environments.R -\docType{data} \name{env_current} \alias{env_current} \title{The elements that are added to this environment are:} -\format{ -An object of class \code{environment} of length 0. -} \usage{ env_current } diff --git a/man/next_non_comment.Rd b/man/next_non_comment.Rd index b66b1825c..a2806fb8a 100644 --- a/man/next_non_comment.Rd +++ b/man/next_non_comment.Rd @@ -26,8 +26,8 @@ previous_non_comment(child, 4L) next_non_comment(child, 2L) } \seealso{ -Other third-party style guide helpers: +Other third-party style guide helpers: \code{\link{pd_is}}, -\code{\link{scope_normalize}()} +\code{\link[=scope_normalize]{scope_normalize()}} } \concept{third-party style guide helpers} diff --git a/man/pd_is.Rd b/man/pd_is.Rd index d05b1b731..3f935feb6 100644 --- a/man/pd_is.Rd +++ b/man/pd_is.Rd @@ -112,8 +112,8 @@ is_asymmetric_tilde_expr(pd$child[[1]]$child[[3]]) } \seealso{ -Other third-party style guide helpers: -\code{\link{next_non_comment}()}, -\code{\link{scope_normalize}()} +Other third-party style guide helpers: +\code{\link[=next_non_comment]{next_non_comment()}}, +\code{\link[=scope_normalize]{scope_normalize()}} } \concept{third-party style guide helpers} diff --git a/man/scope_normalize.Rd b/man/scope_normalize.Rd index bd98878e0..8dd712010 100644 --- a/man/scope_normalize.Rd +++ b/man/scope_normalize.Rd @@ -25,8 +25,8 @@ scope_normalize(I("tokens")) scope_normalize(I(c("indention", "tokens"))) } \seealso{ -Other third-party style guide helpers: -\code{\link{next_non_comment}()}, +Other third-party style guide helpers: +\code{\link[=next_non_comment]{next_non_comment()}}, \code{\link{pd_is}} } \concept{third-party style guide helpers} diff --git a/man/set_line_break_before_curly_opening.Rd b/man/set_line_break_before_curly_opening.Rd index 8501984dd..4f60ca388 100644 --- a/man/set_line_break_before_curly_opening.Rd +++ b/man/set_line_break_before_curly_opening.Rd @@ -19,7 +19,7 @@ line trigger } } \examples{ -\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (FALSE) withAutoprint(\{ # examplesIf} tryCatch( { f(8) diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 62c80e264..084fd8f28 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -112,7 +112,7 @@ See section 'Warning' for a good strategy to apply styling safely. } \examples{ -\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (FALSE) withAutoprint(\{ # examplesIf} style_dir("path/to/dir", filetype = c("rmd", ".R")) # the following is identical (because of ... and defaults) @@ -123,10 +123,10 @@ style_dir(transformers = tidyverse_style(strict = TRUE)) \dontshow{\}) # examplesIf} } \seealso{ -Other stylers: -\code{\link{style_file}()}, -\code{\link{style_pkg}()}, -\code{\link{style_text}()}, +Other stylers: +\code{\link[=style_file]{style_file()}}, +\code{\link[=style_pkg]{style_pkg()}}, +\code{\link[=style_text]{style_text()}}, \code{\link{styler_addins}} } \concept{stylers} diff --git a/man/style_file.Rd b/man/style_file.Rd index 331abbdac..28cda32de 100644 --- a/man/style_file.Rd +++ b/man/style_file.Rd @@ -116,10 +116,10 @@ readLines(file) unlink(file) } \seealso{ -Other stylers: -\code{\link{style_dir}()}, -\code{\link{style_pkg}()}, -\code{\link{style_text}()}, +Other stylers: +\code{\link[=style_dir]{style_dir()}}, +\code{\link[=style_pkg]{style_pkg()}}, +\code{\link[=style_text]{style_text()}}, \code{\link{styler_addins}} } \concept{stylers} diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 637cbc9a7..9c6581765 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -110,7 +110,7 @@ styling whether or not it was actually changed (or would be changed when } \examples{ -\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (FALSE) withAutoprint(\{ # examplesIf} # the following is identical (because of ... and defaults) # but the first is most convenient: style_pkg(strict = TRUE) @@ -128,10 +128,10 @@ style_pkg("/path/to/pkg/", dry = "fail") \dontshow{\}) # examplesIf} } \seealso{ -Other stylers: -\code{\link{style_dir}()}, -\code{\link{style_file}()}, -\code{\link{style_text}()}, +Other stylers: +\code{\link[=style_dir]{style_dir()}}, +\code{\link[=style_file]{style_file()}}, +\code{\link[=style_text]{style_text()}}, \code{\link{styler_addins}} } \concept{stylers} diff --git a/man/style_text.Rd b/man/style_text.Rd index 0b79b6151..90dd4dbf8 100644 --- a/man/style_text.Rd +++ b/man/style_text.Rd @@ -59,10 +59,10 @@ style_text("a\%>\%b; a", scope = "tokens") style_text("a\%>\%b; a", scope = I("tokens")) } \seealso{ -Other stylers: -\code{\link{style_dir}()}, -\code{\link{style_file}()}, -\code{\link{style_pkg}()}, +Other stylers: +\code{\link[=style_dir]{style_dir()}}, +\code{\link[=style_file]{style_file()}}, +\code{\link[=style_pkg]{style_pkg()}}, \code{\link{styler_addins}} } \concept{stylers} diff --git a/man/styler-package.Rd b/man/styler-package.Rd index d69914d9e..fb6662ecc 100644 --- a/man/styler-package.Rd +++ b/man/styler-package.Rd @@ -40,6 +40,7 @@ Useful links: Authors: \itemize{ + \item Lorenz Walthert \email{lorenz.walthert@icloud.com} \item Kirill Müller \email{kirill@cynkra.com} (\href{https://orcid.org/0000-0002-1416-3412}{ORCID}) \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) } diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index 219dd4e8a..f9c2e8f7c 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -58,10 +58,10 @@ options( } } \seealso{ -Other stylers: -\code{\link{style_dir}()}, -\code{\link{style_file}()}, -\code{\link{style_pkg}()}, -\code{\link{style_text}()} +Other stylers: +\code{\link[=style_dir]{style_dir()}}, +\code{\link[=style_file]{style_file()}}, +\code{\link[=style_pkg]{style_pkg()}}, +\code{\link[=style_text]{style_text()}} } \concept{stylers} diff --git a/man/styler_options.Rd b/man/styler_options.Rd index 568af57cc..1ce549de0 100644 --- a/man/styler_options.Rd +++ b/man/styler_options.Rd @@ -18,7 +18,7 @@ The name of the style transformer to use in the addins. (default: \code{"styler::tidyverse_style()"}) \item \code{styler.cache_name}: character. The name of the styler cache to use. -(default: 1.11.0) +(default: 1.11.0.9001) \item \code{styler.cache_root}: character. The directory where the cache files are stored. For more, see \code{help("caching")}. (default: \code{NULL}) @@ -42,7 +42,7 @@ If \code{TRUE}, the package tests whether the directory is writable. } \examples{ -\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (FALSE) withAutoprint(\{ # examplesIf} getOption("styler.ignore_alignment") options( styler.ignore_alignment = TRUE, diff --git a/man/validate_new_pos_ids.Rd b/man/validate_new_pos_ids.Rd index f2b1e22a4..3f8ab0722 100644 --- a/man/validate_new_pos_ids.Rd +++ b/man/validate_new_pos_ids.Rd @@ -19,9 +19,9 @@ x.5 and ids created with \code{after = FALSE} can have \code{pos_id} values betw reference to create the new \code{pos_ids}. } \seealso{ -Other token creators: -\code{\link{create_pos_ids}()}, -\code{\link{create_tokens}()} +Other token creators: +\code{\link[=create_pos_ids]{create_pos_ids()}}, +\code{\link[=create_tokens]{create_tokens()}} } \concept{token creators} \keyword{internal} diff --git a/man/visit.Rd b/man/visit.Rd index f2a757fc9..4fc0dfc35 100644 --- a/man/visit.Rd +++ b/man/visit.Rd @@ -30,8 +30,8 @@ before applying the functions (meaning it first applies the functions to the innermost level of nesting first and then going outwards). } \seealso{ -Other visitors: -\code{\link{visit_one}()} +Other visitors: +\code{\link[=visit_one]{visit_one()}} } \concept{visitors} \keyword{internal} diff --git a/man/visit_one.Rd b/man/visit_one.Rd index 7a3bdce6b..8ed15c5e2 100644 --- a/man/visit_one.Rd +++ b/man/visit_one.Rd @@ -16,7 +16,7 @@ Uses \code{\link[=Reduce]{Reduce()}} to apply each function of \code{funs} seque \code{pd_flat}. } \seealso{ -Other visitors: +Other visitors: \code{\link{visit}} } \concept{visitors} diff --git a/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-in.R b/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-in.R index 81caa6da5..9e5375acb 100644 --- a/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-in.R +++ b/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-in.R @@ -9,6 +9,11 @@ switch( y = 3 ) +# keep it on first line +switch(x, + a = 2, + y = 3 +) switch( # x, diff --git a/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-out.R b/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-out.R index 4c108e249..8d03f919e 100644 --- a/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-out.R +++ b/tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-out.R @@ -3,11 +3,17 @@ call( 3 ) -switch(x, +switch( + x, a = 2, y = 3 ) +# keep it on first line +switch(x, + a = 2, + y = 3 +) switch( # x, @@ -16,18 +22,17 @@ switch( # ) -switch(x, +switch( + x, a = 2, # - y = 3 -) -switch(x, - a = 2, y = 3 ) -switch(x, + +switch( + x, a = 2, y = 3 ) @@ -37,14 +42,12 @@ switch(x, y = 3 ) -switch(x, - a = 2, - y = 3 -) # +switch(x, a = 2, y = 3) + +switch(x, a = 2, y = 3) # switch(x, - a = 2, - y = 3 # + a = 2, y = 3 # ) if_else(a, @@ -62,7 +65,8 @@ base::switch(f, y = 3 ) -base::switch(f, +base::switch( + f, x = 2, y = 3 ) diff --git a/tests/testthat/test-cache-high-level-api.R b/tests/testthat/test-cache-high-level-api.R index e645f8bc8..4240d7eec 100644 --- a/tests/testthat/test-cache-high-level-api.R +++ b/tests/testthat/test-cache-high-level-api.R @@ -134,7 +134,6 @@ test_that("speedup higher when cached roxygen example code is multiple expressio }) - test_that("no speedup when tranformer changes", { skip_on_cran() skip_on_covr() diff --git a/tests/testthat/test-indention_curly.R b/tests/testthat/test-indention_curly.R index 54b7821a9..95ca3367c 100644 --- a/tests/testthat/test-indention_curly.R +++ b/tests/testthat/test-indention_curly.R @@ -28,7 +28,6 @@ test_that("custom indention for curly braces is corretct ", { }) - test_that(paste( "complete styling via top-level api is correct", "(round, curly, spacing)" diff --git a/tests/testthat/test-indention_multiple.R b/tests/testthat/test-indention_multiple.R index aa1523bba..ebb12ecf1 100644 --- a/tests/testthat/test-indention_multiple.R +++ b/tests/testthat/test-indention_multiple.R @@ -27,7 +27,6 @@ test_that("multiple curly and round brackets don't cause extraindention", { }) - test_that("multiple curly and round brackets overall test", { expect_no_warning(test_collection("indention_multiple", "overall", diff --git a/tests/testthat/test-indention_round_brackets.R b/tests/testthat/test-indention_round_brackets.R index 9257810f1..a935676a8 100644 --- a/tests/testthat/test-indention_round_brackets.R +++ b/tests/testthat/test-indention_round_brackets.R @@ -17,8 +17,6 @@ test_that(paste("multi-line function call yields correct indention"), { ## ............................................................................ - - # Does NOT cover indention by operators such as +" test_that("arithmetic grouping with braces yields correctly indention", { diff --git a/tests/testthat/test-interaction-caching-stylerignore.R b/tests/testthat/test-interaction-caching-stylerignore.R index 255cb67a4..28b218d83 100644 --- a/tests/testthat/test-interaction-caching-stylerignore.R +++ b/tests/testthat/test-interaction-caching-stylerignore.R @@ -188,7 +188,6 @@ test_that("changing ignore markers invalidates cache", { }) - test_that("all expressions within a stylerignore sequence (whether cached or not) are put in the same block (low-level)", { transformers <- tidyverse_style() specs <- transformers$more_specs_style_guide diff --git a/tests/testthat/test-public_api-0.R b/tests/testthat/test-public_api-0.R index 70e90cbec..61d5485fd 100644 --- a/tests/testthat/test-public_api-0.R +++ b/tests/testthat/test-public_api-0.R @@ -27,7 +27,6 @@ test_that("styler can style package and exclude some sub-directories", { }) - test_that("styler can style package and exclude some directories and files", { capture_output(expect_true({ styled <- style_pkg(testthat_file("public-api", "xyzpackage"), diff --git a/tests/testthat/test-public_api-1.R b/tests/testthat/test-public_api-1.R index e7297d2dd..08bde5a6a 100644 --- a/tests/testthat/test-public_api-1.R +++ b/tests/testthat/test-public_api-1.R @@ -33,7 +33,6 @@ test_that("styler does not return error when there is no file to style", { }) - test_that("styler can style Rmd file", { expect_false({ out <- style_file( @@ -97,8 +96,6 @@ test_that("styler handles malformed Rmd file and invalid R code in chunk", { }) - - test_that("messages (via cat()) of style_file are correct", { for (encoding in ls_testable_encodings()) { withr::with_options( diff --git a/tests/testthat/test-public_api-2.R b/tests/testthat/test-public_api-2.R index 8606a4afd..1fbbbb635 100644 --- a/tests/testthat/test-public_api-2.R +++ b/tests/testthat/test-public_api-2.R @@ -32,7 +32,6 @@ test_that("styler can style .r and .rmd files only via style_dir()", { }) - test_that("styler can style R and Rmd files via style_pkg()", { msg <- capture_output( style_pkg(testthat_file("public-api", "xyzpackage-rmd"), diff --git a/tests/testthat/test-public_api-3.R b/tests/testthat/test-public_api-3.R index de74e8bfd..e0c832290 100644 --- a/tests/testthat/test-public_api-3.R +++ b/tests/testthat/test-public_api-3.R @@ -24,7 +24,6 @@ test_that("styler handles malformed Rnw file and invalid R code in chunk", { }) - test_that("styler can style R, Rmd and Rnw files via style_pkg()", { msg <- capture_output( style_pkg(testthat_file("public-api", "xyzpackage-rnw"), diff --git a/tests/testthat/test-token_adding_removing.R b/tests/testthat/test-token_adding_removing.R index 584f7f9b9..4dc94e0ca 100644 --- a/tests/testthat/test-token_adding_removing.R +++ b/tests/testthat/test-token_adding_removing.R @@ -36,7 +36,6 @@ test_that("braces only added to pipe if RHS is a symbol", { }) - test_that("No braces are added if conditional statement is within pipe", { expect_no_warning(test_collection("token_adding_removing", "else-pipe", transformer = style_text