Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ CONTRIBUTING.md
^\.gitsum$
^gitsum$

^cran-comments\.md$
7 changes: 2 additions & 5 deletions API
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Exported functions

create_style_guide(initialize = initialize_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
default_style_guide_attributes(pd_flat)
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)
Expand All @@ -12,7 +13,3 @@ style_text(text, ..., style = tidyverse_style, transformers = style(...))
tidyverse_math_token_spacing()
tidyverse_reindention()
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())

## Foreign S3 methods

print.vertical(x, ...)
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: styler
Title: Non-invasive Pretty Printing of R code
Version: 0.0-11
Title: Non-Invasive Pretty Printing of R Code
Version: 1.0.0
Authors@R: c(person("Kirill", "Müller", role = c("aut"), email = "[email protected]"),
person("Lorenz", "Walthert", role = c("cre", "aut"), email = "[email protected]"))
Description:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(print,vertical)
export(create_style_guide)
export(default_style_guide_attributes)
export(specify_math_token_spacing)
export(specify_reindention)
export(style_dir)
Expand Down
258 changes: 71 additions & 187 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,187 +1,71 @@
## styler 0.0-11 (2017-12-05)

- Remove `regex_none()`, document `NULL` default.
- `style_dir()` and `style_pkg()` now also handle `.Rmd` files (#292, @jonmcalder).
- Add test for styling `.Rmd` files.
- Better error message for styling new files via the RStudio addin (#295).
- Spelling via devtools::spell_check() (#294).
- Roundtrip and CLI (#289).


## styler 0.0-10 (2017-11-27)

- Adapt documentation (#290).
- Add roundtrip (#287).
- Fix AppVeyor builds.
- Fix token insertion / comment interaction (#279).
- Clarify labelling strategy (#285).
- Fixing and extending Rstudioaddins (#283).
- Fix eq assign parsing (#276).
- style_files -> vectorized style_file (#273).
- Refactoring (#270).
- Fix CI (#275).
- Fix covr (#274).
- Renaming files (#271).
- Handle styling of an unsaved active file (#243).
- Test R 3.1 and R 3.2 (#249).
- Allow empty {} without line break (#261).
- Wrap expr in expr before enclosing with curly braces (#263).
- Avoid checking for hard-coded dot (#262).
- Account for dependency renaming (utf8 changed to enc) (#264).
- Indention of function declaration and closing braces (#260).
- Only remove line break before closing with strict option (#252).


## styler 0.0-9 (2017-10-23)

- Hotfix: utf8 should not be verbose (#245).
- Allow styling of Rmd files(#233).
- Remove duplicate @family (#244).
- Fixing token insertion (#242).
- Capitalize Addin titles (#241).
- Explicit `NULL` creation to make styler compatible with R3.2.0 (#237).
- Improve vignettes (#232).
- Allow exclusion of files with `style_pkg()` and `style_dir()`.
- Correct styling with long strings (#230).
- Add tools for re-indenting tokens (#217).
- Math token spacing (#221).
- Remove outdated line and col information (#218).
- Empty input for styling does not cause an error (#227, #228).
- Tools to insert tokens + application on `if`-`else` clauses (#212).
- Improve example in documentation (#222).
- Fix spacing around in (#214).
- Maintenance: renaming functions / files, extend helper, documentation, if_else etc. (#204).
- Disallow line break after ( for function calls (#210).
- Preserve space between `!` and bang (#209).
- Simplify RStudio Addin (#208, #211).
- Indention based on square brackets (#207).
- Add vignette on introducing styler (#203).
- Indent function declaration without curly braces correctly (#202).
- Fix indention in if-else statement (#200).
- Sorting key (#196).
- Use safe sequences (#197).
- Fix space between two commas (#195).
- Keep single-line pipes on one line (#74).
- Remove tidyr and dplyr dependency (#182, #183, @jimhester).
- Fix parsing inconsistency (#188).
- Substitute create filler (#190).
- Introducing class vertical (#189).
- Adapt line break rules (#172).
- Fix `R CMD check` (#185).
- Force argument evaluation for proper error handling (#179).
- Add nonstrict version of set_space_before_comment (#174).
- Add installation instructions to README (#175).
- Addin to style highlighted region (#143).
- Improve spelling (#168).
- Add coverage badge
- Change badge from WIP to active
- Add the number of files to message (#166).
- Improve documentation (#164).
- Add informative messages while styling files (#165).
- More examples in help file (#161).
- No line breaks after pipe if comment is next token (#160).
- Fixing spacing around `!` (non-bang-bang) (#157).
- Finalize function documentation (#154).
- Review vignette (#158).
- Update bang-bang rule (#156).


## styler 0.0-8 (2017-08-24)

- Vignette on customizing styler (#145).
- No line break after `switch()` and friends (#152).
- Remove flat relicts completely (#151).
- Don't reindent function calls and break line correctly for multi-line calls (#149).
- Set space between "=" and "," (#150).
- Make R CMD Check perfect (#148).
- Adding tests for exception handling with invalid parse data (#139).
- Fix indention by checking for all potential triggers (#142).
- Fix un-indention (#135).
- Support wide characters (#130).
- No spaces around :, :: and :::.
- Redesigning the API (#123).
- Solve eq_sub indention in general (#125).
- Minor refactorings.
- Re-indent token-dependent (#119).
- Supporting more indention patterns.
- Allow raw indention.
- Definitively fixing eol issue with comments.
- Infrastructure.
- Flattening out the parse table.
- New rule: no space after ! -> !!! for tidyeval.
- Fix spacing around '{'.
- Don't drop tokens! Fixes #101.
- EOL spaces in empty comments (and in general) (#98).
- mal-indention in conditional statement due to wrong specification of indent_without_paren) (#95).
- Complicated indentions based on arithmetic and special operators (#96).
- indention interaction on with assignment operator and other operators (#97).


## styler 0.0-7 (2017-07-27)

- curly braces don't move a line up if previous token is comment.
- wrap parse + transform + serialize in new function.
- Speedup of nested styler functions by ~ 4x.
- Use multi line instead of line1 / line2 for indention and unindention.
- Indent multiple.
- add tidyverse rules (indention not yet working properly).
- Refactoring and add token before and afterwards to parse table.
- Implement post visitor instead of complicated move_up child.


## styler 0.0-6 (2017-07-10)

* Tidy up README (#43).
* Fix indention of long operator chains (#69).
* Properly format unary operators (#38).
* Fixing indent multiple - one more time (#68).
* Remove spaces before comma (#62).
* Fixing indention with multiple parentheses (#57).
* data.tree gains unique node IDs for proper printing.
* Add `style_file()` function to style a single .R file.
* Add RStudio add-in to style active .R file.
* hotfix: make `style_pkg()` and `style_src()` work by passing flat argument.


## styler 0.0-5 (2017-06-30)

* Correctly deal with comments (spacing before comments, start comment with space)
* T more flexibly (`test_collection()` and friends now support `...`)
* Indention based on curly brackets
* Spacing across different levels of nesting (e.g. a space after `)` in `function(x) {...}`)
* Write tree structure to file via test_collection()` for easy understanding of the nested structure
* Outsource tokenise
* Account for situations where code does not start on line1
* Correctly style comments
* Add style_empty for tailored testing
* Initialize indent in create filler
* Adapt vignette and documentation to visitor concept
* Refine testing to use visiting approach
* Parse multiple expressions: Make nested approach wok on multiple expressions too.
* Internal: introducing the visitor concept instead of "looping" many times through whole nested structure.
* Integrate nested approach in top-level APIs `style_text` and friends via an additional argument. `flat`.
* Add tools for scalable testing. Transform *-in.R with a transformer function and check whether result corresponds to *-out.R


## styler 0.0-4 (2017-06-15)

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


## styler 0.0-3 (2017-06-15)

- Technical release for creation of pkgdown documentation.


## styler 0.0-2 (2017-06-15)

- Technical release for creation of pkgdown documentation.


## styler 0.0-1 (2017-06-15)

Initial release, work in progress.

- Create and serialize nested parse data.
- Internal support for indention of expressions with parentheses.
- Adding and removing spaces around operators on flat parse data.
## styler 1.0.0 (2017-12-05)

Initial release.

### stylers
These are functions used to style code. They style a directory, a whole package,
a file or a string.
```
style_dir(path = ".",
..., style = tidyverse_style, transformers = style(...),
filetype = "R", recursive = TRUE, exclude_files = NULL
)

style_pkg(pkg = ".",
..., style = tidyverse_style, transformers = style(...), filetype = "R",
exclude_files = "R/RcppExports.R"
)


style_file(path,
..., style = tidyverse_style, transformers = style(...)
)

style_text(text, ..., style = tidyverse_style, transformers = style(...))
```

### style guides
These functions are the style guides implemented.
```
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()
)
tidyverse_reindention()
tidyverse_math_token_spacing())
```

### style guide creators
This function is used to create a style guide.
```
create_style_guide(
initialize = default_style_guide_attributes,
line_break = NULL,
space = NULL,
token = NULL,
indention = NULL,
use_raw_indention = FALSE,
reindention = tidyverse_reindention()
)
```

### Helpers
These are helper functions used to specify the style guides in use.

```
specify_math_token_spacing(
zero = NULL,
one = c("'+'", "'-'", "'*'", "'/'", "'^'")
)

specify_reindention(
regex_pattern = NULL,
indention = 0,
comments_only = TRUE
)
initialize_default_attributes(pd_flat)
```
15 changes: 8 additions & 7 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ try_transform_as_r_file <- function(context, transformer) {
tryCatch(
transformer(context$contents),
error = function(e) stop(
paste(
"Styling of unsaved files is only supported for R files with valid code.",
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
"can be parsed. Then, try to style again.",
"The error was \n", e
), call. = FALSE
))
paste(
"Styling of unsaved files is only supported for R files with valid code.",
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
"can be parsed. Then, try to style again.",
"The error was \n", e
), call. = FALSE
)
)
}

#' @describeIn styler_addins Styles the highlighted selection in a `.R` or
Expand Down
1 change: 0 additions & 1 deletion R/communicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ communicate_summary <- function(changed, ruler_width) {
cli::cat_bullet("\t", sum(changed, na.rm = TRUE), "\tFile changed.", bullet = "info")
cli::cat_bullet(bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an eror.")
cli::cat_rule(width = max(40, ruler_width))

}
10 changes: 6 additions & 4 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ arrange <- function(.data, ...) {
stopifnot(is.data.frame(.data))
ord <- eval(substitute(order(...)), .data, parent.frame())
if (length(ord) != nrow(.data)) {
stop("Length of ordering vectors don't match data frame size",
call. = FALSE)
stop(
"Length of ordering vectors don't match data frame size",
call. = FALSE
)
}
.data[ord, , drop = FALSE]
}
Expand Down Expand Up @@ -83,7 +85,7 @@ left_join <- function(x, y, by, ...) {
res
}

nth <- function (x, n, order_by = NULL, default = x[NA_real_]) {
nth <- function(x, n, order_by = NULL, default = x[NA_real_]) {
stopifnot(length(n) == 1, is.numeric(n))
n <- trunc(n)
if (n == 0 || n > length(x) || n < -length(x)) {
Expand All @@ -101,7 +103,7 @@ nth <- function (x, n, order_by = NULL, default = x[NA_real_]) {
}


last <- function (x, order_by = NULL, default = x[NA_real_]) {
last <- function(x, order_by = NULL, default = x[NA_real_]) {
nth(x, -1L, order_by = order_by, default = default)
}

Expand Down
2 changes: 1 addition & 1 deletion R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contains_else_expr_that_needs_braces <- function(pd) {
non_comment_after_else <- next_non_comment(pd, else_idx)
sub_expr <- pd$child[[non_comment_after_else]]
# needs braces if NOT if_condition, NOT curly expr
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
} else {
FALSE
}
Expand Down
Loading