Skip to content

Commit 77499f0

Browse files
committed
Merge branch 'r-0.0-5' into production
2 parents 3098247 + 3040540 commit 77499f0

File tree

128 files changed

+2053
-350
lines changed

Some content is hidden

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

128 files changed

+2053
-350
lines changed

API

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

33
## Exported functions
44

5-
get_transformers(strict = TRUE)
6-
style_pkg(pkg = ".", transformers = get_transformers())
7-
style_src(path = ".", transformers = get_transformers(), recursive = TRUE)
8-
style_text(text, transformers = get_transformers())
5+
get_transformers(flat, ...)
6+
get_transformers_flat(strict = TRUE, start_comments_with_one_space = FALSE)
7+
get_transformers_nested(strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE)
8+
style_pkg(pkg = ".", flat = FALSE, transformers = get_transformers(flat = flat))
9+
style_src(path = ".", flat = FALSE, recursive = TRUE, transformers = get_transformers(flat = flat))
10+
style_text(text, flat = FALSE, transformers = get_transformers(flat = flat))

DESCRIPTION

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
Package: styler
22
Title: Non-invasive Pretty Printing of R code
3-
Version: 0.0-4
3+
Version: 0.0-5
44
Authors@R: person("Kirill", "Müller", role = c("aut", "cre"), email = "[email protected]")
55
Description:
66
Pretty-prints R code without changing the user's formatting intent.
77
Imports:
8-
rprojroot,
9-
utf8,
10-
withr,
118
dplyr,
9+
magrittr,
10+
pkgload,
11+
purrr,
12+
readr,
13+
rprojroot,
1214
tibble,
1315
tidyr,
14-
purrr,
15-
magrittr
16+
utf8,
17+
withr
1618
Suggests:
1719
data.tree,
1820
here,
1921
knitr,
2022
rmarkdown,
2123
testthat
2224
Remotes:
25+
r-lib/pkgload,
2326
krlmlr/utf8
2427
License: GPL-3
2528
Encoding: UTF-8
2629
LazyData: true
27-
Date: 2017-06-15
30+
Date: 2017-06-30
2831
BugReports: https://github.com/krlmlr/styler/issues
2932
URL: https://github.com/krlmlr/styler, http://krlmlr.github.io/styler
3033
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "pkgapi::api_roclet"))

NAMESPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(get_transformers)
4+
export(get_transformers_flat)
5+
export(get_transformers_nested)
46
export(style_pkg)
57
export(style_src)
68
export(style_text)
79
import(dplyr)
810
import(tibble)
911
import(tidyr)
12+
importFrom(purrr,flatten_chr)
1013
importFrom(purrr,map)
1114
importFrom(purrr,map2)
15+
importFrom(purrr,partial)
1216
importFrom(purrr,pmap)
17+
importFrom(purrr,pwalk)
18+
importFrom(purrr,reduce)
19+
importFrom(utils,write.table)

NEWS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## styler 0.0-5 (2017-06-30)
2+
3+
* Correctly deal with comments (spacing before comments, start comment with space)
4+
* T more flexibly (`test_collection()` and friends now support `...`)
5+
* Indention based on curly brackets
6+
* Spacing across different levels of nesting (e.g. a space after `)` in `function(x) {...}`)
7+
* Write tree structure to file via test_collection()` for easy understanding of the nested structure
8+
* Outsource tokenise
9+
* Account for situations where code does not start on line1
10+
* Correctly style comments
11+
* Add style_empty for tailored testing
12+
* Initialize indent in create filler
13+
* Adapt vignette and documentation to visitor concept
14+
* Refine testing to use visiting approach
15+
* Parse multiple expressions: Make nested approach wok on multiple expressions too.
16+
* Internal: introducing the visitor concept instead of "looping" many times through whole nested structure.
17+
* Integrate nested approach in top-level APIs `style_text` and friends via an additional argument. `flat`.
18+
* Add tools for scalable testing. Transform *-in.R with a transformer function and check whether result corresponds to *-out.R
19+
20+
121
## styler 0.0-4 (2017-06-15)
222

323
- Fix `README.Rmd` for compatibility with pkgdown.

R/get_transformers.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#' Get the transformer functions for styling
2+
#'
3+
#' @param flat Whether the transformer functions for flat or nested styling
4+
#' should be returned.
5+
#' @return A list of transformer functions that operate on flat parse tables.
6+
#' @param ... Parameters passed to
7+
#' * [get_transformers_flat()] if `flat = TRUE` or
8+
#' * [get_transformers_nested()] if `flat = FALSE`.
9+
#' @export
10+
get_transformers <- function(flat, ...) {
11+
if (flat) {
12+
get_transformers_flat(...)
13+
} else {
14+
get_transformers_nested(...)
15+
}
16+
}
17+
#' Get the transformer functions for flat styling
18+
#'
19+
#' @param strict A logical value indicating whether a set of strict
20+
#' or not so strict transformer functions should be returned.
21+
#' @param start_comments_with_one_space Whether or not comments should start
22+
#' with only one space (see [start_comments_with_space()]).
23+
#' @return A list of transformer functions that operate on flat parse
24+
#' tables.
25+
#' @export
26+
#' @family obtain transformers
27+
#' @importFrom purrr partial
28+
get_transformers_flat <- function(strict = TRUE,
29+
start_comments_with_one_space = FALSE) {
30+
c(
31+
fix_quotes,
32+
remove_space_before_closing_paren,
33+
if (strict) remove_space_before_opening_paren,
34+
add_space_after_for_if_while,
35+
add_space_before_brace,
36+
if (strict) set_space_around_op else add_space_around_op,
37+
if (strict) set_space_after_comma else add_space_after_comma,
38+
remove_space_after_unary_pm,
39+
remove_space_after_opening_paren,
40+
partial(start_comments_with_space,
41+
force_one = start_comments_with_one_space),
42+
NULL)
43+
}
44+
45+
#' Get the transformer functions for nested styling
46+
#'
47+
#' Similar to [get_transformers_flat()], but additionally, returns some
48+
#' functions needed due the fact that styling is done in a nested way.
49+
#' @param indent_by How many spaces of indention should be inserted after
50+
#' operators such as '('.
51+
#' @inheritParams get_transformers_flat
52+
#' @family obtain transformers
53+
#' @importFrom purrr partial
54+
#' @export
55+
get_transformers_nested <- function(strict = TRUE,
56+
indent_by = 2,
57+
start_comments_with_one_space = FALSE) {
58+
c(create_filler,
59+
partial(indent_round, indent_by = indent_by),
60+
partial(indent_curly, indent_by = indent_by),
61+
strip_eol_spaces,
62+
get_transformers_flat(strict, start_comments_with_one_space),
63+
set_space_before_comments,
64+
set_space_between_levels
65+
)
66+
}

R/modify_pd.R

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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 [create_filler()] or
5-
#' [create_filler_nested()].
4+
#' line break and space information via [create_filler()].
65
#' @param indent_by How many spaces should be added after the token of interest.
76
#' @name update_indention
87
NULL
@@ -17,41 +16,33 @@ indent_round <- function(pd, indent_by) {
1716
start <- stop <- 0
1817
}
1918
pd <- pd %>%
20-
mutate(indent = ifelse(seq_len(nrow(pd)) %in% start:stop, indent_by, 0)) %>%
19+
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop, indent_by, 0)) %>%
2120
select_(~indent, ~newlines, ~everything())
2221
pd
2322
}
2423

2524

26-
27-
#' Update indention information of nested parse data
28-
#'
29-
#' These functions apply the update functions of the same name but without
30-
#' suffix nested to each level of nesting of the nested parse table.
31-
#' @param pd A nested parse table that is already enhanced with
32-
#' line break and space information via [create_filler_nested].
33-
#' @name update_indention_nested
34-
NULL
35-
36-
#' @rdname update_indention_nested
37-
indent_round_nested <- function(pd) {
38-
if (is.null(pd)) return(pd)
25+
#' @rdname update_indention
26+
indent_curly <- function(pd, indent_by) {
27+
opening <- which(pd$token == "'{'")
28+
if (length(opening) > 0) {
29+
start <- opening + 1
30+
stop <- nrow(pd) - 1
31+
} else {
32+
start <- stop <- 0
33+
}
3934
pd <- pd %>%
40-
indent_round(indent_by = 2) %>%
41-
mutate(child = map(child, indent_round_nested))
35+
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop, indent_by, 0)) %>%
36+
select_(~indent, ~newlines, ~everything())
4237
pd
4338
}
4439

45-
4640
#' Strip EOL spaces
4741
#'
4842
#' Remove end-of-line spaces.
49-
#' @param pd_nested A nested parse table.
43+
#' @param pd_flat A flat parse table.
5044
#' @return A nested parse table.
51-
strip_eol_spaces_nested <- function(pd_nested) {
52-
if (is.null(pd_nested)) return()
53-
pd_nested <- pd_nested %>%
54-
mutate(spaces = spaces * (newlines == 0),
55-
child = map(child, strip_eol_spaces_nested))
56-
pd_nested
45+
strip_eol_spaces <- function(pd_flat) {
46+
pd_flat %>%
47+
mutate(spaces = spaces * (newlines == 0))
5748
}

0 commit comments

Comments
 (0)