From 5c5e352e4547e3e9226c4578f2cb3712995774a0 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:07:39 +0200 Subject: [PATCH 1/2] add docs to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 09a72cbe9..10c71b274 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData inst/doc +docs From 8d453aeddf10708530d91eb7467b9a3b99df1b23 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Mon, 21 Aug 2017 09:56:53 +0200 Subject: [PATCH 2/2] replacing create_filler Replacing create_filler with initialize_attributes() and split it up further. --- DESCRIPTION | 2 +- R/initialize.R | 64 +++++++++++++++++++ R/modify_pd.R | 2 +- R/nested_to_tree.R | 4 +- R/parsed.R | 32 ---------- R/serialized_tests.R | 8 +-- R/style_guides.R | 8 +-- R/transform.R | 2 +- man/create_filler.Rd | 19 ------ man/create_node_from_nested_root.Rd | 2 +- man/create_style_guide.Rd | 4 +- man/initialize_attributes.Rd | 48 ++++++++++++++ man/update_indention.Rd | 2 +- vignettes/customizing_styler.Rmd | 6 +- .../manipulating_nested_parse_data.Rmd | 6 +- 15 files changed, 136 insertions(+), 73 deletions(-) create mode 100644 R/initialize.R delete mode 100644 R/parsed.R delete mode 100644 man/create_filler.Rd create mode 100644 man/initialize_attributes.Rd diff --git a/DESCRIPTION b/DESCRIPTION index d14b23246..d7e9d6e03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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' diff --git a/R/initialize.R b/R/initialize.R new file mode 100644 index 000000000..933da4464 --- /dev/null +++ b/R/initialize.R @@ -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 +} diff --git a/R/modify_pd.R b/R/modify_pd.R index c91a1534c..1516827b8 100644 --- a/R/modify_pd.R +++ b/R/modify_pd.R @@ -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 diff --git a/R/nested_to_tree.R b/R/nested_to_tree.R index cccfbca47..4690ddfee 100644 --- a/R/nested_to_tree.R +++ b/R/nested_to_tree.R @@ -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() } @@ -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})") diff --git a/R/parsed.R b/R/parsed.R deleted file mode 100644 index 664288e2f..000000000 --- a/R/parsed.R +++ /dev/null @@ -1,32 +0,0 @@ -#' 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. -#' @return A parse table with two three columns: lag_newlines, newlines and -#' spaces. -#' @importFrom utils tail -create_filler <- function(pd_flat) { - - pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1)) - pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L) - pd_flat$newlines <- pd_flat$line3 - pd_flat$line2 - pd_flat$lag_newlines <- lag(pd_flat$newlines, default = 0L) - 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$multi_line <- ifelse(pd_flat$terminal, FALSE, NA) - pd_flat$indention_ref_id <- NA - ret <- pd_flat[, !(names(pd_flat) %in% c("line3", "col3", "col2_nl"))] - - - if (!("indent" %in% names(ret))) { - ret$indent <- 0 - } - - if (any(ret$spaces < 0L)) { - stop("Invalid parse data") - } - - ret -} - diff --git a/R/serialized_tests.R b/R/serialized_tests.R index bf95ddb6e..53e045df0 100644 --- a/R/serialized_tests.R +++ b/R/serialized_tests.R @@ -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, @@ -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, @@ -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)), @@ -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, diff --git a/R/style_guides.R b/R/style_guides.R index e350845de..6ab6ea876 100644 --- a/R/style_guides.R +++ b/R/style_guides.R @@ -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, @@ -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. @@ -152,7 +152,7 @@ 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, @@ -160,7 +160,7 @@ create_style_guide <- function(filler = create_filler, use_raw_indention = FALSE) { lst( # transformer functions - filler, + initialize, line_break, space, token, diff --git a/R/transform.R b/R/transform.R index d7aa593bf..4d1a4cdfe 100644 --- a/R/transform.R +++ b/R/transform.R @@ -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) ) diff --git a/man/create_filler.Rd b/man/create_filler.Rd deleted file mode 100644 index ae8910010..000000000 --- a/man/create_filler.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/parsed.R -\name{create_filler} -\alias{create_filler} -\title{Enrich parse table with space and line break information} -\usage{ -create_filler(pd_flat) -} -\arguments{ -\item{pd_flat}{A parse table.} -} -\value{ -A parse table with two three columns: lag_newlines, newlines and -spaces. -} -\description{ -This function computes difference (as column and line difference) between two -entries in the parse table and adds this information to the table. -} diff --git a/man/create_node_from_nested_root.Rd b/man/create_node_from_nested_root.Rd index 9919dff13..b78a00a6b 100644 --- a/man/create_node_from_nested_root.Rd +++ b/man/create_node_from_nested_root.Rd @@ -19,6 +19,6 @@ at once. \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) } diff --git a/man/create_style_guide.Rd b/man/create_style_guide.Rd index 92c088ddd..3136b60e5 100644 --- a/man/create_style_guide.Rd +++ b/man/create_style_guide.Rd @@ -4,12 +4,12 @@ \alias{create_style_guide} \title{Create a style guide} \usage{ -create_style_guide(filler = create_filler, line_break = NULL, +create_style_guide(initialize = initialize_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE) } \arguments{ -\item{filler}{A filler function that initializes various variables on each +\item{initialize}{A function that initializes various variables on each level of nesting.} \item{line_break}{A list of transformer functions that manipulate line_break diff --git a/man/initialize_attributes.Rd b/man/initialize_attributes.Rd new file mode 100644 index 000000000..5d615e9f3 --- /dev/null +++ b/man/initialize_attributes.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/initialize.R +\name{initialize_attributes} +\alias{initialize_attributes} +\alias{initialize_newlines} +\alias{initialize_spaces} +\alias{initialize_multi_line} +\alias{initialize_indention_ref_id} +\alias{initialize_indent} +\alias{validate_parse_data} +\title{Enrich parse table with space and line break information} +\usage{ +initialize_attributes(pd_flat) + +initialize_newlines(pd_flat) + +initialize_spaces(pd_flat) + +initialize_multi_line(pd_flat) + +initialize_indention_ref_id(pd_flat) + +initialize_indent(pd_flat) + +validate_parse_data(pd_flat) +} +\arguments{ +\item{pd_flat}{A parse table.} +} +\description{ +This function computes difference (as column and line difference) between two +entries in the parse table and adds this information to the table. +} +\section{Functions}{ +\itemize{ +\item \code{initialize_newlines}: Initializes \code{newlines} and \code{lag_newlines}. + +\item \code{initialize_spaces}: Initializes \code{spaces}. + +\item \code{initialize_multi_line}: Initializes \code{multi_line}. + +\item \code{initialize_indention_ref_id}: Initializes \code{indention_ref_ind}. + +\item \code{initialize_indent}: Initializes \code{indent}. + +\item \code{validate_parse_data}: validates the parse data. +}} + diff --git a/man/update_indention.Rd b/man/update_indention.Rd index 0604013a4..a74d24665 100644 --- a/man/update_indention.Rd +++ b/man/update_indention.Rd @@ -25,7 +25,7 @@ indent_without_paren(pd, indent_by = 2) } \arguments{ \item{pd}{A nested or flat parse table that is already enhanced with -line break and space information via \code{\link[=create_filler]{create_filler()}}.} +line break and space information via \code{\link[=initialize_attributes]{initialize_attributes()}}.} \item{indent_by}{How many spaces should be added after the token of interest.} diff --git a/vignettes/customizing_styler.Rmd b/vignettes/customizing_styler.Rmd index 401a33a3b..598902c82 100644 --- a/vignettes/customizing_styler.Rmd +++ b/vignettes/customizing_styler.Rmd @@ -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 @@ -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`, diff --git a/vignettes/gsoc_proposal/manipulating_nested_parse_data.Rmd b/vignettes/gsoc_proposal/manipulating_nested_parse_data.Rmd index 8e2ddec4f..414f34dbe 100644 --- a/vignettes/gsoc_proposal/manipulating_nested_parse_data.Rmd +++ b/vignettes/gsoc_proposal/manipulating_nested_parse_data.Rmd @@ -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