Skip to content

Commit

Permalink
resolve with simple for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Jan 23, 2021
1 parent 48f53a3 commit 4c1f95a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 57 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ importFrom(purrr,map_lgl)
importFrom(purrr,partial)
importFrom(purrr,pmap)
importFrom(purrr,pwalk)
importFrom(purrr,walk)
importFrom(purrr,when)
importFrom(rlang,abort)
importFrom(rlang,is_empty)
Expand Down
4 changes: 4 additions & 0 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ tidyverse_style <- function(scope = "tokens",
#' they will yield generic code and we loose the specific value of `arg` (see
#' [styler::cache_make_key()]), even when unquoting these inputs with `!!`
#' beforehand in `purrr::partial()`.
#' @param subset_transformers A list specifying under which conditions
#' transformer functions can be dropped since they have no effect on the
#' code to format. This is argument experimental and may change in future
#' releases without prior notification.
#' @examples
#' set_line_break_before_curly_opening <- function(pd_flat) {
#' op <- pd_flat$token %in% "'{'"
Expand Down
43 changes: 15 additions & 28 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,34 +250,17 @@ parse_transform_serialize_r <- function(text,
text_out
}

#' Removes the transformers if tokens in x
#' @param transformers The style full style guide, i.e. the result of
#' [create_style_guide()].
#' @param token Named character vector: Names are rules and values are the token
#' that trigger are required to be absent to trigger a removal.
#' @param scope The low-level scope, e.g. 'token'.
#' @param code tokenized code for which we check if `token` is in them.
#' @importFrom purrr walk
transformers_subset_impl <- function(transformers, token, scope, code) {
transformer_names <- names(token)
walk(seq_along(token), function(i) {
if (!any(token[[i]] %in% code)) {
transformers[[scope]][[transformer_names[i]]] <- NULL
}
})
transformers
}

#' Remove transformers that are not needed
#'
#' For every transformer, at least one token must be given to make subsetting.
#' active.
#' The goal is to speed up styling by removing all rules that are only
#' applicable in contexts that don't occur often, e.g. for most code, we don't
#' expect ";" to be in it, so we don't need to apply [resolve_semicolon()] on
#' every *nest*.
#' @param text Text to parse. Can also be the column `text` of the output of
#' [compute_parse_data_nested()], where each element is a token (instead of a
#' line).
#' @return
#' Returns `transformers`, but stripped away those rules that are not used when
#' styling `text`.
#' @param transformers the transformers.
#' @keywords internal
transformers_subset <- function(text, transformers) {
is_colon <- text == ";"
Expand All @@ -286,12 +269,16 @@ transformers_subset <- function(text, transformers) {
# here since text is output of compute_parse_data_nested.
text <- c(text[!is_colon], "1;")
}
purrr::reduce2(
transformers$subset_transformers,
names(transformers$subset_transformers),
transformers_subset_impl, unique(tokenize(text)$token),
.init = transformers
)
token <- unique(tokenize(text)$token)
for (scope in c("line_break", "space", "token", "indention")) {
rules <- transformers$subset_transformers[[scope]]
for (rule in names(rules)) {
if (!any(rules[[rule]] %in% token)) {
transformers[[scope]][rule] <- NULL
}
}
}
transformers
}

#' Apply transformers to a parse table
Expand Down
5 changes: 5 additions & 0 deletions man/create_style_guide.Rd

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

12 changes: 6 additions & 6 deletions man/transformers_subset.Rd

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

22 changes: 0 additions & 22 deletions man/transformers_subset_impl.Rd

This file was deleted.

0 comments on commit 4c1f95a

Please sign in to comment.