Skip to content

Commit c3ad733

Browse files
committed
Prefer vec_split() over split()
1 parent 9510f3b commit c3ad733

File tree

10 files changed

+31
-22
lines changed

10 files changed

+31
-22
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ importFrom(rlang,set_names)
5454
importFrom(rlang,warn)
5555
importFrom(vctrs,vec_rbind)
5656
importFrom(vctrs,vec_slice)
57+
importFrom(vctrs,vec_split)

R/compat-tidyr.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ nest_ <- function(data, key_col, nest_cols = character()) {
33
key_data <- data[[key_column]]
44
key_levels <- unique(key_data)
55
key_factor <- factor(key_data, levels = key_levels)
6-
res <- list()
7-
res[[key_column]] <- key_levels
8-
res[[key_col]] <- split(data[, nest_cols], key_factor)
9-
new_styler_df(res)
6+
7+
res <- vec_split(data[, nest_cols], key_factor)
8+
names(res) <- c(key_column, key_col)
9+
res
1010
}

R/detect-alignment.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ token_is_on_aligned_line <- function(pd_flat) {
4646
# pos_id too expensive to construct in alignment_ensure_trailing_comma()
4747
pd_flat$lag_newlines <- pd_flat$pos_id <- NULL
4848
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
49-
pd_by_line <- split(pd_flat, line_idx)
49+
pd_by_line_split <- vec_split(pd_flat, line_idx)
50+
51+
# FIXME: Why are we using names here?
52+
pd_by_line <- pd_by_line_split[[2L]]
53+
names(pd_by_line) <- as.character(pd_by_line_split[[1L]])
54+
5055
pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL
5156
if (length(pd_by_line) < 1L) {
5257
return(TRUE)

R/nest.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ shallowify <- function(pd) {
100100
if (cache_is_activated()) {
101101
order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
102102
pd_parent_first <- vec_slice(pd, order)
103-
pos_ids_to_keep <- pd_parent_first %>%
104-
split(cumsum(pd_parent_first$parent == 0L)) %>%
103+
pd_parent_first_split <- vec_split(pd_parent_first, cumsum(pd_parent_first$parent == 0L))
104+
pos_ids_to_keep <- pd_parent_first_split[[2L]] %>%
105105
map(find_pos_id_to_keep) %>%
106106
unlist(use.names = FALSE)
107107
shallow <- vec_slice(pd, pd$pos_id %in% pos_ids_to_keep)
@@ -335,10 +335,9 @@ nest_parse_data <- function(pd_flat) {
335335
return(pd_flat)
336336
}
337337
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
338-
split_data <- split(pd_flat, pd_flat$internal)
339338

340-
child <- split_data$`FALSE`
341-
internal <- split_data$`TRUE`
339+
child <- vec_slice(pd_flat, !pd_flat$internal)
340+
internal <- vec_slice(pd_flat, pd_flat$internal)
342341

343342
internal$internal_child <- internal$child
344343
internal$child <- NULL

R/relevel.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ relocate_eq_assign_nest <- function(pd) {
178178
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
179179
if (length(idx_eq_assign) > 0L) {
180180
block_id <- find_block_id(pd)
181-
blocks <- split(pd, block_id)
182-
pd <- map_dfr(blocks, relocate_eq_assign_one)
181+
blocks <- vec_split(pd, block_id)
182+
pd <- map_dfr(blocks[[2L]], relocate_eq_assign_one)
183183
}
184184
pd
185185
}

R/roxygen-examples.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#' @inheritSection parse_transform_serialize_roxygen Hierarchy
99
#' @keywords internal
1010
style_roxygen_code_example <- function(example, transformers, base_indention) {
11-
example <- split(example, cumsum(grepl("^#' *@examples", example)))
11+
example <- vec_split(example, cumsum(grepl("^#' *@examples", example)))
1212
purrr::map(
13-
example, style_roxygen_code_example_one,
13+
example[[2L]], style_roxygen_code_example_one,
1414
transformers = transformers, base_indention = base_indention
1515
) %>%
1616
flatten_chr()
@@ -28,8 +28,8 @@ style_roxygen_code_example_one <- function(example_one,
2828
example_one <- example_one[example_one != ""]
2929

3030
bare <- parse_roxygen(example_one)
31-
one_dont <- split(bare$text, factor(cumsum(bare$text %in% dont_keywords())))
32-
unmasked <- map(one_dont, style_roxygen_code_example_segment,
31+
one_dont <- vec_split(bare$text, factor(cumsum(bare$text %in% dont_keywords())))
32+
unmasked <- map(one_dont[[2L]], style_roxygen_code_example_segment,
3333
transformers = transformers,
3434
base_indention = base_indention
3535
) %>%

R/styler-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names
2727
#' @importFrom vctrs vec_rbind
2828
#' @importFrom vctrs vec_slice
29+
#' @importFrom vctrs vec_split
2930
## usethis namespace: end
3031
NULL
3132

R/stylerignore.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ env_add_stylerignore <- function(pd_flat) {
2424
pd_flat_temp$stylerignore,
2525
default = pd_flat_temp$stylerignore[1L]
2626
)
27-
pd_flat_temp$first_pos_id_in_segment <- split(
27+
28+
pos_id_split <- vec_split(
2829
pd_flat_temp$pos_id, cumsum(is_stylerignore_switchpoint)
29-
) %>%
30+
)
31+
32+
pd_flat_temp$first_pos_id_in_segment <- pos_id_split[[2L]] %>%
3033
map(~ rep(.x[1L], length(.x))) %>%
3134
unlist(use.names = FALSE)
3235
pd_flat_temp$lag_newlines <- pd_flat_temp$lag_newlines

R/transform-files.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ split_roxygen_segments <- function(text, roxygen_examples) {
216216
all_lines <- seq2(1L, length(text))
217217
active_segment <- as.integer(all_lines %in% roxygen_examples)
218218
segment_id <- cumsum(abs(c(0L, diff(active_segment)))) + 1L
219-
separated <- split(text, factor(segment_id))
219+
separated <- vec_split(text, factor(segment_id))[[2L]]
220220
restyle_selector <- if (roxygen_examples[1L] == 1L) {
221221
odd_index
222222
} else {
@@ -260,7 +260,7 @@ parse_transform_serialize_r <- function(text,
260260
)
261261

262262
strict <- transformers$more_specs_style_guide$strict %||% TRUE
263-
pd_split <- unname(split(pd_nested, pd_nested$block))
263+
pd_split <- vec_split(pd_nested, pd_nested$block)[[2L]]
264264
pd_blank <- find_blank_lines_to_next_block(pd_nested)
265265

266266
text_out <- vector("list", length(pd_split))

R/visit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) {
231231
flattened_pd$newlines <- lead(flattened_pd$lag_newlines, default = 0L)
232232
flattened_pd$nchar <- nchar(flattened_pd$text, type = "width")
233233
groups <- flattened_pd$line1
234-
flattened_pd <- flattened_pd %>%
235-
split(groups) %>%
234+
split_pd <- vec_split(flattened_pd, groups)[[2L]]
235+
flattened_pd <- split_pd %>%
236236
map_dfr(function(.x) {
237237
.x$col2 <- cumsum(.x$nchar + .x$lag_spaces)
238238
.x

0 commit comments

Comments
 (0)