Skip to content

Commit

Permalink
Merge branch 'main' into issue-1136
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Oct 19, 2024
2 parents 8b2b788 + faa0558 commit 0c47f71
Show file tree
Hide file tree
Showing 41 changed files with 165 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.6.1
uses: JamesIves/github-pages-deploy-action@v4.6.8
with:
clean: false
branch: gh-pages
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5.1.0
uses: actions/setup-python@v5.2.0
with:
python-version: "3.9"
architecture: "x64"
Expand Down
1 change: 1 addition & 0 deletions .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ linters: all_linters(
todo_comment_linter = NULL,
nonportable_path_linter = NULL,
string_boundary_linter = NULL,
strings_as_factors_linter = NULL, # since we use R >= 4.0
undesirable_function_linter = NULL,
undesirable_operator_linter = NULL
)
Expand Down
5 changes: 2 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ repos:
)$
- id: roxygenize
additional_dependencies:
- r-lib/pkgapi
- dplyr
- roxygen2
- rlang
Expand Down Expand Up @@ -102,7 +101,7 @@ repos:
)$
- id: pkgdown
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-added-large-files
args: ["--maxkb=200"]
Expand All @@ -120,7 +119,7 @@ repos:
tests/testthat/_snaps/.*|
)$
- repo: https://github.com/lorenzwalthert/gitignore-tidy
rev: 517cddbf1d8514ddaf43159686617ae65895dc99
rev: 0.1.2
hooks:
- id: tidy-gitignore
- repo: local
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ The source code is organized as follows:
| token-create.R | Utilities for creating tokens, mostly to insert braces around mutli-line if statements. |
| token-define.R | Defines which tokens belong to which group. |
| transform-code.R, transform-files.R | Transformation of code for APIs that manipulate files (e.g. `style_file()`). |
| ui.R | User interaces. Top-level functions for styling. |
| ui.R | User interfaces. Top-level functions for styling. |
| unindent.R | Certain tokens cause unindention, e.g. closing braces. |
| utils.R | low-level general purpose utilities. |
| vertical.R | S3 class for pretty printing of styled code. |
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Authors@R:
email = "lorenz.walthert@icloud.com"),
person(given = "Indrajeet",
family = "Patil",
role = "ctb",
role = "aut",
email = "patilindrajeet.science@gmail.com",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")))
Description: Pretty-prints R code without changing the user's formatting
Expand All @@ -27,7 +27,7 @@ Depends:
Imports:
cli (>= 3.1.1),
magrittr (>= 2.0.0),
purrr (>= 0.2.3),
purrr (>= 1.0.2),
R.cache (>= 0.15.0),
rlang (>= 1.0.0),
rprojroot (>= 1.1),
Expand All @@ -52,7 +52,8 @@ Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace", "collate",
if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet" else {
warning("Please install r-lib/pkgapi to make sure the file API is kept
up to date"); NULL}))
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Language: en-US
Config/testthat/edition: 3
Config/testthat/parallel: true
Collate:
Expand Down
12 changes: 0 additions & 12 deletions R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ lead <- function(x, n = 1L, default = NA) {
c(x[-seq_len(n)], rep(default, n))
}


arrange <- function(.data, ...) {
ord <- eval(substitute(order(...)), .data, parent.frame())
vec_slice(.data, ord)
}

arrange_pos_id <- function(data) {
pos_id <- data$pos_id
if (is.unsorted(pos_id)) {
Expand Down Expand Up @@ -51,9 +45,3 @@ left_join <- function(x, y, by) {
last <- function(x) {
x[[length(x)]]
}

map_dfr <- function(.x, .f, ...) {
.f <- purrr::as_mapper(.f, ...)
res <- map(.x, .f, ...)
vec_rbind(!!!res)
}
6 changes: 5 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ transform_utf8_one <- function(path, fun, dry) {
if (inherits(e, "dryError")) {
rlang::abort(conditionMessage(e))
}
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
show_path <- cli::style_hyperlink(
cli::col_blue(basename(path)),
paste0("file://", path)
)
cli::cli_warn("When processing {show_path}:", parent = e)
NA
}
)
Expand Down
35 changes: 18 additions & 17 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
#'
#' styler:::parse_safely("a + 3 -4 -> \n glück + 1")
parse_safely <- function(text, ...) {
tried_parsing <- rlang::try_fetch(
tried_parsing <- withCallingHandlers(
parse(text = text, ...),
error = function(e) e,
warning = function(w) w
)
if (inherits(tried_parsing, "error")) {
if (has_crlf_as_first_line_sep(tried_parsing$message, text)) {
abort(paste0(
"The code to style seems to use Windows style line endings (CRLF). ",
"styler currently only supports Unix style line endings (LF). ",
"Please change the EOL character in your editor to Unix style and try ",
"again.\nThe parsing error was:\n", tried_parsing$message
))
} else {
abort(tried_parsing$message)
error = function(e) {
if (has_crlf_as_first_line_sep(e$message, text)) {
msg <- c(
x = "The code to style seems to use Windows style line endings (CRLF).",
`!` = "styler currently only supports Unix style line endings (LF). ",
i = "Please change the EOL character in your editor to Unix style
and try again."
)
} else {
msg <- c(x = "Styling failed")
}
cli::cli_abort(msg, parent = e, call = NULL)
},
warning = function(w) {
cli::cli_warn(w$message)
w
}
} else if (inherits(tried_parsing, "warning")) {
warn(tried_parsing$message)
}
)
tried_parsing
}

Expand Down
24 changes: 15 additions & 9 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,21 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
}

style_line_break_around_curly <- function(strict, pd) {
if (is_curly_expr(pd) && nrow(pd) > 2L) {
closing_before <- pd$token == "'}'"
opening_before <- (pd$token == "'{'")
to_break <- lag(opening_before, default = FALSE) | closing_before
pd$lag_newlines[to_break] <- ifelse(
pd$token[to_break] == "COMMENT",
pmin(1L, pd$lag_newlines[to_break]),
if (strict) 1L else pmax(1L, pd$lag_newlines[to_break])
)
if (is_curly_expr(pd)) {
n_row <- nrow(pd)
if (n_row > 2L) {
closing_before <- pd$token == "'}'"
opening_before <- (pd$token == "'{'")
to_break <- lag(opening_before, default = FALSE) | closing_before
pd$lag_newlines[to_break] <- ifelse(
pd$token[to_break] == "COMMENT",
pmin(1L, pd$lag_newlines[to_break]),
if (strict) 1L else pmax(1L, pd$lag_newlines[to_break])
)
} else if (n_row == 2L) {
# pd represents {}
pd$lag_newlines[2L] <- 0L
}
} else {
is_else <- pd$token == "ELSE"
if (any(pd$token_before[is_else] == "'}'")) {
Expand Down
7 changes: 6 additions & 1 deletion R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ add_space_after_for_if_while <- function(pd_flat) {

#' @rdname set_line_break_around_curly_curly
#' @keywords internal
set_space_in_curly_curly <- function(pd) {
set_space_in_curly <- function(pd) {
if (is_curly_expr(pd)) {
# curly-curly
after_inner_opening <- pd$token == "'{'" & pd$token_before == "'{'"
before_inner_closing <- lead(pd$token == "'}'" & pd$token_after == "'}'")
is_curly_curly_inner <- any(after_inner_opening, na.rm = TRUE) &&
Expand All @@ -193,6 +194,10 @@ set_space_in_curly_curly <- function(pd) {
pd$spaces[after_outer_opening] <- 0L
pd$spaces[before_outer_closing] <- 0L
}

# empty curly
after_is_empty_curly <- lead(pd$token == "'}'" & pd$token_before == "'{'")
pd$spaces[after_is_empty_curly] <- 0L
}
pd
}
Expand Down
4 changes: 2 additions & 2 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ tidyverse_style <- function(scope = "tokens",
},
set_space_between_levels = set_space_between_levels,
set_space_between_eq_sub_and_comma = set_space_between_eq_sub_and_comma,
set_space_in_curly_curly = set_space_in_curly_curly
set_space_in_curly = set_space_in_curly
)
}

Expand Down Expand Up @@ -209,7 +209,7 @@ tidyverse_style <- function(scope = "tokens",
start_comments_with_space = "COMMENT",
remove_space_after_unary_pm_nested = c("'+'", "'-'"),
spacing_before_comments = "COMMENT",
set_space_in_curly_curly = c("'{'", "'}'")
set_space_in_curly = c("'{'", "'}'")
),
indention = list(
# indent_braces = c("'('", "'['", "'{'", "')'", "']'", "'}'"),
Expand Down
44 changes: 1 addition & 43 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ transform_and_check <- function(in_item, out_item,
unclass()
if (!file.exists(out_item)) {
warn(paste(
"File", out_item, "does not exist. Creating it from transormation."
"File", out_item, "does not exist. Creating it from transformation."
))
file.create(out_item)
}
Expand Down Expand Up @@ -267,48 +267,6 @@ n_times_faster_bench <- function(i, x1, x2, fun, ..., n, clear) {
)
}


#' Generate a comprehensive collection test cases for comment / insertion
#' interaction
#' Test consist of if / if-else / if-else-if-else cases, paired with various
#' line-break and comment configurations. Used for internal testing.
#' @return
#' The function is called for its side effects, i.e. to write the
#' test cases to *-in.R files that can be tested with [test_collection()]. Note
#' that a few of the test cases are invalid and need to be removed / commented
#' out manually.
#' @keywords internal
generate_test_samples <- function() {
gen <- function(x) {
if (length(x) == 0L) {
""
} else {
c(
paste0(x[1L], gen(x[-1L])),
paste0(x[1L], " # comment\n", paste(x[-1L], collapse = ""))
)
}
}

collapse <- function(x) paste(x, collapse = "\n\n")

cat(
collapse(gen(c("if", "(", "TRUE", ")", "NULL"))),
file = "tests/testthat/insertion_comment_interaction/just_if-in.R"
)
cat(
collapse(gen(c("if", "(", "TRUE", ")", "NULL", " else", " NULL"))),
file = "tests/testthat/insertion_comment_interaction/if_else-in.R"
)
cat(
collapse(gen(c(
"if", "(", "TRUE", ")", "NULL", " else", " if", "(", "FALSE", ")", "NULL",
" else", " NULL"
))),
file = "tests/testthat/insertion_comment_interaction/if_else_if_else-in.R"
)
}

#' @include ui-caching.R
clear_testthat_cache <- purrr::partial(cache_clear, "testthat", ask = FALSE)
activate_testthat_cache <- purrr::partial(cache_activate, "testthat")
Expand Down
3 changes: 1 addition & 2 deletions R/token-define.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ token <- rbind.data.frame(
c("if", "cond", "IF"),
c("else", "cond", "ELSE"),
c("in", "loop_cond", "IN"),
c("while", "loop_cond", "WHILE"),
stringsAsFactors = FALSE
c("while", "loop_cond", "WHILE")
)
# styler: on

Expand Down
5 changes: 3 additions & 2 deletions R/transform-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ identify_raw_chunks <- function(lines,

if (filetype == "Rmd") {
starts <- grep(
"^[\t >]*```+\\s*\\{([Rr]( *[ ,].*)?)\\}\\s*$", lines,
perl = TRUE
"^[\t >]*```+\\s*\\{((r|webr-r|webr)( *[ ,].*)?)\\}\\s*$",
lines,
perl = TRUE, ignore.case = TRUE
)
ends <- grep("^[\t >]*```+\\s*$", lines, perl = TRUE)
ends <- purrr::imap_int(starts, ~ ends[which(ends > .x)[1L]]) %>%
Expand Down
3 changes: 1 addition & 2 deletions R/ui-caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ cache_info <- function(cache_name = NULL, format = "both") {
last_modified = suppressWarnings(max(file_info$mtime)),
created = file.info(path_cache)$ctime,
location = path_cache,
activated = cache_is_activated(cache_name),
stringsAsFactors = FALSE
activated = cache_is_activated(cache_name)
)

if (any(c("lucid", "both") == format)) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ is_cached <- function(text,
#' a new cache.
#' * transformers. Cannot easily hash them because two environments won't be
#' identical even if they contain the same objects (see 'Experiments'). Simple
#' `as.character(transformers)` will not consider infinitively recursive
#' `as.character(transformers)` will not consider infinitely recursive
#' code dependencies.
#' To fix this, transformers must have names and version number as described
#' in [create_style_guide()]. Now, the only way to fool the cache invalidation
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ re_match <- function(text, pattern) {
end <- start + length - 1L
matchstr <- substring(text, start, end)
matchstr[start == -1L] <- NA_character_
res <- data.frame(stringsAsFactors = FALSE, .text = text, .match = matchstr)
res <- data.frame(.text = text, .match = matchstr)

gstart <- attr(match, "capture.start")
glength <- attr(match, "capture.length")
gend <- gstart + glength - 1L
groupstr <- substring(text, gstart, gend)
groupstr[gstart == -1L] <- NA_character_
dim(groupstr) <- dim(gstart)
res <- cbind(groupstr, res, stringsAsFactors = FALSE)
res <- cbind(groupstr, res)

names(res) <- c(attr(match, "capture.names"), ".text", ".match")
res
Expand Down
5 changes: 3 additions & 2 deletions R/visit.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) {
groups <- flattened_pd$line1
split_pd <- vec_split(flattened_pd, groups)[[2L]]
flattened_pd <- split_pd %>%
map_dfr(function(.x) {
purrr::map(function(.x) {
.x$col2 <- cumsum(.x$nchar + .x$lag_spaces)
.x
})
}) %>%
purrr::list_rbind()
flattened_pd$col1 <- flattened_pd$col2 - flattened_pd$nchar
flattened_pd
}
Expand Down
6 changes: 3 additions & 3 deletions _pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ reference:
- print.vertical

template:
bootstrap: 5
params:
bootswatch: flatly # https://bootswatch.com/flatly/
docsearch:
api_key: 13580d327d8a7159f83a7cff178d2141
index_name: r-lib_styler

authors:
Kirill Müller:
href: https://krlmlr.info
Lorenz Walthert:
href: https://lorenzwalthert.com
Indrajeet Patil:
href: https://sites.google.com/site/indrajeetspatilmorality/

development:
mode: auto
Expand Down
Loading

0 comments on commit 0c47f71

Please sign in to comment.