Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ style_active_file <- function() {
abort("Can only style .R, .Rmd and .Rnw files.")
}
rstudioapi::modifyRange(
c(1, 1, length(context$contents) + 1, 1),
c(1L, 1L, length(context$contents) + 1L, 1L),
paste0(ensure_last_n_empty(out), collapse = "\n"),
id = context$id
)
Expand Down Expand Up @@ -134,7 +134,7 @@ style_selection <- function() {
context$selection[[1]]$range,
paste0(c(
out,
if (context$selection[[1]]$range$end[2] == 1) ""
if (context$selection[[1]]$range$end[2] == 1L) ""
), collapse = "\n"),
id = context$id
)
Expand Down
2 changes: 1 addition & 1 deletion R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ left_join <- function(x, y, by) {
res <- new_tibble(res, nrow = nrow(res))
# dplyr::left_join set unknown list columns to NULL, merge sets them
# to NA
if (exists("child", res) && any(is.na(res$child))) {
if (exists("child", res) && anyNA(res$child)) {
res$child[is.na(res$child)] <- list(NULL)
}
res
Expand Down
27 changes: 13 additions & 14 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ token_is_on_aligned_line <- function(pd_flat) {
# now, pd only contains arguments separated by values, ideal for iterating
# over columns.
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
previous_line <- 0
current_col <- 0
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
for (column in seq2(1, max(n_cols))) {
previous_line <- current_col <- 0L
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1L, 2L)
for (column in seq2(1L, max(n_cols))) {
by_line <- alignment_serialize_column(pd_by_line, column) %>%
compact() %>%
unlist() %>%
Expand All @@ -120,7 +119,7 @@ token_is_on_aligned_line <- function(pd_flat) {
current_col <- nchar(by_line) - as.integer(column > 1)
# Problem `by_line` counting from comma before column 3, previous_line
# counting 1 space before ~
if (column > 1) {
if (column > 1L) {
previous_line <- previous_line[
intersect(names(previous_line), names(by_line))
]
Expand All @@ -129,28 +128,28 @@ token_is_on_aligned_line <- function(pd_flat) {
}

is_aligned <- length(unique(current_col)) == 1L
if (!is_aligned || length(current_col) < 2) {
if (!is_aligned || length(current_col) < 2L) {
# check 2: left aligned after , (comma to next token)
current_col <- "^(,[\\s\\t]*)[^ ]*.*$" %>%
gsub("\\1", by_line, perl = TRUE) %>%
nchar() %>%
magrittr::subtract(1)

if (column > 1) {
if (column > 1L) {
# must add previous columns, as first column might not align
current_col <- previous_line + current_col
}
if (length(current_col) > 1) {
if (length(current_col) > 1L) {
is_aligned <- length(unique(current_col)) == 1L
} else {
is_aligned <- current_col - max_previous_col == 1
is_aligned <- current_col - max_previous_col == 1L
current_col <- max_previous_col + current_col
}

if (is_aligned) {
# if left aligned after ,
start_eval <- 2
previous_line <- nchar(by_line) - 1 + previous_line # comma to comma
start_eval <- 2L
previous_line <- nchar(by_line) - 1L + previous_line # comma to comma
}
} else {
previous_line <- current_col
Expand All @@ -162,22 +161,22 @@ token_is_on_aligned_line <- function(pd_flat) {
# match left aligned after =
start_after_eq <- regexpr("= [^ ]", by_line)
names(start_after_eq) <- names(by_line)
start_after_eq <- start_after_eq[start_after_eq > 0]
start_after_eq <- start_after_eq[start_after_eq > 0L]

if (column >= start_eval) {
if (length(start_after_eq) == 0L) {
return(FALSE)
}
# when match via , unsuccessful, matching by = must yield at least one =
if (column == 1) {
if (column == 1L) {
current_col <- start_after_eq
} else {
current_col <- start_after_eq +
previous_line[intersect(names(previous_line), names(start_after_eq))]
}
is_aligned <- all(
length(unique(current_col)) == 1L,
length(start_after_eq) > 1
length(start_after_eq) > 1L
)
if (!is_aligned) {
return(FALSE)
Expand Down
4 changes: 1 addition & 3 deletions R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ is_subset_expr <- function(pd) {
#' @keywords internal
is_shebang <- function(pd) {
is_first_comment <- pd$pos_id == 1L
is_first_comment[is_first_comment] <- grepl(
"^#!", pd$text[is_first_comment]
)
is_first_comment[is_first_comment] <- startsWith(pd$text[is_first_comment], "#!")
is_first_comment
}

Expand Down
2 changes: 1 addition & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ read_utf8 <- function(path) {
} else if (inherits(out, "warning")) {
list(
text = read_utf8_bare(path, warn = FALSE),
missing_EOF_line_break = grepl("incomplete", out$message)
missing_EOF_line_break = grepl("incomplete", out$message, fixed = TRUE)
)
}
}
Expand Down
5 changes: 1 addition & 4 deletions R/roxygen-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ style_roxygen_example_snippet <- function(code_snippet,
)
}

code_snippet <- ensure_last_n_empty(
code_snippet,
n = ifelse(append_empty, 1L, 0L)
)
code_snippet <- ensure_last_n_empty(code_snippet, n = as.integer(append_empty))

if (!is_cached && cache_is_active) {
cache_write(
Expand Down
4 changes: 2 additions & 2 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ remove_line_breaks_in_fun_dec <- function(pd) {
#' @importFrom rlang seq2
add_line_break_after_pipe <- function(pd) {
is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE")
pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1] <- 1L
pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1L] <- 1L

if (sum(is_pipe & pd$token_after != "COMMENT") > 1 &&
if (sum(is_pipe & pd$token_after != "COMMENT") > 1L &&
!(next_terminal(pd, vars = "token_before")$token_before %in% c("'('", "EQ_SUB", "','"))) {
pd$lag_newlines[lag(is_pipe) & pd$token != "COMMENT"] <- 1L
}
Expand Down
4 changes: 2 additions & 2 deletions R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ style_space_around_tilde <- function(pd_flat, strict) {
} else if (is_asymmetric_tilde_expr(pd_flat)) {
pd_flat <- style_space_around_token(pd_flat,
strict = TRUE, "'~'", level_before = 1L,
level_after = ifelse(nrow(pd_flat$child[[2]]) > 1L, 1L, 0L)
level_after = as.integer(nrow(pd_flat$child[[2]]) > 1L)
)
}
pd_flat
Expand Down Expand Up @@ -229,7 +229,7 @@ set_space_between_levels <- function(pd_flat) {
index <- pd_flat$token == "')'" & pd_flat$newlines == 0L
pd_flat$spaces[index] <- 1L
} else if (pd_flat$token[1] == "FOR") {
index <- pd_flat$token == "forcond" & pd_flat$newlines == 0
index <- pd_flat$token == "forcond" & pd_flat$newlines == 0L
pd_flat$spaces[index] <- 1L
}
pd_flat
Expand Down
14 changes: 7 additions & 7 deletions R/rules-tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2) {
if (length(key_token) > 0L) {
pd <- pd %>%
wrap_multiline_curly(indent_by,
space_after = ifelse(contains_else_expr(pd), 1, 0),
key_token = key_token
key_token = key_token,
space_after = as.integer(contains_else_expr(pd))
)
}
if (is_cond_expr(pd)) {
pd <- pd %>%
wrap_else_multiline_curly(indent_by, space_after = 0)
wrap_else_multiline_curly(indent_by, space_after = 0L)
}
pd
}
Expand All @@ -98,13 +98,13 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2) {
#' the expression to be wrapped (ignoring comments). For if and while loops,
#' this is the closing "')'", for a for-loop it's "forcond".
#' @keywords internal
wrap_multiline_curly <- function(pd, indent_by, space_after = 1L, key_token) {
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)[1]
)
next_terminal <- next_terminal(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") {
if (requires_braces || next_terminal == "return") {
closing_brace_ind <- which(pd$token == key_token)[1]
pd$spaces[closing_brace_ind] <- 1L

Expand All @@ -116,7 +116,7 @@ wrap_multiline_curly <- function(pd, indent_by, space_after = 1L, key_token) {
pd, all_to_be_wrapped_ind, indent_by, space_after
)

if (nrow(pd) > 5) pd$lag_newlines[6] <- 0L
if (nrow(pd) > 5L) pd$lag_newlines[6] <- 0L
}
pd
}
Expand Down Expand Up @@ -163,7 +163,7 @@ wrap_subexpr_in_curly <- function(pd,
stretch_out = c(!to_be_wrapped_starts_with_comment, TRUE),
space_after = space_after
)
new_expr$indent <- max(pd$indent[last(ind_to_be_wrapped)] - indent_by, 0)
new_expr$indent <- max(pd$indent[last(ind_to_be_wrapped)] - indent_by, 0L)
new_expr_in_expr <- new_expr %>%
wrap_expr_in_expr() %>%
remove_attributes(c("token_before", "token_after"))
Expand Down
2 changes: 1 addition & 1 deletion R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @keywords internal
set_arg_write_tree <- function(write_tree) {
if (is.na(write_tree)) {
write_tree <- ifelse(is_installed("data.tree"), TRUE, FALSE)
write_tree <- is_installed("data.tree")
} else if (write_tree) {
assert_data.tree_installation()
}
Expand Down
2 changes: 1 addition & 1 deletion R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ tidyverse_style <- function(scope = "tokens",
except_token_after = "COMMENT",
# don't modify line break here
except_text_before = c("ifelse", "if_else"),
force_text_before = c("switch") # force line break after first token
force_text_before = "switch" # force line break after first token
)
},
remove_line_break_in_fun_call = purrr::partial(
Expand Down
2 changes: 1 addition & 1 deletion R/stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ env_add_stylerignore <- function(pd_flat) {
map(~ rep(.x[1], 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 = 0)
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, ]
}
Expand Down
4 changes: 2 additions & 2 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ style_empty <- function(text, base_indention = 0) {

#' @describeIn test_transformer Transformations for indention based on operators
#' @keywords internal
style_op <- function(text, base_indention = 0) {
style_op <- function(text, base_indention = 0L) {
transformers <- list(
# transformer functions
initialize = default_style_guide_attributes,
line_break = NULL,
space = partial(indent_op, indent_by = 2),
space = partial(indent_op, indent_by = 2L),
token = NULL,
# transformer options
use_raw_indention = FALSE,
Expand Down
6 changes: 3 additions & 3 deletions R/token-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ validate_new_pos_ids <- function(new_ids, after) {
#' @keywords internal
wrap_expr_in_curly <- function(pd,
stretch_out = c(FALSE, FALSE),
space_after = 1) {
space_after = 1L) {
if (is_curly_expr(pd)) {
return(pd)
}
Expand All @@ -154,8 +154,8 @@ wrap_expr_in_curly <- function(pd,
}

opening <- create_tokens("'{'", "{",
pos_ids = create_pos_ids(pd, 1, after = FALSE),
spaces = 1 - as.integer(stretch_out[1]),
pos_ids = create_pos_ids(pd, 1L, after = FALSE),
spaces = 1L - as.integer(stretch_out[1]),
stylerignore = pd$stylerignore[1],
indents = pd$indent[1]
)
Expand Down
2 changes: 1 addition & 1 deletion R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ prettify_any <- function(transformers,
recursive = FALSE
)
} else {
files_other <- c()
files_other <- NULL
}
transform_files(
setdiff(c(files_root, files_other), exclude_files),
Expand Down
2 changes: 1 addition & 1 deletion R/utils-navigate-nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ previous_non_comment <- function(pd, pos) {
next_terminal <- function(pd,
stack = FALSE,
vars = c("pos_id", "token", "text"),
tokens_exclude = c()) {
tokens_exclude = NULL) {
pd$position <- seq2(1, nrow(pd))
pd <- pd[!(pd$token %in% tokens_exclude), ]
if (pd$terminal[1]) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ calls_sys <- function(sys_call, ...) {
#' option was not set.
#' @keywords internal
option_read <- function(x, default = NULL, error_if_not_found = TRUE) {
if (x %in% names(options()) | !error_if_not_found) {
if (x %in% names(options()) || !error_if_not_found) {
getOption(x, default)
} else {
rlang::abort(paste("R option", x, "must be set."))
Expand Down
2 changes: 1 addition & 1 deletion man/next_terminal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/test_transformer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/wrap_expr_in_curly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/wrap_multiline_curly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.