Skip to content

Substitute create filler #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
inst/doc
docs
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ RoxygenNote: 6.0.1.9000
VignetteBuilder: knitr
Collate:
'addins.R'
'initialize.R'
'modify_pd.R'
'nested.R'
'nested_to_tree.R'
'parsed.R'
'reindent.R'
'token.R'
'relevel.R'
Expand Down
64 changes: 64 additions & 0 deletions R/initialize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' Enrich parse table with space and line break information
#'
#' This function computes difference (as column and line difference) between two
#' entries in the parse table and adds this information to the table.
#' @param pd_flat A parse table.
#' @importFrom utils tail
initialize_attributes <- function(pd_flat) {

init_pd <-
initialize_newlines(pd_flat) %>%
initialize_spaces() %>%
initialize_multi_line() %>%
initialize_indention_ref_id() %>%
initialize_indent() %>%
validate_parse_data()
init_pd
}

#' @describeIn initialize_attributes Initializes `newlines` and `lag_newlines`.
initialize_newlines <- function(pd_flat) {
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))
pd_flat$newlines <- pd_flat$line3 - pd_flat$line2
pd_flat$lag_newlines <- lag(pd_flat$newlines, default = 0L)
pd_flat$line3 <- NULL
pd_flat
}

#' @describeIn initialize_attributes Initializes `spaces`.
initialize_spaces <- function(pd_flat) {
pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L)
pd_flat$col2_nl <- if_else(pd_flat$newlines > 0L, 0L, pd_flat$col2)
pd_flat$spaces <- pd_flat$col3 - pd_flat$col2_nl - 1L
pd_flat$col3 <- NULL
pd_flat$col2_nl <- NULL
pd_flat
}

#' @describeIn initialize_attributes Initializes `multi_line`.
initialize_multi_line <- function(pd_flat) {
pd_flat$multi_line <- ifelse(pd_flat$terminal, FALSE, NA)
pd_flat
}

#' @describeIn initialize_attributes Initializes `indention_ref_ind`.
initialize_indention_ref_id <- function(pd_flat) {
pd_flat$indention_ref_id <- NA
pd_flat
}

#' @describeIn initialize_attributes Initializes `indent`.
initialize_indent <- function(pd_flat) {
if (!("indent" %in% names(pd_flat))) {
pd_flat$indent <- 0
}
pd_flat
}

#' @describeIn initialize_attributes validates the parse data.
validate_parse_data <- function(pd_flat) {
if (any(pd_flat$spaces < 0L)) {
stop("Invalid parse data")
}
pd_flat
}
2 changes: 1 addition & 1 deletion R/modify_pd.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Update indention information of parse data
#'
#' @param pd A nested or flat parse table that is already enhanced with
#' line break and space information via [create_filler()].
#' line break and space information via [initialize_attributes()].
#' @param indent_by How many spaces should be added after the token of interest.
#' @param token The token the indention should be based on.
#' @name update_indention
Expand Down
4 changes: 2 additions & 2 deletions R/nested_to_tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @importFrom purrr when
create_tree <- function(text) {
compute_parse_data_nested(text) %>%
pre_visit(c(create_filler)) %>%
pre_visit(c(initialize_attributes)) %>%
create_node_from_nested_root() %>%
as.data.frame()
}
Expand All @@ -19,7 +19,7 @@ create_tree <- function(text) {
#' @examples
#' 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(styler:::create_filler))
#' initialized <- styler:::pre_visit(nested_pd, c(styler:::initialize_attributes))
#' styler:::create_node_from_nested_root(initialized)
create_node_from_nested_root <- function(pd_nested) {
n <- data.tree::Node$new("ROOT (token: short_text [lag_newlines/spaces] {id})")
Expand Down
32 changes: 0 additions & 32 deletions R/parsed.R

This file was deleted.

8 changes: 4 additions & 4 deletions R/serialized_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ NULL
style_empty <- function(text) {
transformers <- list(
# transformer functions
filler = create_filler,
initialize = initialize_attributes,
line_break = NULL,
space = NULL,
token = NULL,
Expand All @@ -156,7 +156,7 @@ style_indent_curly <- function(text) {

transformers <- list(
# transformer functions
filler = create_filler,
initialize = initialize_attributes,
line_break = NULL,
space = partial(indent_curly, indent_by = 2),
token = NULL,
Expand All @@ -175,7 +175,7 @@ style_indent_curly <- function(text) {
style_indent_curly_round <- function(text) {
transformers <- list(
# transformer functions
filler = create_filler,
initialize = initialize_attributes,
line_break = NULL,
space = c(partial(indent_curly, indent_by = 2),
partial(indent_round, indent_by = 2)),
Expand All @@ -195,7 +195,7 @@ style_op <- function(text) {

transformers <- list(
# transformer functions
filler = create_filler,
initialize = initialize_attributes,
line_break = NULL,
space = partial(indent_op, indent_by = 2),
token = NULL,
Expand Down
8 changes: 4 additions & 4 deletions R/style_guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ tidyverse_style <- function(scope = "tokens",

create_style_guide(
# transformer functions
filler = create_filler,
initialize = initialize_attributes,
line_break = line_break_manipulators,
space = space_manipulators,
token = token_manipulators,
Expand All @@ -141,7 +141,7 @@ tidyverse_style <- function(scope = "tokens",
#' transformer function corresponds to one styling rule. The output of this
#' function can be used as an argument for \code{style} in top level functions
#' like [style_text()] and friends.
#' @param filler A filler function that initializes various variables on each
#' @param initialize A function that initializes various variables on each
#' level of nesting.
#' @param line_break A list of transformer functions that manipulate line_break
#' information.
Expand All @@ -152,15 +152,15 @@ tidyverse_style <- function(scope = "tokens",
#' @param use_raw_indention Boolean indicating whether or not the raw indention
#' should be used.
#' @export
create_style_guide <- function(filler = create_filler,
create_style_guide <- function(initialize = initialize_attributes,
line_break = NULL,
space = NULL,
token = NULL,
indention = NULL,
use_raw_indention = FALSE) {
lst(
# transformer functions
filler,
initialize,
line_break,
space,
token,
Expand Down
2 changes: 1 addition & 1 deletion R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ parse_transform_serialize <- function(text, transformers) {
apply_transformers <- function(pd_nested, transformers) {
transformed_line_breaks <- pre_visit(
pd_nested,
c(transformers$filler,
c(transformers$initialize,
transformers$line_break)
)

Expand Down
19 changes: 0 additions & 19 deletions man/create_filler.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/create_node_from_nested_root.Rd

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

4 changes: 2 additions & 2 deletions man/create_style_guide.Rd

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

48 changes: 48 additions & 0 deletions man/initialize_attributes.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/update_indention.Rd

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

6 changes: 3 additions & 3 deletions vignettes/customizing_styler.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ table"):
```{r}
string_to_format <- "call( 3)"
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
styler:::pre_visit(c(styler:::create_filler))
styler:::pre_visit(c(styler:::initialize_attributes))
pd$child[[1]] %>%
select(token, terminal, text, newlines, spaces)
```

`create_filler()` is called to initialize some variables, it does not actually
`initialize_attributes()` is called to initialize some variables, it does not actually
transform the parse table.

All the function `remove_space_after_opening_paren()` now does is to look for
Expand Down Expand Up @@ -155,7 +155,7 @@ respectively, so we need both.
The sequence in which styler applies rules on each level of nesting is given in
the list below:

* call `create_filler()` to initialize some variables.
* call `initialize_attributes()` to initialize some variables.
* modify the line breaks (modifying `lag_newlines` only based on
`token`, `token_before`, `token_after` and `text`).
* modify the spaces (modifying `spaces` only based on `lag_newlines`,
Expand Down
6 changes: 4 additions & 2 deletions vignettes/gsoc_proposal/manipulating_nested_parse_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ In order to compute the white space information in a nested data structure, we
use a [visitor approach](https://en.wikipedia.org/wiki/Visitor_pattern) to
separate the algorithm (computing white space information and later apply
transformations) from the object (nested data structure).
The function `create_filler()` can then be used to compute current
The function `create_filler()` (name depreciated, now called
`initialize_attributes()`) can then be used to compute current
white space information on every level of nesting within the nested parse data
if applied in combination with the visitor. In the sequel, a parse table at
one level of nesting will be denoted with the term *nest*, which always
represents a complete expression. Our visiting functions `pre_visit()` and `post_visit()` take an object to
represents a complete expression. Our visiting functions `pre_visit()` and
`post_visit()` take an object to
operate on and a list of functions. Concretely, the object is the
nested parse table. Each function is applied at each level of
nesting nesting before the next level of nesting is entered. You can find out
Expand Down