diff --git a/API b/API index e03fbdc3d..afc7bbff5 100644 --- a/API +++ b/API @@ -6,10 +6,10 @@ create_style_guide(initialize = default_style_guide_attributes, line_break = NUL default_style_guide_attributes(pd_flat) specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'")) specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE) -style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL) -style_file(path, ..., style = tidyverse_style, transformers = style(...)) -style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R") -style_text(text, ..., style = tidyverse_style, transformers = style(...)) +style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL, include_roxygen_examples = TRUE) +style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE) +style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) +style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE) tidyverse_math_token_spacing() tidyverse_reindention() tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE, reindention = tidyverse_reindention(), math_token_spacing = tidyverse_math_token_spacing()) diff --git a/DESCRIPTION b/DESCRIPTION index 84f0b5265..20cae7759 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,15 @@ Package: styler Title: Non-Invasive Pretty Printing of R Code Version: 1.0.2.9000 +Date: 2018-06-21 Authors@R: c(person("Kirill", "Müller", role = c("aut"), email = "krlmlr+r@mailbox.org"), person("Lorenz", "Walthert", role = c("cre", "aut"), email = "lorenz.walthert@icloud.com")) Description: Pretty-prints R code without changing the user's formatting intent. -Imports: +License: GPL-3 +URL: https://github.com/r-lib/styler +BugReports: https://github.com/r-lib/styler/issues +Imports: backports, cli, enc (>= 0.2), @@ -15,8 +19,9 @@ Imports: rlang, rprojroot, tibble (>= 1.4.2), + tools, withr -Suggests: +Suggests: data.tree, dplyr, here, @@ -24,15 +29,11 @@ Suggests: rmarkdown, rstudioapi, testthat -License: GPL-3 +VignetteBuilder: knitr Encoding: UTF-8 LazyData: true -Date: 2018-06-21 -BugReports: https://github.com/r-lib/styler/issues -URL: https://github.com/r-lib/styler Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "pkgapi::api_roclet")) RoxygenNote: 6.0.1 -VignetteBuilder: knitr Collate: 'addins.R' 'communicate.R' @@ -47,6 +48,10 @@ Collate: 'reindent.R' 'token-define.R' 'relevel.R' + 'roxygen-examples-add-remove.R' + 'roxygen-examples-find.R' + 'roxygen-examples-parse.R' + 'roxygen-examples.R' 'rules-line-break.R' 'rules-other.R' 'rules-replacement.R' diff --git a/NAMESPACE b/NAMESPACE index 5d185f4f3..a2c5581f5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,7 +22,9 @@ importFrom(purrr,flatten_int) importFrom(purrr,map) importFrom(purrr,map2) importFrom(purrr,map2_lgl) +importFrom(purrr,map_at) importFrom(purrr,map_chr) +importFrom(purrr,map_int) importFrom(purrr,map_lgl) importFrom(purrr,partial) importFrom(purrr,pmap) diff --git a/R/addins.R b/R/addins.R index 0b5e96e99..66e74d3c8 100644 --- a/R/addins.R +++ b/R/addins.R @@ -20,7 +20,9 @@ NULL #' `strict = TRUE`. #' @keywords internal style_active_file <- function() { - transformer <- make_transformer(tidyverse_style()) + transformer <- make_transformer(tidyverse_style(), + include_roxygen_examples = TRUE + ) context <- get_rstudio_context() if (is_rmd_file(context$path)) { out <- transform_rmd(context$contents, transformer) diff --git a/R/roxygen-examples-add-remove.R b/R/roxygen-examples-add-remove.R new file mode 100644 index 000000000..910cc59f5 --- /dev/null +++ b/R/roxygen-examples-add-remove.R @@ -0,0 +1,40 @@ +#' Remove dont* mask +#' +#' @param roxygen Roxygen code examples that contains a dont* segment only. +#' @keywords internal +#' @importFrom rlang seq2 +remove_dont_mask <- function(roxygen) { + mask <- c( + 1L, 2L, if (roxygen[3] == "\n") 3L, last(which(roxygen == "}")) + ) %>% sort() + list( + code = roxygen[-mask], mask = paste(roxygen[seq2(1, 2)], collapse = "") + ) +} + +remove_blank_lines <- function(code) { + code[code != "\n"] +} + +remove_roxygen_mask <- function(text) { + code_with_header <- gsub(pattern = "^#'\\s*", "", text) + remove_roxygen_header(code_with_header) +} + +#' Remove roxygen header +#' +#' Can't simply remove the element with the regex because it may happen that +#' the roxygen tag is on the same line as its contents start. +#' @examples +#' #' @examples c(1, 2) +#' @keywords internal +remove_roxygen_header <- function(text) { + text <- gsub("^\\s*@examples\\s*", "", text, perl = TRUE) + starts_with_blank <- text[1] == "\n" + c(text[1][!starts_with_blank], text[-1]) +} + +#' @importFrom purrr map_chr +add_roxygen_mask <- function(text) { + c(paste0("#' @examples"), map_chr(text, ~paste0("#' ", .x))) +} diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R new file mode 100644 index 000000000..15ea0925d --- /dev/null +++ b/R/roxygen-examples-find.R @@ -0,0 +1,56 @@ +#' Figure out where code examples start and stop +#' +#' Finds the sequence from start to stop of the lines in `text` that are +#' code examples in roxygen comments. +#' @param text A text consisting of code and/or roxygen comments. +#' @importFrom purrr map_int map2 +#' @importFrom rlang seq2 +#' @keywords internal +identify_start_to_stop_of_roxygen_examples_from_text <- function(text) { + starts <- grep("^#'\\s*@examples", text, perl = TRUE) + stop_candidates <- grep("^[^#]|^#'\\s*@", text, perl = TRUE) + stops <- map_int(starts, match_stop_to_start, stop_candidates) + map2(starts, stops, seq2) +} + +identify_start_to_stop_of_roxygen_examples <- function(path) { + content <- enc::read_lines_enc(path) + identify_start_to_stop_of_roxygen_examples_from_text(content) +} + +#' Match a stop candidate to a start +#' @param start An integer. +#' @param stop_candidates Potential stop candidates. +#' @examples +#' styler:::match_stop_to_start(1, c(3, 4, 5)) +#' @keywords internal +match_stop_to_start <- function(start, stop_candidates) { + min(stop_candidates[stop_candidates > start]) - 1L +} + +#' Find dontrun and friend sequences +#' +#' Returns the indices of the lines that correspond to a `dontrun` or +#' friends sequence. +#' @param bare Bare code. +#' @importFrom purrr map2 map_int +#' @keywords internal +find_dont_seqs <- function(bare) { + dont_openings <- which(bare %in% dont_keywords()) + dont_type <- bare[dont_openings] + dont_closings <- map_int(dont_openings + 1L, find_dont_closings, bare = bare) + map2(dont_openings, dont_closings, seq2) +} + +#' @importFrom rlang seq2 +find_dont_closings <- function(bare, dont_openings) { + opening <- cumsum(bare == "{") + closing <- cumsum(bare == "}") + diff <- opening - closing + level_dont <- diff[dont_openings] + match_closing <- intersect( + seq2(dont_openings + 1L, length(bare)), + which(diff == level_dont - 1L) + )[1] + match_closing + 1L +} diff --git a/R/roxygen-examples-parse.R b/R/roxygen-examples-parse.R new file mode 100644 index 000000000..334a58b22 --- /dev/null +++ b/R/roxygen-examples-parse.R @@ -0,0 +1,38 @@ +#' Parse roxygen comments into text +#' +#' Used to parse roxygen code examples. Removes line break before +#' `\\dontrun{...}` and friends because it does not occurr for segments other +#' than `\\dont{...}` and friends. +#' @param roxygen Roxygen comments. +#' @examples +#' styler:::parse_roxygen(c( +#' "#' @examples", +#' "#' 1+ 1" +#' )) +#' styler:::parse_roxygen(c( +#' "#' @examples 33", +#' "#'1+ 1" +#' )) +#' @keywords internal +parse_roxygen <- function(roxygen) { + parsed <- remove_roxygen_mask(roxygen) %>% + textConnection() %>% + tools::parse_Rd(fragment = TRUE) %>% + as.character() + is_line_break <- parsed[1] == "\n" + c(parsed[1][!is_line_break], parsed[-1]) +} + +#' Changing the line definition +#' +#' Input: New line denoted with `\\n`. Lines can span accross elements. +#' Output: Each element in the vector is one line. +#' +#' @param raw Raw code to post-process. +#' @keywords internal +post_parse_roxygen <- function(raw) { + split <- raw %>% + paste0(collapse = "") %>% + strsplit("\n", fixed = TRUE) + split[[1]] +} diff --git a/R/roxygen-examples.R b/R/roxygen-examples.R new file mode 100644 index 000000000..0869968aa --- /dev/null +++ b/R/roxygen-examples.R @@ -0,0 +1,80 @@ +#' Style a roxygen code example that may contain dontrun and friends +#' +#' Parses roxygen2 comments into code, breaks it into dont* (dontrun, dontest, +#' dontshow) and run sections and processes each segment indicidually using +#' [style_roxygen_example_snippet()]. +#' @inheritParams parse_transform_serialize_r +#' @param example Roxygen example code. +#' @inheritSection parse_transform_serialize_roxygen Hierarchy +#' @importFrom purrr map flatten_chr +#' @keywords internal +style_roxygen_code_example <- function(example, transformers) { + bare <- parse_roxygen(example) + one_dont <- split(bare, factor(cumsum(bare %in% dont_keywords()))) + map(one_dont, style_roxygen_code_example_segment, transformers) %>% + flatten_chr() %>% + add_roxygen_mask() +} + +#' Style a roxygen code example segment +#' +#' A roxygen code example segment corresponds to roxygen example code that +#' contains at most one `\\dontrun{...}` or friends. +#' We drop all newline characters first because otherwise the code segment +#' passed to this function was previously parsed with [parse_roxygen()] and +#' line-breaks in and after the `\\dontrun{...}` are expressed with `"\n"`, which +#' contradicts to the definition used elsewhere in this package, where every +#' element in a vector corresponds to a line. These line-breaks don't get +#' eliminated because they move to the front of a `code_segment` and +#' `style_text("\n1")` gives `"\n1"`, i.e. trailing newlines are not +#' eliminated. +#' @param one_dont Bare R code containing at most one `\\dontrun{...}` or +#' friends. +#' @inheritParams parse_transform_serialize_r +#' @inheritSection parse_transform_serialize_roxygen Hierarchy +#' @importFrom rlang seq2 +#' @importFrom purrr map2 flatten_chr +#' @keywords internal +style_roxygen_code_example_segment <- function(one_dont, transformers) { + if (length(one_dont) < 1L) return(character()) + dont_seqs <- find_dont_seqs(one_dont) + split_segments <- split_roxygen_segments(one_dont, unlist(dont_seqs)) + is_dont <- + seq2(1L, length(split_segments$separated)) %in% split_segments$selectors + + map2(split_segments$separated, is_dont, + style_roxygen_example_snippet, + transformers = transformers + ) %>% + flatten_chr() +} + +#' Given a code snippet is dont* or run, style it +#' +#' @param code_snippet A character vector with code to style. +#' @param is_dont Whether the snippet to process is a dontrun, dontshow, +#' donttest segemnt or not. +#' @inheritParams parse_transform_serialize_r +#' @inheritSection parse_transform_serialize_roxygen Hierarchy +#' @keywords internal +style_roxygen_example_snippet <- function(code_snippet, + transformers, + is_dont) { + if (is_dont) { + decomposed <- remove_dont_mask(code_snippet) + code_snippet <- decomposed$code + mask <- decomposed$mask + } + code_snippet <- post_parse_roxygen(code_snippet) %>% + paste0(collapse = "\n") %>% + parse_transform_serialize_r(transformers) + + if (is_dont) { + code_snippet <- c(mask, code_snippet, "}") + } + code_snippet +} + +dont_keywords <- function() { + c("\\dontrun", "\\dontshow", "\\donttest") +} diff --git a/R/testing.R b/R/testing.R index 82728d5c6..466be9eea 100644 --- a/R/testing.R +++ b/R/testing.R @@ -8,17 +8,13 @@ #' @param sub_test A regex pattern to further reduce the amount of test files #' to be tested in the test. `sub_test` must match the beginning of file #' names in tests/testthat. `NULL` matches all files. -#' @details Each file name that matches `test` and `sub_test` and ends with -#' "-in.R" is considered as an input to test. Its counterpart, -#' the reference to compare it against is the *-out.R file. It is constructed -#' by taking the substring of the *-in.R file before the -#' first dash and adding -out.R. This allows for multiple in.R files to -#' share one out.R file. You could have one_line-out.R as the reference to -#' compare one_line-random-something-stuff-in.R and -#' one_line-random-but-not-so-much-in.R. -#' -#' This also implies that -out.R files cannot have more than one dash in -#' their name, i.e. just the one before out.R. +#' @details +#' Each file name that matches `test` and `sub_test` and ends with +#' "-in.R" is considered as an input to test. Its counterpart, +#' the reference to compare it against is the *-out.R file. It is constructed +#' by taking the substring of the *-in.R file before the +#' last dash and adding -out.R. In contrast to older versions of this +#' function, every *-out.R file has just one in file. #' @inheritParams transform_and_check #' @importFrom purrr flatten_chr pwalk map #' @keywords internal @@ -68,7 +64,7 @@ test_collection <- function(test, sub_test = NULL, #' "path/to/file/first-extended-in.R")) #' @keywords internal construct_out <- function(in_paths) { - gsub("\\-.*([.]R(?:|md))$", "\\-out\\1", in_paths) + gsub("\\-in([.]R(?:|md))$", "\\-out\\1", in_paths) } #' Construct paths of a tree object given the paths of *-in.R files @@ -125,10 +121,7 @@ transform_and_check <- function(in_item, out_item, immediate. = TRUE, call. = FALSE ) } else { - message( - in_name, " was identical to ", out_name, - immediate. = TRUE, call. = FALSE - ) + message(in_name, " was identical to ", out_name) } } @@ -165,7 +158,7 @@ style_empty <- function(text) { reindention = specify_reindention(), NULL ) - transformed_text <- parse_transform_serialize(text, transformers) + transformed_text <- parse_transform_serialize_r(text, transformers) transformed_text } @@ -184,7 +177,7 @@ style_op <- function(text) { NULL ) - transformed_text <- parse_transform_serialize(text, transformers) + transformed_text <- parse_transform_serialize_r(text, transformers) transformed_text } diff --git a/R/transform-files.R b/R/transform-files.R index 620a3b68c..3710228bb 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -9,8 +9,8 @@ #' Invisibly returns a data frame that indicates for each file considered for #' styling whether or not it was actually changed. #' @keywords internal -transform_files <- function(files, transformers) { - transformer <- make_transformer(transformers) +transform_files <- function(files, transformers, include_roxygen_examples) { + transformer <- make_transformer(transformers, include_roxygen_examples) max_char <- min(max(nchar(files), 0), 80) if (length(files) > 0L) { cat("Styling ", length(files), " files:\n") @@ -51,7 +51,7 @@ transform_file <- function(path, cat( message_before, path, - rep_char(" ", max(0, n_spaces_before_message_after)), + rep_char(" ", max(0L, n_spaces_before_message_after)), append = FALSE ) changed <- transform_code(path, fun = fun, verbose = verbose, ...) @@ -77,22 +77,94 @@ transform_file <- function(path, #' that should be transformed. #' @param transformers A list of transformer functions that operate on flat #' parse tables. +#' @param include_roxygen_examples Whether or not to style code in roxygen +#' examples. #' @keywords internal -make_transformer <- function(transformers) { +#' @importFrom purrr when +make_transformer <- function(transformers, include_roxygen_examples) { force(transformers) function(text) { - transformed_text <- parse_transform_serialize(text, transformers) - transformed_text + transformed_code <- text %>% + parse_transform_serialize_r(transformers) %>% + when(include_roxygen_examples ~ + parse_transform_serialize_roxygen(., transformers), + ~. + ) + transformed_code } } +#' Parse, transform and serialize roxygen comments +#' +#' Splits `text` into roxygen code examples and non-roxygen code examples and +#' then maps over these examples by applyingj +#' [style_roxygen_code_example()]. +#' @section Hierarchy: +#' Styling involves splitting roxygen example code into segments, and segments +#' into snippets. This describes the proccess for input of +#' [parse_transform_serialize_roxygen()]: +#' +#' - Splitting code into roxygen example code and other code. Downstream, +#' we are only concerned about roxygen code. See +#' [parse_transform_serialize_roxygen()]. +#' - Every roxygen example code can have zero or more +#' dontrun / dontshow / donttest sequences. We next create segments of roxygen +#' code examples that contain at most one of these. See +#' [style_roxygen_code_example()]. +#' - We further split the segment that contains at most one dont* sequence into +#' snippets that are either don* or not. See +#' [style_roxygen_code_example_segment()]. +#' +#' Finally, that we have roxygen code snippets that are either dont* or not, +#' we style them in [style_roxygen_example_snippet()] using +#' [parse_transform_serialize_r()]. +#' @importFrom purrr map_at flatten_chr +#' @keywords internal +parse_transform_serialize_roxygen <- function(text, transformers) { + roxygen_seqs <- identify_start_to_stop_of_roxygen_examples_from_text(text) + if (length(roxygen_seqs) < 1L) return(text) + split_segments <- split_roxygen_segments(text, unlist(roxygen_seqs)) + map_at(split_segments$separated, split_segments$selectors, + style_roxygen_code_example, + transformers = transformers + ) %>% + flatten_chr() +} + +#' Split text into roxygen and non-roxygen example segments +#' +#' @param text Roxygen comments +#' @param roxygen_examples Integer sequence that indicates which lines in `text` +#' are roxygen examples. Most conveniently obtained with +#' [identify_start_to_stop_of_roxygen_examples_from_text]. +#' @return +#' A list with two elements: +#' +#' * A list that contains elements grouped into roxygen and non-rogxygen +#' sections. This list is named `separated`. +#' * An integer vector with the indices that correspond to roxygen code +#' examples in `separated`. +#' @importFrom rlang seq2 +#' @keywords internal +split_roxygen_segments <- function(text, roxygen_examples) { + if (is.null(roxygen_examples)) return(lst(separated = list(text), selectors = NULL)) + all_lines <- seq2(1L, length(text)) + active_segemnt <- as.integer(all_lines %in% roxygen_examples) + segment_id <- cumsum(abs(c(0L, diff(active_segemnt)))) + 1L + separated <- split(text, factor(segment_id)) + restyle_selector <- ifelse(roxygen_examples[1] == 1L, odd_index, even_index) + + lst(separated, selectors = restyle_selector(separated)) +} + #' Parse, transform and serialize text #' #' Wrapper function for the common three operations. #' @inheritParams compute_parse_data_nested #' @inheritParams apply_transformers +#' @seealso [parse_transform_serialize_roxygen()] #' @keywords internal -parse_transform_serialize <- function(text, transformers) { +parse_transform_serialize_r <- function(text, transformers) { text <- assert_text(text) pd_nested <- compute_parse_data_nested(text) start_line <- find_start_line(pd_nested) diff --git a/R/ui.R b/R/ui.R index dddec1a4e..402491ab8 100644 --- a/R/ui.R +++ b/R/ui.R @@ -23,6 +23,8 @@ NULL #' or `c("r", "rmd")`. #' @param exclude_files Character vector with paths to files that should be #' excluded from styling. +#' @param include_roxygen_examples Whether or not to style code in roxygen +#' examples. #' @section Warning: #' This function overwrites files (if styling results in a change of the #' code to be formatted). It is strongly suggested to only style files @@ -69,15 +71,19 @@ style_pkg <- function(pkg = ".", style = tidyverse_style, transformers = style(...), filetype = "R", - exclude_files = "R/RcppExports.R") { + exclude_files = "R/RcppExports.R", + include_roxygen_examples = TRUE) { pkg_root <- rprojroot::find_package_root_file(path = pkg) - changed <- withr::with_dir(pkg_root, - prettify_pkg(transformers, filetype, exclude_files) - ) + changed <- withr::with_dir(pkg_root, prettify_pkg( + transformers, filetype, exclude_files, include_roxygen_examples + )) invisible(changed) } -prettify_pkg <- function(transformers, filetype, exclude_files) { +prettify_pkg <- function(transformers, + filetype, + exclude_files, + include_roxygen_examples) { filetype <- set_and_assert_arg_filetype(filetype) r_files <- vignette_files <- readme <- NULL @@ -97,7 +103,7 @@ prettify_pkg <- function(transformers, filetype, exclude_files) { } files <- setdiff(c(r_files, vignette_files, readme), exclude_files) - transform_files(files, transformers) + transform_files(files, transformers, include_roxygen_examples) } @@ -121,8 +127,9 @@ prettify_pkg <- function(transformers, filetype, exclude_files) { style_text <- function(text, ..., style = tidyverse_style, - transformers = style(...)) { - transformer <- make_transformer(transformers) + transformers = style(...), + include_roxygen_examples = TRUE) { + transformer <- make_transformer(transformers, include_roxygen_examples) styled_text <- transformer(text) construct_vertical(styled_text) } @@ -150,9 +157,12 @@ style_dir <- function(path = ".", transformers = style(...), filetype = "R", recursive = TRUE, - exclude_files = NULL) { + exclude_files = NULL, + include_roxygen_examples = TRUE) { changed <- withr::with_dir( - path, prettify_any(transformers, filetype, recursive, exclude_files) + path, prettify_any( + transformers, filetype, recursive, exclude_files, include_roxygen_examples + ) ) invisible(changed) } @@ -164,12 +174,18 @@ style_dir <- function(path = ".", #' @param recursive A logical value indicating whether or not files in subdirectories #' should be styled as well. #' @keywords internal -prettify_any <- function(transformers, filetype, recursive, exclude_files) { +prettify_any <- function(transformers, + filetype, + recursive, + exclude_files, + include_roxygen_examples) { files <- dir( path = ".", pattern = map_filetype_to_pattern(filetype), ignore.case = TRUE, recursive = recursive, full.names = TRUE ) - transform_files(setdiff(files, exclude_files), transformers) + transform_files( + setdiff(files, exclude_files), transformers, include_roxygen_examples + ) } #' Style `.R` and/or `.Rmd` files @@ -197,10 +213,11 @@ prettify_any <- function(transformers, filetype, recursive, exclude_files) { style_file <- function(path, ..., style = tidyverse_style, - transformers = style(...)) { + transformers = style(...), + include_roxygen_examples = TRUE) { changed <- withr::with_dir( dirname(path), - transform_files(basename(path), transformers) + transform_files(basename(path), transformers, include_roxygen_examples) ) invisible(changed) } diff --git a/R/utils.R b/R/utils.R index 269953861..37c68ede7 100644 --- a/R/utils.R +++ b/R/utils.R @@ -13,11 +13,21 @@ two_cols_match <- function(col1, col2, data) { } odd <- function(x) { - x[seq(1L, length(x), by = 2)] + x[odd_index(x)] +} + +odd_index <- function(x) { + if (length(x) < 1) return(NULL) + seq(1L, length(x), by = 2) } even <- function(x) { - x[seq(2L, length(x), by = 2)] + if (length(x) < 2) return(NULL) + x[even_index(x)] +} + +even_index <- function(x) { + seq(2L, length(x), by = 2) } #' Repeat elements of a character vector `times` times and collapse it diff --git a/man/find_dont_seqs.Rd b/man/find_dont_seqs.Rd new file mode 100644 index 000000000..b2b9d8bcd --- /dev/null +++ b/man/find_dont_seqs.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-find.R +\name{find_dont_seqs} +\alias{find_dont_seqs} +\title{Find dontrun and friend sequences} +\usage{ +find_dont_seqs(bare) +} +\arguments{ +\item{bare}{Bare code.} +} +\description{ +Returns the indices of the lines that correspond to a \code{dontrun} or +friends sequence. +} +\keyword{internal} diff --git a/man/identify_start_to_stop_of_roxygen_examples_from_text.Rd b/man/identify_start_to_stop_of_roxygen_examples_from_text.Rd new file mode 100644 index 000000000..7bb975d59 --- /dev/null +++ b/man/identify_start_to_stop_of_roxygen_examples_from_text.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-find.R +\name{identify_start_to_stop_of_roxygen_examples_from_text} +\alias{identify_start_to_stop_of_roxygen_examples_from_text} +\title{Figure out where code examples start and stop} +\usage{ +identify_start_to_stop_of_roxygen_examples_from_text(text) +} +\arguments{ +\item{text}{A text consisting of code and/or roxygen comments.} +} +\description{ +Finds the sequence from start to stop of the lines in \code{text} that are +code examples in roxygen comments. +} +\keyword{internal} diff --git a/man/make_transformer.Rd b/man/make_transformer.Rd index def49be5c..1a4c9cc88 100644 --- a/man/make_transformer.Rd +++ b/man/make_transformer.Rd @@ -4,11 +4,14 @@ \alias{make_transformer} \title{Closure to return a transformer function} \usage{ -make_transformer(transformers) +make_transformer(transformers, include_roxygen_examples) } \arguments{ \item{transformers}{A list of transformer functions that operate on flat parse tables.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ This function takes a list of transformer functions as input and diff --git a/man/match_stop_to_start.Rd b/man/match_stop_to_start.Rd new file mode 100644 index 000000000..9701156af --- /dev/null +++ b/man/match_stop_to_start.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-find.R +\name{match_stop_to_start} +\alias{match_stop_to_start} +\title{Match a stop candidate to a start} +\usage{ +match_stop_to_start(start, stop_candidates) +} +\arguments{ +\item{start}{An integer.} + +\item{stop_candidates}{Potential stop candidates.} +} +\description{ +Match a stop candidate to a start +} +\examples{ +styler:::match_stop_to_start(1, c(3, 4, 5)) +} +\keyword{internal} diff --git a/man/parse_roxygen.Rd b/man/parse_roxygen.Rd new file mode 100644 index 000000000..e2abfe895 --- /dev/null +++ b/man/parse_roxygen.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-parse.R +\name{parse_roxygen} +\alias{parse_roxygen} +\title{Parse roxygen comments into text} +\usage{ +parse_roxygen(roxygen) +} +\arguments{ +\item{roxygen}{Roxygen comments.} +} +\description{ +Used to parse roxygen code examples. Removes line break before +\code{\\dontrun{...}} and friends because it does not occurr for segments other +than \code{\\dont{...}} and friends. +} +\examples{ +styler:::parse_roxygen(c( +"#' @examples", + "#' 1+ 1" +)) +styler:::parse_roxygen(c( +"#' @examples 33", + "#' 1+ 1" +)) +styler:::parse_roxygen(c( +"#' @examples", +"#' \\\dontrun{1+ 1}" +)) +} +\keyword{internal} diff --git a/man/parse_transform_serialize.Rd b/man/parse_transform_serialize_r.Rd similarity index 62% rename from man/parse_transform_serialize.Rd rename to man/parse_transform_serialize_r.Rd index 045fa867f..a7d850e6c 100644 --- a/man/parse_transform_serialize.Rd +++ b/man/parse_transform_serialize_r.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/transform-files.R -\name{parse_transform_serialize} -\alias{parse_transform_serialize} +\name{parse_transform_serialize_r} +\alias{parse_transform_serialize_r} \title{Parse, transform and serialize text} \usage{ -parse_transform_serialize(text, transformers) +parse_transform_serialize_r(text, transformers) } \arguments{ \item{text}{A character vector to parse.} @@ -14,4 +14,7 @@ parse_transform_serialize(text, transformers) \description{ Wrapper function for the common three operations. } +\seealso{ +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}} +} \keyword{internal} diff --git a/man/parse_transform_serialize_roxygen.Rd b/man/parse_transform_serialize_roxygen.Rd new file mode 100644 index 000000000..fed1b79c7 --- /dev/null +++ b/man/parse_transform_serialize_roxygen.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform-files.R +\name{parse_transform_serialize_roxygen} +\alias{parse_transform_serialize_roxygen} +\title{Parse, transform and serialize roxygen comments} +\usage{ +parse_transform_serialize_roxygen(text, transformers) +} +\description{ +Splits \code{text} into roxygen code examples and non-roxygen code examples and +then maps over these examples by applyingj +\code{\link[=style_roxygen_code_example]{style_roxygen_code_example()}}. +} +\section{Hierarchy}{ + +Styling involves splitting roxygen example code into segments, and segments +into snippets. This describes the proccess for input of +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}: +\itemize{ +\item Splitting code into roxygen example code and other code. Downstream, +we are only concerned about roxygen code. See +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}. +\item Every roxygen example code can have zero or more +dontrun / dontshow / donttest sequences. We next create segments of roxygen +code examples that contain at most one of these. See +\code{\link[=style_roxygen_code_example]{style_roxygen_code_example()}}. +\item We further split the segment that contains at most one dont* sequence into +snippets that are either don* or not. See +\code{\link[=style_roxygen_code_example_segment]{style_roxygen_code_example_segment()}}. +} + +Finally, that we have roxygen code snippets that are either dont* or not, +we style them in \code{\link[=style_roxygen_example_snippet]{style_roxygen_example_snippet()}} using +\code{\link[=parse_transform_serialize_r]{parse_transform_serialize_r()}}. +} + +\keyword{internal} diff --git a/man/post_parse_roxygen.Rd b/man/post_parse_roxygen.Rd new file mode 100644 index 000000000..b1d926849 --- /dev/null +++ b/man/post_parse_roxygen.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-parse.R +\name{post_parse_roxygen} +\alias{post_parse_roxygen} +\title{Changing the line definition} +\usage{ +post_parse_roxygen(raw) +} +\arguments{ +\item{raw}{Raw code to post-process.} +} +\description{ +Input: New line denoted with \code{\\n}. Lines can span accross elements. +Output: Each element in the vector is one line. +} +\keyword{internal} diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index 71914a9a5..1b2a1b546 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -4,7 +4,8 @@ \alias{prettify_any} \title{Prettify R code in current working directory} \usage{ -prettify_any(transformers, filetype, recursive, exclude_files) +prettify_any(transformers, filetype, recursive, exclude_files, + include_roxygen_examples) } \arguments{ \item{transformers}{A set of transformer functions. This argument is most @@ -20,6 +21,9 @@ should be styled as well.} \item{exclude_files}{Character vector with paths to files that should be excluded from styling.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ This is a helper function for style_dir. diff --git a/man/remove_dont_mask.Rd b/man/remove_dont_mask.Rd new file mode 100644 index 000000000..616fbcb51 --- /dev/null +++ b/man/remove_dont_mask.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-add-remove.R +\name{remove_dont_mask} +\alias{remove_dont_mask} +\title{Remove dont* mask} +\usage{ +remove_dont_mask(roxygen) +} +\arguments{ +\item{roxygen}{Roxygen code examples that contains a dont* segment only.} +} +\description{ +Remove dont* mask +} +\keyword{internal} diff --git a/man/remove_roxygen_header.Rd b/man/remove_roxygen_header.Rd new file mode 100644 index 000000000..0eaa658f4 --- /dev/null +++ b/man/remove_roxygen_header.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples-add-remove.R +\name{remove_roxygen_header} +\alias{remove_roxygen_header} +\title{Remove roxygen header} +\usage{ +remove_roxygen_header(text) +} +\description{ +Can't simply remove the element with the regex because it may happen that +the roxygen tag is on the same line as its contents start. +} +\examples{ +#' @examples c(1, 2) +} +\keyword{internal} diff --git a/man/split_roxygen_segments.Rd b/man/split_roxygen_segments.Rd new file mode 100644 index 000000000..a42689754 --- /dev/null +++ b/man/split_roxygen_segments.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform-files.R +\name{split_roxygen_segments} +\alias{split_roxygen_segments} +\title{Split text into roxygen and non-roxygen example segments} +\usage{ +split_roxygen_segments(text, roxygen_examples) +} +\arguments{ +\item{text}{Roxygen comments} + +\item{roxygen_examples}{Integer sequence that indicates which lines in \code{text} +are roxygen examples. Most conveniently obtained with +\link{identify_start_to_stop_of_roxygen_examples_from_text}.} +} +\value{ +A list with two elements: +\itemize{ +\item A list that contains elements grouped into roxygen and non-rogxygen +sections. This list is named \code{separated}. +\item An integer vector with the indices that correspond to roxygen code +examples in \code{separated}. +} +} +\description{ +Split text into roxygen and non-roxygen example segments +} +\keyword{internal} diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 54daa2dfa..676a05c56 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -6,7 +6,7 @@ \usage{ style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, - exclude_files = NULL) + exclude_files = NULL, include_roxygen_examples = TRUE) } \arguments{ \item{path}{Path to a directory with files to transform.} @@ -31,6 +31,9 @@ of \code{path} should be styled as well.} \item{exclude_files}{Character vector with paths to files that should be excluded from styling.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ Performs various substitutions in all \code{.R} files in a directory. diff --git a/man/style_file.Rd b/man/style_file.Rd index 53d18bd62..83f5cde06 100644 --- a/man/style_file.Rd +++ b/man/style_file.Rd @@ -4,7 +4,8 @@ \alias{style_file} \title{Style \code{.R} and/or \code{.Rmd} files} \usage{ -style_file(path, ..., style = tidyverse_style, transformers = style(...)) +style_file(path, ..., style = tidyverse_style, transformers = style(...), + include_roxygen_examples = TRUE) } \arguments{ \item{path}{A character vector with paths to files to style.} @@ -19,6 +20,9 @@ further except to construct the argument \code{transformers}. See \item{transformers}{A set of transformer functions. This argument is most conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ Performs various substitutions in the files specified. diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 3f2b0433a..2587e7933 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -6,7 +6,7 @@ \usage{ style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", - exclude_files = "R/RcppExports.R") + exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) } \arguments{ \item{pkg}{Path to a (subdirectory of an) R package.} @@ -28,6 +28,9 @@ or \code{c("r", "rmd")}.} \item{exclude_files}{Character vector with paths to files that should be excluded from styling.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ Performs various substitutions in all \code{.R} files in a package diff --git a/man/style_roxygen_code_example.Rd b/man/style_roxygen_code_example.Rd new file mode 100644 index 000000000..661101a54 --- /dev/null +++ b/man/style_roxygen_code_example.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples.R +\name{style_roxygen_code_example} +\alias{style_roxygen_code_example} +\title{Style a roxygen code example that may contain dontrun and friends} +\usage{ +style_roxygen_code_example(example, transformers) +} +\arguments{ +\item{example}{Roxygen example code.} + +\item{transformers}{A list of \emph{named} transformer functions} +} +\description{ +Parses roxygen2 comments into code, breaks it into dont* (dontrun, dontest, +dontshow) and run sections and processes each segment indicidually using +\code{\link[=style_roxygen_example_snippet]{style_roxygen_example_snippet()}}. +} +\section{Hierarchy}{ + +Styling involves splitting roxygen example code into segments, and segments +into snippets. This describes the proccess for input of +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}: +\itemize{ +\item Splitting code into roxygen example code and other code. Downstream, +we are only concerned about roxygen code. See +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}. +\item Every roxygen example code can have zero or more +dontrun / dontshow / donttest sequences. We next create segments of roxygen +code examples that contain at most one of these. See +\code{\link[=style_roxygen_code_example]{style_roxygen_code_example()}}. +\item We further split the segment that contains at most one dont* sequence into +snippets that are either don* or not. See +\code{\link[=style_roxygen_code_example_segment]{style_roxygen_code_example_segment()}}. +} + +Finally, that we have roxygen code snippets that are either dont* or not, +we style them in \code{\link[=style_roxygen_example_snippet]{style_roxygen_example_snippet()}} using +\code{\link[=parse_transform_serialize_r]{parse_transform_serialize_r()}}. +} + +\keyword{internal} diff --git a/man/style_roxygen_code_example_segment.Rd b/man/style_roxygen_code_example_segment.Rd new file mode 100644 index 000000000..25dcf5607 --- /dev/null +++ b/man/style_roxygen_code_example_segment.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples.R +\name{style_roxygen_code_example_segment} +\alias{style_roxygen_code_example_segment} +\title{Style a roxygen code example segment} +\usage{ +style_roxygen_code_example_segment(one_dont, transformers) +} +\arguments{ +\item{one_dont}{Bare R code containing at most one \code{\\dontrun{...}} or +friends.} + +\item{transformers}{A list of \emph{named} transformer functions} +} +\description{ +A roxygen code example segment corresponds to roxygen example code that +contains at most one \code{\\dontrun{...}} or friends. +We drop all newline characters first because otherwise the code segment +passed to this function was previously parsed with \code{\link[=parse_roxygen]{parse_roxygen()}} and +line-breaks in and after the \code{\\dontrun{...}} are expressed with \code{"\n"}, which +contradicts to the definition used elsewhere in this package, where every +element in a vector corresponds to a line. These line-breaks don't get +eliminated because they move to the front of a \code{code_segment} and +\code{style_text("\n1")} gives \code{"\n1"}, i.e. trailing newlines are not +eliminated. +} +\section{Hierarchy}{ + +Styling involves splitting roxygen example code into segments, and segments +into snippets. This describes the proccess for input of +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}: +\itemize{ +\item Splitting code into roxygen example code and other code. Downstream, +we are only concerned about roxygen code. See +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}. +\item Every roxygen example code can have zero or more +dontrun / dontshow / donttest sequences. We next create segments of roxygen +code examples that contain at most one of these. See +\code{\link[=style_roxygen_code_example]{style_roxygen_code_example()}}. +\item We further split the segment that contains at most one dont* sequence into +snippets that are either don* or not. See +\code{\link[=style_roxygen_code_example_segment]{style_roxygen_code_example_segment()}}. +} + +Finally, that we have roxygen code snippets that are either dont* or not, +we style them in \code{\link[=style_roxygen_example_snippet]{style_roxygen_example_snippet()}} using +\code{\link[=parse_transform_serialize_r]{parse_transform_serialize_r()}}. +} + +\keyword{internal} diff --git a/man/style_roxygen_example_snippet.Rd b/man/style_roxygen_example_snippet.Rd new file mode 100644 index 000000000..10bbe383b --- /dev/null +++ b/man/style_roxygen_example_snippet.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen-examples.R +\name{style_roxygen_example_snippet} +\alias{style_roxygen_example_snippet} +\title{Given a code snippet is dont* or run, style it} +\usage{ +style_roxygen_example_snippet(code_snippet, transformers, is_dont) +} +\arguments{ +\item{code_snippet}{A character vector with code to style.} + +\item{transformers}{A list of \emph{named} transformer functions} + +\item{is_dont}{Whether the snippet to process is a dontrun, dontshow, +donttest segemnt or not.} +} +\description{ +Given a code snippet is dont* or run, style it +} +\section{Hierarchy}{ + +Styling involves splitting roxygen example code into segments, and segments +into snippets. This describes the proccess for input of +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}: +\itemize{ +\item Splitting code into roxygen example code and other code. Downstream, +we are only concerned about roxygen code. See +\code{\link[=parse_transform_serialize_roxygen]{parse_transform_serialize_roxygen()}}. +\item Every roxygen example code can have zero or more +dontrun / dontshow / donttest sequences. We next create segments of roxygen +code examples that contain at most one of these. See +\code{\link[=style_roxygen_code_example]{style_roxygen_code_example()}}. +\item We further split the segment that contains at most one dont* sequence into +snippets that are either don* or not. See +\code{\link[=style_roxygen_code_example_segment]{style_roxygen_code_example_segment()}}. +} + +Finally, that we have roxygen code snippets that are either dont* or not, +we style them in \code{\link[=style_roxygen_example_snippet]{style_roxygen_example_snippet()}} using +\code{\link[=parse_transform_serialize_r]{parse_transform_serialize_r()}}. +} + +\keyword{internal} diff --git a/man/style_text.Rd b/man/style_text.Rd index a7e0537b2..51200f137 100644 --- a/man/style_text.Rd +++ b/man/style_text.Rd @@ -4,7 +4,8 @@ \alias{style_text} \title{Style a string} \usage{ -style_text(text, ..., style = tidyverse_style, transformers = style(...)) +style_text(text, ..., style = tidyverse_style, transformers = style(...), + include_roxygen_examples = TRUE) } \arguments{ \item{text}{A character vector with text to style.} @@ -19,6 +20,9 @@ further except to construct the argument \code{transformers}. See \item{transformers}{A set of transformer functions. This argument is most conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ Styles a character vector. Each element of the character vector corresponds diff --git a/man/test_collection.Rd b/man/test_collection.Rd index ab0f8b0c6..a0a999c4b 100644 --- a/man/test_collection.Rd +++ b/man/test_collection.Rd @@ -36,12 +36,7 @@ Each file name that matches \code{test} and \code{sub_test} and ends with "-in.R" is considered as an input to test. Its counterpart, the reference to compare it against is the *-out.R file. It is constructed by taking the substring of the *-in.R file before the -first dash and adding -out.R. This allows for multiple in.R files to -share one out.R file. You could have one_line-out.R as the reference to -compare one_line-random-something-stuff-in.R and -one_line-random-but-not-so-much-in.R. - -This also implies that -out.R files cannot have more than one dash in -their name, i.e. just the one before out.R. +last dash and adding -out.R. In contrast to older versions of this +function, every *-out.R file has just one in file. } \keyword{internal} diff --git a/man/transform_files.Rd b/man/transform_files.Rd index 6edca2383..fcd1caf5b 100644 --- a/man/transform_files.Rd +++ b/man/transform_files.Rd @@ -4,7 +4,7 @@ \alias{transform_files} \title{Transform files with transformer functions} \usage{ -transform_files(files, transformers) +transform_files(files, transformers, include_roxygen_examples) } \arguments{ \item{files}{A character vector with paths to the file that should be @@ -12,6 +12,9 @@ transformed.} \item{transformers}{A list of transformer functions that operate on flat parse tables.} + +\item{include_roxygen_examples}{Whether or not to style code in roxygen +examples.} } \description{ \code{transform_files} applies transformations to file contents and writes back diff --git a/tests/testthat/indention_round_brackets/multi_line-out.R b/tests/testthat/indention_round_brackets/multi_line-no-indention-out.R similarity index 100% rename from tests/testthat/indention_round_brackets/multi_line-out.R rename to tests/testthat/indention_round_brackets/multi_line-no-indention-out.R diff --git a/tests/testthat/indention_round_brackets/multi_line-random-out.R b/tests/testthat/indention_round_brackets/multi_line-random-out.R new file mode 100644 index 000000000..e960a3d0d --- /dev/null +++ b/tests/testthat/indention_round_brackets/multi_line-random-out.R @@ -0,0 +1,9 @@ +call( + 1, + call2( + 2, 3, + call3(1, 2, 22), + 5 + ), + 144 +) diff --git a/tests/testthat/indention_round_brackets/one_line-nested-out.R b/tests/testthat/indention_round_brackets/one_line-nested-out.R new file mode 100644 index 000000000..1f0bb9616 --- /dev/null +++ b/tests/testthat/indention_round_brackets/one_line-nested-out.R @@ -0,0 +1 @@ +a <- xyz(x, 22, if (x > 1) 33 else 4) diff --git a/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in.R b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in.R new file mode 100644 index 000000000..5087af071 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in.R @@ -0,0 +1,7 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#'@examples style_pkg(style= tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in_tree b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in_tree new file mode 100644 index 000000000..6e65a6a78 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-in_tree @@ -0,0 +1,13 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Pr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' (c [1/0] {4} + ¦--COMMENT: #' Ca [1/0] {5} + ¦--COMMENT: #'@ex [1/0] {6} + °--expr: [1/0] {7} + ¦--expr: [0/1] {9} + ¦ °--SYMBOL: a [0/0] {8} + ¦--LEFT_ASSIGN: <- [0/1] {10} + °--expr: [0/0] {12} + °--NUM_CONST: 2 [0/0] {11} diff --git a/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-out.R b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-out.R new file mode 100644 index 000000000..9fc62c2f7 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/1-one-function-example-last-proper-run-out.R @@ -0,0 +1,8 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in.R b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in.R new file mode 100644 index 000000000..50756a7aa --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in.R @@ -0,0 +1,222 @@ +#' @api +#' @import tibble +#' @importFrom magrittr %>% +NULL + +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' +#' @param pkg Path to a (subdirectory of an) R package. +#' @param ... Arguments passed on to the `style` function. +#' @param style A function that creates a style guide to use, by default +#' [tidyverse_style()] (without the parentheses). Not used +#' further except to construct the argument `transformers`. See +#' [style_guides()] for details. +#' @param transformers A set of transformer functions. This argument is most +#' conveniently constructed via the `style` argument and `...`. See +#' 'Examples'. +#' @param filetype Vector of file extensions indicating which file types should +#' be styled. Case is ignored, and the `.` is optional, e.g. `c(".R", ".Rmd")` +#' or `c("r", "rmd")`. +#' @param exclude_files Character vector with paths to files that should be +#' excluded from styling. +#' @param include_roxygen_examples Whether or not to style code in roxygen +#' examples. +#' @section Warning: +#' This function overwrites files (if styling results in a change of the +#' code to be formatted). It is strongly suggested to only style files +#' that are under version control or to create a backup copy. +#' +#' We suggest to first style with `scope < "tokens"` and inspect and commit +#' changes, because these changes are guaranteed to leave the abstract syntax +#' tree (AST) unchanged. See section 'Roundtrip Validation' for details. +#' +#' Then, we suggest to style with `scope = "tokens"` (if desired) and carefully +#' inspect the changes to make sure the AST is not changed in an unexpected way +#' that invalidates code. +#' @section Roundtrip Validation: +#' The following section describes when and how styling is guaranteed to +#' yield correct code. +#' +#' If the style guide has `scope < "tokens"`, no tokens are changed and the +#' abstract syntax tree (AST) should not change. +#' Hence, it is possible to validate the styling by comparing whether the parsed +#' expression before and after styling have the same AST. +#' This comparison omits comments. styler compares +#' error if the AST has changed through styling. +#' +#' Note that with `scope = "tokens"` such a comparison is not conducted because +#' the AST might well change and such a change is intended. There is no way +#' styler can validate styling, that is why we inform the user to carefully +#' inspect the changes. +#' +#' See section 'Warning' for a good strategy to apply styling safely. +#' @inheritSection transform_files Value +#' @family stylers +#' @examples +#' \dontrun{ +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specify_math_token_spacing(zero = "'+'") +#' ) +#' } +#' @export +style_pkg <- function(pkg = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + exclude_files = "R/RcppExports.R", + include_roxygen_examples = TRUE) { + pkg_root <- rprojroot::find_package_root_file(path = pkg) + changed <- withr::with_dir(pkg_root, prettify_pkg( + transformers, filetype, exclude_files, include_roxygen_examples + )) + invisible(changed) +} + +prettify_pkg <- function(transformers, + filetype, + exclude_files, + include_roxygen_examples) { + filetype <- set_and_assert_arg_filetype(filetype) + r_files <- vignette_files <- readme <- NULL + + if ("\\.r" %in% filetype) { + r_files <- dir( + path = c("R", "tests", "data-raw"), pattern = "\\.r$", + ignore.case = TRUE, recursive = TRUE, full.names = TRUE + ) + } + + if ("\\.rmd" %in% filetype) { + vignette_files <- dir( + path = "vignettes", pattern = "\\.rmd$", + ignore.case = TRUE, recursive = TRUE, full.names = TRUE + ) + readme <- dir(pattern = "^readme\\.rmd$", ignore.case = TRUE) + } + + files <- setdiff(c(r_files, vignette_files, readme), exclude_files) + transform_files(files, transformers, include_roxygen_examples) +} + + +#' Style a string +#' +#' Styles a character vector. Each element of the character vector corresponds +#' to one line of code. +#' @param text A character vector with text to style. +#' @inheritParams style_pkg +#' @family stylers +#' @examples +#' style_text("call( 1)") +#' style_text("1 + 1", strict = FALSE) +#' style_text("a%>%b", scope = "spaces") +#' style_text("a%>%b; a", scope = "line_breaks") +#' style_text("a%>%b; a", scope = "tokens") +#' # the following is identical but the former is more convenient: +#' style_text("a<-3++1", style = tidyverse_style, strict = TRUE) +#' style_text("a<-3++1", transformers = tidyverse_style(strict = TRUE)) +#' @export +style_text <- function(text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + transformer <- make_transformer(transformers, include_roxygen_examples) + styled_text <- transformer(text) + construct_vertical(styled_text) +} + +#' Prettify arbitrary R code +#' +#' Performs various substitutions in all `.R` files in a directory. +#' Carefully examine the results after running this function! +#' @param path Path to a directory with files to transform. +#' @param recursive A logical value indicating whether or not files in subdirectories +#' of `path` should be styled as well. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @family stylers +#' @examples +#' \dontrun{ +#' style_dir(file_type = "r") +#' } +#' @export +style_dir <- function(path = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + recursive = TRUE, + exclude_files = NULL, + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + path, prettify_any( + transformers, filetype, recursive, exclude_files, include_roxygen_examples + ) + ) + invisible(changed) +} + +#' Prettify R code in current working directory +#' +#' This is a helper function for style_dir. +#' @inheritParams style_pkg +#' @param recursive A logical value indicating whether or not files in subdirectories +#' should be styled as well. +#' @keywords internal +prettify_any <- function(transformers, + filetype, + recursive, + exclude_files, + include_roxygen_examples) { + files <- dir( + path = ".", pattern = map_filetype_to_pattern(filetype), + ignore.case = TRUE, recursive = recursive, full.names = TRUE + ) + transform_files( + setdiff(files, exclude_files), transformers, include_roxygen_examples + ) +} + +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @section Encoding: +#' UTF-8 encoding is assumed. Please convert your code to UTF-8 if necessary +#' before applying styler. +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file <- tempfile("styler", fileext = ".R") +#' enc::write_lines_enc("1++1", file) +#' style_file(file, style = tidyverse_style, strict = TRUE) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' unlink(file) +#' @family stylers +#' @export +style_file <- function(path, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + dirname(path), + transform_files(basename(path), transformers, include_roxygen_examples) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in_tree b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in_tree new file mode 100644 index 000000000..0a9cd8501 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-in_tree @@ -0,0 +1,745 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' @a [0/0] {1} + ¦--COMMENT: #' @i [1/0] {2} + ¦--COMMENT: #' @i [1/0] {3} + ¦--expr: [1/0] {5} + ¦ °--NULL_CONST: NULL [0/0] {4} + ¦--COMMENT: #' Pr [2/0] {6} + ¦--COMMENT: #' [1/0] {7} + ¦--COMMENT: #' Pe [1/0] {8} + ¦--COMMENT: #' (c [1/0] {9} + ¦--COMMENT: #' Ca [1/0] {10} + ¦--COMMENT: #' [1/0] {11} + ¦--COMMENT: #' @p [1/0] {12} + ¦--COMMENT: #' @p [1/0] {13} + ¦--COMMENT: #' @p [1/0] {14} + ¦--COMMENT: #' [1/0] {15} + ¦--COMMENT: #' [1/0] {16} + ¦--COMMENT: #' [1/0] {17} + ¦--COMMENT: #' @p [1/0] {18} + ¦--COMMENT: #' [1/0] {19} + ¦--COMMENT: #' [1/0] {20} + ¦--COMMENT: #' @p [1/0] {21} + ¦--COMMENT: #' [1/0] {22} + ¦--COMMENT: #' [1/0] {23} + ¦--COMMENT: #' @p [1/0] {24} + ¦--COMMENT: #' [1/0] {25} + ¦--COMMENT: #' @p [1/0] {26} + ¦--COMMENT: #' [1/0] {27} + ¦--COMMENT: #' @s [1/0] {28} + ¦--COMMENT: #' Th [1/0] {29} + ¦--COMMENT: #' co [1/0] {30} + ¦--COMMENT: #' th [1/0] {31} + ¦--COMMENT: #' [1/0] {32} + ¦--COMMENT: #' We [1/0] {33} + ¦--COMMENT: #' ch [1/0] {34} + ¦--COMMENT: #' tr [1/0] {35} + ¦--COMMENT: #' [1/0] {36} + ¦--COMMENT: #' Th [1/0] {37} + ¦--COMMENT: #' in [1/0] {38} + ¦--COMMENT: #' th [1/0] {39} + ¦--COMMENT: #' @s [1/0] {40} + ¦--COMMENT: #' Th [1/0] {41} + ¦--COMMENT: #' yi [1/0] {42} + ¦--COMMENT: #' [1/0] {43} + ¦--COMMENT: #' If [1/0] {44} + ¦--COMMENT: #' ab [1/0] {45} + ¦--COMMENT: #' He [1/0] {46} + ¦--COMMENT: #' ex [1/0] {47} + ¦--COMMENT: #' Th [1/0] {48} + ¦--COMMENT: #' er [1/0] {49} + ¦--COMMENT: #' [1/0] {50} + ¦--COMMENT: #' No [1/0] {51} + ¦--COMMENT: #' th [1/0] {52} + ¦--COMMENT: #' st [1/0] {53} + ¦--COMMENT: #' in [1/0] {54} + ¦--COMMENT: #' [1/0] {55} + ¦--COMMENT: #' Se [1/0] {56} + ¦--COMMENT: #' @i [1/0] {57} + ¦--COMMENT: #' @f [1/0] {58} + ¦--COMMENT: #' @e [1/0] {59} + ¦--COMMENT: #' \d [1/0] {60} + ¦--COMMENT: #' st [1/0] {61} + ¦--COMMENT: #' st [1/0] {62} + ¦--COMMENT: #' [1/0] {63} + ¦--COMMENT: #' [1/0] {64} + ¦--COMMENT: #' ) [1/0] {65} + ¦--COMMENT: #' } [1/0] {66} + ¦--COMMENT: #' @e [1/0] {67} + ¦--expr: [1/0] {68} + ¦ ¦--expr: [0/1] {70} + ¦ ¦ °--SYMBOL: style [0/0] {69} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {71} + ¦ °--expr: [0/0] {72} + ¦ ¦--FUNCTION: funct [0/0] {73} + ¦ ¦--'(': ( [0/0] {74} + ¦ ¦--SYMBOL_FORMALS: pkg [0/1] {75} + ¦ ¦--EQ_FORMALS: = [0/1] {76} + ¦ ¦--expr: [0/0] {78} + ¦ ¦ °--STR_CONST: "." [0/0] {77} + ¦ ¦--',': , [0/22] {79} + ¦ ¦--SYMBOL_FORMALS: ... [1/0] {80} + ¦ ¦--',': , [0/22] {81} + ¦ ¦--SYMBOL_FORMALS: style [1/1] {82} + ¦ ¦--EQ_FORMALS: = [0/1] {83} + ¦ ¦--expr: [0/0] {85} + ¦ ¦ °--SYMBOL: tidyv [0/0] {84} + ¦ ¦--',': , [0/22] {86} + ¦ ¦--SYMBOL_FORMALS: trans [1/1] {87} + ¦ ¦--EQ_FORMALS: = [0/1] {88} + ¦ ¦--expr: [0/0] {89} + ¦ ¦ ¦--expr: [0/0] {91} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {90} + ¦ ¦ ¦--'(': ( [0/0] {92} + ¦ ¦ ¦--expr: [0/0] {94} + ¦ ¦ ¦ °--SYMBOL: ... [0/0] {93} + ¦ ¦ °--')': ) [0/0] {95} + ¦ ¦--',': , [0/22] {96} + ¦ ¦--SYMBOL_FORMALS: filet [1/1] {97} + ¦ ¦--EQ_FORMALS: = [0/1] {98} + ¦ ¦--expr: [0/0] {100} + ¦ ¦ °--STR_CONST: "R" [0/0] {99} + ¦ ¦--',': , [0/22] {101} + ¦ ¦--SYMBOL_FORMALS: exclu [1/1] {102} + ¦ ¦--EQ_FORMALS: = [0/1] {103} + ¦ ¦--expr: [0/0] {105} + ¦ ¦ °--STR_CONST: "R/Rc [0/0] {104} + ¦ ¦--',': , [0/22] {106} + ¦ ¦--SYMBOL_FORMALS: inclu [1/1] {107} + ¦ ¦--EQ_FORMALS: = [0/1] {108} + ¦ ¦--expr: [0/0] {110} + ¦ ¦ °--NUM_CONST: TRUE [0/0] {109} + ¦ ¦--')': ) [0/1] {111} + ¦ °--expr: [0/0] {112} + ¦ ¦--'{': { [0/2] {113} + ¦ ¦--expr: [1/2] {114} + ¦ ¦ ¦--expr: [0/1] {116} + ¦ ¦ ¦ °--SYMBOL: pkg_r [0/0] {115} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {117} + ¦ ¦ °--expr: [0/0] {118} + ¦ ¦ ¦--expr: [0/0] {119} + ¦ ¦ ¦ ¦--SYMBOL_PACKAGE: rproj [0/0] {120} + ¦ ¦ ¦ ¦--NS_GET: :: [0/0] {121} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: find_ [0/0] {122} + ¦ ¦ ¦--'(': ( [0/0] {123} + ¦ ¦ ¦--SYMBOL_SUB: path [0/1] {124} + ¦ ¦ ¦--EQ_SUB: = [0/1] {125} + ¦ ¦ ¦--expr: [0/0] {127} + ¦ ¦ ¦ °--SYMBOL: pkg [0/0] {126} + ¦ ¦ °--')': ) [0/0] {128} + ¦ ¦--expr: [1/2] {129} + ¦ ¦ ¦--expr: [0/1] {131} + ¦ ¦ ¦ °--SYMBOL: chang [0/0] {130} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {132} + ¦ ¦ °--expr: [0/0] {133} + ¦ ¦ ¦--expr: [0/0] {134} + ¦ ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {135} + ¦ ¦ ¦ ¦--NS_GET: :: [0/0] {136} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {137} + ¦ ¦ ¦--'(': ( [0/0] {138} + ¦ ¦ ¦--expr: [0/0] {140} + ¦ ¦ ¦ °--SYMBOL: pkg_r [0/0] {139} + ¦ ¦ ¦--',': , [0/1] {141} + ¦ ¦ ¦--expr: [0/0] {142} + ¦ ¦ ¦ ¦--expr: [0/0] {144} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: prett [0/0] {143} + ¦ ¦ ¦ ¦--'(': ( [0/4] {145} + ¦ ¦ ¦ ¦--expr: [1/0] {147} + ¦ ¦ ¦ ¦ °--SYMBOL: trans [0/0] {146} + ¦ ¦ ¦ ¦--',': , [0/1] {148} + ¦ ¦ ¦ ¦--expr: [0/0] {150} + ¦ ¦ ¦ ¦ °--SYMBOL: filet [0/0] {149} + ¦ ¦ ¦ ¦--',': , [0/1] {151} + ¦ ¦ ¦ ¦--expr: [0/0] {153} + ¦ ¦ ¦ ¦ °--SYMBOL: exclu [0/0] {152} + ¦ ¦ ¦ ¦--',': , [0/1] {154} + ¦ ¦ ¦ ¦--expr: [0/2] {156} + ¦ ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {155} + ¦ ¦ ¦ °--')': ) [1/0] {157} + ¦ ¦ °--')': ) [0/0] {158} + ¦ ¦--expr: [1/0] {159} + ¦ ¦ ¦--expr: [0/0] {161} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {160} + ¦ ¦ ¦--'(': ( [0/0] {162} + ¦ ¦ ¦--expr: [0/0] {164} + ¦ ¦ ¦ °--SYMBOL: chang [0/0] {163} + ¦ ¦ °--')': ) [0/0] {165} + ¦ °--'}': } [1/0] {166} + ¦--expr: [2/0] {167} + ¦ ¦--expr: [0/1] {169} + ¦ ¦ °--SYMBOL: prett [0/0] {168} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {170} + ¦ °--expr: [0/0] {171} + ¦ ¦--FUNCTION: funct [0/0] {172} + ¦ ¦--'(': ( [0/0] {173} + ¦ ¦--SYMBOL_FORMALS: trans [0/0] {174} + ¦ ¦--',': , [0/25] {175} + ¦ ¦--SYMBOL_FORMALS: filet [1/0] {176} + ¦ ¦--',': , [0/25] {177} + ¦ ¦--SYMBOL_FORMALS: exclu [1/0] {178} + ¦ ¦--',': , [0/25] {179} + ¦ ¦--SYMBOL_FORMALS: inclu [1/0] {180} + ¦ ¦--')': ) [0/1] {181} + ¦ °--expr: [0/0] {182} + ¦ ¦--'{': { [0/2] {183} + ¦ ¦--expr: [1/2] {184} + ¦ ¦ ¦--expr: [0/1] {186} + ¦ ¦ ¦ °--SYMBOL: filet [0/0] {185} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {187} + ¦ ¦ °--expr: [0/0] {188} + ¦ ¦ ¦--expr: [0/0] {190} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: set_a [0/0] {189} + ¦ ¦ ¦--'(': ( [0/0] {191} + ¦ ¦ ¦--expr: [0/0] {193} + ¦ ¦ ¦ °--SYMBOL: filet [0/0] {192} + ¦ ¦ °--')': ) [0/0] {194} + ¦ ¦--expr: [1/2] {195} + ¦ ¦ ¦--expr: [0/1] {197} + ¦ ¦ ¦ °--SYMBOL: r_fil [0/0] {196} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {198} + ¦ ¦ ¦--expr: [0/1] {201} + ¦ ¦ ¦ °--SYMBOL: vigne [0/0] {200} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {202} + ¦ ¦ ¦--expr: [0/1] {205} + ¦ ¦ ¦ °--SYMBOL: readm [0/0] {204} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {206} + ¦ ¦ °--expr: [0/0] {208} + ¦ ¦ °--NULL_CONST: NULL [0/0] {207} + ¦ ¦--expr: [2/2] {209} + ¦ ¦ ¦--IF: if [0/1] {210} + ¦ ¦ ¦--'(': ( [0/0] {211} + ¦ ¦ ¦--expr: [0/0] {212} + ¦ ¦ ¦ ¦--expr: [0/1] {214} + ¦ ¦ ¦ ¦ °--STR_CONST: "\\.r [0/0] {213} + ¦ ¦ ¦ ¦--SPECIAL-IN: %in% [0/1] {215} + ¦ ¦ ¦ °--expr: [0/0] {217} + ¦ ¦ ¦ °--SYMBOL: filet [0/0] {216} + ¦ ¦ ¦--')': ) [0/1] {218} + ¦ ¦ °--expr: [0/0] {219} + ¦ ¦ ¦--'{': { [0/4] {220} + ¦ ¦ ¦--expr: [1/2] {221} + ¦ ¦ ¦ ¦--expr: [0/1] {223} + ¦ ¦ ¦ ¦ °--SYMBOL: r_fil [0/0] {222} + ¦ ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {224} + ¦ ¦ ¦ °--expr: [0/0] {225} + ¦ ¦ ¦ ¦--expr: [0/0] {227} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dir [0/0] {226} + ¦ ¦ ¦ ¦--'(': ( [0/6] {228} + ¦ ¦ ¦ ¦--SYMBOL_SUB: path [1/1] {229} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {230} + ¦ ¦ ¦ ¦--expr: [0/0] {231} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {233} + ¦ ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: c [0/0] {232} + ¦ ¦ ¦ ¦ ¦--'(': ( [0/0] {234} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {236} + ¦ ¦ ¦ ¦ ¦ °--STR_CONST: "R" [0/0] {235} + ¦ ¦ ¦ ¦ ¦--',': , [0/1] {237} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {239} + ¦ ¦ ¦ ¦ ¦ °--STR_CONST: "test [0/0] {238} + ¦ ¦ ¦ ¦ ¦--',': , [0/1] {240} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {242} + ¦ ¦ ¦ ¦ ¦ °--STR_CONST: "data [0/0] {241} + ¦ ¦ ¦ ¦ °--')': ) [0/0] {243} + ¦ ¦ ¦ ¦--',': , [0/1] {244} + ¦ ¦ ¦ ¦--SYMBOL_SUB: patte [0/1] {245} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {246} + ¦ ¦ ¦ ¦--expr: [0/0] {248} + ¦ ¦ ¦ ¦ °--STR_CONST: "\\.r [0/0] {247} + ¦ ¦ ¦ ¦--',': , [0/6] {249} + ¦ ¦ ¦ ¦--SYMBOL_SUB: ignor [1/1] {250} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {251} + ¦ ¦ ¦ ¦--expr: [0/0] {253} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {252} + ¦ ¦ ¦ ¦--',': , [0/1] {254} + ¦ ¦ ¦ ¦--SYMBOL_SUB: recur [0/1] {255} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {256} + ¦ ¦ ¦ ¦--expr: [0/0] {258} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {257} + ¦ ¦ ¦ ¦--',': , [0/1] {259} + ¦ ¦ ¦ ¦--SYMBOL_SUB: full. [0/1] {260} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {261} + ¦ ¦ ¦ ¦--expr: [0/4] {263} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {262} + ¦ ¦ ¦ °--')': ) [1/0] {264} + ¦ ¦ °--'}': } [1/0] {265} + ¦ ¦--expr: [2/2] {266} + ¦ ¦ ¦--IF: if [0/1] {267} + ¦ ¦ ¦--'(': ( [0/0] {268} + ¦ ¦ ¦--expr: [0/0] {269} + ¦ ¦ ¦ ¦--expr: [0/1] {271} + ¦ ¦ ¦ ¦ °--STR_CONST: "\\.r [0/0] {270} + ¦ ¦ ¦ ¦--SPECIAL-IN: %in% [0/1] {272} + ¦ ¦ ¦ °--expr: [0/0] {274} + ¦ ¦ ¦ °--SYMBOL: filet [0/0] {273} + ¦ ¦ ¦--')': ) [0/1] {275} + ¦ ¦ °--expr: [0/0] {276} + ¦ ¦ ¦--'{': { [0/4] {277} + ¦ ¦ ¦--expr: [1/4] {278} + ¦ ¦ ¦ ¦--expr: [0/1] {280} + ¦ ¦ ¦ ¦ °--SYMBOL: vigne [0/0] {279} + ¦ ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {281} + ¦ ¦ ¦ °--expr: [0/0] {282} + ¦ ¦ ¦ ¦--expr: [0/0] {284} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dir [0/0] {283} + ¦ ¦ ¦ ¦--'(': ( [0/6] {285} + ¦ ¦ ¦ ¦--SYMBOL_SUB: path [1/1] {286} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {287} + ¦ ¦ ¦ ¦--expr: [0/0] {289} + ¦ ¦ ¦ ¦ °--STR_CONST: "vign [0/0] {288} + ¦ ¦ ¦ ¦--',': , [0/1] {290} + ¦ ¦ ¦ ¦--SYMBOL_SUB: patte [0/1] {291} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {292} + ¦ ¦ ¦ ¦--expr: [0/0] {294} + ¦ ¦ ¦ ¦ °--STR_CONST: "\\.r [0/0] {293} + ¦ ¦ ¦ ¦--',': , [0/6] {295} + ¦ ¦ ¦ ¦--SYMBOL_SUB: ignor [1/1] {296} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {297} + ¦ ¦ ¦ ¦--expr: [0/0] {299} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {298} + ¦ ¦ ¦ ¦--',': , [0/1] {300} + ¦ ¦ ¦ ¦--SYMBOL_SUB: recur [0/1] {301} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {302} + ¦ ¦ ¦ ¦--expr: [0/0] {304} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {303} + ¦ ¦ ¦ ¦--',': , [0/1] {305} + ¦ ¦ ¦ ¦--SYMBOL_SUB: full. [0/1] {306} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {307} + ¦ ¦ ¦ ¦--expr: [0/4] {309} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {308} + ¦ ¦ ¦ °--')': ) [1/0] {310} + ¦ ¦ ¦--expr: [1/2] {311} + ¦ ¦ ¦ ¦--expr: [0/1] {313} + ¦ ¦ ¦ ¦ °--SYMBOL: readm [0/0] {312} + ¦ ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {314} + ¦ ¦ ¦ °--expr: [0/0] {315} + ¦ ¦ ¦ ¦--expr: [0/0] {317} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dir [0/0] {316} + ¦ ¦ ¦ ¦--'(': ( [0/0] {318} + ¦ ¦ ¦ ¦--SYMBOL_SUB: patte [0/1] {319} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {320} + ¦ ¦ ¦ ¦--expr: [0/0] {322} + ¦ ¦ ¦ ¦ °--STR_CONST: "^rea [0/0] {321} + ¦ ¦ ¦ ¦--',': , [0/1] {323} + ¦ ¦ ¦ ¦--SYMBOL_SUB: ignor [0/1] {324} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {325} + ¦ ¦ ¦ ¦--expr: [0/0] {327} + ¦ ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {326} + ¦ ¦ ¦ °--')': ) [0/0] {328} + ¦ ¦ °--'}': } [1/0] {329} + ¦ ¦--expr: [2/2] {330} + ¦ ¦ ¦--expr: [0/1] {332} + ¦ ¦ ¦ °--SYMBOL: files [0/0] {331} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {333} + ¦ ¦ °--expr: [0/0] {334} + ¦ ¦ ¦--expr: [0/0] {336} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: setdi [0/0] {335} + ¦ ¦ ¦--'(': ( [0/0] {337} + ¦ ¦ ¦--expr: [0/0] {338} + ¦ ¦ ¦ ¦--expr: [0/0] {340} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: c [0/0] {339} + ¦ ¦ ¦ ¦--'(': ( [0/0] {341} + ¦ ¦ ¦ ¦--expr: [0/0] {343} + ¦ ¦ ¦ ¦ °--SYMBOL: r_fil [0/0] {342} + ¦ ¦ ¦ ¦--',': , [0/1] {344} + ¦ ¦ ¦ ¦--expr: [0/0] {346} + ¦ ¦ ¦ ¦ °--SYMBOL: vigne [0/0] {345} + ¦ ¦ ¦ ¦--',': , [0/1] {347} + ¦ ¦ ¦ ¦--expr: [0/0] {349} + ¦ ¦ ¦ ¦ °--SYMBOL: readm [0/0] {348} + ¦ ¦ ¦ °--')': ) [0/0] {350} + ¦ ¦ ¦--',': , [0/1] {351} + ¦ ¦ ¦--expr: [0/0] {353} + ¦ ¦ ¦ °--SYMBOL: exclu [0/0] {352} + ¦ ¦ °--')': ) [0/0] {354} + ¦ ¦--expr: [1/0] {355} + ¦ ¦ ¦--expr: [0/0] {357} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {356} + ¦ ¦ ¦--'(': ( [0/0] {358} + ¦ ¦ ¦--expr: [0/0] {360} + ¦ ¦ ¦ °--SYMBOL: files [0/0] {359} + ¦ ¦ ¦--',': , [0/1] {361} + ¦ ¦ ¦--expr: [0/0] {363} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {362} + ¦ ¦ ¦--',': , [0/1] {364} + ¦ ¦ ¦--expr: [0/0] {366} + ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {365} + ¦ ¦ °--')': ) [0/0] {367} + ¦ °--'}': } [1/0] {368} + ¦--COMMENT: #' St [3/0] {369} + ¦--COMMENT: #' [1/0] {370} + ¦--COMMENT: #' St [1/0] {371} + ¦--COMMENT: #' to [1/0] {372} + ¦--COMMENT: #' @p [1/0] {373} + ¦--COMMENT: #' @i [1/0] {374} + ¦--COMMENT: #' @f [1/0] {375} + ¦--COMMENT: #' @e [1/0] {376} + ¦--COMMENT: #' st [1/0] {377} + ¦--COMMENT: #' st [1/0] {378} + ¦--COMMENT: #' st [1/0] {379} + ¦--COMMENT: #' st [1/0] {380} + ¦--COMMENT: #' st [1/0] {381} + ¦--COMMENT: #' # [1/0] {382} + ¦--COMMENT: #' st [1/0] {383} + ¦--COMMENT: #' st [1/0] {384} + ¦--COMMENT: #' @e [1/0] {385} + ¦--expr: [1/0] {386} + ¦ ¦--expr: [0/1] {388} + ¦ ¦ °--SYMBOL: style [0/0] {387} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {389} + ¦ °--expr: [0/0] {390} + ¦ ¦--FUNCTION: funct [0/0] {391} + ¦ ¦--'(': ( [0/0] {392} + ¦ ¦--SYMBOL_FORMALS: text [0/0] {393} + ¦ ¦--',': , [0/23] {394} + ¦ ¦--SYMBOL_FORMALS: ... [1/0] {395} + ¦ ¦--',': , [0/23] {396} + ¦ ¦--SYMBOL_FORMALS: style [1/1] {397} + ¦ ¦--EQ_FORMALS: = [0/1] {398} + ¦ ¦--expr: [0/0] {400} + ¦ ¦ °--SYMBOL: tidyv [0/0] {399} + ¦ ¦--',': , [0/23] {401} + ¦ ¦--SYMBOL_FORMALS: trans [1/1] {402} + ¦ ¦--EQ_FORMALS: = [0/1] {403} + ¦ ¦--expr: [0/0] {404} + ¦ ¦ ¦--expr: [0/0] {406} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {405} + ¦ ¦ ¦--'(': ( [0/0] {407} + ¦ ¦ ¦--expr: [0/0] {409} + ¦ ¦ ¦ °--SYMBOL: ... [0/0] {408} + ¦ ¦ °--')': ) [0/0] {410} + ¦ ¦--',': , [0/23] {411} + ¦ ¦--SYMBOL_FORMALS: inclu [1/1] {412} + ¦ ¦--EQ_FORMALS: = [0/1] {413} + ¦ ¦--expr: [0/0] {415} + ¦ ¦ °--NUM_CONST: TRUE [0/0] {414} + ¦ ¦--')': ) [0/1] {416} + ¦ °--expr: [0/0] {417} + ¦ ¦--'{': { [0/2] {418} + ¦ ¦--expr: [1/2] {419} + ¦ ¦ ¦--expr: [0/1] {421} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {420} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {422} + ¦ ¦ °--expr: [0/0] {423} + ¦ ¦ ¦--expr: [0/0] {425} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: make_ [0/0] {424} + ¦ ¦ ¦--'(': ( [0/0] {426} + ¦ ¦ ¦--expr: [0/0] {428} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {427} + ¦ ¦ ¦--',': , [0/1] {429} + ¦ ¦ ¦--expr: [0/0] {431} + ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {430} + ¦ ¦ °--')': ) [0/0] {432} + ¦ ¦--expr: [1/2] {433} + ¦ ¦ ¦--expr: [0/1] {435} + ¦ ¦ ¦ °--SYMBOL: style [0/0] {434} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {436} + ¦ ¦ °--expr: [0/0] {437} + ¦ ¦ ¦--expr: [0/0] {439} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {438} + ¦ ¦ ¦--'(': ( [0/0] {440} + ¦ ¦ ¦--expr: [0/0] {442} + ¦ ¦ ¦ °--SYMBOL: text [0/0] {441} + ¦ ¦ °--')': ) [0/0] {443} + ¦ ¦--expr: [1/0] {444} + ¦ ¦ ¦--expr: [0/0] {446} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: const [0/0] {445} + ¦ ¦ ¦--'(': ( [0/0] {447} + ¦ ¦ ¦--expr: [0/0] {449} + ¦ ¦ ¦ °--SYMBOL: style [0/0] {448} + ¦ ¦ °--')': ) [0/0] {450} + ¦ °--'}': } [1/0] {451} + ¦--COMMENT: #' Pr [2/0] {452} + ¦--COMMENT: #' [1/0] {453} + ¦--COMMENT: #' Pe [1/0] {454} + ¦--COMMENT: #' Ca [1/0] {455} + ¦--COMMENT: #' @p [1/0] {456} + ¦--COMMENT: #' @p [1/0] {457} + ¦--COMMENT: #' [1/0] {458} + ¦--COMMENT: #' @i [1/0] {459} + ¦--COMMENT: #' @i [1/0] {460} + ¦--COMMENT: #' @i [1/0] {461} + ¦--COMMENT: #' @i [1/0] {462} + ¦--COMMENT: #' @f [1/0] {463} + ¦--COMMENT: #' @e [1/0] {464} + ¦--COMMENT: #' \d [1/0] {465} + ¦--COMMENT: #' st [1/0] {466} + ¦--COMMENT: #' } [1/0] {467} + ¦--COMMENT: #' @e [1/0] {468} + ¦--expr: [1/0] {469} + ¦ ¦--expr: [0/1] {471} + ¦ ¦ °--SYMBOL: style [0/0] {470} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {472} + ¦ °--expr: [0/0] {473} + ¦ ¦--FUNCTION: funct [0/0] {474} + ¦ ¦--'(': ( [0/0] {475} + ¦ ¦--SYMBOL_FORMALS: path [0/1] {476} + ¦ ¦--EQ_FORMALS: = [0/1] {477} + ¦ ¦--expr: [0/0] {479} + ¦ ¦ °--STR_CONST: "." [0/0] {478} + ¦ ¦--',': , [0/22] {480} + ¦ ¦--SYMBOL_FORMALS: ... [1/0] {481} + ¦ ¦--',': , [0/22] {482} + ¦ ¦--SYMBOL_FORMALS: style [1/1] {483} + ¦ ¦--EQ_FORMALS: = [0/1] {484} + ¦ ¦--expr: [0/0] {486} + ¦ ¦ °--SYMBOL: tidyv [0/0] {485} + ¦ ¦--',': , [0/22] {487} + ¦ ¦--SYMBOL_FORMALS: trans [1/1] {488} + ¦ ¦--EQ_FORMALS: = [0/1] {489} + ¦ ¦--expr: [0/0] {490} + ¦ ¦ ¦--expr: [0/0] {492} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {491} + ¦ ¦ ¦--'(': ( [0/0] {493} + ¦ ¦ ¦--expr: [0/0] {495} + ¦ ¦ ¦ °--SYMBOL: ... [0/0] {494} + ¦ ¦ °--')': ) [0/0] {496} + ¦ ¦--',': , [0/22] {497} + ¦ ¦--SYMBOL_FORMALS: filet [1/1] {498} + ¦ ¦--EQ_FORMALS: = [0/1] {499} + ¦ ¦--expr: [0/0] {501} + ¦ ¦ °--STR_CONST: "R" [0/0] {500} + ¦ ¦--',': , [0/22] {502} + ¦ ¦--SYMBOL_FORMALS: recur [1/1] {503} + ¦ ¦--EQ_FORMALS: = [0/1] {504} + ¦ ¦--expr: [0/0] {506} + ¦ ¦ °--NUM_CONST: TRUE [0/0] {505} + ¦ ¦--',': , [0/22] {507} + ¦ ¦--SYMBOL_FORMALS: exclu [1/1] {508} + ¦ ¦--EQ_FORMALS: = [0/1] {509} + ¦ ¦--expr: [0/0] {511} + ¦ ¦ °--NULL_CONST: NULL [0/0] {510} + ¦ ¦--',': , [0/22] {512} + ¦ ¦--SYMBOL_FORMALS: inclu [1/1] {513} + ¦ ¦--EQ_FORMALS: = [0/1] {514} + ¦ ¦--expr: [0/0] {516} + ¦ ¦ °--NUM_CONST: TRUE [0/0] {515} + ¦ ¦--')': ) [0/1] {517} + ¦ °--expr: [0/0] {518} + ¦ ¦--'{': { [0/2] {519} + ¦ ¦--expr: [1/2] {520} + ¦ ¦ ¦--expr: [0/1] {522} + ¦ ¦ ¦ °--SYMBOL: chang [0/0] {521} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {523} + ¦ ¦ °--expr: [0/0] {524} + ¦ ¦ ¦--expr: [0/0] {525} + ¦ ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {526} + ¦ ¦ ¦ ¦--NS_GET: :: [0/0] {527} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {528} + ¦ ¦ ¦--'(': ( [0/4] {529} + ¦ ¦ ¦--expr: [1/0] {531} + ¦ ¦ ¦ °--SYMBOL: path [0/0] {530} + ¦ ¦ ¦--',': , [0/1] {532} + ¦ ¦ ¦--expr: [0/2] {533} + ¦ ¦ ¦ ¦--expr: [0/0] {535} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: prett [0/0] {534} + ¦ ¦ ¦ ¦--'(': ( [0/6] {536} + ¦ ¦ ¦ ¦--expr: [1/0] {538} + ¦ ¦ ¦ ¦ °--SYMBOL: trans [0/0] {537} + ¦ ¦ ¦ ¦--',': , [0/1] {539} + ¦ ¦ ¦ ¦--expr: [0/0] {541} + ¦ ¦ ¦ ¦ °--SYMBOL: filet [0/0] {540} + ¦ ¦ ¦ ¦--',': , [0/1] {542} + ¦ ¦ ¦ ¦--expr: [0/0] {544} + ¦ ¦ ¦ ¦ °--SYMBOL: recur [0/0] {543} + ¦ ¦ ¦ ¦--',': , [0/1] {545} + ¦ ¦ ¦ ¦--expr: [0/0] {547} + ¦ ¦ ¦ ¦ °--SYMBOL: exclu [0/0] {546} + ¦ ¦ ¦ ¦--',': , [0/1] {548} + ¦ ¦ ¦ ¦--expr: [0/4] {550} + ¦ ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {549} + ¦ ¦ ¦ °--')': ) [1/0] {551} + ¦ ¦ °--')': ) [1/0] {552} + ¦ ¦--expr: [1/0] {553} + ¦ ¦ ¦--expr: [0/0] {555} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {554} + ¦ ¦ ¦--'(': ( [0/0] {556} + ¦ ¦ ¦--expr: [0/0] {558} + ¦ ¦ ¦ °--SYMBOL: chang [0/0] {557} + ¦ ¦ °--')': ) [0/0] {559} + ¦ °--'}': } [1/0] {560} + ¦--COMMENT: #' Pr [2/0] {561} + ¦--COMMENT: #' [1/0] {562} + ¦--COMMENT: #' Th [1/0] {563} + ¦--COMMENT: #' @i [1/0] {564} + ¦--COMMENT: #' @p [1/0] {565} + ¦--COMMENT: #' [1/0] {566} + ¦--COMMENT: #' @k [1/0] {567} + ¦--expr: [1/0] {568} + ¦ ¦--expr: [0/1] {570} + ¦ ¦ °--SYMBOL: prett [0/0] {569} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {571} + ¦ °--expr: [0/0] {572} + ¦ ¦--FUNCTION: funct [0/0] {573} + ¦ ¦--'(': ( [0/0] {574} + ¦ ¦--SYMBOL_FORMALS: trans [0/0] {575} + ¦ ¦--',': , [0/25] {576} + ¦ ¦--SYMBOL_FORMALS: filet [1/0] {577} + ¦ ¦--',': , [0/25] {578} + ¦ ¦--SYMBOL_FORMALS: recur [1/0] {579} + ¦ ¦--',': , [0/25] {580} + ¦ ¦--SYMBOL_FORMALS: exclu [1/0] {581} + ¦ ¦--',': , [0/25] {582} + ¦ ¦--SYMBOL_FORMALS: inclu [1/0] {583} + ¦ ¦--')': ) [0/1] {584} + ¦ °--expr: [0/0] {585} + ¦ ¦--'{': { [0/2] {586} + ¦ ¦--expr: [1/2] {587} + ¦ ¦ ¦--expr: [0/1] {589} + ¦ ¦ ¦ °--SYMBOL: files [0/0] {588} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {590} + ¦ ¦ °--expr: [0/0] {591} + ¦ ¦ ¦--expr: [0/0] {593} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dir [0/0] {592} + ¦ ¦ ¦--'(': ( [0/4] {594} + ¦ ¦ ¦--SYMBOL_SUB: path [1/1] {595} + ¦ ¦ ¦--EQ_SUB: = [0/1] {596} + ¦ ¦ ¦--expr: [0/0] {598} + ¦ ¦ ¦ °--STR_CONST: "." [0/0] {597} + ¦ ¦ ¦--',': , [0/1] {599} + ¦ ¦ ¦--SYMBOL_SUB: patte [0/1] {600} + ¦ ¦ ¦--EQ_SUB: = [0/1] {601} + ¦ ¦ ¦--expr: [0/0] {602} + ¦ ¦ ¦ ¦--expr: [0/0] {604} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: map_f [0/0] {603} + ¦ ¦ ¦ ¦--'(': ( [0/0] {605} + ¦ ¦ ¦ ¦--expr: [0/0] {607} + ¦ ¦ ¦ ¦ °--SYMBOL: filet [0/0] {606} + ¦ ¦ ¦ °--')': ) [0/0] {608} + ¦ ¦ ¦--',': , [0/4] {609} + ¦ ¦ ¦--SYMBOL_SUB: ignor [1/1] {610} + ¦ ¦ ¦--EQ_SUB: = [0/1] {611} + ¦ ¦ ¦--expr: [0/0] {613} + ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {612} + ¦ ¦ ¦--',': , [0/1] {614} + ¦ ¦ ¦--SYMBOL_SUB: recur [0/1] {615} + ¦ ¦ ¦--EQ_SUB: = [0/1] {616} + ¦ ¦ ¦--expr: [0/0] {618} + ¦ ¦ ¦ °--SYMBOL: recur [0/0] {617} + ¦ ¦ ¦--',': , [0/1] {619} + ¦ ¦ ¦--SYMBOL_SUB: full. [0/1] {620} + ¦ ¦ ¦--EQ_SUB: = [0/1] {621} + ¦ ¦ ¦--expr: [0/2] {623} + ¦ ¦ ¦ °--NUM_CONST: TRUE [0/0] {622} + ¦ ¦ °--')': ) [1/0] {624} + ¦ ¦--expr: [1/0] {625} + ¦ ¦ ¦--expr: [0/0] {627} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {626} + ¦ ¦ ¦--'(': ( [0/4] {628} + ¦ ¦ ¦--expr: [1/0] {629} + ¦ ¦ ¦ ¦--expr: [0/0] {631} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: setdi [0/0] {630} + ¦ ¦ ¦ ¦--'(': ( [0/0] {632} + ¦ ¦ ¦ ¦--expr: [0/0] {634} + ¦ ¦ ¦ ¦ °--SYMBOL: files [0/0] {633} + ¦ ¦ ¦ ¦--',': , [0/1] {635} + ¦ ¦ ¦ ¦--expr: [0/0] {637} + ¦ ¦ ¦ ¦ °--SYMBOL: exclu [0/0] {636} + ¦ ¦ ¦ °--')': ) [0/0] {638} + ¦ ¦ ¦--',': , [0/1] {639} + ¦ ¦ ¦--expr: [0/0] {641} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {640} + ¦ ¦ ¦--',': , [0/1] {642} + ¦ ¦ ¦--expr: [0/2] {644} + ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {643} + ¦ ¦ °--')': ) [1/0] {645} + ¦ °--'}': } [1/0] {646} + ¦--COMMENT: #' St [2/0] {647} + ¦--COMMENT: #' [1/0] {648} + ¦--COMMENT: #' Pe [1/0] {649} + ¦--COMMENT: #' Ca [1/0] {650} + ¦--COMMENT: #' @s [1/0] {651} + ¦--COMMENT: #' UT [1/0] {652} + ¦--COMMENT: #' be [1/0] {653} + ¦--COMMENT: #' @p [1/0] {654} + ¦--COMMENT: #' @i [1/0] {655} + ¦--COMMENT: #' @i [1/0] {656} + ¦--COMMENT: #' @i [1/0] {657} + ¦--COMMENT: #' @i [1/0] {658} + ¦--COMMENT: #' @e [1/0] {659} + ¦--COMMENT: #' # [1/0] {660} + ¦--COMMENT: #' fi [1/0] {661} + ¦--COMMENT: #' en [1/0] {662} + ¦--COMMENT: #' st [1/0] {663} + ¦--COMMENT: #' st [1/0] {664} + ¦--COMMENT: #' en [1/0] {665} + ¦--COMMENT: #' un [1/0] {666} + ¦--COMMENT: #' @f [1/0] {667} + ¦--COMMENT: #' @e [1/0] {668} + °--expr: [1/0] {669} + ¦--expr: [0/1] {671} + ¦ °--SYMBOL: style [0/0] {670} + ¦--LEFT_ASSIGN: <- [0/1] {672} + °--expr: [0/0] {673} + ¦--FUNCTION: funct [0/0] {674} + ¦--'(': ( [0/0] {675} + ¦--SYMBOL_FORMALS: path [0/0] {676} + ¦--',': , [0/23] {677} + ¦--SYMBOL_FORMALS: ... [1/0] {678} + ¦--',': , [0/23] {679} + ¦--SYMBOL_FORMALS: style [1/1] {680} + ¦--EQ_FORMALS: = [0/1] {681} + ¦--expr: [0/0] {683} + ¦ °--SYMBOL: tidyv [0/0] {682} + ¦--',': , [0/23] {684} + ¦--SYMBOL_FORMALS: trans [1/1] {685} + ¦--EQ_FORMALS: = [0/1] {686} + ¦--expr: [0/0] {687} + ¦ ¦--expr: [0/0] {689} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {688} + ¦ ¦--'(': ( [0/0] {690} + ¦ ¦--expr: [0/0] {692} + ¦ ¦ °--SYMBOL: ... [0/0] {691} + ¦ °--')': ) [0/0] {693} + ¦--',': , [0/23] {694} + ¦--SYMBOL_FORMALS: inclu [1/1] {695} + ¦--EQ_FORMALS: = [0/1] {696} + ¦--expr: [0/0] {698} + ¦ °--NUM_CONST: TRUE [0/0] {697} + ¦--')': ) [0/1] {699} + °--expr: [0/0] {700} + ¦--'{': { [0/2] {701} + ¦--expr: [1/2] {702} + ¦ ¦--expr: [0/1] {704} + ¦ ¦ °--SYMBOL: chang [0/0] {703} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {705} + ¦ °--expr: [0/0] {706} + ¦ ¦--expr: [0/0] {707} + ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {708} + ¦ ¦ ¦--NS_GET: :: [0/0] {709} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {710} + ¦ ¦--'(': ( [0/4] {711} + ¦ ¦--expr: [1/0] {712} + ¦ ¦ ¦--expr: [0/0] {714} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dirna [0/0] {713} + ¦ ¦ ¦--'(': ( [0/0] {715} + ¦ ¦ ¦--expr: [0/0] {717} + ¦ ¦ ¦ °--SYMBOL: path [0/0] {716} + ¦ ¦ °--')': ) [0/0] {718} + ¦ ¦--',': , [0/4] {719} + ¦ ¦--expr: [1/2] {720} + ¦ ¦ ¦--expr: [0/0] {722} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {721} + ¦ ¦ ¦--'(': ( [0/0] {723} + ¦ ¦ ¦--expr: [0/0] {724} + ¦ ¦ ¦ ¦--expr: [0/0] {726} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: basen [0/0] {725} + ¦ ¦ ¦ ¦--'(': ( [0/0] {727} + ¦ ¦ ¦ ¦--expr: [0/0] {729} + ¦ ¦ ¦ ¦ °--SYMBOL: path [0/0] {728} + ¦ ¦ ¦ °--')': ) [0/0] {730} + ¦ ¦ ¦--',': , [0/1] {731} + ¦ ¦ ¦--expr: [0/0] {733} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {732} + ¦ ¦ ¦--',': , [0/1] {734} + ¦ ¦ ¦--expr: [0/0] {736} + ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {735} + ¦ ¦ °--')': ) [0/0] {737} + ¦ °--')': ) [1/0] {738} + ¦--expr: [1/0] {739} + ¦ ¦--expr: [0/0] {741} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {740} + ¦ ¦--'(': ( [0/0] {742} + ¦ ¦--expr: [0/0] {744} + ¦ ¦ °--SYMBOL: chang [0/0] {743} + ¦ °--')': ) [0/0] {745} + °--'}': } [1/0] {746} diff --git a/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R new file mode 100644 index 000000000..50756a7aa --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R @@ -0,0 +1,222 @@ +#' @api +#' @import tibble +#' @importFrom magrittr %>% +NULL + +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' +#' @param pkg Path to a (subdirectory of an) R package. +#' @param ... Arguments passed on to the `style` function. +#' @param style A function that creates a style guide to use, by default +#' [tidyverse_style()] (without the parentheses). Not used +#' further except to construct the argument `transformers`. See +#' [style_guides()] for details. +#' @param transformers A set of transformer functions. This argument is most +#' conveniently constructed via the `style` argument and `...`. See +#' 'Examples'. +#' @param filetype Vector of file extensions indicating which file types should +#' be styled. Case is ignored, and the `.` is optional, e.g. `c(".R", ".Rmd")` +#' or `c("r", "rmd")`. +#' @param exclude_files Character vector with paths to files that should be +#' excluded from styling. +#' @param include_roxygen_examples Whether or not to style code in roxygen +#' examples. +#' @section Warning: +#' This function overwrites files (if styling results in a change of the +#' code to be formatted). It is strongly suggested to only style files +#' that are under version control or to create a backup copy. +#' +#' We suggest to first style with `scope < "tokens"` and inspect and commit +#' changes, because these changes are guaranteed to leave the abstract syntax +#' tree (AST) unchanged. See section 'Roundtrip Validation' for details. +#' +#' Then, we suggest to style with `scope = "tokens"` (if desired) and carefully +#' inspect the changes to make sure the AST is not changed in an unexpected way +#' that invalidates code. +#' @section Roundtrip Validation: +#' The following section describes when and how styling is guaranteed to +#' yield correct code. +#' +#' If the style guide has `scope < "tokens"`, no tokens are changed and the +#' abstract syntax tree (AST) should not change. +#' Hence, it is possible to validate the styling by comparing whether the parsed +#' expression before and after styling have the same AST. +#' This comparison omits comments. styler compares +#' error if the AST has changed through styling. +#' +#' Note that with `scope = "tokens"` such a comparison is not conducted because +#' the AST might well change and such a change is intended. There is no way +#' styler can validate styling, that is why we inform the user to carefully +#' inspect the changes. +#' +#' See section 'Warning' for a good strategy to apply styling safely. +#' @inheritSection transform_files Value +#' @family stylers +#' @examples +#' \dontrun{ +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specify_math_token_spacing(zero = "'+'") +#' ) +#' } +#' @export +style_pkg <- function(pkg = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + exclude_files = "R/RcppExports.R", + include_roxygen_examples = TRUE) { + pkg_root <- rprojroot::find_package_root_file(path = pkg) + changed <- withr::with_dir(pkg_root, prettify_pkg( + transformers, filetype, exclude_files, include_roxygen_examples + )) + invisible(changed) +} + +prettify_pkg <- function(transformers, + filetype, + exclude_files, + include_roxygen_examples) { + filetype <- set_and_assert_arg_filetype(filetype) + r_files <- vignette_files <- readme <- NULL + + if ("\\.r" %in% filetype) { + r_files <- dir( + path = c("R", "tests", "data-raw"), pattern = "\\.r$", + ignore.case = TRUE, recursive = TRUE, full.names = TRUE + ) + } + + if ("\\.rmd" %in% filetype) { + vignette_files <- dir( + path = "vignettes", pattern = "\\.rmd$", + ignore.case = TRUE, recursive = TRUE, full.names = TRUE + ) + readme <- dir(pattern = "^readme\\.rmd$", ignore.case = TRUE) + } + + files <- setdiff(c(r_files, vignette_files, readme), exclude_files) + transform_files(files, transformers, include_roxygen_examples) +} + + +#' Style a string +#' +#' Styles a character vector. Each element of the character vector corresponds +#' to one line of code. +#' @param text A character vector with text to style. +#' @inheritParams style_pkg +#' @family stylers +#' @examples +#' style_text("call( 1)") +#' style_text("1 + 1", strict = FALSE) +#' style_text("a%>%b", scope = "spaces") +#' style_text("a%>%b; a", scope = "line_breaks") +#' style_text("a%>%b; a", scope = "tokens") +#' # the following is identical but the former is more convenient: +#' style_text("a<-3++1", style = tidyverse_style, strict = TRUE) +#' style_text("a<-3++1", transformers = tidyverse_style(strict = TRUE)) +#' @export +style_text <- function(text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + transformer <- make_transformer(transformers, include_roxygen_examples) + styled_text <- transformer(text) + construct_vertical(styled_text) +} + +#' Prettify arbitrary R code +#' +#' Performs various substitutions in all `.R` files in a directory. +#' Carefully examine the results after running this function! +#' @param path Path to a directory with files to transform. +#' @param recursive A logical value indicating whether or not files in subdirectories +#' of `path` should be styled as well. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @family stylers +#' @examples +#' \dontrun{ +#' style_dir(file_type = "r") +#' } +#' @export +style_dir <- function(path = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + recursive = TRUE, + exclude_files = NULL, + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + path, prettify_any( + transformers, filetype, recursive, exclude_files, include_roxygen_examples + ) + ) + invisible(changed) +} + +#' Prettify R code in current working directory +#' +#' This is a helper function for style_dir. +#' @inheritParams style_pkg +#' @param recursive A logical value indicating whether or not files in subdirectories +#' should be styled as well. +#' @keywords internal +prettify_any <- function(transformers, + filetype, + recursive, + exclude_files, + include_roxygen_examples) { + files <- dir( + path = ".", pattern = map_filetype_to_pattern(filetype), + ignore.case = TRUE, recursive = recursive, full.names = TRUE + ) + transform_files( + setdiff(files, exclude_files), transformers, include_roxygen_examples + ) +} + +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @section Encoding: +#' UTF-8 encoding is assumed. Please convert your code to UTF-8 if necessary +#' before applying styler. +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file <- tempfile("styler", fileext = ".R") +#' enc::write_lines_enc("1++1", file) +#' style_file(file, style = tidyverse_style, strict = TRUE) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' unlink(file) +#' @family stylers +#' @export +style_file <- function(path, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + dirname(path), + transform_files(basename(path), transformers, include_roxygen_examples) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in.R b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in.R new file mode 100644 index 000000000..35a804639 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in.R @@ -0,0 +1,25 @@ +#' Hi +#' +#' x +#' @examples +#' \dontrun{ +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specfy_math_token_spacing(zero = "'+'") +#' ) +#' } +#' @export +style_pkg <- function(pkg = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + exclude_files = "R/RcppExports.R", + include_roxygen_examples = TRUE) { + pkg_root <- rprojroot::find_package_root_file(path = pkg) + changed <- withr::with_dir(pkg_root, prettify_pkg( + transformers, filetype, exclude_files, include_roxygen_examples + )) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in_tree b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in_tree new file mode 100644 index 000000000..b348ca120 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-in_tree @@ -0,0 +1,112 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Hi [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' x [1/0] {3} + ¦--COMMENT: #' @e [1/0] {4} + ¦--COMMENT: #' \d [1/0] {5} + ¦--COMMENT: #' st [1/0] {6} + ¦--COMMENT: #' st [1/0] {7} + ¦--COMMENT: #' [1/0] {8} + ¦--COMMENT: #' [1/0] {9} + ¦--COMMENT: #' ) [1/0] {10} + ¦--COMMENT: #' } [1/0] {11} + ¦--COMMENT: #' @e [1/0] {12} + °--expr: [1/0] {13} + ¦--expr: [0/1] {15} + ¦ °--SYMBOL: style [0/0] {14} + ¦--LEFT_ASSIGN: <- [0/1] {16} + °--expr: [0/0] {17} + ¦--FUNCTION: funct [0/0] {18} + ¦--'(': ( [0/0] {19} + ¦--SYMBOL_FORMALS: pkg [0/1] {20} + ¦--EQ_FORMALS: = [0/1] {21} + ¦--expr: [0/0] {23} + ¦ °--STR_CONST: "." [0/0] {22} + ¦--',': , [0/22] {24} + ¦--SYMBOL_FORMALS: ... [1/0] {25} + ¦--',': , [0/22] {26} + ¦--SYMBOL_FORMALS: style [1/1] {27} + ¦--EQ_FORMALS: = [0/1] {28} + ¦--expr: [0/0] {30} + ¦ °--SYMBOL: tidyv [0/0] {29} + ¦--',': , [0/22] {31} + ¦--SYMBOL_FORMALS: trans [1/1] {32} + ¦--EQ_FORMALS: = [0/1] {33} + ¦--expr: [0/0] {34} + ¦ ¦--expr: [0/0] {36} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {35} + ¦ ¦--'(': ( [0/0] {37} + ¦ ¦--expr: [0/0] {39} + ¦ ¦ °--SYMBOL: ... [0/0] {38} + ¦ °--')': ) [0/0] {40} + ¦--',': , [0/22] {41} + ¦--SYMBOL_FORMALS: filet [1/1] {42} + ¦--EQ_FORMALS: = [0/1] {43} + ¦--expr: [0/0] {45} + ¦ °--STR_CONST: "R" [0/0] {44} + ¦--',': , [0/22] {46} + ¦--SYMBOL_FORMALS: exclu [1/1] {47} + ¦--EQ_FORMALS: = [0/1] {48} + ¦--expr: [0/0] {50} + ¦ °--STR_CONST: "R/Rc [0/0] {49} + ¦--',': , [0/22] {51} + ¦--SYMBOL_FORMALS: inclu [1/1] {52} + ¦--EQ_FORMALS: = [0/1] {53} + ¦--expr: [0/0] {55} + ¦ °--NUM_CONST: TRUE [0/0] {54} + ¦--')': ) [0/1] {56} + °--expr: [0/0] {57} + ¦--'{': { [0/2] {58} + ¦--expr: [1/2] {59} + ¦ ¦--expr: [0/1] {61} + ¦ ¦ °--SYMBOL: pkg_r [0/0] {60} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {62} + ¦ °--expr: [0/0] {63} + ¦ ¦--expr: [0/0] {64} + ¦ ¦ ¦--SYMBOL_PACKAGE: rproj [0/0] {65} + ¦ ¦ ¦--NS_GET: :: [0/0] {66} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: find_ [0/0] {67} + ¦ ¦--'(': ( [0/0] {68} + ¦ ¦--SYMBOL_SUB: path [0/1] {69} + ¦ ¦--EQ_SUB: = [0/1] {70} + ¦ ¦--expr: [0/0] {72} + ¦ ¦ °--SYMBOL: pkg [0/0] {71} + ¦ °--')': ) [0/0] {73} + ¦--expr: [1/2] {74} + ¦ ¦--expr: [0/1] {76} + ¦ ¦ °--SYMBOL: chang [0/0] {75} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {77} + ¦ °--expr: [0/0] {78} + ¦ ¦--expr: [0/0] {79} + ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {80} + ¦ ¦ ¦--NS_GET: :: [0/0] {81} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {82} + ¦ ¦--'(': ( [0/0] {83} + ¦ ¦--expr: [0/0] {85} + ¦ ¦ °--SYMBOL: pkg_r [0/0] {84} + ¦ ¦--',': , [0/1] {86} + ¦ ¦--expr: [0/0] {87} + ¦ ¦ ¦--expr: [0/0] {89} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: prett [0/0] {88} + ¦ ¦ ¦--'(': ( [0/4] {90} + ¦ ¦ ¦--expr: [1/0] {92} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {91} + ¦ ¦ ¦--',': , [0/1] {93} + ¦ ¦ ¦--expr: [0/0] {95} + ¦ ¦ ¦ °--SYMBOL: filet [0/0] {94} + ¦ ¦ ¦--',': , [0/1] {96} + ¦ ¦ ¦--expr: [0/0] {98} + ¦ ¦ ¦ °--SYMBOL: exclu [0/0] {97} + ¦ ¦ ¦--',': , [0/1] {99} + ¦ ¦ ¦--expr: [0/2] {101} + ¦ ¦ ¦ °--SYMBOL: inclu [0/0] {100} + ¦ ¦ °--')': ) [1/0] {102} + ¦ °--')': ) [0/0] {103} + ¦--expr: [1/0] {104} + ¦ ¦--expr: [0/0] {106} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {105} + ¦ ¦--'(': ( [0/0] {107} + ¦ ¦--expr: [0/0] {109} + ¦ ¦ °--SYMBOL: chang [0/0] {108} + ¦ °--')': ) [0/0] {110} + °--'}': } [1/0] {111} diff --git a/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R new file mode 100644 index 000000000..35a804639 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R @@ -0,0 +1,25 @@ +#' Hi +#' +#' x +#' @examples +#' \dontrun{ +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specfy_math_token_spacing(zero = "'+'") +#' ) +#' } +#' @export +style_pkg <- function(pkg = ".", + ..., + style = tidyverse_style, + transformers = style(...), + filetype = "R", + exclude_files = "R/RcppExports.R", + include_roxygen_examples = TRUE) { + pkg_root <- rprojroot::find_package_root_file(path = pkg) + changed <- withr::with_dir(pkg_root, prettify_pkg( + transformers, filetype, exclude_files, include_roxygen_examples + )) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in.R b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in.R new file mode 100644 index 000000000..bf0285cd8 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in.R @@ -0,0 +1,93 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' @examples +#' \dontshow{ +#' { +#' x +#' } +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening)) +#' } +#' @examples +#' \dontrun{ +#' style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} + + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' @examples +#' \dontshow{ +#' {x +#' } +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break= tibble::lst(set_line_break_before_curly_opening)) +#' } +#' @examples +#' \donttest{style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + #transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + )%>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in_tree b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in_tree new file mode 100644 index 000000000..a7eb3e0a0 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-in_tree @@ -0,0 +1,233 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Cr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' @p [1/0] {3} + ¦--COMMENT: #' [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' se [1/0] {6} + ¦--COMMENT: #' [1/0] {7} + ¦--COMMENT: #' [1/0] {8} + ¦--COMMENT: #' [1/0] {9} + ¦--COMMENT: #' } [1/0] {10} + ¦--COMMENT: #' @e [1/0] {11} + ¦--COMMENT: #' \d [1/0] {12} + ¦--COMMENT: #' { [1/0] {13} + ¦--COMMENT: #' [1/0] {14} + ¦--COMMENT: #' } [1/0] {15} + ¦--COMMENT: #' } [1/0] {16} + ¦--COMMENT: #' se [1/0] {17} + ¦--COMMENT: #' [1/0] {18} + ¦--COMMENT: #' } [1/0] {19} + ¦--COMMENT: #' @e [1/0] {20} + ¦--COMMENT: #' \d [1/0] {21} + ¦--COMMENT: #' st [1/0] {22} + ¦--COMMENT: #' ", [1/0] {23} + ¦--COMMENT: #' } [1/0] {24} + ¦--COMMENT: #' @i [1/0] {25} + ¦--COMMENT: #' @e [1/0] {26} + ¦--expr: [1/0] {27} + ¦ ¦--expr: [0/1] {29} + ¦ ¦ °--SYMBOL: creat [0/0] {28} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {30} + ¦ °--expr: [0/0] {31} + ¦ ¦--FUNCTION: funct [0/0] {32} + ¦ ¦--'(': ( [0/0] {33} + ¦ ¦--SYMBOL_FORMALS: initi [0/1] {34} + ¦ ¦--EQ_FORMALS: = [0/1] {35} + ¦ ¦--expr: [0/0] {37} + ¦ ¦ °--SYMBOL: defau [0/0] {36} + ¦ ¦--',': , [0/31] {38} + ¦ ¦--SYMBOL_FORMALS: line_ [1/1] {39} + ¦ ¦--EQ_FORMALS: = [0/1] {40} + ¦ ¦--expr: [0/0] {42} + ¦ ¦ °--NULL_CONST: NULL [0/0] {41} + ¦ ¦--',': , [0/31] {43} + ¦ ¦--SYMBOL_FORMALS: space [1/1] {44} + ¦ ¦--EQ_FORMALS: = [0/1] {45} + ¦ ¦--expr: [0/0] {47} + ¦ ¦ °--NULL_CONST: NULL [0/0] {46} + ¦ ¦--',': , [0/31] {48} + ¦ ¦--SYMBOL_FORMALS: token [1/1] {49} + ¦ ¦--EQ_FORMALS: = [0/1] {50} + ¦ ¦--expr: [0/0] {52} + ¦ ¦ °--NULL_CONST: NULL [0/0] {51} + ¦ ¦--',': , [0/31] {53} + ¦ ¦--SYMBOL_FORMALS: inden [1/1] {54} + ¦ ¦--EQ_FORMALS: = [0/1] {55} + ¦ ¦--expr: [0/0] {57} + ¦ ¦ °--NULL_CONST: NULL [0/0] {56} + ¦ ¦--',': , [0/31] {58} + ¦ ¦--SYMBOL_FORMALS: use_r [1/1] {59} + ¦ ¦--EQ_FORMALS: = [0/1] {60} + ¦ ¦--expr: [0/0] {62} + ¦ ¦ °--NUM_CONST: FALSE [0/0] {61} + ¦ ¦--',': , [0/31] {63} + ¦ ¦--SYMBOL_FORMALS: reind [1/1] {64} + ¦ ¦--EQ_FORMALS: = [0/1] {65} + ¦ ¦--expr: [0/0] {66} + ¦ ¦ ¦--expr: [0/0] {68} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: tidyv [0/0] {67} + ¦ ¦ ¦--'(': ( [0/0] {69} + ¦ ¦ °--')': ) [0/0] {70} + ¦ ¦--')': ) [0/1] {71} + ¦ °--expr: [0/0] {72} + ¦ ¦--'{': { [0/2] {73} + ¦ ¦--expr: [1/0] {74} + ¦ ¦ ¦--expr: [0/1] {75} + ¦ ¦ ¦ ¦--expr: [0/0] {77} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {76} + ¦ ¦ ¦ ¦--'(': ( [0/4] {78} + ¦ ¦ ¦ ¦--COMMENT: # tra [1/4] {79} + ¦ ¦ ¦ ¦--SYMBOL_SUB: initi [1/1] {80} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {81} + ¦ ¦ ¦ ¦--expr: [0/0] {82} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {84} + ¦ ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {83} + ¦ ¦ ¦ ¦ ¦--'(': ( [0/0] {85} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {87} + ¦ ¦ ¦ ¦ ¦ °--SYMBOL: initi [0/0] {86} + ¦ ¦ ¦ ¦ °--')': ) [0/0] {88} + ¦ ¦ ¦ ¦--',': , [0/4] {89} + ¦ ¦ ¦ ¦--expr: [1/0] {91} + ¦ ¦ ¦ ¦ °--SYMBOL: line_ [0/0] {90} + ¦ ¦ ¦ ¦--',': , [0/4] {92} + ¦ ¦ ¦ ¦--expr: [1/0] {94} + ¦ ¦ ¦ ¦ °--SYMBOL: space [0/0] {93} + ¦ ¦ ¦ ¦--',': , [0/4] {95} + ¦ ¦ ¦ ¦--expr: [1/0] {97} + ¦ ¦ ¦ ¦ °--SYMBOL: token [0/0] {96} + ¦ ¦ ¦ ¦--',': , [0/4] {98} + ¦ ¦ ¦ ¦--expr: [1/0] {100} + ¦ ¦ ¦ ¦ °--SYMBOL: inden [0/0] {99} + ¦ ¦ ¦ ¦--',': , [0/4] {101} + ¦ ¦ ¦ ¦--COMMENT: # tra [1/4] {102} + ¦ ¦ ¦ ¦--expr: [1/0] {104} + ¦ ¦ ¦ ¦ °--SYMBOL: use_r [0/0] {103} + ¦ ¦ ¦ ¦--',': , [0/4] {105} + ¦ ¦ ¦ ¦--expr: [1/2] {107} + ¦ ¦ ¦ ¦ °--SYMBOL: reind [0/0] {106} + ¦ ¦ ¦ °--')': ) [1/0] {108} + ¦ ¦ ¦--SPECIAL-PIPE: %>% [0/4] {109} + ¦ ¦ °--expr: [1/0] {110} + ¦ ¦ ¦--expr: [0/0] {112} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: map [0/0] {111} + ¦ ¦ ¦--'(': ( [0/0] {113} + ¦ ¦ ¦--expr: [0/0] {115} + ¦ ¦ ¦ °--SYMBOL: compa [0/0] {114} + ¦ ¦ °--')': ) [0/0] {116} + ¦ °--'}': } [1/0] {117} + ¦--COMMENT: #' Cr [3/0] {118} + ¦--COMMENT: #' [1/0] {119} + ¦--COMMENT: #' @p [1/0] {120} + ¦--COMMENT: #' [1/0] {121} + ¦--COMMENT: #' @e [1/0] {122} + ¦--COMMENT: #' se [1/0] {123} + ¦--COMMENT: #' [1/0] {124} + ¦--COMMENT: #' [1/0] {125} + ¦--COMMENT: #' [1/0] {126} + ¦--COMMENT: #' } [1/0] {127} + ¦--COMMENT: #' @e [1/0] {128} + ¦--COMMENT: #' \d [1/0] {129} + ¦--COMMENT: #' {x [1/0] {130} + ¦--COMMENT: #' } [1/0] {131} + ¦--COMMENT: #' } [1/0] {132} + ¦--COMMENT: #' se [1/0] {133} + ¦--COMMENT: #' cr [1/0] {134} + ¦--COMMENT: #' } [1/0] {135} + ¦--COMMENT: #' @e [1/0] {136} + ¦--COMMENT: #' \d [1/0] {137} + ¦--COMMENT: #' ", [1/0] {138} + ¦--COMMENT: #' } [1/0] {139} + ¦--COMMENT: #' @i [1/0] {140} + ¦--COMMENT: #' @e [1/0] {141} + °--expr: [1/0] {142} + ¦--expr: [0/1] {144} + ¦ °--SYMBOL: creat [0/0] {143} + ¦--LEFT_ASSIGN: <- [0/1] {145} + °--expr: [0/0] {146} + ¦--FUNCTION: funct [0/0] {147} + ¦--'(': ( [0/0] {148} + ¦--SYMBOL_FORMALS: initi [0/1] {149} + ¦--EQ_FORMALS: = [0/1] {150} + ¦--expr: [0/0] {152} + ¦ °--SYMBOL: defau [0/0] {151} + ¦--',': , [0/31] {153} + ¦--SYMBOL_FORMALS: line_ [1/1] {154} + ¦--EQ_FORMALS: = [0/1] {155} + ¦--expr: [0/0] {157} + ¦ °--NULL_CONST: NULL [0/0] {156} + ¦--',': , [0/31] {158} + ¦--SYMBOL_FORMALS: space [1/1] {159} + ¦--EQ_FORMALS: = [0/1] {160} + ¦--expr: [0/0] {162} + ¦ °--NULL_CONST: NULL [0/0] {161} + ¦--',': , [0/31] {163} + ¦--SYMBOL_FORMALS: token [1/1] {164} + ¦--EQ_FORMALS: = [0/1] {165} + ¦--expr: [0/0] {167} + ¦ °--NULL_CONST: NULL [0/0] {166} + ¦--',': , [0/31] {168} + ¦--SYMBOL_FORMALS: inden [1/1] {169} + ¦--EQ_FORMALS: = [0/1] {170} + ¦--expr: [0/0] {172} + ¦ °--NULL_CONST: NULL [0/0] {171} + ¦--',': , [0/31] {173} + ¦--SYMBOL_FORMALS: use_r [1/1] {174} + ¦--EQ_FORMALS: = [0/1] {175} + ¦--expr: [0/0] {177} + ¦ °--NUM_CONST: FALSE [0/0] {176} + ¦--',': , [0/31] {178} + ¦--SYMBOL_FORMALS: reind [1/1] {179} + ¦--EQ_FORMALS: = [0/1] {180} + ¦--expr: [0/0] {181} + ¦ ¦--expr: [0/0] {183} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: tidyv [0/0] {182} + ¦ ¦--'(': ( [0/0] {184} + ¦ °--')': ) [0/0] {185} + ¦--')': ) [0/1] {186} + °--expr: [0/0] {187} + ¦--'{': { [0/2] {188} + ¦--expr: [1/0] {189} + ¦ ¦--expr: [0/0] {190} + ¦ ¦ ¦--expr: [0/0] {192} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {191} + ¦ ¦ ¦--'(': ( [0/4] {193} + ¦ ¦ ¦--COMMENT: #tran [1/4] {194} + ¦ ¦ ¦--SYMBOL_SUB: initi [1/1] {195} + ¦ ¦ ¦--EQ_SUB: = [0/1] {196} + ¦ ¦ ¦--expr: [0/0] {197} + ¦ ¦ ¦ ¦--expr: [0/0] {199} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {198} + ¦ ¦ ¦ ¦--'(': ( [0/0] {200} + ¦ ¦ ¦ ¦--expr: [0/0] {202} + ¦ ¦ ¦ ¦ °--SYMBOL: initi [0/0] {201} + ¦ ¦ ¦ °--')': ) [0/0] {203} + ¦ ¦ ¦--',': , [0/4] {204} + ¦ ¦ ¦--expr: [1/0] {206} + ¦ ¦ ¦ °--SYMBOL: line_ [0/0] {205} + ¦ ¦ ¦--',': , [0/4] {207} + ¦ ¦ ¦--expr: [1/0] {209} + ¦ ¦ ¦ °--SYMBOL: space [0/0] {208} + ¦ ¦ ¦--',': , [0/4] {210} + ¦ ¦ ¦--expr: [1/0] {212} + ¦ ¦ ¦ °--SYMBOL: token [0/0] {211} + ¦ ¦ ¦--',': , [0/4] {213} + ¦ ¦ ¦--expr: [1/0] {215} + ¦ ¦ ¦ °--SYMBOL: inden [0/0] {214} + ¦ ¦ ¦--',': , [0/4] {216} + ¦ ¦ ¦--COMMENT: # tra [1/4] {217} + ¦ ¦ ¦--expr: [1/0] {219} + ¦ ¦ ¦ °--SYMBOL: use_r [0/0] {218} + ¦ ¦ ¦--',': , [0/4] {220} + ¦ ¦ ¦--expr: [1/2] {222} + ¦ ¦ ¦ °--SYMBOL: reind [0/0] {221} + ¦ ¦ °--')': ) [1/0] {223} + ¦ ¦--SPECIAL-PIPE: %>% [0/4] {224} + ¦ °--expr: [1/0] {225} + ¦ ¦--expr: [0/0] {227} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: map [0/0] {226} + ¦ ¦--'(': ( [0/0] {228} + ¦ ¦--expr: [0/0] {230} + ¦ ¦ °--SYMBOL: compa [0/0] {229} + ¦ °--')': ) [0/0] {231} + °--'}': } [1/0] {232} diff --git a/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-out.R b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-out.R new file mode 100644 index 000000000..8d9a74ed3 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-dontshow-dontrun-donttest-out.R @@ -0,0 +1,91 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' \dontshow{ +#' { +#' x +#' } +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening)) +#' } +#' \dontrun{ +#' style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} + + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' \dontshow{ +#' { +#' x +#' } +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening)) +#' } +#' \donttest{ +#' style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in.R b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in.R new file mode 100644 index 000000000..3d97321e2 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in.R @@ -0,0 +1,38 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening)) +#' } +#' style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in_tree b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in_tree new file mode 100644 index 000000000..751bb092c --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-in_tree @@ -0,0 +1,109 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Cr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' @p [1/0] {3} + ¦--COMMENT: #' [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' se [1/0] {6} + ¦--COMMENT: #' [1/0] {7} + ¦--COMMENT: #' [1/0] {8} + ¦--COMMENT: #' [1/0] {9} + ¦--COMMENT: #' } [1/0] {10} + ¦--COMMENT: #' se [1/0] {11} + ¦--COMMENT: #' [1/0] {12} + ¦--COMMENT: #' } [1/0] {13} + ¦--COMMENT: #' st [1/0] {14} + ¦--COMMENT: #' ", [1/0] {15} + ¦--COMMENT: #' @i [1/0] {16} + ¦--COMMENT: #' @e [1/0] {17} + °--expr: [1/0] {18} + ¦--expr: [0/1] {20} + ¦ °--SYMBOL: creat [0/0] {19} + ¦--LEFT_ASSIGN: <- [0/1] {21} + °--expr: [0/0] {22} + ¦--FUNCTION: funct [0/0] {23} + ¦--'(': ( [0/0] {24} + ¦--SYMBOL_FORMALS: initi [0/1] {25} + ¦--EQ_FORMALS: = [0/1] {26} + ¦--expr: [0/0] {28} + ¦ °--SYMBOL: defau [0/0] {27} + ¦--',': , [0/31] {29} + ¦--SYMBOL_FORMALS: line_ [1/1] {30} + ¦--EQ_FORMALS: = [0/1] {31} + ¦--expr: [0/0] {33} + ¦ °--NULL_CONST: NULL [0/0] {32} + ¦--',': , [0/31] {34} + ¦--SYMBOL_FORMALS: space [1/1] {35} + ¦--EQ_FORMALS: = [0/1] {36} + ¦--expr: [0/0] {38} + ¦ °--NULL_CONST: NULL [0/0] {37} + ¦--',': , [0/31] {39} + ¦--SYMBOL_FORMALS: token [1/1] {40} + ¦--EQ_FORMALS: = [0/1] {41} + ¦--expr: [0/0] {43} + ¦ °--NULL_CONST: NULL [0/0] {42} + ¦--',': , [0/31] {44} + ¦--SYMBOL_FORMALS: inden [1/1] {45} + ¦--EQ_FORMALS: = [0/1] {46} + ¦--expr: [0/0] {48} + ¦ °--NULL_CONST: NULL [0/0] {47} + ¦--',': , [0/31] {49} + ¦--SYMBOL_FORMALS: use_r [1/1] {50} + ¦--EQ_FORMALS: = [0/1] {51} + ¦--expr: [0/0] {53} + ¦ °--NUM_CONST: FALSE [0/0] {52} + ¦--',': , [0/31] {54} + ¦--SYMBOL_FORMALS: reind [1/1] {55} + ¦--EQ_FORMALS: = [0/1] {56} + ¦--expr: [0/0] {57} + ¦ ¦--expr: [0/0] {59} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: tidyv [0/0] {58} + ¦ ¦--'(': ( [0/0] {60} + ¦ °--')': ) [0/0] {61} + ¦--')': ) [0/1] {62} + °--expr: [0/0] {63} + ¦--'{': { [0/2] {64} + ¦--expr: [1/0] {65} + ¦ ¦--expr: [0/1] {66} + ¦ ¦ ¦--expr: [0/0] {68} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {67} + ¦ ¦ ¦--'(': ( [0/4] {69} + ¦ ¦ ¦--COMMENT: # tra [1/4] {70} + ¦ ¦ ¦--SYMBOL_SUB: initi [1/1] {71} + ¦ ¦ ¦--EQ_SUB: = [0/1] {72} + ¦ ¦ ¦--expr: [0/0] {73} + ¦ ¦ ¦ ¦--expr: [0/0] {75} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {74} + ¦ ¦ ¦ ¦--'(': ( [0/0] {76} + ¦ ¦ ¦ ¦--expr: [0/0] {78} + ¦ ¦ ¦ ¦ °--SYMBOL: initi [0/0] {77} + ¦ ¦ ¦ °--')': ) [0/0] {79} + ¦ ¦ ¦--',': , [0/4] {80} + ¦ ¦ ¦--expr: [1/0] {82} + ¦ ¦ ¦ °--SYMBOL: line_ [0/0] {81} + ¦ ¦ ¦--',': , [0/4] {83} + ¦ ¦ ¦--expr: [1/0] {85} + ¦ ¦ ¦ °--SYMBOL: space [0/0] {84} + ¦ ¦ ¦--',': , [0/4] {86} + ¦ ¦ ¦--expr: [1/0] {88} + ¦ ¦ ¦ °--SYMBOL: token [0/0] {87} + ¦ ¦ ¦--',': , [0/4] {89} + ¦ ¦ ¦--expr: [1/0] {91} + ¦ ¦ ¦ °--SYMBOL: inden [0/0] {90} + ¦ ¦ ¦--',': , [0/4] {92} + ¦ ¦ ¦--COMMENT: # tra [1/4] {93} + ¦ ¦ ¦--expr: [1/0] {95} + ¦ ¦ ¦ °--SYMBOL: use_r [0/0] {94} + ¦ ¦ ¦--',': , [0/4] {96} + ¦ ¦ ¦--expr: [1/2] {98} + ¦ ¦ ¦ °--SYMBOL: reind [0/0] {97} + ¦ ¦ °--')': ) [1/0] {99} + ¦ ¦--SPECIAL-PIPE: %>% [0/4] {100} + ¦ °--expr: [1/0] {101} + ¦ ¦--expr: [0/0] {103} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: map [0/0] {102} + ¦ ¦--'(': ( [0/0] {104} + ¦ ¦--expr: [0/0] {106} + ¦ ¦ °--SYMBOL: compa [0/0] {105} + ¦ °--')': ) [0/0] {107} + °--'}': } [1/0] {108} diff --git a/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-out.R b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-out.R new file mode 100644 index 000000000..3d97321e2 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/12-fun-decs-in-examples-out.R @@ -0,0 +1,38 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' set_line_break_before_crly_opening <- function(pd_flat) { +#' op <- pd_flat$token %in% "'{'" +#' pd_flat$lag_newlines[op] <- 1L +#' pd_flat +#' } +#' set_line_break_before_curly_opening_style <- function() { +#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening)) +#' } +#' style_text("a <- function(x) { x } +#' ", style = set_line_break_before_curly_opening_style) +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/13-empty-lines-in.R b/tests/testthat/roxygen-examples-complete/13-empty-lines-in.R new file mode 100644 index 000000000..e6b0fbeba --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/13-empty-lines-in.R @@ -0,0 +1,48 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' # empty +#' +#' +#' # two +#' +#' +#' +#' +#' # more +#' a <- 3 +#' # a comment +#' \dontrun{ +#' x +#' +#' y # hi +#' +#' # more +#' +#' a <- 3 +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/13-empty-lines-in_tree b/tests/testthat/roxygen-examples-complete/13-empty-lines-in_tree new file mode 100644 index 000000000..1346e0f4d --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/13-empty-lines-in_tree @@ -0,0 +1,119 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Cr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' @p [1/0] {3} + ¦--COMMENT: #' [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' # [1/0] {6} + ¦--COMMENT: #' [1/0] {7} + ¦--COMMENT: #' [1/0] {8} + ¦--COMMENT: #' # [1/0] {9} + ¦--COMMENT: #' [1/0] {10} + ¦--COMMENT: #' [1/0] {11} + ¦--COMMENT: #' [1/0] {12} + ¦--COMMENT: #' [1/0] {13} + ¦--COMMENT: #' # [1/0] {14} + ¦--COMMENT: #' a [1/0] {15} + ¦--COMMENT: #' # [1/0] {16} + ¦--COMMENT: #' \d [1/0] {17} + ¦--COMMENT: #' x [1/0] {18} + ¦--COMMENT: #' [1/0] {19} + ¦--COMMENT: #' y [1/0] {20} + ¦--COMMENT: #' [1/0] {21} + ¦--COMMENT: #' # [1/0] {22} + ¦--COMMENT: #' [1/0] {23} + ¦--COMMENT: #' a [1/0] {24} + ¦--COMMENT: #' } [1/0] {25} + ¦--COMMENT: #' @i [1/0] {26} + ¦--COMMENT: #' @e [1/0] {27} + °--expr: [1/0] {28} + ¦--expr: [0/1] {30} + ¦ °--SYMBOL: creat [0/0] {29} + ¦--LEFT_ASSIGN: <- [0/1] {31} + °--expr: [0/0] {32} + ¦--FUNCTION: funct [0/0] {33} + ¦--'(': ( [0/0] {34} + ¦--SYMBOL_FORMALS: initi [0/1] {35} + ¦--EQ_FORMALS: = [0/1] {36} + ¦--expr: [0/0] {38} + ¦ °--SYMBOL: defau [0/0] {37} + ¦--',': , [0/31] {39} + ¦--SYMBOL_FORMALS: line_ [1/1] {40} + ¦--EQ_FORMALS: = [0/1] {41} + ¦--expr: [0/0] {43} + ¦ °--NULL_CONST: NULL [0/0] {42} + ¦--',': , [0/31] {44} + ¦--SYMBOL_FORMALS: space [1/1] {45} + ¦--EQ_FORMALS: = [0/1] {46} + ¦--expr: [0/0] {48} + ¦ °--NULL_CONST: NULL [0/0] {47} + ¦--',': , [0/31] {49} + ¦--SYMBOL_FORMALS: token [1/1] {50} + ¦--EQ_FORMALS: = [0/1] {51} + ¦--expr: [0/0] {53} + ¦ °--NULL_CONST: NULL [0/0] {52} + ¦--',': , [0/31] {54} + ¦--SYMBOL_FORMALS: inden [1/1] {55} + ¦--EQ_FORMALS: = [0/1] {56} + ¦--expr: [0/0] {58} + ¦ °--NULL_CONST: NULL [0/0] {57} + ¦--',': , [0/31] {59} + ¦--SYMBOL_FORMALS: use_r [1/1] {60} + ¦--EQ_FORMALS: = [0/1] {61} + ¦--expr: [0/0] {63} + ¦ °--NUM_CONST: FALSE [0/0] {62} + ¦--',': , [0/31] {64} + ¦--SYMBOL_FORMALS: reind [1/1] {65} + ¦--EQ_FORMALS: = [0/1] {66} + ¦--expr: [0/0] {67} + ¦ ¦--expr: [0/0] {69} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: tidyv [0/0] {68} + ¦ ¦--'(': ( [0/0] {70} + ¦ °--')': ) [0/0] {71} + ¦--')': ) [0/1] {72} + °--expr: [0/0] {73} + ¦--'{': { [0/2] {74} + ¦--expr: [1/0] {75} + ¦ ¦--expr: [0/1] {76} + ¦ ¦ ¦--expr: [0/0] {78} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {77} + ¦ ¦ ¦--'(': ( [0/4] {79} + ¦ ¦ ¦--COMMENT: # tra [1/4] {80} + ¦ ¦ ¦--SYMBOL_SUB: initi [1/1] {81} + ¦ ¦ ¦--EQ_SUB: = [0/1] {82} + ¦ ¦ ¦--expr: [0/0] {83} + ¦ ¦ ¦ ¦--expr: [0/0] {85} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: lst [0/0] {84} + ¦ ¦ ¦ ¦--'(': ( [0/0] {86} + ¦ ¦ ¦ ¦--expr: [0/0] {88} + ¦ ¦ ¦ ¦ °--SYMBOL: initi [0/0] {87} + ¦ ¦ ¦ °--')': ) [0/0] {89} + ¦ ¦ ¦--',': , [0/4] {90} + ¦ ¦ ¦--expr: [1/0] {92} + ¦ ¦ ¦ °--SYMBOL: line_ [0/0] {91} + ¦ ¦ ¦--',': , [0/4] {93} + ¦ ¦ ¦--expr: [1/0] {95} + ¦ ¦ ¦ °--SYMBOL: space [0/0] {94} + ¦ ¦ ¦--',': , [0/4] {96} + ¦ ¦ ¦--expr: [1/0] {98} + ¦ ¦ ¦ °--SYMBOL: token [0/0] {97} + ¦ ¦ ¦--',': , [0/4] {99} + ¦ ¦ ¦--expr: [1/0] {101} + ¦ ¦ ¦ °--SYMBOL: inden [0/0] {100} + ¦ ¦ ¦--',': , [0/4] {102} + ¦ ¦ ¦--COMMENT: # tra [1/4] {103} + ¦ ¦ ¦--expr: [1/0] {105} + ¦ ¦ ¦ °--SYMBOL: use_r [0/0] {104} + ¦ ¦ ¦--',': , [0/4] {106} + ¦ ¦ ¦--expr: [1/2] {108} + ¦ ¦ ¦ °--SYMBOL: reind [0/0] {107} + ¦ ¦ °--')': ) [1/0] {109} + ¦ ¦--SPECIAL-PIPE: %>% [0/4] {110} + ¦ °--expr: [1/0] {111} + ¦ ¦--expr: [0/0] {113} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: map [0/0] {112} + ¦ ¦--'(': ( [0/0] {114} + ¦ ¦--expr: [0/0] {116} + ¦ ¦ °--SYMBOL: compa [0/0] {115} + ¦ °--')': ) [0/0] {117} + °--'}': } [1/0] {118} diff --git a/tests/testthat/roxygen-examples-complete/13-empty-lines-out.R b/tests/testthat/roxygen-examples-complete/13-empty-lines-out.R new file mode 100644 index 000000000..33058695c --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/13-empty-lines-out.R @@ -0,0 +1,48 @@ + +#' Create a style guide +#' +#' @param reindention A list of parameters for regex re-indention, most +#' conveniently constructed using [specify_reindention()]. +#' @examples +#' # empty +#' +#' +#' # two +#' +#' +#' +#' +#' # more +#' a <- 3 +#' # a comment +#' \dontrun{ +#' x +#' +#' y # hi +#' +#' # more +#' +#' a <- 3 +#' } +#' @importFrom purrr compact +#' @export +create_style_guide <- function(initialize = default_style_guide_attributes, + line_break = NULL, + space = NULL, + token = NULL, + indention = NULL, + use_raw_indention = FALSE, + reindention = tidyverse_reindention()) { + lst( + # transformer functions + initialize = lst(initialize), + line_break, + space, + token, + indention, + # transformer options + use_raw_indention, + reindention + ) %>% + map(compact) +} diff --git a/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in.R b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in.R new file mode 100644 index 000000000..ffbb6e1ec --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in.R @@ -0,0 +1,17 @@ +#' Hi +#' +#' x +#' @examples +#' a %>% +#' x(b =3) %>% +#' ff() +#' \dontrun{ +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specfy_math_token_spacing(zero = "'+'") +#' )%>% +#' there() +#' } +#' call("\\n", x = 3) +#' @export +NULL diff --git a/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in_tree b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in_tree new file mode 100644 index 000000000..0efe73062 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-in_tree @@ -0,0 +1,19 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Hi [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' x [1/0] {3} + ¦--COMMENT: #' @e [1/0] {4} + ¦--COMMENT: #' a [1/0] {5} + ¦--COMMENT: #' [1/0] {6} + ¦--COMMENT: #' [1/0] {7} + ¦--COMMENT: #' \d [1/0] {8} + ¦--COMMENT: #' st [1/0] {9} + ¦--COMMENT: #' [1/0] {10} + ¦--COMMENT: #' [1/0] {11} + ¦--COMMENT: #' )% [1/0] {12} + ¦--COMMENT: #' [1/0] {13} + ¦--COMMENT: #' } [1/0] {14} + ¦--COMMENT: #' ca [1/0] {15} + ¦--COMMENT: #' @e [1/0] {16} + °--expr: [1/0] {18} + °--NULL_CONST: NULL [0/0] {17} diff --git a/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-out.R b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-out.R new file mode 100644 index 000000000..05980799a --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/14-pipe-dontrun-out.R @@ -0,0 +1,17 @@ +#' Hi +#' +#' x +#' @examples +#' a %>% +#' x(b = 3) %>% +#' ff() +#' \dontrun{ +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specfy_math_token_spacing(zero = "'+'") +#' ) %>% +#' there() +#' } +#' call("\n", x = 3) +#' @export +NULL diff --git a/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in.R b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in.R new file mode 100644 index 000000000..7cf13d129 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in.R @@ -0,0 +1,12 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope ="line_breaks", +#' math_token_spacing = specify_math_token_spacing( zero = "'+'") +#' ) +a = call diff --git a/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in_tree b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in_tree new file mode 100644 index 000000000..1c39fca04 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-in_tree @@ -0,0 +1,18 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Pr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' (c [1/0] {4} + ¦--COMMENT: #' Ca [1/0] {5} + ¦--COMMENT: #' @e [1/0] {6} + ¦--COMMENT: #' st [1/0] {7} + ¦--COMMENT: #' st [1/0] {8} + ¦--COMMENT: #' [1/0] {9} + ¦--COMMENT: #' [1/0] {10} + ¦--COMMENT: #' ) [1/0] {11} + °--expr: [1/0] {11.9} + ¦--expr: [0/1] {13} + ¦ °--SYMBOL: a [0/0] {12} + ¦--EQ_ASSIGN: = [0/1] {14} + °--expr: [0/0] {16} + °--SYMBOL: call [0/0] {15} diff --git a/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-out.R b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-out.R new file mode 100644 index 000000000..5d3d4873e --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/2-one-function-examples-last-proper-run-out.R @@ -0,0 +1,12 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specify_math_token_spacing(zero = "'+'") +#' ) +a <- call diff --git a/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in.R b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in.R new file mode 100644 index 000000000..3dce9f800 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in.R @@ -0,0 +1,7 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = tidyverse_style , strict =TRUE) +#' @name k +a<- 2 diff --git a/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in_tree b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in_tree new file mode 100644 index 000000000..c32ba1bc4 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-in_tree @@ -0,0 +1,13 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Pr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' Ca [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' @n [1/0] {6} + °--expr: [1/0] {7} + ¦--expr: [0/0] {9} + ¦ °--SYMBOL: a [0/0] {8} + ¦--LEFT_ASSIGN: <- [0/1] {10} + °--expr: [0/0] {12} + °--NUM_CONST: 2 [0/0] {11} diff --git a/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-out.R b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-out.R new file mode 100644 index 000000000..ce8ebd565 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/3-one-function-example-not-last-proper-run-out.R @@ -0,0 +1,8 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' @name k +a <- 2 diff --git a/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in.R b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in.R new file mode 100644 index 000000000..295aa90b4 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in.R @@ -0,0 +1,12 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3","a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a<-call diff --git a/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in_tree b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in_tree new file mode 100644 index 000000000..99d5e726e --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-in_tree @@ -0,0 +1,18 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Th [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' St [1/0] {3} + ¦--COMMENT: #' @f [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' st [1/0] {6} + ¦--COMMENT: #' st [1/0] {7} + ¦--COMMENT: #' st [1/0] {8} + ¦--COMMENT: #' st [1/0] {9} + ¦--COMMENT: #' @i [1/0] {10} + ¦--COMMENT: #' @e [1/0] {11} + °--expr: [1/0] {12} + ¦--expr: [0/0] {14} + ¦ °--SYMBOL: a [0/0] {13} + ¦--LEFT_ASSIGN: <- [0/0] {15} + °--expr: [0/0] {17} + °--SYMBOL: call [0/0] {16} diff --git a/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-out.R b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-out.R new file mode 100644 index 000000000..81a774367 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/4-one-function-examples-not-last-proper-run-out.R @@ -0,0 +1,12 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a <- call diff --git a/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in.R b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in.R new file mode 100644 index 000000000..0b8c3748d --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in.R @@ -0,0 +1,18 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE)# keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +a <- call + +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in_tree b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in_tree new file mode 100644 index 000000000..9ca818289 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-in_tree @@ -0,0 +1,28 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Th [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' St [1/0] {3} + ¦--COMMENT: #' @f [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' st [1/0] {6} + ¦--COMMENT: #' st [1/0] {7} + ¦--COMMENT: #' st [1/0] {8} + ¦--COMMENT: #' st [1/0] {9} + ¦--expr: [1/0] {10} + ¦ ¦--expr: [0/1] {12} + ¦ ¦ °--SYMBOL: a [0/0] {11} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {13} + ¦ °--expr: [0/0] {15} + ¦ °--SYMBOL: call [0/0] {14} + ¦--COMMENT: #' Pr [2/0] {16} + ¦--COMMENT: #' [1/0] {17} + ¦--COMMENT: #' Pe [1/0] {18} + ¦--COMMENT: #' (c [1/0] {19} + ¦--COMMENT: #' Ca [1/0] {20} + ¦--COMMENT: #' @e [1/0] {21} + °--expr: [1/0] {22} + ¦--expr: [0/1] {24} + ¦ °--SYMBOL: a [0/0] {23} + ¦--LEFT_ASSIGN: <- [0/3] {25} + °--expr: [0/0] {27} + °--NUM_CONST: 2 [0/0] {26} diff --git a/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-out.R b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-out.R new file mode 100644 index 000000000..410c87085 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/5-multiple-function-examples-last-proper-run-out.R @@ -0,0 +1,19 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +a <- call + +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in.R b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in.R new file mode 100644 index 000000000..8ae3f8614 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in.R @@ -0,0 +1,23 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = +#' tidyverse_style, strict = TRUE) +#' @name k +a <- 2 + +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text( +#' c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a <- call; + diff --git a/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in_tree b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in_tree new file mode 100644 index 000000000..6f9d083cf --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-in_tree @@ -0,0 +1,33 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' Pr [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' Ca [1/0] {4} + ¦--COMMENT: #' @e [1/0] {5} + ¦--COMMENT: #' ti [1/0] {6} + ¦--COMMENT: #' @n [1/0] {7} + ¦--expr: [1/0] {8} + ¦ ¦--expr: [0/1] {10} + ¦ ¦ °--SYMBOL: a [0/0] {9} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {11} + ¦ °--expr: [0/0] {13} + ¦ °--NUM_CONST: 2 [0/0] {12} + ¦--COMMENT: #' Th [2/0] {14} + ¦--COMMENT: #' [1/0] {15} + ¦--COMMENT: #' St [1/0] {16} + ¦--COMMENT: #' @f [1/0] {17} + ¦--COMMENT: #' @e [1/0] {18} + ¦--COMMENT: #' st [1/0] {19} + ¦--COMMENT: #' st [1/0] {20} + ¦--COMMENT: #' st [1/0] {21} + ¦--COMMENT: #' c( [1/0] {22} + ¦--COMMENT: #' st [1/0] {23} + ¦--COMMENT: #' @i [1/0] {24} + ¦--COMMENT: #' @e [1/0] {25} + ¦--expr: [1/0] {26} + ¦ ¦--expr: [0/5] {28} + ¦ ¦ °--SYMBOL: a [0/0] {27} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {29} + ¦ °--expr: [0/0] {31} + ¦ °--SYMBOL: call [0/0] {30} + °--';': ; [0/0] {32} diff --git a/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-out.R b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-out.R new file mode 100644 index 000000000..f9bbc04bd --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/6-multiple-function-examples-no-last-run-out.R @@ -0,0 +1,27 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg( +#' style = +#' tidyverse_style, strict = TRUE +#' ) +#' @name k +a <- 2 + +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text( +#' c("ab <- 3", "a <-3"), +#' strict = FALSE +#' ) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a <- call diff --git a/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in.R b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in.R new file mode 100644 index 000000000..f8b42aedd --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in.R @@ -0,0 +1,33 @@ +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file<- tempfile("styler", +#' fileext = ".R") +#' enc::write_lines_enc("1++1", file) +#' style_file( +#' file, style = tidyverse_style, strict = TRUE) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' unlink(file2) +#' @family stylers +#' @export +style_file <- function(path, + ... , + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed<- withr::with_dir( + dirname(path + ), + transform_files(basename(path), transformers) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in_tree b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in_tree new file mode 100644 index 000000000..eeb7600b3 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-in_tree @@ -0,0 +1,97 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' St [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' [1/0] {4} + ¦--COMMENT: #' @p [1/0] {5} + ¦--COMMENT: #' @i [1/0] {6} + ¦--COMMENT: #' @i [1/0] {7} + ¦--COMMENT: #' @i [1/0] {8} + ¦--COMMENT: #' @i [1/0] {9} + ¦--COMMENT: #' @e [1/0] {10} + ¦--COMMENT: #' # [1/0] {11} + ¦--COMMENT: #' fi [1/0] {12} + ¦--COMMENT: #' fi [1/0] {13} + ¦--COMMENT: #' en [1/0] {14} + ¦--COMMENT: #' st [1/0] {15} + ¦--COMMENT: #' fi [1/0] {16} + ¦--COMMENT: #' st [1/0] {17} + ¦--COMMENT: #' en [1/0] {18} + ¦--COMMENT: #' un [1/0] {19} + ¦--COMMENT: #' @f [1/0] {20} + ¦--COMMENT: #' @e [1/0] {21} + °--expr: [1/0] {22} + ¦--expr: [0/1] {24} + ¦ °--SYMBOL: style [0/0] {23} + ¦--LEFT_ASSIGN: <- [0/1] {25} + °--expr: [0/0] {26} + ¦--FUNCTION: funct [0/0] {27} + ¦--'(': ( [0/0] {28} + ¦--SYMBOL_FORMALS: path [0/0] {29} + ¦--',': , [0/23] {30} + ¦--SYMBOL_FORMALS: ... [1/1] {31} + ¦--',': , [0/23] {32} + ¦--SYMBOL_FORMALS: style [1/1] {33} + ¦--EQ_FORMALS: = [0/1] {34} + ¦--expr: [0/0] {36} + ¦ °--SYMBOL: tidyv [0/0] {35} + ¦--',': , [0/23] {37} + ¦--SYMBOL_FORMALS: trans [1/1] {38} + ¦--EQ_FORMALS: = [0/1] {39} + ¦--expr: [0/0] {40} + ¦ ¦--expr: [0/0] {42} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {41} + ¦ ¦--'(': ( [0/0] {43} + ¦ ¦--expr: [0/0] {45} + ¦ ¦ °--SYMBOL: ... [0/0] {44} + ¦ °--')': ) [0/0] {46} + ¦--',': , [0/23] {47} + ¦--SYMBOL_FORMALS: inclu [1/1] {48} + ¦--EQ_FORMALS: = [0/1] {49} + ¦--expr: [0/0] {51} + ¦ °--NUM_CONST: TRUE [0/0] {50} + ¦--')': ) [0/1] {52} + °--expr: [0/0] {53} + ¦--'{': { [0/2] {54} + ¦--expr: [1/2] {55} + ¦ ¦--expr: [0/0] {57} + ¦ ¦ °--SYMBOL: chang [0/0] {56} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {58} + ¦ °--expr: [0/0] {59} + ¦ ¦--expr: [0/0] {60} + ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {61} + ¦ ¦ ¦--NS_GET: :: [0/0] {62} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {63} + ¦ ¦--'(': ( [0/4] {64} + ¦ ¦--expr: [1/0] {65} + ¦ ¦ ¦--expr: [0/0] {67} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dirna [0/0] {66} + ¦ ¦ ¦--'(': ( [0/0] {68} + ¦ ¦ ¦--expr: [0/12] {70} + ¦ ¦ ¦ °--SYMBOL: path [0/0] {69} + ¦ ¦ °--')': ) [1/0] {71} + ¦ ¦--',': , [0/4] {72} + ¦ ¦--expr: [1/2] {73} + ¦ ¦ ¦--expr: [0/0] {75} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {74} + ¦ ¦ ¦--'(': ( [0/0] {76} + ¦ ¦ ¦--expr: [0/0] {77} + ¦ ¦ ¦ ¦--expr: [0/0] {79} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: basen [0/0] {78} + ¦ ¦ ¦ ¦--'(': ( [0/0] {80} + ¦ ¦ ¦ ¦--expr: [0/0] {82} + ¦ ¦ ¦ ¦ °--SYMBOL: path [0/0] {81} + ¦ ¦ ¦ °--')': ) [0/0] {83} + ¦ ¦ ¦--',': , [0/1] {84} + ¦ ¦ ¦--expr: [0/0] {86} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {85} + ¦ ¦ °--')': ) [0/0] {87} + ¦ °--')': ) [1/0] {88} + ¦--expr: [1/0] {89} + ¦ ¦--expr: [0/0] {91} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {90} + ¦ ¦--'(': ( [0/0] {92} + ¦ ¦--expr: [0/0] {94} + ¦ ¦ °--SYMBOL: chang [0/0] {93} + ¦ °--')': ) [0/0] {95} + °--'}': } [1/0] {96} diff --git a/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-out.R b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-out.R new file mode 100644 index 000000000..2c4d18a1b --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/7-roxygen-no-dontrun-out.R @@ -0,0 +1,35 @@ +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file <- tempfile("styler", +#' fileext = ".R" +#' ) +#' enc::write_lines_enc("1++1", file) +#' style_file( +#' file, +#' style = tidyverse_style, strict = TRUE +#' ) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' unlink(file2) +#' @family stylers +#' @export +style_file <- function(path, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + dirname(path), + transform_files(basename(path), transformers) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in.R b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in.R new file mode 100644 index 000000000..696f0f744 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in.R @@ -0,0 +1,38 @@ +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file<- tempfile("styler", +#' fileext = ".R") +#' \dontrun{enc::write_lines_enc("1++1",file)} +#' style_file( +#' file, style = tidyverse_style, strict = TRUE) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' \dontrun{unlink(file2)} +#' \dontrun{ +#' { x +#' } +#' unlink(file2) +#' } +#' @family stylers +#' @export +style_file <- function(path, + ... , + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed<- withr::with_dir( + dirname(path + ), + transform_files(basename(path), transformers) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in_tree b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in_tree new file mode 100644 index 000000000..1b1221e51 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-in_tree @@ -0,0 +1,102 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' St [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' Pe [1/0] {3} + ¦--COMMENT: #' [1/0] {4} + ¦--COMMENT: #' @p [1/0] {5} + ¦--COMMENT: #' @i [1/0] {6} + ¦--COMMENT: #' @i [1/0] {7} + ¦--COMMENT: #' @i [1/0] {8} + ¦--COMMENT: #' @i [1/0] {9} + ¦--COMMENT: #' @e [1/0] {10} + ¦--COMMENT: #' # [1/0] {11} + ¦--COMMENT: #' fi [1/0] {12} + ¦--COMMENT: #' fi [1/0] {13} + ¦--COMMENT: #' \d [1/0] {14} + ¦--COMMENT: #' st [1/0] {15} + ¦--COMMENT: #' fi [1/0] {16} + ¦--COMMENT: #' st [1/0] {17} + ¦--COMMENT: #' en [1/0] {18} + ¦--COMMENT: #' \d [1/0] {19} + ¦--COMMENT: #' \d [1/0] {20} + ¦--COMMENT: #' { [1/0] {21} + ¦--COMMENT: #' } [1/0] {22} + ¦--COMMENT: #' un [1/0] {23} + ¦--COMMENT: #' } [1/0] {24} + ¦--COMMENT: #' @f [1/0] {25} + ¦--COMMENT: #' @e [1/0] {26} + °--expr: [1/0] {27} + ¦--expr: [0/1] {29} + ¦ °--SYMBOL: style [0/0] {28} + ¦--LEFT_ASSIGN: <- [0/1] {30} + °--expr: [0/0] {31} + ¦--FUNCTION: funct [0/0] {32} + ¦--'(': ( [0/0] {33} + ¦--SYMBOL_FORMALS: path [0/0] {34} + ¦--',': , [0/23] {35} + ¦--SYMBOL_FORMALS: ... [1/1] {36} + ¦--',': , [0/23] {37} + ¦--SYMBOL_FORMALS: style [1/1] {38} + ¦--EQ_FORMALS: = [0/1] {39} + ¦--expr: [0/0] {41} + ¦ °--SYMBOL: tidyv [0/0] {40} + ¦--',': , [0/23] {42} + ¦--SYMBOL_FORMALS: trans [1/1] {43} + ¦--EQ_FORMALS: = [0/1] {44} + ¦--expr: [0/0] {45} + ¦ ¦--expr: [0/0] {47} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {46} + ¦ ¦--'(': ( [0/0] {48} + ¦ ¦--expr: [0/0] {50} + ¦ ¦ °--SYMBOL: ... [0/0] {49} + ¦ °--')': ) [0/0] {51} + ¦--',': , [0/23] {52} + ¦--SYMBOL_FORMALS: inclu [1/1] {53} + ¦--EQ_FORMALS: = [0/1] {54} + ¦--expr: [0/0] {56} + ¦ °--NUM_CONST: TRUE [0/0] {55} + ¦--')': ) [0/1] {57} + °--expr: [0/0] {58} + ¦--'{': { [0/2] {59} + ¦--expr: [1/2] {60} + ¦ ¦--expr: [0/0] {62} + ¦ ¦ °--SYMBOL: chang [0/0] {61} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {63} + ¦ °--expr: [0/0] {64} + ¦ ¦--expr: [0/0] {65} + ¦ ¦ ¦--SYMBOL_PACKAGE: withr [0/0] {66} + ¦ ¦ ¦--NS_GET: :: [0/0] {67} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: with_ [0/0] {68} + ¦ ¦--'(': ( [0/4] {69} + ¦ ¦--expr: [1/0] {70} + ¦ ¦ ¦--expr: [0/0] {72} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: dirna [0/0] {71} + ¦ ¦ ¦--'(': ( [0/0] {73} + ¦ ¦ ¦--expr: [0/12] {75} + ¦ ¦ ¦ °--SYMBOL: path [0/0] {74} + ¦ ¦ °--')': ) [1/0] {76} + ¦ ¦--',': , [0/4] {77} + ¦ ¦--expr: [1/2] {78} + ¦ ¦ ¦--expr: [0/0] {80} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {79} + ¦ ¦ ¦--'(': ( [0/0] {81} + ¦ ¦ ¦--expr: [0/0] {82} + ¦ ¦ ¦ ¦--expr: [0/0] {84} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: basen [0/0] {83} + ¦ ¦ ¦ ¦--'(': ( [0/0] {85} + ¦ ¦ ¦ ¦--expr: [0/0] {87} + ¦ ¦ ¦ ¦ °--SYMBOL: path [0/0] {86} + ¦ ¦ ¦ °--')': ) [0/0] {88} + ¦ ¦ ¦--',': , [0/1] {89} + ¦ ¦ ¦--expr: [0/0] {91} + ¦ ¦ ¦ °--SYMBOL: trans [0/0] {90} + ¦ ¦ °--')': ) [0/0] {92} + ¦ °--')': ) [1/0] {93} + ¦--expr: [1/0] {94} + ¦ ¦--expr: [0/0] {96} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {95} + ¦ ¦--'(': ( [0/0] {97} + ¦ ¦--expr: [0/0] {99} + ¦ ¦ °--SYMBOL: chang [0/0] {98} + ¦ °--')': ) [0/0] {100} + °--'}': } [1/0] {101} diff --git a/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-out.R b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-out.R new file mode 100644 index 000000000..8ae8a1d46 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/8-roxygen-dontrun-out.R @@ -0,0 +1,45 @@ +#' Style `.R` and/or `.Rmd` files +#' +#' Performs various substitutions in the files specified. +#' Carefully examine the results after running this function! +#' @param path A character vector with paths to files to style. +#' @inheritParams style_pkg +#' @inheritSection transform_files Value +#' @inheritSection style_pkg Warning +#' @inheritSection style_pkg Roundtrip Validation +#' @examples +#' # the following is identical but the former is more convenient: +#' file <- tempfile("styler", +#' fileext = ".R" +#' ) +#' \dontrun{ +#' enc::write_lines_enc("1++1", file) +#' } +#' style_file( +#' file, +#' style = tidyverse_style, strict = TRUE +#' ) +#' style_file(file, transformers = tidyverse_style(strict = TRUE)) +#' enc::read_lines_enc(file) +#' \dontrun{ +#' unlink(file2) +#' } +#' \dontrun{ +#' { +#' x +#' } +#' unlink(file2) +#' } +#' @family stylers +#' @export +style_file <- function(path, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + changed <- withr::with_dir( + dirname(path), + transform_files(basename(path), transformers) + ) + invisible(changed) +} diff --git a/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in.R b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in.R new file mode 100644 index 000000000..3fe6ac77b --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in.R @@ -0,0 +1,27 @@ +#' Style a string +#' +#' Styles a character vector. Each element of the character vector corresponds +#' to one line of code. +#' @param text A character vector with text to style. +#' @inheritParams style_pkg +#' @family stylers +#' @examples +#' style_text("call( 1)") +#' style_text("1 + 1", strict = FALSE) +#' style_text("a%>%b", scope = "spaces") +#' style_text("a%>%b; a", scope = "line_breaks") +#' style_text("a%>%b; a", scope = "tokens") +#' # the following is identical but the former is more convenient: +#' style_text("a<-3++1", style = tidyverse_style, strict = TRUE) +#' @examples +#' \dontrun{style_text("a<-3++1", transformers = tidyverse_style(strict = TRUE))} +#' @export +style_text <- function(text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + transformer <- make_transformer(transformers, include_roxygen_examples) + styled_text <- transformer(text) + construct_vertical(styled_text) +} diff --git a/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in_tree b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in_tree new file mode 100644 index 000000000..b880bd709 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-in_tree @@ -0,0 +1,85 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: #' St [0/0] {1} + ¦--COMMENT: #' [1/0] {2} + ¦--COMMENT: #' St [1/0] {3} + ¦--COMMENT: #' to [1/0] {4} + ¦--COMMENT: #' @p [1/0] {5} + ¦--COMMENT: #' @i [1/0] {6} + ¦--COMMENT: #' @f [1/0] {7} + ¦--COMMENT: #' @e [1/0] {8} + ¦--COMMENT: #' st [1/0] {9} + ¦--COMMENT: #' st [1/0] {10} + ¦--COMMENT: #' st [1/0] {11} + ¦--COMMENT: #' st [1/0] {12} + ¦--COMMENT: #' st [1/0] {13} + ¦--COMMENT: #' # [1/0] {14} + ¦--COMMENT: #' st [1/0] {15} + ¦--COMMENT: #' @e [1/0] {16} + ¦--COMMENT: #' \d [1/0] {17} + ¦--COMMENT: #' @e [1/0] {18} + °--expr: [1/0] {19} + ¦--expr: [0/1] {21} + ¦ °--SYMBOL: style [0/0] {20} + ¦--LEFT_ASSIGN: <- [0/1] {22} + °--expr: [0/0] {23} + ¦--FUNCTION: funct [0/0] {24} + ¦--'(': ( [0/0] {25} + ¦--SYMBOL_FORMALS: text [0/0] {26} + ¦--',': , [0/23] {27} + ¦--SYMBOL_FORMALS: ... [1/0] {28} + ¦--',': , [0/23] {29} + ¦--SYMBOL_FORMALS: style [1/1] {30} + ¦--EQ_FORMALS: = [0/1] {31} + ¦--expr: [0/0] {33} + ¦ °--SYMBOL: tidyv [0/0] {32} + ¦--',': , [0/23] {34} + ¦--SYMBOL_FORMALS: trans [1/1] {35} + ¦--EQ_FORMALS: = [0/1] {36} + ¦--expr: [0/0] {37} + ¦ ¦--expr: [0/0] {39} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: style [0/0] {38} + ¦ ¦--'(': ( [0/0] {40} + ¦ ¦--expr: [0/0] {42} + ¦ ¦ °--SYMBOL: ... [0/0] {41} + ¦ °--')': ) [0/0] {43} + ¦--',': , [0/23] {44} + ¦--SYMBOL_FORMALS: inclu [1/1] {45} + ¦--EQ_FORMALS: = [0/1] {46} + ¦--expr: [0/0] {48} + ¦ °--NUM_CONST: TRUE [0/0] {47} + ¦--')': ) [0/1] {49} + °--expr: [0/0] {50} + ¦--'{': { [0/2] {51} + ¦--expr: [1/2] {52} + ¦ ¦--expr: [0/1] {54} + ¦ ¦ °--SYMBOL: trans [0/0] {53} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {55} + ¦ °--expr: [0/0] {56} + ¦ ¦--expr: [0/0] {58} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: make_ [0/0] {57} + ¦ ¦--'(': ( [0/0] {59} + ¦ ¦--expr: [0/0] {61} + ¦ ¦ °--SYMBOL: trans [0/0] {60} + ¦ ¦--',': , [0/1] {62} + ¦ ¦--expr: [0/0] {64} + ¦ ¦ °--SYMBOL: inclu [0/0] {63} + ¦ °--')': ) [0/0] {65} + ¦--expr: [1/2] {66} + ¦ ¦--expr: [0/1] {68} + ¦ ¦ °--SYMBOL: style [0/0] {67} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {69} + ¦ °--expr: [0/0] {70} + ¦ ¦--expr: [0/0] {72} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: trans [0/0] {71} + ¦ ¦--'(': ( [0/0] {73} + ¦ ¦--expr: [0/0] {75} + ¦ ¦ °--SYMBOL: text [0/0] {74} + ¦ °--')': ) [0/0] {76} + ¦--expr: [1/0] {77} + ¦ ¦--expr: [0/0] {79} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: const [0/0] {78} + ¦ ¦--'(': ( [0/0] {80} + ¦ ¦--expr: [0/0] {82} + ¦ ¦ °--SYMBOL: style [0/0] {81} + ¦ °--')': ) [0/0] {83} + °--'}': } [1/0] {84} diff --git a/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-out.R b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-out.R new file mode 100644 index 000000000..8a4f99f98 --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/9-styler-r-ui-style-string-multiple-out.R @@ -0,0 +1,28 @@ +#' Style a string +#' +#' Styles a character vector. Each element of the character vector corresponds +#' to one line of code. +#' @param text A character vector with text to style. +#' @inheritParams style_pkg +#' @family stylers +#' @examples +#' style_text("call( 1)") +#' style_text("1 + 1", strict = FALSE) +#' style_text("a%>%b", scope = "spaces") +#' style_text("a%>%b; a", scope = "line_breaks") +#' style_text("a%>%b; a", scope = "tokens") +#' # the following is identical but the former is more convenient: +#' style_text("a<-3++1", style = tidyverse_style, strict = TRUE) +#' \dontrun{ +#' style_text("a<-3++1", transformers = tidyverse_style(strict = TRUE)) +#' } +#' @export +style_text <- function(text, + ..., + style = tidyverse_style, + transformers = style(...), + include_roxygen_examples = TRUE) { + transformer <- make_transformer(transformers, include_roxygen_examples) + styled_text <- transformer(text) + construct_vertical(styled_text) +} diff --git a/tests/testthat/roxygen-examples-identify/1-one-function-example-last-proper-run.R b/tests/testthat/roxygen-examples-identify/1-one-function-example-last-proper-run.R new file mode 100644 index 000000000..c5119969a --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/1-one-function-example-last-proper-run.R @@ -0,0 +1,7 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#'@examples style_pkg(style = tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-identify/2-one-function-examples-last-proper-run.R b/tests/testthat/roxygen-examples-identify/2-one-function-examples-last-proper-run.R new file mode 100644 index 000000000..5d3d4873e --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/2-one-function-examples-last-proper-run.R @@ -0,0 +1,12 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples +#' style_pkg(style = tidyverse_style, strict = TRUE) +#' style_pkg( +#' scope = "line_breaks", +#' math_token_spacing = specify_math_token_spacing(zero = "'+'") +#' ) +a <- call diff --git a/tests/testthat/roxygen-examples-identify/3-one-function-example-not-last-proper-run.R b/tests/testthat/roxygen-examples-identify/3-one-function-example-not-last-proper-run.R new file mode 100644 index 000000000..5c4814dbf --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/3-one-function-example-not-last-proper-run.R @@ -0,0 +1,7 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = tidyverse_style, strict = TRUE) +#' @name k +a <- 2 diff --git a/tests/testthat/roxygen-examples-identify/4-one-function-examples-not-last-proper-run.R b/tests/testthat/roxygen-examples-identify/4-one-function-examples-not-last-proper-run.R new file mode 100644 index 000000000..81a774367 --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/4-one-function-examples-not-last-proper-run.R @@ -0,0 +1,12 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a <- call diff --git a/tests/testthat/roxygen-examples-identify/5-multiple-function-examples-last-proper-run.R b/tests/testthat/roxygen-examples-identify/5-multiple-function-examples-last-proper-run.R new file mode 100644 index 000000000..c35de4e6e --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/5-multiple-function-examples-last-proper-run.R @@ -0,0 +1,18 @@ +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +a <- call + +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package +#' (code and tests). +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = tidyverse_style, strict = TRUE) +a <- 2 diff --git a/tests/testthat/roxygen-examples-identify/6-multiple-function-examples-not-last-proper-run.R b/tests/testthat/roxygen-examples-identify/6-multiple-function-examples-not-last-proper-run.R new file mode 100644 index 000000000..2156d22ad --- /dev/null +++ b/tests/testthat/roxygen-examples-identify/6-multiple-function-examples-not-last-proper-run.R @@ -0,0 +1,21 @@ +#' Prettify R source code +#' +#' Performs various substitutions in all `.R` files in a package... +#' Carefully examine the results after running this function! +#' @examples style_pkg(style = tidyverse_style, strict = TRUE) +#' @name k +a <- 2 + +#' The tidyverse style +#' +#' Style code according to the tidyverse style guide. +#' @family style_guides +#' @examples +#' style_text("call( 1)", style = tidyverse_style, scope = "spaces") +#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) +#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" +#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-" +#' @importFrom purrr partial +#' @export +a <- call + diff --git a/tests/testthat/serialize_tests/k2-another-in_file-out.R b/tests/testthat/serialize_tests/k2-another-in_file-out.R new file mode 100644 index 000000000..9f77f05f3 --- /dev/null +++ b/tests/testthat/serialize_tests/k2-another-in_file-out.R @@ -0,0 +1,3 @@ +call(1, + call2(call(3, 1, 2), + 4)) diff --git a/tests/testthat/test-identify-roxygen-examples.R b/tests/testthat/test-identify-roxygen-examples.R new file mode 100644 index 000000000..18af11053 --- /dev/null +++ b/tests/testthat/test-identify-roxygen-examples.R @@ -0,0 +1,57 @@ +context("test-roxygen-examples-identify.R") + +#' Things to consider: +#' * one function declaration or many +#' * example(s) is last tag or not? +#' * malformatted examples +#' * \dontrun examples + +test_that("one function, last tag, properly formatted, no dontrun", { + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/1-one-function-example-last-proper-run.R" + )), + list(c(6)) + ) + + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/2-one-function-examples-last-proper-run.R" + )), + list(seq(6, 11)) + ) +}) + +test_that("one function, not last, tag, properly formatted, no dontrun", { + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/3-one-function-example-not-last-proper-run.R" + )), + list(seq(5, 5)) + ) + + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/4-one-function-examples-not-last-proper-run.R" + )), + list(seq(5, 9)) + ) +}) + +test_that("multiple functions, last, tag, properly formatted, no dontrun", { + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/5-multiple-function-examples-last-proper-run.R" + )), + list(seq(5, 9), seq(17, 17)) + ) +}) + +test_that("multiple functions, not last, tag, properly formatted, no dontrun", { + expect_equal( + identify_start_to_stop_of_roxygen_examples(testthat_file( + "roxygen-examples-identify/6-multiple-function-examples-not-last-proper-run.R" + )), + list(seq(5, 5), seq(13, 17)) + ) +}) diff --git a/tests/testthat/test-roxygen-examples-complete.R b/tests/testthat/test-roxygen-examples-complete.R new file mode 100644 index 000000000..1954960a8 --- /dev/null +++ b/tests/testthat/test-roxygen-examples-complete.R @@ -0,0 +1,73 @@ +context("test-roxygen-examples-complete") + +test_that("analogous to test-roxygen-examples-complete", { + expect_warning(test_collection( + "roxygen-examples-complete", "^1[^1234567890]", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^11", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^12", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^13", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^14", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^2", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^3", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^4", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^5", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^6", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^7", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^8", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^9", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^10", + transformer = style_text + ), NA) +}) diff --git a/tests/testthat/test-serialize_tests.R b/tests/testthat/test-serialize_tests.R index 6c65a58df..e35ea6ef3 100644 --- a/tests/testthat/test-serialize_tests.R +++ b/tests/testthat/test-serialize_tests.R @@ -5,17 +5,6 @@ test_that("No files to compare returns error", { transformer = as_is),"no items") }) -test_that("Can handle multiple in for one out file", { - expect_warning(test_collection("serialize_tests", "k2", - transformer = identity), - c("k2\\-another\\-in_file.*k2\\-out")) - - expect_warning(test_collection("serialize_tests", "k2", - transformer = identity), - c("k2\\-in.*k2\\-out")) -}) - - test_that("properly detects non-match", { expect_warning(test_collection("serialize_tests", "k3", transformer = identity, diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 000000000..097b24163 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html +*.R diff --git a/vignettes/styling-roxygen-code-examples.Rmd b/vignettes/styling-roxygen-code-examples.Rmd new file mode 100644 index 000000000..c796a0b14 --- /dev/null +++ b/vignettes/styling-roxygen-code-examples.Rmd @@ -0,0 +1,12 @@ +--- +title: "Styling roxygen code examples" +author: "Lorenz Walthert" +date: "`r Sys.Date()`" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Styling roxygen code examples} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +This vignette explains on a high level