Skip to content

Commit 7031c22

Browse files
committed
pre_visit_one() and post_visit_one()
1 parent 4d0668c commit 7031c22

9 files changed

+56
-13
lines changed

R/initialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' {
1111
#' string_to_format <- "call( 3)"
1212
#' pd <- styler:::compute_parse_data_nested(string_to_format)
13-
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
13+
#' styler:::pre_visit_one(pd, default_style_guide_attributes)
1414
#' }
1515
#' )
1616
#' @export

R/nested-to-tree.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @keywords internal
99
create_tree <- function(text, structure_only = FALSE) {
1010
compute_parse_data_nested(text, transformers = NULL) %>%
11-
pre_visit(c(default_style_guide_attributes)) %>%
11+
pre_visit_one(default_style_guide_attributes) %>%
1212
create_tree_from_pd_with_default_style_attributes(structure_only)
1313
}
1414

@@ -36,8 +36,8 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
3636
#' {
3737
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
3838
#' nested_pd <- styler:::compute_parse_data_nested(code)
39-
#' initialized <- styler:::pre_visit(
40-
#' nested_pd, c(default_style_guide_attributes)
39+
#' initialized <- styler:::pre_visit_one(
40+
#' nested_pd, default_style_guide_attributes
4141
#' )
4242
#' styler:::create_node_from_nested_root(initialized,
4343
#' structure_only = FALSE

R/relevel.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#' @param pd_nested A nested parse table to partially flatten.
1313
#' @keywords internal
1414
flatten_operators <- function(pd_nested) {
15-
pd_nested %>%
16-
post_visit(c(flatten_operators_one))
15+
post_visit_one(pd_nested, flatten_operators_one)
1716
}
1817

1918
#' Flatten one level of nesting with its child
@@ -144,8 +143,7 @@ wrap_expr_in_expr <- function(pd) {
144143
#' @keywords internal
145144
relocate_eq_assign <- function(pd) {
146145
if (parser_version_get() < 2) {
147-
pd %>%
148-
post_visit(c(relocate_eq_assign_nest))
146+
post_visit_one(pd, relocate_eq_assign_nest)
149147
} else {
150148
pd
151149
}

R/transform-block.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ parse_transform_serialize_r_block <- function(pd_nested,
2828
base_indention) {
2929
if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) {
3030
transformed_pd <- apply_transformers(pd_nested, transformers)
31-
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
31+
flattened_pd <- post_visit_one(transformed_pd, extract_terminals) %>%
3232
enrich_terminals(transformers$use_raw_indention) %>%
3333
apply_ref_indention() %>%
3434
set_regex_indention(

R/visit.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ pre_visit <- function(pd_nested, funs) {
3636
pd_nested
3737
}
3838

39+
#' @rdname visit
40+
#' @keywords internal
41+
pre_visit_one <- function(pd_nested, fun) {
42+
if (is.null(pd_nested)) {
43+
return()
44+
}
45+
pd_nested <- fun(pd_nested)
46+
47+
children <- pd_nested$child
48+
for (i in seq_along(children)) {
49+
child <- children[[i]]
50+
if (!is.null(child)) {
51+
children[[i]] <- pre_visit_one(child, fun)
52+
}
53+
}
54+
pd_nested$child <- children
55+
pd_nested
56+
}
57+
3958
#' @rdname visit
4059
#' @keywords internal
4160
post_visit <- function(pd_nested, funs) {
@@ -58,6 +77,26 @@ post_visit <- function(pd_nested, funs) {
5877
visit_one(pd_nested, funs)
5978
}
6079

80+
#' @rdname visit
81+
#' @keywords internal
82+
post_visit_one <- function(pd_nested, fun) {
83+
if (is.null(pd_nested)) {
84+
return()
85+
}
86+
force(fun)
87+
88+
children <- pd_nested$child
89+
for (i in seq_along(children)) {
90+
child <- children[[i]]
91+
if (!is.null(child)) {
92+
children[[i]] <- post_visit_one(child, fun)
93+
}
94+
}
95+
pd_nested$child <- children
96+
97+
fun(pd_nested)
98+
}
99+
61100
#' Transform a flat parse table with a list of transformers
62101
#'
63102
#' Uses [Reduce()] to apply each function of `funs` sequentially to

man/create_node_from_nested_root.Rd

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

man/default_style_guide_attributes.Rd

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

man/visit.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/customizing_styler.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ As the name says, this function removes spaces after the opening parenthesis. Bu
4040
```{r}
4141
string_to_format <- "call( 3)"
4242
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
43-
styler:::pre_visit(c(default_style_guide_attributes))
43+
styler:::pre_visit_one(default_style_guide_attributes)
4444
pd$child[[1]] %>%
4545
select(token, terminal, text, newlines, spaces)
4646
```

0 commit comments

Comments
 (0)