Skip to content

Commit 9510f3b

Browse files
committed
Prefer vec_slice() over [
1 parent 3b08d35 commit 9510f3b

15 files changed

+30
-28
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ importFrom(rlang,seq2)
5353
importFrom(rlang,set_names)
5454
importFrom(rlang,warn)
5555
importFrom(vctrs,vec_rbind)
56+
importFrom(vctrs,vec_slice)

R/detect-alignment-utils.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
1515
pd_by_line[-length(pd_by_line)]
1616
} else {
1717
# only drop last elment of last line
18-
pd_by_line[[length(pd_by_line)]] <- last[seq2(1L, nrow(last) - 1L), ]
18+
pd_by_line[[length(pd_by_line)]] <- vec_slice(last, seq2(1L, nrow(last) - 1L))
1919
pd_by_line
2020
}
2121
}
@@ -29,7 +29,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
2929
#' @keywords internal
3030
alignment_drop_comments <- function(pd_by_line) {
3131
map(pd_by_line, function(x) {
32-
out <- x[x$token != "COMMENT", ]
32+
out <- vec_slice(x, x$token != "COMMENT")
3333
if (nrow(out) < 1L) {
3434
return(NULL)
3535
} else {
@@ -62,7 +62,7 @@ alignment_drop_last_expr <- function(pds_by_line) {
6262
pd_last_line <- pds_by_line[[length(pds_by_line)]]
6363
last_two_lines <- pd_last_line$token[c(nrow(pd_last_line) - 1L, nrow(pd_last_line))]
6464
if (identical(last_two_lines, c("')'", "expr"))) {
65-
pd_last_line <- pd_last_line[-nrow(pd_last_line), ]
65+
pd_last_line <- vec_slice(pd_last_line, -nrow(pd_last_line))
6666
}
6767
pds_by_line[[length(pds_by_line)]] <- pd_last_line
6868
pds_by_line
@@ -141,7 +141,7 @@ alignment_serialize_line <- function(relevant_pd_by_line, column) {
141141
return(NULL)
142142
}
143143
between_commas <- seq2(max(1L, comma_idx[column - 1L]), comma_idx[column])
144-
relevant_pd_by_line <- relevant_pd_by_line[between_commas, ]
144+
relevant_pd_by_line <- vec_slice(relevant_pd_by_line, between_commas)
145145
alignment_serialize(relevant_pd_by_line)
146146
}
147147

R/indent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ needs_indention_one <- function(pd,
179179
potential_trigger_pos, before_first_break
180180
)
181181
multi_line_token <- pd_is_multi_line(
182-
pd[row_idx_between_trigger_and_line_break, ]
182+
vec_slice(pd, row_idx_between_trigger_and_line_break)
183183
)
184184
remaining_row_idx_between_trigger_and_line_break <- setdiff(
185185
row_idx_between_trigger_and_line_break,

R/nest.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ add_cache_block <- function(pd_nested) {
9999
shallowify <- function(pd) {
100100
if (cache_is_activated()) {
101101
order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
102-
pd_parent_first <- pd[order, ]
102+
pd_parent_first <- vec_slice(pd, order)
103103
pos_ids_to_keep <- pd_parent_first %>%
104104
split(cumsum(pd_parent_first$parent == 0L)) %>%
105105
map(find_pos_id_to_keep) %>%
106106
unlist(use.names = FALSE)
107-
shallow <- pd[pd$pos_id %in% pos_ids_to_keep, ]
107+
shallow <- vec_slice(pd, pd$pos_id %in% pos_ids_to_keep)
108108
shallow$terminal[shallow$is_cached] <- TRUE
109109
# all cached expressions need to be marked as terminals because to
110110
# [apply_stylerignore()], we rely on terminals only.
@@ -376,5 +376,5 @@ combine_children <- function(child, internal_child) {
376376
if (nrow(bound) == 0L) {
377377
return(NULL)
378378
}
379-
bound[order(bound$pos_id), ]
379+
vec_slice(bound, order(bound$pos_id))
380380
}

R/parse.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ ensure_correct_txt <- function(pd, text) {
137137
if (!any(is_problematic_text)) {
138138
return(pd)
139139
}
140-
problematic_text <- pd[is_problematic_text, ]
140+
problematic_text <- vec_slice(pd, is_problematic_text)
141141
is_parent_of_problematic_string <- pd$id %in% problematic_text$parent
142142

143143
is_unaffected_token <- !magrittr::or(
@@ -169,8 +169,8 @@ ensure_correct_txt <- function(pd, text) {
169169
)
170170
vec_rbind(
171171
new_text[, names_to_keep],
172-
pd[is_unaffected_token, ],
173-
pd[is_parent_of_problematic_string, ]
172+
vec_slice(pd, is_unaffected_token),
173+
vec_slice(pd, is_parent_of_problematic_string)
174174
) %>%
175175
arrange_pos_id()
176176
}

R/reindent.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ set_regex_indention <- function(flattened_pd,
9696
if (length(cond) < 1L) {
9797
return(flattened_pd)
9898
}
99-
to_check <- flattened_pd[cond, ]
100-
not_to_check <- flattened_pd[-cond, ]
99+
to_check <- vec_slice(flattened_pd, cond)
100+
not_to_check <- vec_slice(flattened_pd, -cond)
101101
} else {
102102
to_check <- flattened_pd
103103
not_to_check <- NULL

R/relevel.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ relocate_eq_assign_one <- function(pd) {
217217
eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L)
218218
# initialize because wrap_expr_in_expr -> create_tokens -> requires it
219219
pd$indent <- 0L
220-
eq_expr <- pd[eq_ind, ] %>%
220+
eq_expr <- vec_slice(pd, eq_ind) %>%
221221
wrap_expr_in_expr() %>%
222222
add_line_col_to_wrapped_expr() %>%
223223
remove_attributes(c(
@@ -227,7 +227,7 @@ relocate_eq_assign_one <- function(pd) {
227227
eq_expr$id <- NA
228228
eq_expr$parent <- NA
229229
pd$indent <- NULL
230-
non_eq_expr <- pd[-eq_ind, ]
230+
non_eq_expr <- vec_slice(pd, -eq_ind)
231231
pd <- vec_rbind(eq_expr, non_eq_expr) %>%
232232
arrange_pos_id()
233233
pd

R/rules-indention.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ unindent_fun_dec <- function(pd, indent_by = 2L) {
4040
#' @inheritParams tidyverse_style
4141
#' @keywords internal
4242
is_double_indent_function_declaration <- function(pd, indent_by = 2L) {
43-
head_pd <- pd[-nrow(pd), ]
43+
head_pd <- vec_slice(pd, -nrow(pd))
4444
line_break_in_header <- which(head_pd$lag_newlines > 0L & head_pd$token == "SYMBOL_FORMALS")
4545
if (length(line_break_in_header) > 0L) {
4646
# indent results from applying the rules, spaces is the initial spaces

R/rules-line-breaks.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ set_line_break_before_curly_opening <- function(pd) {
6060
if (length(line_break_to_set_idx) > 0L) {
6161
is_not_curly_curly <- map_chr(
6262
line_break_to_set_idx + 1L,
63-
~ next_terminal(pd[.x, ], vars = "token_after")$token_after
63+
~ next_terminal(vec_slice(pd, .x), vars = "token_after")$token_after
6464
) != "'{'"
6565
last_expr_idx <- max(which(pd$token == "expr"))
6666
is_last_expr <- if (any(c("IF", "WHILE") == pd$token[1L])) {

R/rules-tokens.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resolve_semicolon <- function(pd) {
1212
return(pd)
1313
}
1414
pd$lag_newlines[lag(is_semicolon)] <- 1L
15-
pd <- pd[!is_semicolon, ]
15+
pd <- vec_slice(pd, !is_semicolon)
1616
pd
1717
}
1818

@@ -99,7 +99,7 @@ wrap_multiline_curly <- function(pd, indent_by, key_token, space_after = 1L) {
9999
to_be_wrapped_expr_with_child <- next_non_comment(
100100
pd, which(pd$token == key_token)[1L]
101101
)
102-
next_terminal <- next_terminal(pd[to_be_wrapped_expr_with_child, ])$text
102+
next_terminal <- next_terminal(vec_slice(pd, to_be_wrapped_expr_with_child))$text
103103
requires_braces <- if_for_while_part_requires_braces(pd, key_token) && !any(pd$stylerignore)
104104
if (requires_braces || next_terminal == "return") {
105105
closing_brace_ind <- which(pd$token == key_token)[1L]
@@ -156,7 +156,7 @@ wrap_subexpr_in_curly <- function(pd,
156156
to_be_wrapped_starts_with_comment <-
157157
pd$token[ind_to_be_wrapped[1L]] == "COMMENT"
158158
new_expr <- wrap_expr_in_curly(
159-
pd[ind_to_be_wrapped, ],
159+
vec_slice(pd, ind_to_be_wrapped),
160160
stretch_out = c(!to_be_wrapped_starts_with_comment, TRUE),
161161
space_after = space_after
162162
)

0 commit comments

Comments
 (0)