Skip to content

Commit 85a0ab7

Browse files
committed
Merge branch 'r-0.0-6' into production
2 parents 77499f0 + fd02dde commit 85a0ab7

File tree

130 files changed

+3030
-1117
lines changed

Some content is hidden

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

130 files changed

+3030
-1117
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
^.travis.yml
77
^appveyor\.yml$
88
^tic\.R$
9+
^\.travis\.yml$

.travis.yml

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r
2+
# Default configuration for use with tic package
3+
# Usually you shouldn't need to change the first part of the file
4+
5+
# DO NOT CHANGE THE CODE BELOW
6+
before_install: R -q -e 'install.packages("remotes"); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages(); tic::before_install()'
7+
install: R -q -e 'tic::install()'
8+
after_install: R -q -e 'tic::after_install()'
9+
before_script: R -q -e 'tic::before_script()'
10+
script: R -q -e 'tic::script()'
11+
after_success: R -q -e 'tic::after_success()'
12+
after_failure: R -q -e 'tic::after_failure()'
13+
before_deploy: R -q -e 'tic::before_deploy()'
14+
deploy:
15+
provider: script
16+
script: R -q -e 'tic::deploy()'
17+
on:
18+
all_branches: true
19+
after_deploy: R -q -e 'tic::after_deploy()'
20+
after_script: R -q -e 'tic::after_script()'
21+
# DO NOT CHANGE THE CODE ABOVE
22+
23+
# Custom parts:
224

325
# Header
426
language: r
@@ -13,57 +35,5 @@ env:
1335
- _R_CHECK_FORCE_SUGGESTS_=false
1436
- MAKEFLAGS="-j 2"
1537

16-
#addons
17-
addons:
18-
apt:
19-
packages:
20-
- libmagick++-dev
21-
- libssh2-1-dev
22-
23-
#matrix: 3x Linux, 1x OS X
24-
matrix:
25-
include:
26-
- r: release
27-
env:
28-
- BUILD_PKGDOWN=true
29-
- r: oldrel
30-
- r: devel
31-
- os: osx
32-
osx_image: xcode7.3
33-
r: release
34-
latex: false
35-
36-
#notifications
37-
notifications:
38-
email:
39-
on_success: change
40-
on_failure: change
41-
42-
#before_script
43-
before_script:
44-
- R -q -e 'install.packages("remotes"); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages()'
45-
46-
#after_success
47-
after_success:
48-
- R -q -e 'tic::after_success()'
49-
50-
#deploy // https://github.com/travis-ci/travis-build/blob/85ef7108/lib/travis/build/addons/deploy/script.rb#L100-L107
51-
deploy:
52-
provider: script
53-
script: R -q -e 'tic::deploy()'
54-
on:
55-
all_branches: true
56-
57-
# Custom parts:
58-
59-
#r_github_packages
60-
r_github_packages:
61-
62-
#r_packages
63-
r_packages:
64-
6538
#services
6639
services:
67-
68-
#before_install
69-
before_install:

API

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

33
## Exported functions
44

5-
get_transformers(flat, ...)
5+
get_transformers(flat = FALSE, ...)
66
get_transformers_flat(strict = TRUE, start_comments_with_one_space = FALSE)
77
get_transformers_nested(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))
810
style_pkg(pkg = ".", flat = FALSE, transformers = get_transformers(flat = flat))
9-
style_src(path = ".", flat = FALSE, recursive = TRUE, transformers = get_transformers(flat = flat))
1011
style_text(text, flat = FALSE, transformers = get_transformers(flat = flat))

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: styler
22
Title: Non-invasive Pretty Printing of R code
3-
Version: 0.0-5
3+
Version: 0.0-6
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.
@@ -20,14 +20,16 @@ Suggests:
2020
here,
2121
knitr,
2222
rmarkdown,
23-
testthat
23+
rstudioapi,
24+
testthat,
25+
pkgload
2426
Remotes:
2527
r-lib/pkgload,
2628
krlmlr/utf8
2729
License: GPL-3
2830
Encoding: UTF-8
2931
LazyData: true
30-
Date: 2017-06-30
32+
Date: 2017-07-10
3133
BugReports: https://github.com/krlmlr/styler/issues
3234
URL: https://github.com/krlmlr/styler, http://krlmlr.github.io/styler
3335
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "pkgapi::api_roclet"))

NAMESPACE

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
export(get_transformers)
44
export(get_transformers_flat)
55
export(get_transformers_nested)
6+
export(style_dir)
7+
export(style_file)
68
export(style_pkg)
7-
export(style_src)
89
export(style_text)
910
import(dplyr)
1011
import(tibble)
1112
import(tidyr)
1213
importFrom(purrr,flatten_chr)
1314
importFrom(purrr,map)
1415
importFrom(purrr,map2)
16+
importFrom(purrr,map_lgl)
1517
importFrom(purrr,partial)
1618
importFrom(purrr,pmap)
1719
importFrom(purrr,pwalk)
1820
importFrom(purrr,reduce)
21+
importFrom(purrr,when)
1922
importFrom(utils,write.table)

NEWS.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
## styler 0.0-6 (2017-07-10)
2+
3+
* Tidy up README (#43).
4+
* Fix indention of long operator chains (#69).
5+
* Properly format unary operators (#38).
6+
* Fixing indent multiple - one more time (#68).
7+
* Remove spaces before comma (#62).
8+
* Fixing indention with multiple parentheses (#57).
9+
* data.tree gains unique node IDs for proper printing.
10+
* Add `style_file()` function to style a single .R file.
11+
* Add RStudio add-in to style active .R file.
12+
* hotfix: make `style_pkg()` and `style_src()` work by passing flat argument.
13+
14+
115
## styler 0.0-5 (2017-06-30)
216

317
* Correctly deal with comments (spacing before comments, start comment with space)
418
* T more flexibly (`test_collection()` and friends now support `...`)
5-
* Indention based on curly brackets
19+
* Indention based on curly brackets
620
* Spacing across different levels of nesting (e.g. a space after `)` in `function(x) {...}`)
721
* Write tree structure to file via test_collection()` for easy understanding of the nested structure
8-
* Outsource tokenise
22+
* Outsource tokenise
923
* Account for situations where code does not start on line1
10-
* Correctly style comments
11-
* Add style_empty for tailored testing
24+
* Correctly style comments
25+
* Add style_empty for tailored testing
1226
* Initialize indent in create filler
1327
* Adapt vignette and documentation to visitor concept
1428
* Refine testing to use visiting approach

R/get_transformers.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' * [get_transformers_flat()] if `flat = TRUE` or
88
#' * [get_transformers_nested()] if `flat = FALSE`.
99
#' @export
10-
get_transformers <- function(flat, ...) {
10+
get_transformers <- function(flat = FALSE, ...) {
1111
if (flat) {
1212
get_transformers_flat(...)
1313
} else {
@@ -35,6 +35,7 @@ get_transformers_flat <- function(strict = TRUE,
3535
add_space_before_brace,
3636
if (strict) set_space_around_op else add_space_around_op,
3737
if (strict) set_space_after_comma else add_space_after_comma,
38+
remove_space_before_comma,
3839
remove_space_after_unary_pm,
3940
remove_space_after_opening_paren,
4041
partial(start_comments_with_space,
@@ -58,8 +59,10 @@ get_transformers_nested <- function(strict = TRUE,
5859
c(create_filler,
5960
partial(indent_round, indent_by = indent_by),
6061
partial(indent_curly, indent_by = indent_by),
62+
partial(indent_op, indent_by = indent_by),
6163
strip_eol_spaces,
6264
get_transformers_flat(strict, start_comments_with_one_space),
65+
remove_space_after_unary_pm_nested,
6366
set_space_before_comments,
6467
set_space_between_levels
6568
)

R/modify_pd.R

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,108 @@
33
#' @param pd A nested or flat parse table that is already enhanced with
44
#' line break and space information via [create_filler()].
55
#' @param indent_by How many spaces should be added after the token of interest.
6+
#' @param token The token the indention should be based on.
67
#' @name update_indention
78
NULL
89

910
#' @rdname update_indention
1011
indent_round <- function(pd, indent_by) {
11-
opening <- which(pd$token == "'('")
12-
if (length(opening) > 0) {
12+
indention_needed <- needs_indention(pd, token = "'('")
13+
if (indention_needed) {
14+
opening <- which(pd$token == "'('")
1315
start <- opening + 1
1416
stop <- nrow(pd) - 1
15-
} else {
16-
start <- stop <- 0
17+
pd <- pd %>%
18+
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop,
19+
indent_by, 0))
20+
}
21+
pd %>%
22+
set_unindention_child(token = "')'", unindent_by = indent_by) %>%
23+
select_(~indent, ~newlines, ~everything())
24+
25+
}
26+
27+
#' @rdname update_indention
28+
indent_curly <- function(pd, indent_by) {
29+
indention_needed <- needs_indention(pd, token = "'{'")
30+
if (indention_needed) {
31+
opening <- which(pd$token == "'{'")
32+
start <- opening + 1
33+
stop <- nrow(pd) - 1
34+
pd <- pd %>%
35+
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop,
36+
indent_by, 0))
1737
}
1838
pd <- pd %>%
19-
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop, indent_by, 0)) %>%
39+
set_unindention_child(token = "'}'", unindent_by = indent_by) %>%
2040
select_(~indent, ~newlines, ~everything())
2141
pd
2242
}
2343

2444

45+
#' Check whether indention is needed
46+
#'
47+
#' @param pd A parse table.
48+
#' @param token Which token the check should be based on.
49+
#' @return returns `TRUE` if indention is needed, `FALSE` otherwise. Indention
50+
#' is needed:
51+
#' * if `token` occurs in `pd`.
52+
#' * if there is no child that starts on the same line as `token` and
53+
#' "opens" indention without closing it on this line.
54+
#' @return `TRUE` if indention is needed, `FALSE` otherwise.
55+
needs_indention <- function(pd, token = "'('") {
56+
opening <- which(pd$token %in% token)
57+
length(opening) > 0 && !child_indents(pd, opening, c("'('", "'{'"))
58+
}
59+
60+
#' Check whether a child will indent
61+
#'
62+
#' @param pd A parse table.
63+
#' @param opening The row number of the opening token in the parse table.
64+
#' @return Returns `TRUE` if `pd` has at least one child that indents starting
65+
#' on the same line as the opening token, `FALSE` otherwise.
66+
#' @param token On which token the indention check should be based on.
67+
#' @importFrom purrr map_lgl
68+
child_indents <- function(pd, opening, token) {
69+
if (is.null(pd$child)) return(FALSE)
70+
opening_line <- pd$line1[opening]
71+
pd <- pd %>%
72+
filter(!terminal, line1 == opening_line, line2 != opening_line)
73+
if (nrow(pd) == 0) return(FALSE)
74+
children_indent <- map_lgl(pd$child, pd_has_token, token)
75+
any(children_indent)
76+
}
77+
78+
#' Check whether a parse table contains a token
79+
#'
80+
#' Checks whether a token is in a parse table. `pd_has_token_not_one` is usesful
81+
#' in the situation where one wants to check whether the *operator* + or - is
82+
#' in the parse table, but not the *sign* + or -.
83+
#' We use the fact that signs always appear as first tokens in the parse
84+
#' table. There are two functions and not just one for performance reasons
85+
#' only, as this function is rather low level and gets called very often.
86+
#' @param pd A parse table.
87+
#' @param token The token for which it should be checked whether it is in the
88+
#' parse table.
89+
#' @return `TRUE` if the token is in the parse table, `FALSE` otherwise.
90+
pd_has_token <- function(pd, token) {
91+
has_indention_token <- token %in% pd$token
92+
any(has_indention_token)
93+
}
94+
95+
#' @rdname pd_has_token
96+
pd_has_token_not_first <- function(pd, token) {
97+
has_indention_token <- token %in% pd$token[-1]
98+
any(has_indention_token)
99+
}
100+
101+
25102
#' @rdname update_indention
26-
indent_curly <- function(pd, indent_by) {
27-
opening <- which(pd$token == "'{'")
103+
indent_op <- function(pd, indent_by, token = c(math_token, "SPECIAL")) {
104+
opening <- which(pd$token %in% token)
28105
if (length(opening) > 0) {
29-
start <- opening + 1
30-
stop <- nrow(pd) - 1
106+
start <- opening[1] + 1
107+
stop <- nrow(pd)
31108
} else {
32109
start <- stop <- 0
33110
}
@@ -37,6 +114,7 @@ indent_curly <- function(pd, indent_by) {
37114
pd
38115
}
39116

117+
40118
#' Strip EOL spaces
41119
#'
42120
#' Remove end-of-line spaces.

R/nested_to_tree.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
#'
33
#' Create a tree representation from a text.
44
#' @param text A character vector.
5+
#' @param re_nest Whether or not the parse table should be renested with
6+
#' [re_nest()].
57
#' @return A data frame.
6-
create_tree <- function(text) {
8+
#' @importFrom purrr when
9+
create_tree <- function(text, re_nest = FALSE) {
710
compute_parse_data_nested(text) %>%
8-
visit(c(styler:::create_filler)) %>%
11+
visit(c(create_filler)) %>%
12+
when(re_nest ~ re_nest(.), ~.) %>%
913
create_node_from_nested_root() %>%
1014
as.data.frame()
1115
}
@@ -23,7 +27,7 @@ create_tree <- function(text) {
2327
#' styler:::visit(c(styler:::create_filler)) %>%
2428
#' styler:::create_node_from_nested_root()
2529
create_node_from_nested_root <- function(pd_nested) {
26-
n <- data.tree::Node$new("ROOT (token: short_text [newlines/spaces])")
30+
n <- data.tree::Node$new("ROOT (token: short_text [newlines/spaces] {id})")
2731
create_node_from_nested(pd_nested, n)
2832
n
2933
}
@@ -39,7 +43,7 @@ create_node_from_nested <- function(pd_nested, parent) {
3943

4044
node_info <-
4145
pd_nested %>%
42-
transmute(formatted = paste0(token, ": ", short, " [", newlines, "/", spaces, "]")) %>%
46+
transmute(formatted = paste0(token, ": ", short, " [", newlines, "/", spaces, "] {", id, "}")) %>%
4347
.[["formatted"]]
4448

4549
child_nodes <-

R/parsed.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,14 @@ create_filler <- function(pd_flat) {
113113
newlines = ~line3 - line2,
114114
lag_newlines = ~lag(newlines, default = 0),
115115
col2_nl = ~if_else(newlines > 0L, 0L, col2),
116-
spaces = ~col3 - col2_nl - 1L,
117-
indent = 0
116+
spaces = ~col3 - col2_nl - 1L
118117
) %>%
119118
select_(~-line3, ~-col3, ~-col2_nl)
120119

120+
if (!("indent" %in% names(ret))) {
121+
ret$indent <- 0
122+
}
123+
121124
if (any(ret$spaces < 0L)) {
122125
stop("Invalid parse data")
123126
}

0 commit comments

Comments
 (0)