Skip to content

Commit aa6a21e

Browse files
major API change
- Remove relicts from flat approach - Rename get_transformers to tidyverse_style - Pass arguments to transformers via ... by default
1 parent 325d4ac commit aa6a21e

28 files changed

+258
-427
lines changed

API

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
## Exported functions
44

5-
get_transformers(flat = FALSE, ...)
6-
get_transformers_flat(strict = TRUE, start_comments_with_one_space = FALSE)
7-
get_transformers_nested(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE)
8-
style_dir(path = ".", flat = FALSE, recursive = TRUE, transformers = get_transformers(flat = flat))
9-
style_file(path, flat = FALSE, transformers = get_transformers(flat = flat))
10-
style_pkg(pkg = ".", flat = FALSE, transformers = get_transformers(flat = flat))
11-
style_text(text, flat = FALSE, transformers = get_transformers(flat = flat))
5+
create_style_guide(filler, line_break, space, token, indention, use_raw_indention = FALSE)
6+
style_dir(path = ".", recursive = TRUE, ..., style = tidyverse_style, transformers = style(...))
7+
style_file(path, ..., style = tidyverse_style, transformers = style(...))
8+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...))
9+
style_text(text, ..., style = tidyverse_style, transformers = style(...))
10+
tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE)

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "pkgapi
3535
RoxygenNote: 6.0.1.9000
3636
VignetteBuilder: knitr
3737
Collate:
38-
'get_transformers.R'
3938
'modify_pd.R'
4039
'nested.R'
4140
'nested_to_tree.R'
@@ -49,6 +48,7 @@ Collate:
4948
'rules-spacing.R'
5049
'serialize.R'
5150
'serialized_tests.R'
51+
'style_guides.R'
5252
'styler.R'
5353
'transform.R'
5454
'unindent.R'

NAMESPACE

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(create_style_guide)
4-
export(get_transformers)
5-
export(get_transformers_flat)
6-
export(get_transformers_nested)
74
export(style_dir)
85
export(style_file)
96
export(style_pkg)
@@ -23,4 +20,5 @@ importFrom(purrr,pmap)
2320
importFrom(purrr,pwalk)
2421
importFrom(purrr,reduce)
2522
importFrom(purrr,when)
23+
importFrom(utils,tail)
2624
importFrom(utils,write.table)

R/parsed.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ verify_roundtrip <- function(pd_flat, text) {
8383
#' @param pd_flat A parse table.
8484
#' @return A parse table with two three columns: lag_newlines, newlines and
8585
#' spaces.
86+
#' @importFrom utils tail
8687
create_filler <- function(pd_flat) {
8788

8889
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))

R/get_transformers.R renamed to R/style_guides.R

Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,13 @@
11
#' The tidyverse style
2-
#' @param ... ...
3-
#' @export
4-
tidyverse_style <- function(...) {
5-
get_transformers_nested(...)
6-
}
7-
8-
#' Get the transformer functions for styling
9-
#'
10-
#' @param flat Whether the transformer functions for flat or nested styling
11-
#' should be returned.
12-
#' @return A list of transformer functions that operate on flat parse tables.
13-
#' @param ... Parameters passed to
14-
#' * [get_transformers_flat()] if `flat = TRUE` or
15-
#' * [get_transformers_nested()] if `flat = FALSE`.
16-
#' @export
17-
get_transformers <- function(flat = FALSE, ...) {
18-
if (flat) {
19-
get_transformers_flat(...)
20-
} else {
21-
get_transformers_nested(...)
22-
}
23-
}
24-
25-
#' Get the transformer functions for flat styling
26-
#'
27-
#' @param strict A logical value indicating whether a set of strict
28-
#' or not so strict transformer functions should be returned.
29-
#' @param start_comments_with_one_space Whether or not comments should start
30-
#' with only one space (see [start_comments_with_space()]).
31-
#' @return A list of transformer functions that operate on flat parse
32-
#' tables.
33-
#' @export
34-
#' @family obtain transformers
35-
#' @importFrom purrr partial
36-
get_transformers_flat <- function(strict = TRUE,
37-
start_comments_with_one_space = FALSE) {
38-
c(
39-
fix_quotes,
40-
remove_space_before_closing_paren,
41-
if (strict) remove_space_before_opening_paren,
42-
add_space_after_for_if_while,
43-
add_space_before_brace,
44-
if (strict) set_space_around_op else add_space_around_op,
45-
if (strict) set_space_after_comma else add_space_after_comma,
46-
remove_space_before_comma,
47-
remove_space_after_opening_paren,
48-
remove_space_after_excl,
49-
remove_space_before_dollar,
50-
partial(start_comments_with_space,
51-
force_one = start_comments_with_one_space),
52-
NULL)
53-
}
54-
55-
#' Get the transformer functions for nested styling
56-
#'
57-
#' Similar to [get_transformers_flat()], but additionally, returns some
58-
#' functions needed due the fact that styling is done in a nested way.
592
#' @param scope The extent of manipulation. Can range from "none" (least
603
#' invasive) to "token" (most invasive). See 'Details'. This argument is a
614
#' vector of length one.
625
#' @param indent_by How many spaces of indention should be inserted after
636
#' operators such as '('.
64-
#' @inheritParams get_transformers_flat
7+
#' @param strict A logical value indicating whether a set of strict
8+
#' or not so strict transformer functions should be returned.
9+
#' @param start_comments_with_one_space Whether or not comments should start
10+
#' with only one space (see [start_comments_with_space()]).
6511
#' @details The following options for `scope` are available.
6612
#'
6713
#' * "none": Performs no transformation at all.
@@ -78,11 +24,10 @@ get_transformers_flat <- function(strict = TRUE,
7824
#' @family obtain transformers
7925
#' @importFrom purrr partial
8026
#' @export
81-
get_transformers_nested <- function(
82-
scope = "tokens",
83-
strict = TRUE,
84-
indent_by = 2,
85-
start_comments_with_one_space = FALSE) {
27+
tidyverse_style <- function(scope = "tokens",
28+
strict = TRUE,
29+
indent_by = 2,
30+
start_comments_with_one_space = FALSE) {
8631

8732
scope <- character_to_ordered(
8833
scope,
@@ -96,7 +41,21 @@ get_transformers_nested <- function(
9641
partial(indent_op, indent_by = indent_by),
9742
partial(indent_eq_sub, indent_by = indent_by),
9843
partial(indent_without_paren, indent_by = indent_by),
99-
get_transformers_flat(strict, start_comments_with_one_space),
44+
45+
fix_quotes,
46+
remove_space_before_closing_paren,
47+
if (strict) remove_space_before_opening_paren,
48+
add_space_after_for_if_while,
49+
add_space_before_brace,
50+
if (strict) set_space_around_op else add_space_around_op,
51+
if (strict) set_space_after_comma else add_space_after_comma,
52+
remove_space_before_comma,
53+
remove_space_after_opening_paren,
54+
remove_space_after_excl,
55+
remove_space_before_dollar,
56+
partial(start_comments_with_space,
57+
force_one = start_comments_with_one_space),
58+
10059
remove_space_after_unary_pm_nested,
10160
set_space_before_comments,
10261
set_space_between_levels
@@ -139,6 +98,23 @@ get_transformers_nested <- function(
13998
)
14099
}
141100

101+
#' Create a style guide
102+
#'
103+
#' This is a helper function to create a style guide, which is technically
104+
#' speaking a named list of groups of transformer functions where each
105+
#' transformer function corresponds to one styling rule. The output of this
106+
#' function can be used as an argument for \code{style} in top level functions
107+
#' like [style_text()] and friends.
108+
#' @param filler A filler function that initializes various variables on each
109+
#' level of nesting.
110+
#' @param line_break A list of transformer functiosn that manipulate line_break
111+
#' information.
112+
#' @param space A list of transformer functions that manipulate spacing
113+
#' information.
114+
#' @param token A list of transformer functions that manipulate token text.
115+
#' @param indention A list of transformer functions that manipulate indention.
116+
#' @param use_raw_indention Boolean indicating wheter or not the raw indention
117+
#' should be used.
142118
#' @export
143119
create_style_guide <- function(filler,
144120
line_break,

R/transform.R

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#' @return A logical value that indicates whether or not any file was changed is
99
#' returned invisibly. If files were changed, the user is informed to
1010
#' carefully inspect the changes via a message sent to the console.
11-
transform_files <- function(files, transformers, flat) {
12-
transformer <- make_transformer(transformers, flat = flat)
11+
transform_files <- function(files, transformers) {
12+
transformer <- make_transformer(transformers)
1313

1414
changed <- utf8::transform_lines_enc(files, transformer)
1515
if (any(changed)) {
@@ -24,49 +24,7 @@ transform_files <- function(files, transformers, flat) {
2424
#' that should be transformed.
2525
#' @param transformers A list of transformer functions that operate on flat
2626
#' parse tables.
27-
#' @param flat Whether to do the styling with a flat approach or with a nested
28-
#' approach.
29-
#' @family make transformers
30-
make_transformer <- function(transformers, flat) {
31-
if (flat) {
32-
make_transformer_flat(transformers = transformers)
33-
} else {
34-
make_transformer_nested(transformers = transformers)
35-
}
36-
}
37-
38-
#' A Closure to return transformer function
39-
#'
40-
#' Returns a closure that turns `text` into a flat parse table and applies
41-
#' `transformers` on it.
42-
#' @inheritParams make_transformer
43-
#' @family make transformers
44-
make_transformer_flat <- function(transformers) {
45-
function(text) {
46-
text <- gsub(" +$", "", text)
47-
text <- gsub("\t", " ", text)
48-
49-
pd_flat <- compute_parse_data_flat_enhanced(text)
50-
51-
# May strip empty lines before EOF
52-
text <- verify_roundtrip(pd_flat, text)
53-
54-
transformed_pd_flat <- Reduce(function(x, fun) fun(x),
55-
transformers,
56-
init = pd_flat)
57-
58-
new_text <- serialize_parse_data_flat(transformed_pd_flat)
59-
new_text
60-
}
61-
}
62-
63-
#' Closure to return transformer function
64-
#'
65-
#' Returns a closure that turns `text` into a nested parse table and applies
66-
#' `transformers` on it.
67-
#' @inheritParams make_transformer
68-
#' @family make transformers
69-
make_transformer_nested <- function(transformers) {
27+
make_transformer <- function(transformers) {
7028
function(text) {
7129
if (is.null(transformers$space)) return(text)
7230
transformed_text <- parse_transform_serialize(text, transformers)
@@ -75,7 +33,6 @@ make_transformer_nested <- function(transformers) {
7533
}
7634
}
7735

78-
7936
#' Parse, transform and serialize text
8037
#'
8138
#' Wrapper function for the common three operations.

R/ws.R

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,43 @@ NULL
1010
#' (code and tests).
1111
#' Carefully examine the results after running this function!
1212
#'
13-
#' @param pkg Path to a (subdirectory of an) R package
14-
#' @param ... Passed on to [get_transformers()]
15-
#' @inheritParams style_text
13+
#' @param pkg Path to a (subdirectory of an) R package.
14+
#' @param ... Passed on to the `style` function.
15+
#' @param style The unquoted name of a style guide to use. Will not be used
16+
#' further except to construct the argument `transformers`.
17+
#' @param transformers A set of transformer functions.
1618
#' @export
1719
#' @family stylers
1820
style_pkg <- function(pkg = ".",
1921
...,
2022
style = tidyverse_style,
2123
transformers = style(...)) {
2224
pkg_root <- rprojroot::find_package_root_file(path = pkg)
23-
withr::with_dir(pkg_root, prettify_local(transformers, flat = FALSE))
25+
withr::with_dir(pkg_root, prettify_local(transformers))
2426
}
2527

26-
prettify_local <- function(transformers, flat) {
28+
prettify_local <- function(transformers) {
2729
r_files <- dir(path = "R", pattern = "[.][rR]$", recursive = TRUE, full.names = TRUE)
2830
r_files <- grep("/RcppExports[.]R$", r_files, invert = TRUE, value = TRUE)
2931
test_files <- dir(path = "tests/testthat", pattern = "[.][rR]$", recursive = TRUE, full.names = TRUE)
3032
files <- c(r_files, test_files)
3133

32-
transform_files(files, transformers, flat)
34+
transform_files(files, transformers)
3335
}
3436

3537

3638
#' Style a string
3739
#'
3840
#' Styles a character vector
3941
#' @param text A character vector with text to style.
40-
#' @param transformers A list with functions to be applied to the parsed data.
41-
#' @param flat Whether to do the styling with a flat approach or with a nested
42-
#' approach.
42+
#' @inheritParams style_pkg
4343
#' @family stylers
4444
#' @export
4545
style_text <- function(text,
46-
flat = FALSE,
47-
transformers = get_transformers(flat = flat)) {
48-
transformer <- make_transformer(transformers, flat = flat)
46+
...,
47+
style = tidyverse_style,
48+
transformers = style(...)) {
49+
transformer <- make_transformer(transformers)
4950
transformer(text)
5051
}
5152

@@ -56,28 +57,26 @@ style_text <- function(text,
5657
#' @param path Path to a directory with files to transform.
5758
#' @param recursive A logical value indicating whether or not files in subdirectories
5859
#' of `path` should be styled as well.
59-
#' @inheritParams style_text
60+
#' @inheritParams style_pkg
6061
#' @family stylers
6162
#' @export
6263
style_dir <- function(path = ".",
63-
flat = FALSE,
64-
recursive = TRUE,
65-
transformers = get_transformers(flat = flat)) {
66-
withr::with_dir(path, prettify_any(transformers,
67-
recursive = recursive,
68-
flat = flat))
64+
recursive = TRUE,
65+
...,
66+
style = tidyverse_style,
67+
transformers = style(...)) {
68+
withr::with_dir(path, prettify_any(transformers, recursive = recursive))
6969
}
7070

7171
#' Prettify R code in current working directory
7272
#'
7373
#' This is a helper function for style_dir.
74-
#' @inheritParams style_dir
74+
#' @inheritParams style_pkg
7575
#' @param recursive A logical value indicating whether or not files in subdirectories
7676
#' should be styled as well.
77-
#' @keywords internal
78-
prettify_any <- function(transformers, recursive, flat) {
77+
prettify_any <- function(transformers, recursive) {
7978
files <- dir(path = ".", pattern = "[.][rR]$", recursive = recursive, full.names = TRUE)
80-
transform_files(files, transformers, flat = flat)
79+
transform_files(files, transformers)
8180

8281
}
8382

@@ -86,32 +85,31 @@ prettify_any <- function(transformers, recursive, flat) {
8685
#' Performs various substitutions in the `.R` file specified.
8786
#' Carefully examine the results after running this function!
8887
#' @param path A path to a file to style.
89-
#' @inheritParams style_text
88+
#' @inheritParams style_pkg
9089
#' @family stylers
9190
#' @export
9291
style_file <- function(path,
93-
flat = FALSE,
94-
transformers = get_transformers(flat = flat)) {
92+
...,
93+
style = tidyverse_style,
94+
transformers = style(...)) {
9595
withr::with_dir(dirname(path),
96-
prettify_one(transformers, flat, basename(path)))
96+
prettify_one(transformers, basename(path)))
9797
}
9898

9999
#' Prettify one R file
100100
#'
101101
#' This is a helper function for style_dir.
102102
#' @inheritParams style_dir
103103
#' @param path The path to a file that should be styled.
104-
prettify_one <- function(transformers, flat, path) {
104+
prettify_one <- function(transformers, path) {
105105
if (!grepl("\\.[Rr]$", path)) stop(path, " is not a .R file")
106-
transform_files(path, transformers, flat = flat)
106+
transform_files(path, transformers)
107107
}
108108

109109
#' Style the active file
110110
#'
111111
#' Helper function fot RStudio Add-in.
112112
style_active_file <- function() {
113113
file <- rstudioapi::getActiveDocumentContext()$path
114-
style_file(file,
115-
flat = FALSE,
116-
transformers = get_transformers(flat = FALSE))
114+
style_file(file, transformers = tidyverse_style)
117115
}

0 commit comments

Comments
 (0)