Skip to content

Commit a68fdce

Browse files
Merge pull request #298 from lorenzwalthert/r-1.0.0
- R 1.0.0 (into r-1.0.0 instead of master) (#298).
2 parents bd27568 + 285dba4 commit a68fdce

File tree

120 files changed

+193
-2321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+193
-2321
lines changed

API

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## Exported functions
44

5-
create_style_guide(initialize = initialize_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
5+
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
6+
default_style_guide_attributes(pd_flat)
67
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
78
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
89
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)
@@ -12,7 +13,3 @@ style_text(text, ..., style = tidyverse_style, transformers = style(...))
1213
tidyverse_math_token_spacing()
1314
tidyverse_reindention()
1415
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())
15-
16-
## Foreign S3 methods
17-
18-
print.vertical(x, ...)

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: styler
2-
Title: Non-Invasive Pretty Printing of R code
2+
Title: Non-Invasive Pretty Printing of R Code
33
Version: 1.0.0
44
Authors@R: c(person("Kirill", "Müller", role = c("aut"), email = "[email protected]"),
55
person("Lorenz", "Walthert", role = c("cre", "aut"), email = "[email protected]"))

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method(print,vertical)
44
export(create_style_guide)
5+
export(default_style_guide_attributes)
56
export(specify_math_token_spacing)
67
export(specify_reindention)
78
export(style_dir)

NEWS.md

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
1-
## styler 0.1.0 (2017-12-05)
2-
3-
create_style_guide(initialize = initialize_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
4-
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
5-
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
6-
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)
7-
style_file(path, ..., style = tidyverse_style, transformers = style(...))
8-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R")
1+
## styler 1.0.0 (2017-12-05)
2+
3+
Initial release.
4+
5+
### stylers
6+
These are functions used to style code. They style a directory, a whole package,
7+
a file or a string.
8+
```
9+
style_dir(path = ".",
10+
..., style = tidyverse_style, transformers = style(...),
11+
filetype = "R", recursive = TRUE, exclude_files = NULL
12+
)
13+
14+
style_pkg(pkg = ".",
15+
..., style = tidyverse_style, transformers = style(...), filetype = "R",
16+
exclude_files = "R/RcppExports.R"
17+
)
18+
19+
20+
style_file(path,
21+
..., style = tidyverse_style, transformers = style(...)
22+
)
23+
924
style_text(text, ..., style = tidyverse_style, transformers = style(...))
10-
tidyverse_math_token_spacing()
25+
```
26+
27+
### style guides
28+
These functions are the style guides implemented.
29+
```
30+
tidyverse_style(
31+
scope = "tokens",
32+
strict = TRUE,
33+
indent_by = 2,
34+
start_comments_with_one_space = FALSE,
35+
reindention = tidyverse_reindention(),
36+
math_token_spacing = tidyverse_math_token_spacing()
37+
)
1138
tidyverse_reindention()
12-
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())
39+
tidyverse_math_token_spacing())
40+
```
41+
42+
### style guide creators
43+
This function is used to create a style guide.
44+
```
45+
create_style_guide(
46+
initialize = default_style_guide_attributes,
47+
line_break = NULL,
48+
space = NULL,
49+
token = NULL,
50+
indention = NULL,
51+
use_raw_indention = FALSE,
52+
reindention = tidyverse_reindention()
53+
)
54+
```
55+
56+
### Helpers
57+
These are helper functions used to specify the style guides in use.
58+
59+
```
60+
specify_math_token_spacing(
61+
zero = NULL,
62+
one = c("'+'", "'-'", "'*'", "'/'", "'^'")
63+
)
64+
65+
specify_reindention(
66+
regex_pattern = NULL,
67+
indention = 0,
68+
comments_only = TRUE
69+
)
70+
initialize_default_attributes(pd_flat)
71+
```

R/indent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Update indention information of parse data
22
#'
33
#' @param pd A nested or flat parse table that is already enhanced with
4-
#' line break and space information via [initialize_attributes()].
4+
#' line break and space information via [default_style_guide_attributes()].
55
#' @param indent_by How many spaces should be added after the token of interest.
66
#' @param token The token the indention should be based on.
77
#' @name update_indention

R/initialize.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
#' Enrich parse table with space and line break information
1+
#' Intialize default style guide attributes
22
#'
3-
#' This function computes difference (as column and line difference) between two
4-
#' entries in the parse table and adds this information to the table.
3+
#' This function initialises and removes various variables from the parse
4+
#' table.
55
#' @param pd_flat A parse table.
66
#' @importFrom utils tail
7-
initialize_attributes <- function(pd_flat) {
7+
#' @examples
8+
#' string_to_format <- "call( 3)"
9+
#' pd <- styler:::compute_parse_data_nested(string_to_format)
10+
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
11+
#' @export
12+
default_style_guide_attributes <- function(pd_flat) {
813

914
init_pd <-
1015
initialize_newlines(pd_flat) %>%
@@ -17,6 +22,11 @@ initialize_attributes <- function(pd_flat) {
1722
init_pd
1823
}
1924

25+
#' Initialize attributes
26+
#'
27+
#' @name initialize_attributes
28+
NULL
29+
2030
#' @describeIn initialize_attributes Initializes `newlines` and `lag_newlines`.
2131
initialize_newlines <- function(pd_flat) {
2232
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))

R/nested_to_tree.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @importFrom purrr when
88
create_tree <- function(text, structure_only = FALSE) {
99
compute_parse_data_nested(text) %>%
10-
pre_visit(c(initialize_attributes)) %>%
10+
pre_visit(c(default_style_guide_attributes)) %>%
1111
create_node_from_nested_root(structure_only) %>%
1212
as.data.frame()
1313
}
@@ -25,7 +25,7 @@ create_tree <- function(text, structure_only = FALSE) {
2525
#' if (getRversion() >= 3.2) {
2626
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
2727
#' nested_pd <- styler:::compute_parse_data_nested(code)
28-
#' initialized <- styler:::pre_visit(nested_pd, c(styler:::initialize_attributes))
28+
#' initialized <- styler:::pre_visit(nested_pd, c(default_style_guide_attributes))
2929
#' styler:::create_node_from_nested_root(initialized, structure_only = FALSE)
3030
#' }
3131
create_node_from_nested_root <- function(pd_nested, structure_only) {

R/serialized_tests.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ NULL
144144
style_empty <- function(text) {
145145
transformers <- list(
146146
# transformer functions
147-
initialize = initialize_attributes,
147+
initialize = default_style_guide_attributes,
148148
line_break = NULL,
149149
space = NULL,
150150
token = NULL,
@@ -163,7 +163,7 @@ style_op <- function(text) {
163163

164164
transformers <- list(
165165
# transformer functions
166-
initialize = initialize_attributes,
166+
initialize = default_style_guide_attributes,
167167
line_break = NULL,
168168
space = partial(indent_op, indent_by = 2),
169169
token = NULL,

R/style_guides.R

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ NULL
2424
#' line breaks to one if there is none and leave the code untouched otherwise.
2525
#' See 'Examples'.
2626
#' @param start_comments_with_one_space Whether or not comments should start
27-
#' with only one space (see [start_comments_with_space()]).
27+
#' with only one space (see `start_comments_with_space()`).
2828
#' @inheritParams create_style_guide
2929
#' @param math_token_spacing A list of parameters that define spacing around
3030
#' math token, conveniently constructed using [specify_math_token_spacing()].
@@ -46,6 +46,7 @@ NULL
4646
#' @family style_guides
4747
#' @examples
4848
#' style_text("call( 1)", style = tidyverse_style, scope = "spaces")
49+
#' style_text("call( 1)", transformers = tidyverse_style(strict = TRUE))
4950
#' style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-"
5051
#' style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-"
5152
#' @importFrom purrr partial
@@ -135,7 +136,7 @@ tidyverse_style <- function(scope = "tokens",
135136

136137
create_style_guide(
137138
# transformer functions
138-
initialize = initialize_attributes,
139+
initialize = default_style_guide_attributes,
139140
line_break = line_break_manipulators,
140141
space = space_manipulators,
141142
token = token_manipulators,
@@ -153,8 +154,8 @@ tidyverse_style <- function(scope = "tokens",
153154
#' transformer function corresponds to one styling rule. The output of this
154155
#' function can be used as an argument for \code{style} in top level functions
155156
#' like [style_text()] and friends.
156-
#' @param initialize A function that initializes various variables on each
157-
#' level of nesting.
157+
#' @param initialize The bare name of a function that initializes various
158+
#' variables on each level of nesting.
158159
#' @param line_break A list of transformer functions that manipulate line_break
159160
#' information.
160161
#' @param space A list of transformer functions that manipulate spacing
@@ -165,8 +166,18 @@ tidyverse_style <- function(scope = "tokens",
165166
#' should be used.
166167
#' @param reindention A list of parameters for regex re-indention, most
167168
#' conveniently constructed using [specify_reindention()].
169+
#' @examples
170+
#' set_line_break_before_curly_opening <- function(pd_flat) {
171+
#' op <- pd_flat$token %in% "'{'"
172+
#' pd_flat$lag_newlines[op] <- 1L
173+
#' pd_flat
174+
#' }
175+
#' set_line_break_before_curly_opening_style <- function() {
176+
#' create_style_guide(line_break = set_line_break_before_curly_opening)
177+
#' }
178+
#' style_text("a <- function(x) { x }", style = set_line_break_before_curly_opening_style)
168179
#' @export
169-
create_style_guide <- function(initialize = initialize_attributes,
180+
create_style_guide <- function(initialize = default_style_guide_attributes,
170181
line_break = NULL,
171182
space = NULL,
172183
token = NULL,
@@ -205,6 +216,10 @@ NULL
205216

206217
#' @describeIn reindention Allows to specify which tokens are reindented and
207218
#' how.
219+
#' @examples
220+
#' style_text("a <- xyz", reindention = specify_reindention(
221+
#' regex_pattern = "xyz", indention = 4, comments_only = FALSE)
222+
#' )
208223
#' @export
209224
specify_reindention <- function(regex_pattern = NULL,
210225
indention = 0,
@@ -218,6 +233,8 @@ specify_reindention <- function(regex_pattern = NULL,
218233
#' @describeIn reindention Simple forwarder to
219234
#' `specify_reindention` with reindention according to the tidyverse style
220235
#' guide.
236+
#' @examples
237+
#' style_text("a <- xyz", reindention = tidyverse_reindention())
221238
#' @export
222239
tidyverse_reindention <- function() {
223240
specify_reindention(
@@ -281,6 +298,12 @@ specify_math_token_spacing <-
281298
#' @describeIn math_token_spacing Simple forwarder to
282299
#' `specify_math_token_spacing` with spacing around math tokens according to the
283300
#' tidyverse style guide.
301+
#' @examples
302+
#' style_text(
303+
#' "1+1 -3",
304+
#' math_token_spacing = tidyverse_math_token_spacing(),
305+
#' strict = TRUE
306+
#' )
284307
#' @export
285308
tidyverse_math_token_spacing <- function() {
286309
specify_math_token_spacing(one = c("'+'", "'-'", "'*'", "'/'", "'^'"))

R/ui.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ NULL
3131
#' @family stylers
3232
#' @examples
3333
#' \dontrun{
34-
#' # the following is identical but the former is more convenient:
34+
#'
3535
#' style_pkg(style = tidyverse_style, strict = TRUE)
36-
#' style_pkg(transformers = tidyverse_style(strict = TRUE))
36+
#' style_pkg(
37+
#' scope = "line_breaks",
38+
#' math_token_spacing = specify_math_token_spacing(zero = "'+'")
39+
#' )
3740
#' }
3841
#' @export
3942
style_pkg <- function(pkg = ".",
@@ -112,6 +115,10 @@ style_text <- function(text,
112115
#' @inheritSection transform_files Value
113116
#' @inheritSection style_pkg Warning
114117
#' @family stylers
118+
#' @examples
119+
#' \dontrun{
120+
#' style_dir(file_type = "r")
121+
#' }
115122
#' @export
116123
style_dir <- function(path = ".",
117124
...,

0 commit comments

Comments
 (0)