Skip to content

Commit 0ec1076

Browse files
refactor: simplify for speed
1 parent 0272802 commit 0ec1076

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

R/rules-indention.R

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ unindent_function_declaration <- function(pd, indent_by = 2L) {
4747
is_single_indent_function_declaration <- function(pd, indent_by = 2L) {
4848
idx_paren_open <- which(pd$token == "'('")[1L]
4949
idx_paren_close <- which(pd$token == "')'")[1L]
50-
if (is.na(idx_paren_open) || is.na(idx_paren_close)) return(FALSE)
5150

5251
row_idx <- seq_len(nrow(pd))
53-
formals <- which(
54-
pd$token %in% c("SYMBOL_FORMALS", "SYMBOL_SUB") &
52+
is_formal <- (
53+
pd$token == "SYMBOL_FORMALS" &
5554
row_idx > idx_paren_open &
5655
row_idx < idx_paren_close
5756
)
58-
if (length(formals) == 0L) return(FALSE)
57+
if (!any(is_formal)) {
58+
return(FALSE)
59+
}
5960

60-
first_formal_idx <- formals[1L]
61+
first_formal_idx <- which(is_formal)[1L]
6162
# If authored with single indentation (first argument on new line),
6263
# return TRUE unless closing parenthesis shares the line (indicating hanging indentation).
6364
if (any(pd$lag_newlines[seq2(idx_paren_open + 1L, first_formal_idx)] > 0L)) {
@@ -66,20 +67,16 @@ is_single_indent_function_declaration <- function(pd, indent_by = 2L) {
6667

6768
# If first argument shares line with '(', check if subsequent arguments on new lines
6869
# have single indentation (<= 4 spaces) vs. hanging indentation (> 4 spaces).
69-
formals_nl <- which(
70-
pd$token %in% c("SYMBOL_FORMALS", "SYMBOL_SUB") &
71-
pd$lag_newlines > 0L &
72-
row_idx > idx_paren_open &
73-
row_idx < idx_paren_close
74-
)
75-
if (length(formals_nl) == 0L) return(FALSE)
70+
is_formal_nl <- is_formal & pd$lag_newlines > 0L
71+
if (!any(is_formal_nl)) {
72+
return(FALSE)
73+
}
7674

77-
first_nl_formal <- formals_nl[1L]
75+
first_nl_formal <- which(is_formal_nl)[1L]
7876
pd$spaces[first_nl_formal - 1L] <= 2L * indent_by
7977
}
8078

8179

82-
8380
#' @describeIn update_indention Indents *all* tokens after `token` - including
8481
#' the last token.
8582
#' @keywords internal

0 commit comments

Comments
 (0)