Skip to content

Commit 08da821

Browse files
Use more efficient match() alternative (#1001)
* start with a couple * Update indent.R * Update rules-line-breaks.R * Update rules-line-breaks.R
1 parent 38f0a3b commit 08da821

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

R/detect-alignment-utils.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ alignment_ensure_trailing_comma <- function(pd_by_line) {
111111
#' @keywords internal
112112
alignment_col1_all_named <- function(relevant_pd_by_line) {
113113
map_lgl(relevant_pd_by_line, function(x) {
114-
if (nrow(x) < 3) {
114+
if (nrow(x) < 3L) {
115115
return(FALSE)
116116
}
117117
x$token[3] == "expr" &&
118-
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") &&
119-
x$token[2] %in% c(
120-
"EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
121-
)
118+
any(c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") == x$token[1]) &&
119+
any(c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") == x$token[2])
122120
}) %>%
123121
all()
124122
}

R/indent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) {
3333
#' @keywords internal
3434
indent_without_paren_if_else <- function(pd, indent_by) {
3535
expr_after_if <- next_non_comment(pd, which(pd$token == "')'")[1])
36-
is_if <- pd$token[1] %in% "IF"
36+
is_if <- pd$token[1] == "IF"
3737
if (!is_if) {
3838
return(pd)
3939
}

R/rules-line-breaks.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ set_line_break_before_curly_opening <- function(pd) {
6565
~ next_terminal(pd[.x, ], vars = "token_after")$token_after
6666
) != "'{'"
6767
last_expr_idx <- max(which(pd$token == "expr"))
68-
is_last_expr <- ifelse(pd$token[1] %in% c("IF", "WHILE"),
68+
is_last_expr <- ifelse(any(c("IF", "WHILE") == pd$token[1]),
6969
# rule not applicable for if and while
7070
TRUE, (line_break_to_set_idx + 1L) == last_expr_idx
7171
)
7272

73-
no_line_break_before_curly_idx <- pd$token[line_break_to_set_idx] %in% "EQ_SUB"
73+
no_line_break_before_curly_idx <- any(pd$token[line_break_to_set_idx] == "EQ_SUB")
7474
linebreak_before_curly <- ifelse(is_function_call(pd),
7575
# if in function call and has pipe, it is not recognized as function call
7676
# and goes to else case
@@ -85,7 +85,7 @@ set_line_break_before_curly_opening <- function(pd) {
8585
no_line_break_before_curly_idx
8686
)
8787
is_not_curly_curly_idx <- line_break_to_set_idx[should_be_on_same_line]
88-
pd$lag_newlines[1 + is_not_curly_curly_idx] <- 0L
88+
pd$lag_newlines[1L + is_not_curly_curly_idx] <- 0L
8989

9090

9191
# other cases: line breaks
@@ -99,10 +99,10 @@ set_line_break_before_curly_opening <- function(pd) {
9999
]
100100
if (is_function_dec(pd)) {
101101
should_not_be_on_same_line_idx <- setdiff(
102-
1 + should_not_be_on_same_line_idx, nrow(pd)
102+
1L + should_not_be_on_same_line_idx, nrow(pd)
103103
)
104104
} else {
105-
should_not_be_on_same_line_idx <- 1 + should_not_be_on_same_line_idx
105+
should_not_be_on_same_line_idx <- 1L + should_not_be_on_same_line_idx
106106
}
107107
pd$lag_newlines[should_not_be_on_same_line_idx] <- 1L
108108

@@ -150,7 +150,7 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
150150
}
151151

152152
style_line_break_around_curly <- function(strict, pd) {
153-
if (is_curly_expr(pd) && nrow(pd) > 2) {
153+
if (is_curly_expr(pd) && nrow(pd) > 2L) {
154154
closing_before <- pd$token == "'}'"
155155
opening_before <- (pd$token == "'{'")
156156
to_break <- lag(opening_before, default = FALSE) | closing_before

R/ui-caching.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ cache_info <- function(cache_name = NULL, format = "both") {
8585
location = path_cache,
8686
activated = cache_is_activated(cache_name)
8787
)
88-
if (format %in% c("lucid", "both")) {
88+
if (any(c("lucid", "both") == format)) {
8989
cat(
9090
"Size:\t\t", tbl$size, " bytes (", tbl$n, " cached expressions)",
9191
"\nLast modified:\t", as.character(tbl$last_modified),

0 commit comments

Comments
 (0)