Skip to content

Commit

Permalink
pre_visit_one() and post_visit_one()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Aug 21, 2022
1 parent 4d0668c commit 15ffbd5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion R/initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' {
#' string_to_format <- "call( 3)"
#' pd <- styler:::compute_parse_data_nested(string_to_format)
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
#' styler:::pre_visit_one(pd, default_style_guide_attributes)
#' }
#' )
#' @export
Expand Down
6 changes: 3 additions & 3 deletions R/nested-to-tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @keywords internal
create_tree <- function(text, structure_only = FALSE) {
compute_parse_data_nested(text, transformers = NULL) %>%
pre_visit(c(default_style_guide_attributes)) %>%
pre_visit_one(default_style_guide_attributes) %>%
create_tree_from_pd_with_default_style_attributes(structure_only)
}

Expand Down Expand Up @@ -36,8 +36,8 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
#' {
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
#' nested_pd <- styler:::compute_parse_data_nested(code)
#' initialized <- styler:::pre_visit(
#' nested_pd, c(default_style_guide_attributes)
#' initialized <- styler:::pre_visit_one(
#' nested_pd, default_style_guide_attributes
#' )
#' styler:::create_node_from_nested_root(initialized,
#' structure_only = FALSE
Expand Down
6 changes: 2 additions & 4 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#' @param pd_nested A nested parse table to partially flatten.
#' @keywords internal
flatten_operators <- function(pd_nested) {
pd_nested %>%
post_visit(c(flatten_operators_one))
post_visit_one(pd_nested, flatten_operators_one)
}

#' Flatten one level of nesting with its child
Expand Down Expand Up @@ -144,8 +143,7 @@ wrap_expr_in_expr <- function(pd) {
#' @keywords internal
relocate_eq_assign <- function(pd) {
if (parser_version_get() < 2) {
pd %>%
post_visit(c(relocate_eq_assign_nest))
post_visit_one(pd, relocate_eq_assign_nest)
} else {
pd
}
Expand Down
2 changes: 1 addition & 1 deletion R/transform-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parse_transform_serialize_r_block <- function(pd_nested,
base_indention) {
if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) {
transformed_pd <- apply_transformers(pd_nested, transformers)
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
flattened_pd <- post_visit_one(transformed_pd, extract_terminals) %>%
enrich_terminals(transformers$use_raw_indention) %>%
apply_ref_indention() %>%
set_regex_indention(
Expand Down
39 changes: 39 additions & 0 deletions R/visit.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ pre_visit <- function(pd_nested, funs) {
pd_nested
}

#' @rdname visit
#' @keywords internal
pre_visit_one <- function(pd_nested, fun) {
if (is.null(pd_nested)) {
return()
}
pd_nested <- fun(pd_nested)

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

#' @rdname visit
#' @keywords internal
post_visit <- function(pd_nested, funs) {
Expand All @@ -58,6 +77,26 @@ post_visit <- function(pd_nested, funs) {
visit_one(pd_nested, funs)
}

#' @rdname visit
#' @keywords internal
post_visit_one <- function(pd_nested, fun) {
if (is.null(pd_nested)) {
return()
}
force(fun)

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

fun(pd_nested)
}

#' Transform a flat parse table with a list of transformers
#'
#' Uses [Reduce()] to apply each function of `funs` sequentially to
Expand Down
2 changes: 1 addition & 1 deletion vignettes/customizing_styler.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ As the name says, this function removes spaces after the opening parenthesis. Bu
```{r}
string_to_format <- "call( 3)"
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
styler:::pre_visit(c(default_style_guide_attributes))
styler:::pre_visit_one(default_style_guide_attributes)
pd$child[[1]] %>%
select(token, terminal, text, newlines, spaces)
```
Expand Down

0 comments on commit 15ffbd5

Please sign in to comment.