Skip to content

Add yihui_style() #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 API
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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)
equals_style()
specify_math_token_spacing(zero = "'^'", 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, include_roxygen_examples = TRUE)
Expand Down
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,16 @@ indicates for every row in a parse table whether it contains an `else` token.

The use of closures is discouraged. We prefer to prefill a template function
with `purrr::partial()`.

## Testing

We have a testing framework powered by `test_collection()`.
Essentially, there is an \*-in.R file and a \*-out.R file. The \*-in.R file is the
input that is transformed and - if it matches the *-out.R file, the test has
passed. You can create an \*-in.R file, run `devtools::test(f = "[your file]")`
and an \*-out.R file is generated. If the file matches your expectation,
you can commit it. Note that files are overwritten and version control should be
used to track failed tests.
The files are placed in `tests/testthat` under the category they fit.
Please have a look at the documentation for `test_collection()` and see other
unit tests. Let me know if there is anything unclear about this.
18 changes: 9 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ Authors@R:
family = "Walthert",
role = c("cre", "aut"),
email = "[email protected]"))
Description: Pretty-prints R code without changing the
user's formatting intent.
Description: Pretty-prints R code without changing the user's
formatting intent.
License: GPL-3
URL: https://github.com/r-lib/styler
BugReports: https://github.com/r-lib/styler/issues
Imports:
backports,
backports (>= 1.1.0),
cli,
magrittr,
fs,
magrittr (>= 1.0.1),
purrr (>= 0.2.3),
rematch2,
rlang (>= 0.1.1),
rprojroot,
rprojroot (>= 1.1),
tibble (>= 1.4.2),
tools,
withr,
xfun
Suggests:
data.tree,
data.tree (>= 0.1.6),
dplyr,
here,
knitr,
prettycode,
rmarkdown,
rstudioapi,
rstudioapi (>= 0.7),
testthat
VignetteBuilder:
knitr
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(print,vertical)
export(create_style_guide)
export(default_style_guide_attributes)
export(equals_style)
export(specify_math_token_spacing)
export(specify_reindention)
export(style_dir)
Expand Down
11 changes: 6 additions & 5 deletions R/communicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ communicate_summary <- function(changed, ruler_width) {
cli::cat_rule(width = max(40, ruler_width))
}

stop_insufficient_r_version <- function() {
stop(paste0(
"Can't write tree with R version ", getRversion(),
"since data.tree not available. Needs at least R version 3.2."
), call. = FALSE)
#' @importFrom rlang is_installed
assert_data.tree_installation <- function() {
if (!is_installed("data.tree")) {
stop("The package data.tree needs to be installed for this functionality.")
}
}

2 changes: 1 addition & 1 deletion R/environments.R
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' * version 1: Before fix mentioned in #419.
#' * version 2: After #419.
#'
#'The following utilities are available:
#' The following utilities are available:
#'
#' * `parser_version_set()` sets the parser version in the environment
#' `env_current`.
Expand Down
2 changes: 1 addition & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ transform_utf8_one <- function(path, fun, write_back = write_back) {
xfun::write_utf8(new, path)
}
!identical(unclass(old), unclass(new))
}, error = function(e) {
}, error = function(e) {
warning("When processing ", path, ": ", conditionMessage(e), call. = FALSE)
NA
})
Expand Down
6 changes: 3 additions & 3 deletions R/nested-to-tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ create_tree <- function(text, structure_only = FALSE) {

create_tree_from_pd_with_default_style_attributes <- function(pd, structure_only = FALSE) {
pd %>%
create_node_from_nested_root(structure_only) %>%
create_node_from_nested_root(structure_only) %>%
as.data.frame()
}

Expand All @@ -29,15 +29,15 @@ create_tree_from_pd_with_default_style_attributes <- function(pd, structure_only
#' to check whether two structures are identical.
#' @return An object of class "Node" and "R6".
#' @examples
#' if (getRversion() >= 3.2) {
#' if (rlang::is_installed("data.tree")) {
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
#' nested_pd <- styler:::compute_parse_data_nested(code)
#' initialized <- styler:::pre_visit(nested_pd, c(default_style_guide_attributes))
#' styler:::create_node_from_nested_root(initialized, structure_only = FALSE)
#' }
#' @keywords internal
create_node_from_nested_root <- function(pd_nested, structure_only) {
if (getRversion() < 3.2) stop_insufficient_r_version()
assert_data.tree_installation()
n <- data.tree::Node$new(ifelse(
structure_only, "Hierarchical structure",
"ROOT (token: short_text [lag_newlines/spaces] {pos_id})"
Expand Down
2 changes: 1 addition & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ ensure_valid_pd <- function(pd) {
non_terminals <- pd %>%
filter(terminal == FALSE)
valid_pd <- non_terminals$id %>%
map_lgl(~.x %in% pd$parent) %>%
map_lgl(~ .x %in% pd$parent) %>%
all()
if (!valid_pd) {
stop(paste(
Expand Down
2 changes: 1 addition & 1 deletion R/roxygen-examples-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ remove_roxygen_header <- function(text) {

#' @importFrom purrr map_chr
add_roxygen_mask <- function(text) {
c(paste0("#' @examples"), map_chr(text, ~paste0("#' ", .x)))
c(paste0("#' @examples"), map_chr(text, ~ paste0("#' ", .x)))
}
2 changes: 1 addition & 1 deletion R/rules-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ wrap_if_else_multi_line_in_curly <- function(pd, indent_by = 2) {
pd <- pd %>%
wrap_if_multiline_curly(indent_by,
space_after = ifelse(contains_else_expr(pd), 1, 0)
) %>%
) %>%
wrap_else_multiline_curly(indent_by, space_after = 0)
}
pd
Expand Down
7 changes: 3 additions & 4 deletions R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
#' @param write_tree Whether or not to write tree.
#' @keywords internal
set_arg_write_tree <- function(write_tree) {
sufficient_version <- getRversion() >= 3.2
if (is.na(write_tree)) {
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
} else if (!sufficient_version && write_tree) {
stop_insufficient_r_version()
write_tree <- ifelse(is_installed("data.tree"), TRUE, FALSE)
} else if (write_tree) {
assert_data.tree_installation()
}
write_tree
}
Expand Down
30 changes: 30 additions & 0 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,33 @@ tidyverse_math_token_spacing <- function() {
one = c("'+'", "'-'", "'*'", "'/'")
)
}


#' Equals assignment style
#'
#' Use equals assignment instead of arrow assignment.
#'
#' @inheritParams tidyverse_style

#' @details
#'
#' This style guide is the same as [`tidyverse_style()`], except it uses
#' equals (`=`) rather than arrow `<-` assignment.
#'
#' @family style_guides
#' @examples
#' style_text("x <- 1", style = equals_style)
#' @export
equals_style <- function() {
create_style_guide(
token = list(force_assignment_op_equals),
use_raw_indention = TRUE
)
}

force_assignment_op_equals <- function(pd) {
to_replace <- pd$token == "LEFT_ASSIGN"
pd$token[to_replace] <- "EQ_ASSIGN"
pd$text[to_replace] <- "="
pd
}
6 changes: 3 additions & 3 deletions R/transform-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ separate_chunks <- function(lines, filetype) {
r_raw_chunks <- identify_raw_chunks(lines, filetype = filetype)

r_chunks <- map2(
r_raw_chunks$starts, r_raw_chunks$ends, ~lines[seq2(.x + 1, .y - 1)]
r_raw_chunks$starts, r_raw_chunks$ends, ~ lines[seq2(.x + 1, .y - 1)]
)

text_chunks <- map2(
c(1, r_raw_chunks$ends), c(r_raw_chunks$starts, length(lines)),
~lines[seq2(.x, .y)]
~ lines[seq2(.x, .y)]
)
lst(r_chunks, text_chunks)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ get_engine_pattern <- function() {
#' @inheritParams separate_chunks
#' @keywords internal
get_knitr_pattern <- function(filetype) {
if(filetype == "Rnw") {
if (filetype == "Rnw") {
knitr::all_patterns[["rnw"]]
} else if (filetype == "Rmd") {
knitr::all_patterns[["md"]]
Expand Down
2 changes: 1 addition & 1 deletion R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ NULL
#' @family stylers
#' @examples
#' \dontrun{
#'
#'
#' style_pkg(style = tidyverse_style, strict = TRUE)
#' style_pkg(
#' scope = "line_breaks",
Expand Down
15 changes: 7 additions & 8 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.onLoad <- function(libname, pkgname) {
backports::import(pkgname, "trimws")
op <- options()
op.styler <- list(
styler.colored_print.vertical = TRUE
)
toset <- !(names(op.styler) %in% names(op))
if(any(toset)) options(op.styler[toset])
invisible()
op <- options()
op.styler <- list(
styler.colored_print.vertical = TRUE
)
toset <- !(names(op.styler) %in% names(op))
if (any(toset)) options(op.styler[toset])
invisible()
}

Loading