Skip to content

Commit

Permalink
document
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Feb 4, 2020
1 parent 8564e74 commit 7679c2a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 21 deletions.
35 changes: 22 additions & 13 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ compute_parse_data_nested <- function(text,
add_terminal_token_before() %>%
add_terminal_token_after() %>%
add_stylerignore() %>%
add_attributes_caching() %>%
drop_cached_children(transformers)
add_attributes_caching(transformers) %>%
drop_cached_children()

env_add_stylerignore(parse_data)

Expand All @@ -31,7 +31,8 @@ compute_parse_data_nested <- function(text,
#' Add the block id to a parse table
#'
#' Must be after [nest_parse_data()] because requires a nested parse table as
#' input
#' input.
#' @param pd_nested A top level nest.
#' @keywords internal
#' @importFrom rlang seq2
add_cache_block <- function(pd_nested) {
Expand All @@ -48,6 +49,7 @@ add_cache_block <- function(pd_nested) {
#' Note that we do cache top-level comments. Because package code has a lot of
#' roxygen comments and each of them is a top level expresion, so checking is
#' very expensive.
#' @param pd A top-level nest.
#' @details
#' Because we process in blocks of expressions for speed, a cached expression
#' will always end up in a block that won't be styled again (usual case), unless
Expand Down Expand Up @@ -84,19 +86,15 @@ add_cache_block <- function(pd_nested) {
#' first, then children", this function creates a temporary parse table that has
#' this property and then extract the ids and subset the original parse table so
#' it is shallow in the right places.
drop_cached_children <- function(pd, transformers) {
#' @keywords internal
drop_cached_children <- function(pd) {

if (cache_is_activated()) {
pd$is_cached[pd$parent == 0] <- map_lgl(pd$text[pd$parent == 0],
is_cached, transformers, cache_dir_default()
)
is_comment <- pd$token == "COMMENT"
pd$is_cached[is_comment] <- rep(FALSE, sum(is_comment))

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, transformers = transformers) %>%
map(find_pos_id_to_keep) %>%
unlist() %>%
unname()
pd[pd$pos_id %in% pos_ids_to_keep,]
Expand All @@ -106,7 +104,7 @@ drop_cached_children <- function(pd, transformers) {

}

find_pos_id_to_keep <- function(pd, transformers) {
find_pos_id_to_keep <- function(pd) {
if (pd$is_cached[1]) {
pd$pos_id[1]
} else {
Expand Down Expand Up @@ -228,11 +226,22 @@ add_terminal_token_before <- function(pd_flat) {
left_join(pd_flat, ., by = "id")
}

#' Initialiye variables related to caching
#' Initialise variables related to caching
#'
#' @param transformers A list with transformer functions, used to check if
#' the code is cached.
#' @describeIn add_token_terminal Initializes `newlines` and `lag_newlines`.
#' @keywords internal
add_attributes_caching <- function(pd_flat) {
add_attributes_caching <- function(pd_flat, transformers) {
pd_flat$block <- pd_flat$is_cached <- rep(NA, nrow(pd_flat))
if (cache_is_activated()) {
pd_flat$is_cached[pd_flat$parent == 0] <- map_lgl(
pd_flat$text[pd_flat$parent == 0],
is_cached, transformers, cache_dir_default()
)
is_comment <- pd_flat$token == "COMMENT"
pd_flat$is_cached[is_comment] <- rep(FALSE, sum(is_comment))
}
pd_flat
}

Expand Down
4 changes: 4 additions & 0 deletions R/token-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#' @param terminal Boolean vector indicating whether a token is a terminal or
#' not.
#' @param child The children of the tokens.
#' @param stylerignore Boolean to indicate if the line should be ignored by
#' styler.
#' @param block The block (of caching) to which the token belongs. An integer.
#' @param is_cached Whether the token is cached already.
#' @family token creators
#' @keywords internal
create_tokens <- function(tokens,
Expand Down
1 change: 0 additions & 1 deletion R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ make_transformer <- function(transformers,
warn_empty = TRUE) {
force(transformers)
assert_transformers(transformers)
cache_dir <- cache_dir_default()
assert_R.cache_installation(action = "warn")

is_R.cache_installed <- rlang::is_installed("R.cache")
Expand Down
8 changes: 6 additions & 2 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ hash_standardize <- function(text) {
#' @param cache_dir The caching directory relative to the `.Rcache` root to
#' look for a cached value.
#' @keywords internal
is_cached <- function(text, transformers, cache_dir) {
is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
R.cache::generateCache(
key = cache_make_key(text, transformers),
dirs = cache_dir
Expand All @@ -33,8 +33,12 @@ is_cached <- function(text, transformers, cache_dir) {
#' Make a key for `R.cache`
#'
#' This is used to determine if caching already corresponds to a style guide.
#' @param text Code to create a cache for. This should be styled text, as the
#' approach used by styler does not cache input, but styled code.
#' @param transformers A list of transformer functions, because we can only
#' know if text is already correct if we know which transformer function it
#' should be styled with.
#' @details
#'
#' We need to compare:
#'
#' * text to style. Will be passed to hash function as is.
Expand Down
5 changes: 4 additions & 1 deletion man/add_cache_block.Rd

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

7 changes: 5 additions & 2 deletions man/add_token_terminal.Rd

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

8 changes: 8 additions & 0 deletions man/cache_make_key.Rd

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

7 changes: 7 additions & 0 deletions man/create_tokens.Rd

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

6 changes: 5 additions & 1 deletion man/drop_cached_children.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/is_cached.Rd

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

0 comments on commit 7679c2a

Please sign in to comment.