Skip to content

Commit cb15e52

Browse files
authored
Merge 00d46c2 into 4075099
2 parents 4075099 + 00d46c2 commit cb15e52

File tree

6 files changed

+53
-69
lines changed

6 files changed

+53
-69
lines changed

R/testing.R

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,16 @@ NULL
164164
style_empty <- function(text, base_indention = 0) {
165165
transformers <- list(
166166
# transformer functions
167-
initialize = default_style_guide_attributes,
168-
line_break = NULL,
169-
space = NULL,
170-
token = NULL,
167+
initialize = list(
168+
default_style_guide_attributes = default_style_guide_attributes
169+
),
170+
line_break = NULL,
171+
space = NULL,
172+
token = NULL,
171173
# transformer options
172174
use_raw_indention = FALSE,
173-
reindention = specify_reindention(),
174-
indent_character = " ",
175+
reindention = specify_reindention(),
176+
indent_character = " ",
175177
NULL
176178
)
177179
transformed_text <- parse_transform_serialize_r(text,
@@ -186,14 +188,18 @@ style_empty <- function(text, base_indention = 0) {
186188
style_op <- function(text, base_indention = 0) {
187189
transformers <- list(
188190
# transformer functions
189-
initialize = default_style_guide_attributes,
190-
line_break = NULL,
191-
space = partial(indent_op, indent_by = 2),
192-
token = NULL,
191+
initialize = list(
192+
default_style_guide_attributes = default_style_guide_attributes
193+
),
194+
line_break = NULL,
195+
space = list(
196+
indent_op = partial(indent_op, indent_by = 2)
197+
),
198+
token = NULL,
193199
# transformer options
194200
use_raw_indention = FALSE,
195-
reindention = specify_reindention(),
196-
indent_character = " ",
201+
reindention = specify_reindention(),
202+
indent_character = " ",
197203
NULL
198204
)
199205

R/transform-files.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ apply_transformers <- function(pd_nested, transformers) {
329329
transformed_updated_multi_line <- post_visit(
330330
pd_nested,
331331
c(
332-
transformers$initialize, transformers$line_break, set_multi_line,
333-
if (length(transformers$line_break) != 0) update_newlines
332+
transformers$initialize, transformers$line_break,
333+
set_multi_line = set_multi_line,
334+
update_newlines = if (length(transformers$line_break) != 0) update_newlines
334335
)
335336
)
336337

R/visit.R

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,9 @@ pre_visit <- function(pd_nested, funs) {
2323
if (length(funs) == 0) {
2424
return(pd_nested)
2525
}
26-
pd_nested <- visit_one(pd_nested, funs)
2726

28-
children <- pd_nested$child
29-
for (i in seq_along(children)) {
30-
child <- children[[i]]
31-
if (!is.null(child)) {
32-
children[[i]] <- pre_visit(child, funs)
33-
}
34-
}
35-
pd_nested$child <- children
36-
pd_nested
27+
fun <- make_visit_one(funs)
28+
pre_visit_one(pd_nested, fun)
3729
}
3830

3931
#' @rdname visit
@@ -65,16 +57,8 @@ post_visit <- function(pd_nested, funs) {
6557
return(pd_nested)
6658
}
6759

68-
children <- pd_nested$child
69-
for (i in seq_along(children)) {
70-
child <- children[[i]]
71-
if (!is.null(child)) {
72-
children[[i]] <- post_visit(child, funs)
73-
}
74-
}
75-
pd_nested$child <- children
76-
77-
visit_one(pd_nested, funs)
60+
fun <- make_visit_one(funs)
61+
post_visit_one(pd_nested, fun)
7862
}
7963

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

10084
#' Transform a flat parse table with a list of transformers
10185
#'
102-
#' Uses [Reduce()] to apply each function of `funs` sequentially to
103-
#' `pd_flat`.
86+
#' Creates a single transformer function from a list of transformer functions.
87+
#'
88+
#' @details
89+
#' For an input of the form `list(f1 = f1, f2 = f2)`, creates a function
90+
#'
91+
#' ```r
92+
#' function(pd_flat) {
93+
#' pd_flat <- f1(pd_flat)
94+
#' pd_flat <- f2(pd_flat)
95+
#' pd_flat
96+
#' }
97+
#' ```
98+
#'
99+
#' The function's environment is constructed from `rlang::as_environment(funs)`.
100+
#' This makes function sequences called by visitors interpretable in profiling.
101+
#'
104102
#' @param pd_flat A flat parse table.
105-
#' @param funs A list of transformer functions.
103+
#' @param funs A named list of transformer functions.
106104
#' @family visitors
107105
#' @keywords internal
108-
visit_one <- function(pd_flat, funs) {
109-
for (f in funs) {
110-
pd_flat <- f(pd_flat)
111-
}
112-
pd_flat
106+
make_visit_one <- function(funs) {
107+
calls <- map(rlang::syms(names(funs)), ~ rlang::expr(pd_flat <- (!!.x)(pd_flat)))
108+
all_calls <- c(calls, rlang::expr(pd_flat))
109+
body <- rlang::call2("{", !!!all_calls)
110+
111+
env <- rlang::as_environment(funs, rlang::base_env())
112+
rlang::new_function(rlang::pairlist2(pd_flat = ), body, env)
113113
}
114114

115115
#' Propagate context to terminals

man/visit.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/visit_one.Rd

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/testthat/test-indent-character.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
test_that("indention character can be arbitrary", {
22
sg <- function(indent_by = 1) {
33
create_style_guide(
4-
indention = list(purrr::partial(indent_braces, indent_by = indent_by)),
4+
indention = list(
5+
indent_braces = purrr::partial(indent_braces, indent_by = indent_by)
6+
),
57
indent_character = "\t",
68
style_guide_name = "test",
79
style_guide_version = 1

0 commit comments

Comments
 (0)