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
21 changes: 11 additions & 10 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ set_style_transformers <- function() {
current_style
)
if (!is.null(new_style)) {
parsed_new_style <- with_handlers({
transformers <- eval(parse(text = new_style))
style_text(c("a = 2", "function() {", "NULL", "}"))
},
error = function(e) {
abort(paste0(
"The selected style transformers \"",
new_style, "\" is not valid: ", e$message
))
}
parsed_new_style <- with_handlers(
{
transformers <- eval(parse(text = new_style))
style_text(c("a = 2", "function() {", "NULL", "}"))
},
error = function(e) {
abort(paste0(
"The selected style transformers \"",
new_style, "\" is not valid: ", e$message
))
}
)
options(styler.addins_style_transformer = new_style)
}
Expand Down
3 changes: 1 addition & 2 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
#' " a = 2",
#' ")"
#' )) %>%
#' styler:::post_visit(transformers$initialize)
#' styler:::post_visit(transformers$initialize)
#' nest <- pd_nested$child[[1]]
#' styler:::token_is_on_aligned_line(nest)
token_is_on_aligned_line <- function(pd_flat) {

line_idx <- 1 + cumsum(pd_flat$lag_newlines)
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
pd_by_line <- split(pd_flat, line_idx)
Expand Down
2 changes: 1 addition & 1 deletion R/initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
#' @keywords internal
default_style_guide_attributes <- function(pd_flat) {
initialize_newlines(pd_flat) %>%
initialize_newlines(pd_flat) %>%
initialize_spaces() %>%
remove_attributes(c("line1", "line2", "col1", "col2", "parent", "id")) %>%
initialize_multi_line() %>%
Expand Down
29 changes: 16 additions & 13 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ transform_utf8 <- function(path, fun, write_back = TRUE) {

#' @importFrom rlang with_handlers warn
transform_utf8_one <- function(path, fun, write_back) {
with_handlers({
file_with_info <- read_utf8(path)
# only write back when changed OR when there was a missing newline
new <- fun(file_with_info$text)
identical_content <- identical(unclass(file_with_info$text), unclass(new))
identical <- identical_content && !file_with_info$missing_EOF_line_break
if (!identical && write_back) {
xfun::write_utf8(new, path)
with_handlers(
{
file_with_info <- read_utf8(path)
# only write back when changed OR when there was a missing newline
new <- fun(file_with_info$text)
identical_content <- identical(unclass(file_with_info$text), unclass(new))
identical <- identical_content && !file_with_info$missing_EOF_line_break
if (!identical && write_back) {
xfun::write_utf8(new, path)
}
!identical
},
error = function(e) {
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
NA
}
!identical
}, error = function(e) {
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
NA
})
)
}

#' Read UTF-8
Expand Down
17 changes: 8 additions & 9 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ add_cache_block <- function(pd_nested) {
#' it is shallow in the right places.
#' @keywords internal
drop_cached_children <- function(pd) {

if (cache_is_activated()) {

pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)),]
pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)), ]
pos_ids_to_keep <- pd_parent_first %>%
split(cumsum(pd_parent_first$parent == 0)) %>%
map(find_pos_id_to_keep) %>%
unlist() %>%
unname()
pd[pd$pos_id %in% pos_ids_to_keep,]
pd[pd$pos_id %in% pos_ids_to_keep, ]
} else {
pd
}
Expand Down Expand Up @@ -155,7 +153,7 @@ find_pos_id_to_keep <- function(pd) {
#' # styler: on
#' 1+1
#' "
#')
#' )
#'
#' # if there is a stop marker before a start marker, styler won't be able
#' # to figure out which lines you want to ignore and won't ignore anything,
Expand All @@ -169,8 +167,8 @@ find_pos_id_to_keep <- function(pd) {
#' # styler: off
#' 1+1
#' "
#')
#'}
#' )
#' }
#'
NULL

Expand Down Expand Up @@ -214,8 +212,9 @@ add_terminal_token_after <- function(pd_flat) {

new_tibble(list(
pos_id = terminals$pos_id,
token_after = lead(terminals$token, default = "")),
nrow = nrow(terminals)
token_after = lead(terminals$token, default = "")
),
nrow = nrow(terminals)
) %>%
left_join(pd_flat, ., by = "pos_id")
}
Expand Down
3 changes: 2 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
parsed <- parse_safely(text, keep.source = TRUE)
pd <- as_tibble(
utils::getParseData(parsed, includeText = include_text),
.name_repair = "minimal") %>%
.name_repair = "minimal"
) %>%
add_id_and_short()

parser_version_set(parser_version_find(pd))
Expand Down
9 changes: 4 additions & 5 deletions R/rules-line-break.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ NULL
#' @keywords internal
set_line_break_after_opening_if_call_is_multi_line <-
function(pd,
except_token_after = NULL,
except_text_before = NULL) {
except_token_after = NULL,
except_text_before = NULL) {
if (!is_function_call(pd) && !is_subset_expr(pd)) {
return(pd)
}
Expand Down Expand Up @@ -282,19 +282,18 @@ set_linebreak_after_ggplot2_plus <- function(pd) {
next_non_comment <- next_non_comment(pd, first_plus)
is_plus_or_comment_after_plus_before_fun_call <-
lag(is_plus_raw, next_non_comment - first_plus - 1, default = FALSE) &
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
if (any(is_plus_or_comment_after_plus_before_fun_call)) {
gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1]]
if (!is.null(gg_call) && isTRUE(gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot")) {
plus_without_comment_after <- setdiff(
which(is_plus_raw),
which(lead(pd$token == "COMMENT"))
)
)

pd$lag_newlines[plus_without_comment_after + 1] <- 1L
}
}

}
pd
}
2 changes: 1 addition & 1 deletion R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) {
)
}
if (any(pd_flat$token %in% one)) {
pd_flat <- pd_flat %>%
pd_flat <- pd_flat %>%
style_space_around_token(
strict = strict, tokens = one, level_before = 1L, level_after = 1L
)
Expand Down
43 changes: 26 additions & 17 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,24 @@ tidyverse_style <- function(scope = "tokens",
if (strict) remove_line_break_before_round_closing_after_curly,
remove_line_break_before_round_closing_fun_dec =
if (strict) remove_line_break_before_round_closing_fun_dec,
style_line_break_around_curly = partial(style_line_break_around_curly,
style_line_break_around_curly = partial(
style_line_break_around_curly,
strict
),
# must be after style_line_break_around_curly as it remove line
# breaks again for {{.
set_line_break_around_curly_curly,
set_line_break_after_opening_if_call_is_multi_line = if (strict)
set_line_break_after_opening_if_call_is_multi_line = if (strict) {
partial(
set_line_break_after_opening_if_call_is_multi_line,
except_token_after = "COMMENT",
except_text_before = c("switch", "ifelse", "if_else")
),
)
},
set_line_break_before_closing_call = if (strict) {
partial(
set_line_break_before_closing_call, except_token_before = "COMMENT"
set_line_break_before_closing_call,
except_token_before = "COMMENT"
)
},
remove_line_break_in_empty_fun_call,
Expand All @@ -160,19 +163,19 @@ tidyverse_style <- function(scope = "tokens",
update_indention_ref_fun_dec =
if (scope >= "indention") update_indention_ref_fun_dec
)

style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
create_style_guide(
# transformer functions
initialize = default_style_guide_attributes,
line_break = line_break_manipulators,
space = space_manipulators,
token = token_manipulators,
indention = indention_modifier,
line_break = line_break_manipulators,
space = space_manipulators,
token = token_manipulators,
indention = indention_modifier,
# transformer options
use_raw_indention = use_raw_indention,
reindention = reindention,
style_guide_name = "styler::tidyverse_style@https://github.com/r-lib",
style_guide_version = styler_version
use_raw_indention = use_raw_indention,
reindention = reindention,
style_guide_name = style_guide_name,
style_guide_version = styler_version
)
}

Expand Down Expand Up @@ -211,9 +214,14 @@ tidyverse_style <- function(scope = "tokens",
#' pd_flat
#' }
#' set_line_break_before_curly_opening_style <- function() {
#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening))
#' create_style_guide(
#' line_break = tibble::lst(set_line_break_before_curly_opening)
#' )
#' }
#' style_text("a <- function(x) { x }", style = set_line_break_before_curly_opening_style)
#' style_text(
#' "a <- function(x) { x }",
#' style = set_line_break_before_curly_opening_style
#' )
#' @importFrom purrr compact
#' @export
create_style_guide <- function(initialize = default_style_guide_attributes,
Expand Down Expand Up @@ -267,12 +275,13 @@ NULL
#' @export
specify_reindention <- function(regex_pattern = NULL,
indention = 0,
comments_only = TRUE)
comments_only = TRUE) {
lst(
regex_pattern,
indention,
comments_only
)
}

#' @describeIn reindention Simple forwarder to
#' `specify_reindention` with reindention according to the tidyverse style
Expand Down Expand Up @@ -330,7 +339,7 @@ NULL
#' @export
specify_math_token_spacing <-
function(zero = "'^'",
one = c("'+'", "'-'", "'*'", "'/'")) {
one = c("'+'", "'-'", "'*'", "'/'")) {
assert_tokens(c(one, zero))
lst(
one = setdiff(c(math_token, one), zero),
Expand Down
38 changes: 19 additions & 19 deletions R/token-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ create_tokens <- function(tokens,
len_text <- length(texts)
new_tibble(
list(
token = tokens,
text = texts,
short = substr(texts, 1, 5),
lag_newlines = lag_newlines,
newlines = lead(lag_newlines),
pos_id = pos_ids,
token_before = token_before,
token_after = token_after,
terminal = rep(terminal, len_text),
internal = rep(FALSE, len_text),
spaces = spaces,
multi_line = rep(FALSE, len_text),
indention_ref_pos_id = indention_ref_pos_ids,
indent = indents,
child = rep(list(child), len_text),
stylerignore = stylerignore,
block = block,
is_cached = is_cached
),
token = tokens,
text = texts,
short = substr(texts, 1, 5),
lag_newlines = lag_newlines,
newlines = lead(lag_newlines),
pos_id = pos_ids,
token_before = token_before,
token_after = token_after,
terminal = rep(terminal, len_text),
internal = rep(FALSE, len_text),
spaces = spaces,
multi_line = rep(FALSE, len_text),
indention_ref_pos_id = indention_ref_pos_ids,
indent = indents,
child = rep(list(child), len_text),
stylerignore = stylerignore,
block = block,
is_cached = is_cached
),
nrow = len_text
)
}
Expand Down
2 changes: 0 additions & 2 deletions R/transform-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ parse_transform_serialize_r_block <- function(pd_nested,
#' @param pd A top level nest.
#' @keywords internal
cache_find_block <- function(pd) {

first_after_cache_state_switch <- pd$is_cached != lag(pd$is_cached, default = !pd$is_cached[1])

not_first_on_line <- find_blank_lines_to_next_expr(pd) == 0
Expand Down Expand Up @@ -93,4 +92,3 @@ find_blank_lines_to_next_block <- function(pd) {
block_boundary <- pd$block != lag(pd$block, default = 0)
find_blank_lines_to_next_expr(pd)[block_boundary]
}

1 change: 0 additions & 1 deletion R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ prettify_any <- function(transformers,
ignore.case = TRUE, recursive = FALSE,
all.files = TRUE
)

} else {
files_other <- c()
}
Expand Down
8 changes: 3 additions & 5 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
dirs = cache_dir
) %>%
file.exists()

}


Expand Down Expand Up @@ -75,7 +74,7 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
#' behavior.
#' @examples
#' add <- function(x, y) {
#' x + y
#' x + y
#' }
#' add1 <- purrr::partial(add, x = 1)
#' add2 <- purrr::partial(add, x = 1)
Expand Down Expand Up @@ -128,9 +127,9 @@ cache_is_activated <- function(cache_name = NULL) {
#' @keywords internal
cache_by_expression <- function(text, transformers) {
expressions <- parse(text = text, keep.source = TRUE) %>%
utils::getParseData(includeText = TRUE)
utils::getParseData(includeText = TRUE)
expressions[expressions$parent == 0 & expressions$token != "COMMENT", "text"] %>%
map(~cache_write(.x, transformers = transformers))
map(~ cache_write(.x, transformers = transformers))
}

cache_write <- function(text, transformers) {
Expand Down Expand Up @@ -160,4 +159,3 @@ cache_get_or_derive_name <- function(cache_name) {
cache_dir_default <- function() {
c("styler", cache_get_name())
}

Loading