diff --git a/NAMESPACE b/NAMESPACE index 3b20ce190..11b8927ba 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -52,3 +52,6 @@ importFrom(rlang,is_installed) importFrom(rlang,seq2) importFrom(rlang,set_names) importFrom(rlang,warn) +importFrom(vctrs,vec_rbind) +importFrom(vctrs,vec_slice) +importFrom(vctrs,vec_split) diff --git a/R/compat-dplyr.R b/R/compat-dplyr.R index df336de69..340810003 100644 --- a/R/compat-dplyr.R +++ b/R/compat-dplyr.R @@ -13,41 +13,17 @@ lead <- function(x, n = 1L, default = NA) { arrange <- function(.data, ...) { ord <- eval(substitute(order(...)), .data, parent.frame()) - .data[ord, , drop = FALSE] + vec_slice(.data, ord) } arrange_pos_id <- function(data) { pos_id <- data$pos_id if (is.unsorted(pos_id)) { - data <- data[order(pos_id), , drop = FALSE] + data <- vec_slice(data, order(pos_id)) } data } -bind_rows <- function(x, y = NULL, ...) { - if (is.null(x) && is.null(y)) { - return(new_styler_df(list())) - } - if (is.null(x)) { - if (inherits(y, "data.frame")) { - return(y) - } - return(do.call(rbind.data.frame, x)) - } - if (is.null(y)) { - if (inherits(x, "data.frame")) { - return(x) - } - return(do.call(rbind.data.frame, x)) - } - if (NCOL(x) != NCOL(y)) { - for (nme in setdiff(names(x), names(y))) { - y[[nme]] <- NA - } - } - bind_rows(rbind.data.frame(x, y), ...) -} - filter <- function(.data, ...) { subset(.data, ...) } @@ -76,13 +52,8 @@ last <- function(x) { x[[length(x)]] } -slice <- function(.data, ...) { - .data[c(...), , drop = FALSE] -} - -# TODO: Use `purrr::map_dfr()` when it stops implicitly relying on `{dplyr}` -map_dfr <- function(.x, .f, ..., .id = NULL) { +map_dfr <- function(.x, .f, ...) { .f <- purrr::as_mapper(.f, ...) res <- map(.x, .f, ...) - bind_rows(res, .id = .id) + vec_rbind(!!!res) } diff --git a/R/compat-tidyr.R b/R/compat-tidyr.R index ae672a3fa..4606a186b 100644 --- a/R/compat-tidyr.R +++ b/R/compat-tidyr.R @@ -3,8 +3,8 @@ nest_ <- function(data, key_col, nest_cols = character()) { key_data <- data[[key_column]] key_levels <- unique(key_data) key_factor <- factor(key_data, levels = key_levels) - res <- list() - res[[key_column]] <- key_levels - res[[key_col]] <- split(data[, nest_cols], key_factor) - new_styler_df(res) + + res <- vec_split(data[, nest_cols], key_factor) + names(res) <- c(key_column, key_col) + res } diff --git a/R/detect-alignment-utils.R b/R/detect-alignment-utils.R index ce2596e6a..ead1d041e 100644 --- a/R/detect-alignment-utils.R +++ b/R/detect-alignment-utils.R @@ -15,7 +15,7 @@ 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)]] <- last[seq2(1L, nrow(last) - 1L), ] + pd_by_line[[length(pd_by_line)]] <- vec_slice(last, seq2(1L, nrow(last) - 1L)) pd_by_line } } @@ -29,7 +29,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line, #' @keywords internal alignment_drop_comments <- function(pd_by_line) { map(pd_by_line, function(x) { - out <- x[x$token != "COMMENT", ] + out <- vec_slice(x, x$token != "COMMENT") if (nrow(out) < 1L) { return(NULL) } else { @@ -62,7 +62,7 @@ alignment_drop_last_expr <- function(pds_by_line) { 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))] if (identical(last_two_lines, c("')'", "expr"))) { - pd_last_line <- pd_last_line[-nrow(pd_last_line), ] + pd_last_line <- vec_slice(pd_last_line, -nrow(pd_last_line)) } pds_by_line[[length(pds_by_line)]] <- pd_last_line pds_by_line @@ -141,7 +141,7 @@ alignment_serialize_line <- function(relevant_pd_by_line, column) { return(NULL) } between_commas <- seq2(max(1L, comma_idx[column - 1L]), comma_idx[column]) - relevant_pd_by_line <- relevant_pd_by_line[between_commas, ] + relevant_pd_by_line <- vec_slice(relevant_pd_by_line, between_commas) alignment_serialize(relevant_pd_by_line) } diff --git a/R/detect-alignment.R b/R/detect-alignment.R index 63b417bfe..970ab6ffd 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -46,7 +46,12 @@ token_is_on_aligned_line <- function(pd_flat) { # pos_id too expensive to construct in alignment_ensure_trailing_comma() pd_flat$lag_newlines <- pd_flat$pos_id <- NULL pd_flat$.lag_spaces <- lag(pd_flat$spaces) - pd_by_line <- split(pd_flat, line_idx) + pd_by_line_split <- vec_split(pd_flat, line_idx) + + # FIXME: Why are we using names here? + pd_by_line <- pd_by_line_split[[2L]] + names(pd_by_line) <- as.character(pd_by_line_split[[1L]]) + pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL if (length(pd_by_line) < 1L) { return(TRUE) diff --git a/R/indent.R b/R/indent.R index 7cc21d9f2..b6e12285b 100644 --- a/R/indent.R +++ b/R/indent.R @@ -179,7 +179,7 @@ needs_indention_one <- function(pd, potential_trigger_pos, before_first_break ) multi_line_token <- pd_is_multi_line( - pd[row_idx_between_trigger_and_line_break, ] + vec_slice(pd, row_idx_between_trigger_and_line_break) ) remaining_row_idx_between_trigger_and_line_break <- setdiff( row_idx_between_trigger_and_line_break, diff --git a/R/nest.R b/R/nest.R index a8055775b..e18a8c2e7 100644 --- a/R/nest.R +++ b/R/nest.R @@ -99,12 +99,12 @@ add_cache_block <- function(pd_nested) { shallowify <- function(pd) { if (cache_is_activated()) { order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)) - pd_parent_first <- pd[order, ] - pos_ids_to_keep <- pd_parent_first %>% - split(cumsum(pd_parent_first$parent == 0L)) %>% + pd_parent_first <- vec_slice(pd, order) + 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) - shallow <- pd[pd$pos_id %in% pos_ids_to_keep, ] + shallow <- vec_slice(pd, pd$pos_id %in% pos_ids_to_keep) shallow$terminal[shallow$is_cached] <- TRUE # all cached expressions need to be marked as terminals because to # [apply_stylerignore()], we rely on terminals only. @@ -335,10 +335,9 @@ nest_parse_data <- function(pd_flat) { return(pd_flat) } pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L)) - split_data <- split(pd_flat, pd_flat$internal) - child <- split_data$`FALSE` - internal <- split_data$`TRUE` + child <- vec_slice(pd_flat, !pd_flat$internal) + internal <- vec_slice(pd_flat, pd_flat$internal) internal$internal_child <- internal$child internal$child <- NULL @@ -367,14 +366,14 @@ nest_parse_data <- function(pd_flat) { #' the correct order. #' @param child A parse table or `NULL`. #' @param internal_child A parse table or `NULL`. -#' @details Essentially, this is a wrapper around [dplyr::bind_rows()], but -#' returns `NULL` if the result of [dplyr::bind_rows()] is a data frame with +#' @details Essentially, this is a wrapper around vctrs::vec_rbind()], but +#' returns `NULL` if the result of vctrs::vec_rbind()] is a data frame with #' zero rows. #' @keywords internal combine_children <- function(child, internal_child) { - bound <- bind_rows(child, internal_child) + bound <- vec_rbind(child, internal_child) if (nrow(bound) == 0L) { return(NULL) } - bound[order(bound$pos_id), ] + vec_slice(bound, order(bound$pos_id)) } diff --git a/R/parse.R b/R/parse.R index 44164fc4c..054b2006b 100644 --- a/R/parse.R +++ b/R/parse.R @@ -137,7 +137,7 @@ ensure_correct_txt <- function(pd, text) { if (!any(is_problematic_text)) { return(pd) } - problematic_text <- pd[is_problematic_text, ] + problematic_text <- vec_slice(pd, is_problematic_text) is_parent_of_problematic_string <- pd$id %in% problematic_text$parent is_unaffected_token <- !magrittr::or( @@ -167,10 +167,10 @@ ensure_correct_txt <- function(pd, text) { names(new_text), paste0(line_col_names(), "parent") ) - bind_rows( + vec_rbind( new_text[, names_to_keep], - pd[is_unaffected_token, ], - pd[is_parent_of_problematic_string, ] + vec_slice(pd, is_unaffected_token), + vec_slice(pd, is_parent_of_problematic_string) ) %>% arrange_pos_id() } diff --git a/R/reindent.R b/R/reindent.R index d479ba07c..fa78aa840 100644 --- a/R/reindent.R +++ b/R/reindent.R @@ -96,8 +96,8 @@ set_regex_indention <- function(flattened_pd, if (length(cond) < 1L) { return(flattened_pd) } - to_check <- flattened_pd[cond, ] - not_to_check <- flattened_pd[-cond, ] + to_check <- vec_slice(flattened_pd, cond) + not_to_check <- vec_slice(flattened_pd, -cond) } else { to_check <- flattened_pd not_to_check <- NULL @@ -108,6 +108,6 @@ set_regex_indention <- function(flattened_pd, flatten_int() to_check$lag_spaces[indices_to_force] <- target_indention - bind_rows(to_check, not_to_check) %>% + vec_rbind(to_check, not_to_check) %>% arrange_pos_id() } diff --git a/R/relevel.R b/R/relevel.R index 8d6a0ebff..4050faa56 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -85,8 +85,8 @@ flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) { #' @keywords internal bind_with_child <- function(pd_nested, pos) { pd_nested %>% - slice(-pos) %>% - bind_rows(pd_nested$child[[pos]]) %>% + vec_slice(-pos) %>% + vec_rbind(pd_nested$child[[pos]]) %>% arrange_pos_id() } @@ -178,8 +178,8 @@ relocate_eq_assign_nest <- function(pd) { idx_eq_assign <- which(pd$token == "EQ_ASSIGN") if (length(idx_eq_assign) > 0L) { block_id <- find_block_id(pd) - blocks <- split(pd, block_id) - pd <- map_dfr(blocks, relocate_eq_assign_one) + blocks <- vec_split(pd, block_id) + pd <- map_dfr(blocks[[2L]], relocate_eq_assign_one) } pd } @@ -217,7 +217,7 @@ relocate_eq_assign_one <- function(pd) { eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L) # initialize because wrap_expr_in_expr -> create_tokens -> requires it pd$indent <- 0L - eq_expr <- pd[eq_ind, ] %>% + eq_expr <- vec_slice(pd, eq_ind) %>% wrap_expr_in_expr() %>% add_line_col_to_wrapped_expr() %>% remove_attributes(c( @@ -227,8 +227,8 @@ relocate_eq_assign_one <- function(pd) { eq_expr$id <- NA eq_expr$parent <- NA pd$indent <- NULL - non_eq_expr <- pd[-eq_ind, ] - pd <- bind_rows(eq_expr, non_eq_expr) %>% + non_eq_expr <- vec_slice(pd, -eq_ind) + pd <- vec_rbind(eq_expr, non_eq_expr) %>% arrange_pos_id() pd } diff --git a/R/roxygen-examples.R b/R/roxygen-examples.R index 2d80514fc..242d422e1 100644 --- a/R/roxygen-examples.R +++ b/R/roxygen-examples.R @@ -8,9 +8,9 @@ #' @inheritSection parse_transform_serialize_roxygen Hierarchy #' @keywords internal style_roxygen_code_example <- function(example, transformers, base_indention) { - example <- split(example, cumsum(grepl("^#' *@examples", example))) + example <- vec_split(example, cumsum(grepl("^#' *@examples", example))) purrr::map( - example, style_roxygen_code_example_one, + example[[2L]], style_roxygen_code_example_one, transformers = transformers, base_indention = base_indention ) %>% flatten_chr() @@ -28,8 +28,8 @@ style_roxygen_code_example_one <- function(example_one, example_one <- example_one[example_one != ""] bare <- parse_roxygen(example_one) - one_dont <- split(bare$text, factor(cumsum(bare$text %in% dont_keywords()))) - unmasked <- map(one_dont, 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 ) %>% diff --git a/R/rules-indention.R b/R/rules-indention.R index 62616d654..7019ef7e6 100644 --- a/R/rules-indention.R +++ b/R/rules-indention.R @@ -40,7 +40,7 @@ unindent_fun_dec <- function(pd, indent_by = 2L) { #' @inheritParams tidyverse_style #' @keywords internal is_double_indent_function_declaration <- function(pd, indent_by = 2L) { - head_pd <- pd[-nrow(pd), ] + head_pd <- vec_slice(pd, -nrow(pd)) line_break_in_header <- which(head_pd$lag_newlines > 0L & head_pd$token == "SYMBOL_FORMALS") if (length(line_break_in_header) > 0L) { # indent results from applying the rules, spaces is the initial spaces diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index b3def4244..badff81eb 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -60,7 +60,7 @@ set_line_break_before_curly_opening <- function(pd) { if (length(line_break_to_set_idx) > 0L) { is_not_curly_curly <- map_chr( line_break_to_set_idx + 1L, - ~ next_terminal(pd[.x, ], vars = "token_after")$token_after + ~ 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])) { diff --git a/R/rules-tokens.R b/R/rules-tokens.R index 91ea86792..f36ba289a 100644 --- a/R/rules-tokens.R +++ b/R/rules-tokens.R @@ -12,7 +12,7 @@ resolve_semicolon <- function(pd) { return(pd) } pd$lag_newlines[lag(is_semicolon)] <- 1L - pd <- pd[!is_semicolon, ] + pd <- vec_slice(pd, !is_semicolon) pd } @@ -51,10 +51,7 @@ add_brackets_in_pipe_one <- function(pd, pos) { block = NA, is_cached = FALSE ) - pd$child[[next_non_comment]] <- bind_rows( - 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 @@ -102,7 +99,7 @@ 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] ) - next_terminal <- next_terminal(pd[to_be_wrapped_expr_with_child, ])$text + 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] @@ -159,7 +156,7 @@ wrap_subexpr_in_curly <- function(pd, to_be_wrapped_starts_with_comment <- pd$token[ind_to_be_wrapped[1L]] == "COMMENT" new_expr <- wrap_expr_in_curly( - pd[ind_to_be_wrapped, ], + vec_slice(pd, ind_to_be_wrapped), stretch_out = c(!to_be_wrapped_starts_with_comment, TRUE), space_after = space_after ) @@ -169,8 +166,8 @@ wrap_subexpr_in_curly <- function(pd, remove_attributes(c("token_before", "token_after")) pd %>% - slice(-ind_to_be_wrapped) %>% - bind_rows(new_expr_in_expr) %>% + vec_slice(-ind_to_be_wrapped) %>% + vec_rbind(new_expr_in_expr) %>% set_multi_line() %>% arrange_pos_id() } @@ -204,7 +201,7 @@ fix_quotes <- function(pd_flat) { return(pd_flat) } - pd_flat$text[str_const] <- map(pd_flat$text[str_const], fix_quotes_one) + pd_flat$text[str_const] <- map_chr(pd_flat$text[str_const], fix_quotes_one) pd_flat } diff --git a/R/styler-package.R b/R/styler-package.R index cca57954e..373a60140 100644 --- a/R/styler-package.R +++ b/R/styler-package.R @@ -18,14 +18,16 @@ #' style_text("a%>%b; a", scope = "tokens") "_PACKAGE" -## styler namespace: start +## usethis namespace: start #' -#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names -#' @importFrom purrr map map_lgl map_int map_chr map2 map2_chr map_at pmap pwalk -#' @importFrom purrr compact partial flatten flatten_int flatten_chr #' @importFrom magrittr "%>%" -#' -## styler namespace: end +#' @importFrom purrr compact partial flatten flatten_int flatten_chr +#' @importFrom purrr map map_lgl map_int map_chr map2 map2_chr map_at pmap pwalk +#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names +#' @importFrom vctrs vec_rbind +#' @importFrom vctrs vec_slice +#' @importFrom vctrs vec_split +## usethis namespace: end NULL diff --git a/R/stylerignore.R b/R/stylerignore.R index 2eb6cb38d..ce558dfd6 100644 --- a/R/stylerignore.R +++ b/R/stylerignore.R @@ -13,26 +13,29 @@ #' @keywords internal env_add_stylerignore <- function(pd_flat) { if (!env_current$any_stylerignore) { - env_current$stylerignore <- pd_flat[0L, ] + env_current$stylerignore <- vec_slice(pd_flat, 0L) return() } # the whole stylerignore sequence must be contained in one block. # this means the block can contain cached and uncached expressions. - pd_flat_temp <- pd_flat[pd_flat$terminal, ] %>% + 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] ) - pd_flat_temp$first_pos_id_in_segment <- split( + + pos_id_split <- vec_split( pd_flat_temp$pos_id, cumsum(is_stylerignore_switchpoint) - ) %>% + ) + + pd_flat_temp$first_pos_id_in_segment <- pos_id_split[[2L]] %>% map(~ rep(.x[1L], length(.x))) %>% unlist(use.names = FALSE) pd_flat_temp$lag_newlines <- pd_flat_temp$lag_newlines pd_flat_temp$lag_spaces <- lag(pd_flat_temp$spaces, default = 0L) is_terminal_to_ignore <- pd_flat_temp$terminal & pd_flat_temp$stylerignore - env_current$stylerignore <- pd_flat_temp[is_terminal_to_ignore, ] + env_current$stylerignore <- vec_slice(pd_flat_temp, is_terminal_to_ignore) } #' Adds the stylerignore column @@ -120,7 +123,7 @@ apply_stylerignore <- function(flattened_pd) { ) flattened_pd <- merge( - flattened_pd[!(to_ignore & not_first), ], + 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, sort = FALSE diff --git a/R/token-create.R b/R/token-create.R index 35317f7da..d793b89f8 100644 --- a/R/token-create.R +++ b/R/token-create.R @@ -191,6 +191,6 @@ wrap_expr_in_curly <- function(pd, indents = pd$indent[1L] ) - bind_rows(opening, pd, closing) %>% + vec_rbind(opening, pd, closing) %>% set_multi_line() } diff --git a/R/transform-block.R b/R/transform-block.R index fda7abd8e..ba6ccd5f5 100644 --- a/R/transform-block.R +++ b/R/transform-block.R @@ -28,7 +28,9 @@ parse_transform_serialize_r_block <- function(pd_nested, base_indention) { if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) { transformed_pd <- apply_transformers(pd_nested, transformers) - flattened_pd <- post_visit_one(transformed_pd, extract_terminals) %>% + flattened_pd <- + # Special transformer: returns a list of pd + vec_rbind(!!!post_visit_one(transformed_pd, extract_terminals)) %>% enrich_terminals(transformers$use_raw_indention) %>% apply_ref_indention() %>% set_regex_indention( diff --git a/R/transform-files.R b/R/transform-files.R index 2647f5d4b..f564b4632 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -216,7 +216,7 @@ split_roxygen_segments <- function(text, roxygen_examples) { all_lines <- seq2(1L, length(text)) active_segment <- as.integer(all_lines %in% roxygen_examples) segment_id <- cumsum(abs(c(0L, diff(active_segment)))) + 1L - separated <- split(text, factor(segment_id)) + separated <- vec_split(text, factor(segment_id))[[2L]] restyle_selector <- if (roxygen_examples[1L] == 1L) { odd_index } else { @@ -260,7 +260,7 @@ parse_transform_serialize_r <- function(text, ) strict <- transformers$more_specs_style_guide$strict %||% TRUE - pd_split <- unname(split(pd_nested, pd_nested$block)) + pd_split <- vec_split(pd_nested, pd_nested$block)[[2L]] pd_blank <- find_blank_lines_to_next_block(pd_nested) text_out <- vector("list", length(pd_split)) diff --git a/R/unindent.R b/R/unindent.R index 8596faebb..24cf1f2d5 100644 --- a/R/unindent.R +++ b/R/unindent.R @@ -23,16 +23,16 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) { return(pd) } - candidates <- pd[cand_ind, ] + candidates <- vec_slice(pd, cand_ind) - non_candidates <- pd[-cand_ind, ] + non_candidates <- vec_slice(pd, -cand_ind) candidates$child <- map(candidates$child, unindent_child, unindent_by = abs(pd$indent[closing] - pd$indent[closing - 1L]) ) - bind_rows(candidates, non_candidates) %>% + vec_rbind(candidates, non_candidates) %>% arrange_pos_id() } diff --git a/R/utils-navigate-nest.R b/R/utils-navigate-nest.R index b3fa38212..0ef6cf5da 100644 --- a/R/utils-navigate-nest.R +++ b/R/utils-navigate-nest.R @@ -68,7 +68,7 @@ next_terminal <- function(pd, vars = c("pos_id", "token", "text"), tokens_exclude = NULL) { pd$position <- seq2(1L, nrow(pd)) - pd <- pd[!(pd$token %in% tokens_exclude), ] + pd <- vec_slice(pd, !(pd$token %in% tokens_exclude)) if (pd$terminal[1L]) { pd[1L, c("position", vars)] } else { @@ -77,7 +77,7 @@ next_terminal <- function(pd, stack = stack, vars = vars, tokens_exclude = tokens_exclude ) if (stack) { - bind_rows(pd[1L, c("position", vars)], current) + vec_rbind(pd[1L, c("position", vars)], current) } else { current } diff --git a/R/utils.R b/R/utils.R index d5150e89a..e1a235469 100644 --- a/R/utils.R +++ b/R/utils.R @@ -14,7 +14,7 @@ styler_df <- function(...) { #' @keywords internal #' @noRd new_styler_df <- function(x) { - vctrs::new_data_frame(x) + vctrs::data_frame(!!!x) } #' Ensure there is one (and only one) blank line at the end of a vector diff --git a/R/visit.R b/R/visit.R index 7e6600ea3..1d048bbfa 100644 --- a/R/visit.R +++ b/R/visit.R @@ -188,12 +188,19 @@ context_towards_terminals <- function(pd_nested, #' @param pd_nested A nested parse table. #' @keywords internal extract_terminals <- function(pd_nested) { - bind_rows( - ifelse(pd_nested$terminal | pd_nested$is_cached, - split(pd_nested, seq_len(nrow(pd_nested))), - pd_nested$child - ) - ) + terminal <- pd_nested$terminal + is_cached <- pd_nested$is_cached + + child <- pd_nested$child + + for (i in seq_len(nrow(pd_nested))) { + if (terminal[[i]] || is_cached[[i]]) { + child[[i]] <- list(vec_slice(pd_nested, i)) + } + } + + # child is a list of data frame lists here + unlist(unname(child), recursive = FALSE) } #' Enrich flattened parse table @@ -224,8 +231,8 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) { flattened_pd$newlines <- lead(flattened_pd$lag_newlines, default = 0L) flattened_pd$nchar <- nchar(flattened_pd$text, type = "width") groups <- flattened_pd$line1 - flattened_pd <- flattened_pd %>% - split(groups) %>% + split_pd <- vec_split(flattened_pd, groups)[[2L]] + flattened_pd <- split_pd %>% map_dfr(function(.x) { .x$col2 <- cumsum(.x$nchar + .x$lag_spaces) .x diff --git a/inst/WORDLIST b/inst/WORDLIST index 32f09b4ec..0ff1db851 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -288,6 +288,7 @@ usethis utf Uwe vctrs +vec VignetteBuilder Visit'em walthert diff --git a/man/combine_children.Rd b/man/combine_children.Rd index fef5d4a3a..8e4f68a1e 100644 --- a/man/combine_children.Rd +++ b/man/combine_children.Rd @@ -16,8 +16,8 @@ Binds two parse tables together and arranges them so that the tokens are in the correct order. } \details{ -Essentially, this is a wrapper around \code{\link[dplyr:bind]{dplyr::bind_rows()}}, but -returns \code{NULL} if the result of \code{\link[dplyr:bind]{dplyr::bind_rows()}} is a data frame with +Essentially, this is a wrapper around vctrs::vec_rbind()], but +returns \code{NULL} if the result of vctrs::vec_rbind()] is a data frame with zero rows. } \keyword{internal} diff --git a/tests/testthat/test-create_token.R b/tests/testthat/test-create_token.R index 6fd22dd2d..c00892808 100644 --- a/tests/testthat/test-create_token.R +++ b/tests/testthat/test-create_token.R @@ -18,7 +18,7 @@ test_that("pos_id can be created", { pd <- create_tokens("XZY_TEST", "test", pos_ids = 3, stylerignore = FALSE, indents = 0) new_id <- create_pos_ids(pd, 1L, by = 0.4) expect_error( - bind_rows( + vec_rbind( create_tokens("XZY_TEST", "test", pos_ids = new_id, stylerignore = FALSE, indents = 0 @@ -36,7 +36,7 @@ test_that("unambiguous pos_id won't be created (down)", { stylerignore = FALSE, indents = 0 ) new_id <- create_pos_ids(pd, 1L, by = 0.4) - pd <- bind_rows( + pd <- vec_rbind( create_tokens("XZY_TEST", "test", pos_ids = new_id, stylerignore = FALSE, indents = 0 @@ -53,7 +53,7 @@ test_that("unambiguous pos_id won't be created (up)", { ) new_id <- create_pos_ids(pd, 1L, by = 0.4, after = TRUE) - pd <- bind_rows( + pd <- vec_rbind( create_tokens("XZY_TEST", "test", pos_ids = new_id, stylerignore = FALSE, indents = 0), pd ) diff --git a/tests/testthat/tests-cache-require-serial.R b/tests/testthat/tests-cache-require-serial.R index 1515181fb..96c861247 100644 --- a/tests/testthat/tests-cache-require-serial.R +++ b/tests/testthat/tests-cache-require-serial.R @@ -22,10 +22,10 @@ test_that("top-level test: Caches top-level expressions efficiently on style_tex skip_on_cran() skip_on_covr() expect_lt( - partially_cached_benchmark["elapsed"] * 2.4, + partially_cached_benchmark["elapsed"] * 1.5, not_cached_benchmark["elapsed"] ) - expect_lt(full_cached_benchmark["elapsed"] * 45, benchmark["elapsed"]) + expect_lt(full_cached_benchmark["elapsed"] * 35, benchmark["elapsed"]) }) @@ -37,5 +37,5 @@ test_that("roxygen code examples are written to cache as whole expressions bring # don't use full cache, only roxygen cache styled[1] <- "#' This is a nother text" second <- system.time(style_text(styled)) - expect_gt(first["elapsed"], 4 * second["elapsed"]) + expect_gt(first["elapsed"], second["elapsed"] * 2.5) })