Skip to content

Commit a92eb07

Browse files
committed
Merge branch 'r-0.0-10' into production
2 parents 7056922 + 1db8542 commit a92eb07

File tree

246 files changed

+9211
-6577
lines changed

Some content is hidden

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

246 files changed

+9211
-6577
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
^\.travis\.yml$
1010
^docs$
1111
^_pkgdown\.yml$
12+
CONTRIBUTING.md

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.RData
44
inst/doc
55
docs
6+
.gitsum
7+
gitsum

.travis.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Usually you shouldn't need to change the first part of the file
44

55
# 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()'
6+
before_install: R -q -e 'install.packages(c("remotes", "curl")); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages(); tic::before_install()'
77
install: R -q -e 'tic::install()'
88
after_install: R -q -e 'tic::after_install()'
99
before_script: R -q -e 'tic::before_script()'
@@ -29,6 +29,16 @@ dist: trusty
2929
cache: packages
3030
latex: false
3131

32+
matrix:
33+
include:
34+
- r: 3.1
35+
- r: 3.2
36+
- r: oldrel
37+
- r: release
38+
env:
39+
- BUILD_PKGDOWN: true
40+
- r: devel
41+
3242
#env
3343
env:
3444
global:

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Issue Management
2+
3+
This project follows the issue management recommendations outlined by [saamwerk](https://github.com/lorenzwalthert/saamwerk/blob/master/issue-management/labelling-strategy.md).
4+
In particular, issues labelled with `Status: Postponed` are closed even if they are not resolved.

DESCRIPTION

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
Package: styler
22
Title: Non-invasive Pretty Printing of R code
3-
Version: 0.0-9
3+
Version: 0.0-10
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]"))
66
Description:
77
Pretty-prints R code without changing the user's formatting intent.
88
Imports:
9+
backports,
10+
enc,
11+
magrittr,
912
purrr,
13+
rematch2,
1014
rlang,
1115
rprojroot,
1216
tibble,
13-
utf8,
14-
withr,
15-
rematch2,
16-
magrittr
17+
withr
1718
Suggests:
1819
data.tree,
1920
dplyr,
@@ -23,14 +24,12 @@ Suggests:
2324
rmarkdown,
2425
rstudioapi,
2526
testthat
26-
Remotes:
27-
krlmlr/utf8
2827
License: GPL-3
2928
Encoding: UTF-8
3029
LazyData: true
31-
Date: 2017-10-23
32-
BugReports: https://github.com/krlmlr/styler/issues
33-
URL: https://github.com/krlmlr/styler, http://krlmlr.github.io/styler
30+
Date: 2017-11-27
31+
BugReports: https://github.com/r-lib/styler/issues
32+
URL: https://github.com/r-lib/styler, https://r-lib.github.io/styler/
3433
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "pkgapi::api_roclet"))
3534
RoxygenNote: 6.0.1
3635
VignetteBuilder: knitr
@@ -39,9 +38,9 @@ Collate:
3938
'compat-tidyr.R'
4039
'dplyr.R'
4140
'expr-is.R'
41+
'indent.R'
4242
'initialize.R'
43-
'modify_pd.R'
44-
'nested.R'
43+
'nest.R'
4544
'nested_to_tree.R'
4645
'parse.R'
4746
'reindent.R'
@@ -54,12 +53,13 @@ Collate:
5453
'serialize.R'
5554
'serialized_tests.R'
5655
'style_guides.R'
57-
'style_rmd.R'
5856
'styler.R'
5957
'token-create.R'
60-
'transform.R'
58+
'transform-code.R'
59+
'transform-files.R'
60+
'ui.R'
6161
'unindent.R'
6262
'utils.R'
6363
'vertical.R'
6464
'visit.R'
65-
'ws.R'
65+
'zzz.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ importFrom(purrr,flatten_int)
2020
importFrom(purrr,map)
2121
importFrom(purrr,map2)
2222
importFrom(purrr,map_chr)
23+
importFrom(purrr,map_dfr)
2324
importFrom(purrr,map_lgl)
2425
importFrom(purrr,partial)
2526
importFrom(purrr,pmap)

NEWS.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
## styler 0.0-10 (2017-11-27)
2+
3+
- Adapt documentation (#290).
4+
- Add roundtrip (#287).
5+
- Fix AppVeyor builds.
6+
- Fix token insertion / comment interaction (#279).
7+
- Clarify labelling strategy (#285).
8+
- Fixing and extending Rstudioaddins (#283).
9+
- Fix eq assign parsing (#276).
10+
- style_files -> vectorized style_file (#273).
11+
- Refactoring (#270).
12+
- Fix CI (#275).
13+
- Fix covr (#274).
14+
- Renaming files (#271).
15+
- Handle styling of an unsaved active file (#243).
16+
- Test R 3.1 and R 3.2 (#249).
17+
- Allow empty {} without line break (#261).
18+
- Wrap expr in expr before enclosing with curly braces (#263).
19+
- Avoid checking for hard-coded dot (#262).
20+
- Account for dependency renaming (utf8 changed to enc) (#264).
21+
- Indention of function declaration and closing braces (#260).
22+
- Only remove line break before closing with strict option (#252).
23+
24+
125
## styler 0.0-9 (2017-10-23)
226

327
- Hotfix: utf8 should not be verbose (#245).

R/addins.R

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,72 @@
1-
#' Style the active file
1+
#' Stylers for RStudio Addins
22
#'
3-
#' Helper function for RStudio Addin.
3+
#' Helper functions for styling via RStudio Addins.
4+
#'
5+
#' @section Auto-Save Option:
6+
#' By default, both of the RStudio Addins will apply styling to the (selected)
7+
#' file contents without saving changes. Automatic saving can be enabled by
8+
#' setting the environment variable `save_after_styling` to `TRUE`.
9+
#'
10+
#' Consider setting this in your `.Rprofile` file if you want to persist
11+
#' this setting across multiple sessions. Untitled files will always need to be
12+
#' saved manually after styling.
13+
#'
14+
#' @name styler_addins
15+
#' @family stylers
16+
#' @seealso [Sys.setenv()]
17+
NULL
18+
19+
#' @describeIn styler_addins Styles the active file with [tidyverse_style()] and
20+
#' `strict = TRUE`.
421
style_active_file <- function() {
22+
transformer <- make_transformer(tidyverse_style())
523
context <- get_rstudio_context()
6-
style_file(context$path, style = tidyverse_style)
24+
if (is_rmd_file(context$path)) {
25+
out <- transform_rmd(context$contents, transformer)
26+
} else if (is_plain_r_file(context$path) | is_unsaved_file(context$path)) {
27+
out <- try_transform_as_r_file(context, transformer)
28+
} else {
29+
stop("Can only style .R and .Rmd files.", call. = FALSE)
30+
}
31+
rstudioapi::modifyRange(
32+
c(1, 1, length(context$contents) + 1, 1),
33+
paste0(out, collapse = "\n"), id = context$id
34+
)
35+
if (Sys.getenv("save_after_styling") == TRUE && context$path != "") {
36+
rstudioapi::documentSave(context$id)
37+
}
738
}
839

9-
10-
#' Style the highlighted region
40+
#' Style a file as if it was an .R file
1141
#'
12-
#' Helper function for RStudio Addin. This function is complicated because of
13-
#' one thing: You can highlight also just parts of lines.
14-
#' @importFrom rlang seq2
15-
style_active_region <- function() {
42+
#' If not successful, the file is most
43+
#' likely not a .R file, so saving the file and try styling again will work if
44+
#' the file is an .Rmd file. Otherwise, we can throw an error that the file must
45+
#' be a .R or .Rmd file.
46+
#' @param context The context from `styler:::get_rstudio_context()`.
47+
#' @param transformer A transformer function most conveniently constructed with
48+
#' [make_transformer()].
49+
try_transform_as_r_file <- function(context, transformer) {
50+
tryCatch(
51+
transformer(context$contents),
52+
error = function(e) stop(
53+
"Cannot style unsaved files other than .R files. Please save the file.", call. = FALSE
54+
))
55+
}
56+
57+
#' @describeIn styler_addins Styles the highlighted selection in a `.R` or
58+
#' `.Rmd` file.
59+
style_selection <- function() {
1660
context <- get_rstudio_context()
1761
text <- context$selection[[1]]$text
1862
if (all(nchar(text) == 0)) stop("No code selected")
1963
out <- style_text(text)
2064
rstudioapi::modifyRange(
2165
context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id
2266
)
67+
if (Sys.getenv("save_after_styling") == TRUE && context$path != "") {
68+
rstudioapi::documentSave(context$id)
69+
}
2370
}
2471

2572
get_rstudio_context <- function() {

R/dplyr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ left_join <- function(x, y, by, ...) {
7373
by_x <- by_y <- by
7474
}
7575
res <- as_tibble(merge(x, y, by.x = by_x, by.y = by_y, all.x = TRUE, ...))
76-
res <- arrange(res, line1, col1, line2, col2, parent)
76+
res <- arrange(res, pos_id)
7777

7878
# dplyr::left_join set unknown list columns to NULL, merge sets them
7979
# to NA

R/expr-is.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,38 @@ is_function_call <- function(pd) {
2222
if (is.na(pd$token_before[2])) return(FALSE)
2323
pd$token_before[2] == "SYMBOL_FUNCTION_CALL"
2424
}
25+
26+
#' @describeIn pd_is Checks whether `pd` is a function declaration.
27+
is_function_dec <- function(pd) {
28+
if (is.null(pd)) return(FALSE)
29+
pd$token[1] == "FUNCTION"
30+
}
31+
32+
33+
contains_else_expr <- function(pd) {
34+
any(pd$token == "ELSE")
35+
}
36+
37+
#' Check whether an else expression needs braces
38+
#'
39+
#' Checks whether an else expression in a nest needs braces. Note that for
40+
#' if-else-if expressions, there is no need to add braces since the if in
41+
#' else-if will be visited separately with the visitor. This applies to all
42+
#' conditional statents with more than one alternative.
43+
#' @param pd A parse table
44+
contains_else_expr_that_needs_braces <- function(pd) {
45+
else_idx <- which(pd$token == "ELSE")
46+
if (length(else_idx) > 0) {
47+
non_comment_after_else <- next_non_comment(pd, else_idx)
48+
sub_expr <- pd$child[[non_comment_after_else]]
49+
# needs braces if NOT if_condition, NOT curly expr
50+
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
51+
} else {
52+
FALSE
53+
}
54+
}
55+
56+
57+
is_cond_expr <- function(pd) {
58+
pd$token[1] == "IF"
59+
}

0 commit comments

Comments
 (0)