Skip to content

Commit

Permalink
Merge 00d46c2 into 4075099
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Aug 21, 2022
2 parents 4075099 + 00d46c2 commit cb15e52
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 69 deletions.
30 changes: 18 additions & 12 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ NULL
style_empty <- function(text, base_indention = 0) {
transformers <- list(
# transformer functions
initialize = default_style_guide_attributes,
line_break = NULL,
space = NULL,
token = NULL,
initialize = list(
default_style_guide_attributes = default_style_guide_attributes
),
line_break = NULL,
space = NULL,
token = NULL,
# transformer options
use_raw_indention = FALSE,
reindention = specify_reindention(),
indent_character = " ",
reindention = specify_reindention(),
indent_character = " ",
NULL
)
transformed_text <- parse_transform_serialize_r(text,
Expand All @@ -186,14 +188,18 @@ style_empty <- function(text, base_indention = 0) {
style_op <- function(text, base_indention = 0) {
transformers <- list(
# transformer functions
initialize = default_style_guide_attributes,
line_break = NULL,
space = partial(indent_op, indent_by = 2),
token = NULL,
initialize = list(
default_style_guide_attributes = default_style_guide_attributes
),
line_break = NULL,
space = list(
indent_op = partial(indent_op, indent_by = 2)
),
token = NULL,
# transformer options
use_raw_indention = FALSE,
reindention = specify_reindention(),
indent_character = " ",
reindention = specify_reindention(),
indent_character = " ",
NULL
)

Expand Down
5 changes: 3 additions & 2 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,9 @@ apply_transformers <- function(pd_nested, transformers) {
transformed_updated_multi_line <- post_visit(
pd_nested,
c(
transformers$initialize, transformers$line_break, set_multi_line,
if (length(transformers$line_break) != 0) update_newlines
transformers$initialize, transformers$line_break,
set_multi_line = set_multi_line,
update_newlines = if (length(transformers$line_break) != 0) update_newlines
)
)

Expand Down
56 changes: 28 additions & 28 deletions R/visit.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,9 @@ pre_visit <- function(pd_nested, funs) {
if (length(funs) == 0) {
return(pd_nested)
}
pd_nested <- visit_one(pd_nested, funs)

children <- pd_nested$child
for (i in seq_along(children)) {
child <- children[[i]]
if (!is.null(child)) {
children[[i]] <- pre_visit(child, funs)
}
}
pd_nested$child <- children
pd_nested
fun <- make_visit_one(funs)
pre_visit_one(pd_nested, fun)
}

#' @rdname visit
Expand Down Expand Up @@ -65,16 +57,8 @@ post_visit <- function(pd_nested, funs) {
return(pd_nested)
}

children <- pd_nested$child
for (i in seq_along(children)) {
child <- children[[i]]
if (!is.null(child)) {
children[[i]] <- post_visit(child, funs)
}
}
pd_nested$child <- children

visit_one(pd_nested, funs)
fun <- make_visit_one(funs)
post_visit_one(pd_nested, fun)
}

#' @rdname visit
Expand All @@ -99,17 +83,33 @@ post_visit_one <- function(pd_nested, fun) {

#' Transform a flat parse table with a list of transformers
#'
#' Uses [Reduce()] to apply each function of `funs` sequentially to
#' `pd_flat`.
#' Creates a single transformer function from a list of transformer functions.
#'
#' @details
#' For an input of the form `list(f1 = f1, f2 = f2)`, creates a function
#'
#' ```r
#' function(pd_flat) {
#' pd_flat <- f1(pd_flat)
#' pd_flat <- f2(pd_flat)
#' pd_flat
#' }
#' ```
#'
#' The function's environment is constructed from `rlang::as_environment(funs)`.
#' This makes function sequences called by visitors interpretable in profiling.
#'
#' @param pd_flat A flat parse table.
#' @param funs A list of transformer functions.
#' @param funs A named list of transformer functions.
#' @family visitors
#' @keywords internal
visit_one <- function(pd_flat, funs) {
for (f in funs) {
pd_flat <- f(pd_flat)
}
pd_flat
make_visit_one <- function(funs) {
calls <- map(rlang::syms(names(funs)), ~ rlang::expr(pd_flat <- (!!.x)(pd_flat)))
all_calls <- c(calls, rlang::expr(pd_flat))
body <- rlang::call2("{", !!!all_calls)

env <- rlang::as_environment(funs, rlang::base_env())
rlang::new_function(rlang::pairlist2(pd_flat = ), body, env)
}

#' Propagate context to terminals
Expand Down
4 changes: 1 addition & 3 deletions man/visit.Rd

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

23 changes: 0 additions & 23 deletions man/visit_one.Rd

This file was deleted.

4 changes: 3 additions & 1 deletion tests/testthat/test-indent-character.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
test_that("indention character can be arbitrary", {
sg <- function(indent_by = 1) {
create_style_guide(
indention = list(purrr::partial(indent_braces, indent_by = indent_by)),
indention = list(
indent_braces = purrr::partial(indent_braces, indent_by = indent_by)
),
indent_character = "\t",
style_guide_name = "test",
style_guide_version = 1
Expand Down

0 comments on commit cb15e52

Please sign in to comment.