Skip to content

Commit

Permalink
Merge branch 'main' into f-759-named-funs
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Sep 19, 2022
2 parents e35bc18 + 08da821 commit d829284
Show file tree
Hide file tree
Showing 53 changed files with 193 additions and 231 deletions.
8 changes: 4 additions & 4 deletions API
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ cache_info(cache_name = NULL, format = "both")
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs_style_guide = NULL, transformers_drop = specify_transformers_drop(), indent_character = " ")
default_style_guide_attributes(pd_flat)
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE)
specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL)
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports.R", "R/cpp11.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports.R", "R/cpp11.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0)
tidyverse_math_token_spacing()
tidyverse_reindention()
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: styler
Title: Non-Invasive Pretty Printing of R Code
Version: 1.7.0.9001
Version: 1.7.0.9002
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ importFrom(purrr,flatten_int)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map2_chr)
importFrom(purrr,map2_lgl)
importFrom(purrr,map_at)
importFrom(purrr,map_chr)
importFrom(purrr,map_int)
Expand Down
38 changes: 21 additions & 17 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
editor_options:
markdown:
wrap: 79
---
<!-- NEWS.md is maintained by https://cynkra.github.io/fledge, do not edit -->

# styler 1.7.0.9000 (Development version)
# styler 1.7.0.9002 (Development version)

- Same as previous version.


# styler 1.7.0.9000

**User-facing changes**

- `style_dir()` and `style_pkg()` now default to styling all supported file
formats (`.R`, `.Rmd`, `.Rmarkdown`, `.Rnw`, and `.qmd`) in the (package)
directory (\@IndrajeetPatil, #965).
- `style_pkg()` now excludes the auto-generated `R/cpp11.R` file (#977).
- `style_dir()` and `style_pkg()` now default to styling all supported file
formats (`.R`, `.Rmd`, `.Rmarkdown`, `.Rnw`, and `.qmd`) in the (package)
directory (\@IndrajeetPatil, #965, #931).
- `style_pkg()` now excludes the auto-generated `R/cpp11.R` file (#977).
- Minimum needed R version is now bumped to `3.5` (\@IndrajeetPatil, #986).

**Features**

- `filetype` `.qmd` is now supported, but not turned on by default (#931).
- alignment is now detected for function declaration in a similar way as for
function calls (#968).
- new R option `styler.ignore_alignment` controls if alignment should be
Expand All @@ -29,7 +29,7 @@ editor_options:

- If there are only empty lines in a code chunk, they are all removed (#936).

- There is now always one line break after `{` and before `#` (#916).
- There is now always one line break after `{` and before `#` (#952).

- Line breaks may be added to function calls to ensure indention symmetry for
round braces (#975).
Expand All @@ -52,20 +52,24 @@ editor_options:

- \@IndrajeetPatil is now a contributor to {styler}. Welcome and thanks for
everything you did so far! (#988).
- Non-exported and unused functions `odd()` and `even()` were removed
(\@IndrajeetPatil, #989).
- Old (and outdated) vignettes have been removed (\@IndrajeetPatil, #955). To
access them, do `git checkout v1.0.0`.
- Upgrade testing infra to testthat 3e (\@IndrajeetPatil, #949).
- Minor improvements to the documentation (\@IndrajeetPatil, #958).
- turned off `styler.print.Vertical` in vignettes so ANSI output of
{prettycode} not messing with {pkgdown} (\@IndrajeetPatil, #956, #957).
- Non-exported and unused functions `odd()` and `even()` were removed
(\@IndrajeetPatil, #989).
- Upgrade testing infra to testthat 3e (\@IndrajeetPatil, #949).
- All (R)md files in this project's source code are now formatted with
default pandoc markdown formatter. This conversion is required when using
the visual mode in RStudio (#941).
- Update {pkgdown} action to always build, but only deploy on default branch
(#946).
- turned off `styler.print.Vertical` in vignettes so ANSI output of
{prettycode} not messing with {pkgdown} (\@IndrajeetPatil, #956, #957).
- Better stack tracing for profiling (#979, #980).
- Improved code quality by fixing {lintr} warnings (#960).
- Error now on R CMD note (\@IndrajeetPatil, #987).
- Test on latest Ubuntu instead of Ubuntu 18.04 (#982).
- Run tests in parallel (#978, \@krlmlr).

# styler 1.7.0

Expand Down
8 changes: 4 additions & 4 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ style_active_file <- function() {
context <- get_rstudio_context()
transformer <- make_transformer(get_addins_style_transformer(),
include_roxygen_examples = TRUE,
base_indention = 0,
base_indention = 0L,
warn_empty = is_plain_r_file(context$path)
)
is_r_file <- any(
Expand All @@ -71,7 +71,7 @@ style_active_file <- function() {
abort("Can only style .R, .Rmd and .Rnw files.")
}
rstudioapi::modifyRange(
c(1, 1, length(context$contents) + 1, 1),
c(1L, 1L, length(context$contents) + 1L, 1L),
paste0(ensure_last_n_empty(out), collapse = "\n"),
id = context$id
)
Expand Down Expand Up @@ -124,7 +124,7 @@ style_selection <- function() {
communicate_addins_style_transformers()
context <- get_rstudio_context()
text <- context$selection[[1]]$text
if (all(nchar(text) == 0)) abort("No code selected")
if (all(nchar(text) == 0L)) abort("No code selected")
out <- style_text(
text,
transformers = get_addins_style_transformer(),
Expand All @@ -134,7 +134,7 @@ style_selection <- function() {
context$selection[[1]]$range,
paste0(c(
out,
if (context$selection[[1]]$range$end[2] == 1) ""
if (context$selection[[1]]$range$end[2] == 1L) ""
), collapse = "\n"),
id = context$id
)
Expand Down
3 changes: 2 additions & 1 deletion R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ left_join <- function(x, y, by) {
res <- new_tibble(res, nrow = nrow(res))
# dplyr::left_join set unknown list columns to NULL, merge sets them
# to NA
if (exists("child", res) && any(is.na(res$child))) {
if (exists("child", res) && anyNA(res$child)) {
res$child[is.na(res$child)] <- list(NULL)
}
res
Expand All @@ -80,6 +80,7 @@ slice <- function(.data, ...) {
.data[c(...), , drop = FALSE]
}

# TODO: Use `purrr::map_dfr()` when it stops implicitly relying on `{dplyr}`
#' @importFrom purrr as_mapper map
map_dfr <- function(.x, .f, ..., .id = NULL) {
.f <- as_mapper(.f, ...)
Expand Down
14 changes: 6 additions & 8 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
alignment_drop_comments <- function(pd_by_line) {
map(pd_by_line, function(x) {
out <- x[x$token != "COMMENT", ]
if (nrow(out) < 1) {
if (nrow(out) < 1L) {
return(NULL)
} else {
out
Expand Down Expand Up @@ -111,14 +111,12 @@ alignment_ensure_trailing_comma <- function(pd_by_line) {
#' @keywords internal
alignment_col1_all_named <- function(relevant_pd_by_line) {
map_lgl(relevant_pd_by_line, function(x) {
if (nrow(x) < 3) {
if (nrow(x) < 3L) {
return(FALSE)
}
x$token[3] == "expr" &&
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") &&
x$token[2] %in% c(
"EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
)
any(c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") == x$token[1]) &&
any(c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") == x$token[2])
}) %>%
all()
}
Expand Down Expand Up @@ -184,7 +182,7 @@ alignment_serialize <- function(pd_sub) {
#' @keywords internal
alignment_has_correct_spacing_around_comma <- function(pd_sub) {
comma_tokens <- which(pd_sub$token == "','")
if (length(comma_tokens) == 0) {
if (length(comma_tokens) == 0L) {
return(TRUE)
}
relevant_comma_token <- comma_tokens[seq2(1, length(comma_tokens) - 1L)]
Expand All @@ -201,7 +199,7 @@ alignment_has_correct_spacing_around_comma <- function(pd_sub) {
#' @importFrom rlang seq2
alignment_has_correct_spacing_around_eq_sub <- function(pd_sub) {
relevant_eq_sub_token <- which(pd_sub$token == "EQ_SUB")
if (length(relevant_eq_sub_token) == 0) {
if (length(relevant_eq_sub_token) == 0L) {
return(TRUE)
}

Expand Down
37 changes: 18 additions & 19 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' transformers <- tidyverse_style()
#' pd_nested <- styler:::compute_parse_data_nested(c(
#' "call(",
#' " ab = 1,",
#' " ab = 1L,",
#' " a = 2",
#' ")"
#' )) %>%
Expand All @@ -50,7 +50,7 @@ token_is_on_aligned_line <- function(pd_flat) {
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
pd_by_line <- split(pd_flat, line_idx)
pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL
if (length(pd_by_line) < 1) {
if (length(pd_by_line) < 1L) {
return(TRUE)
}
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
Expand Down Expand Up @@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) {
}

pd_by_line <- alignment_drop_comments(pd_by_line)
if (length(pd_by_line) < 1) {
if (length(pd_by_line) < 1L) {
return(TRUE)
}
pd_by_line <- alignment_drop_last_expr(pd_by_line) %>%
Expand All @@ -104,10 +104,9 @@ token_is_on_aligned_line <- function(pd_flat) {
# now, pd only contains arguments separated by values, ideal for iterating
# over columns.
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
previous_line <- 0
current_col <- 0
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
for (column in seq2(1, max(n_cols))) {
previous_line <- current_col <- 0L
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1L, 2L)
for (column in seq2(1L, max(n_cols))) {
by_line <- alignment_serialize_column(pd_by_line, column) %>%
compact() %>%
unlist() %>%
Expand All @@ -120,7 +119,7 @@ token_is_on_aligned_line <- function(pd_flat) {
current_col <- nchar(by_line) - as.integer(column > 1)
# Problem `by_line` counting from comma before column 3, previous_line
# counting 1 space before ~
if (column > 1) {
if (column > 1L) {
previous_line <- previous_line[
intersect(names(previous_line), names(by_line))
]
Expand All @@ -129,28 +128,28 @@ token_is_on_aligned_line <- function(pd_flat) {
}

is_aligned <- length(unique(current_col)) == 1L
if (!is_aligned || length(current_col) < 2) {
if (!is_aligned || length(current_col) < 2L) {
# check 2: left aligned after , (comma to next token)
current_col <- "^(,[\\s\\t]*)[^ ]*.*$" %>%
gsub("\\1", by_line, perl = TRUE) %>%
nchar() %>%
magrittr::subtract(1)

if (column > 1) {
if (column > 1L) {
# must add previous columns, as first column might not align
current_col <- previous_line + current_col
}
if (length(current_col) > 1) {
if (length(current_col) > 1L) {
is_aligned <- length(unique(current_col)) == 1L
} else {
is_aligned <- current_col - max_previous_col == 1
is_aligned <- current_col - max_previous_col == 1L
current_col <- max_previous_col + current_col
}

if (is_aligned) {
# if left aligned after ,
start_eval <- 2
previous_line <- nchar(by_line) - 1 + previous_line # comma to comma
start_eval <- 2L
previous_line <- nchar(by_line) - 1L + previous_line # comma to comma
}
} else {
previous_line <- current_col
Expand All @@ -162,22 +161,22 @@ token_is_on_aligned_line <- function(pd_flat) {
# match left aligned after =
start_after_eq <- regexpr("= [^ ]", by_line)
names(start_after_eq) <- names(by_line)
start_after_eq <- start_after_eq[start_after_eq > 0]
start_after_eq <- start_after_eq[start_after_eq > 0L]

if (column >= start_eval) {
if (length(start_after_eq) == 0) {
if (length(start_after_eq) == 0L) {
return(FALSE)
}
# when match via , unsuccessful, matching by = must yield at least one =
if (column == 1) {
if (column == 1L) {
current_col <- start_after_eq
} else {
current_col <- start_after_eq +
previous_line[intersect(names(previous_line), names(start_after_eq))]
}
is_aligned <- all(
length(unique(current_col)) == 1,
length(start_after_eq) > 1
length(unique(current_col)) == 1L,
length(start_after_eq) > 1L
)
if (!is_aligned) {
return(FALSE)
Expand Down
6 changes: 2 additions & 4 deletions R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ is_subset_expr <- function(pd) {
#' @keywords internal
is_shebang <- function(pd) {
is_first_comment <- pd$pos_id == 1L
is_first_comment[is_first_comment] <- grepl(
"^#!", pd$text[is_first_comment]
)
is_first_comment[is_first_comment] <- startsWith(pd$text[is_first_comment], "#!")
is_first_comment
}

Expand Down Expand Up @@ -150,7 +148,7 @@ contains_else_expr <- function(pd) {
#' @keywords internal
contains_else_expr_that_needs_braces <- function(pd) {
else_idx <- which(pd$token == "ELSE")
if (length(else_idx) > 0) {
if (length(else_idx) > 0L) {
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
Expand Down
16 changes: 6 additions & 10 deletions R/indent.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) {
return(pd)
}

if (pd$newlines[length(pd$newlines) - 1] == 0) {
if (pd$newlines[length(pd$newlines) - 1] == 0L) {
return(pd)
}
pd$indent[nrow] <- indent_by
Expand All @@ -33,9 +33,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) {
#' @keywords internal
indent_without_paren_if_else <- function(pd, indent_by) {
expr_after_if <- next_non_comment(pd, which(pd$token == "')'")[1])
is_if <- pd$token[1] %in% "IF"
has_if_without_curly <-
is_if && pd$child[[expr_after_if]]$token[1] != "'{'"
is_if <- pd$token[1] == "IF"
if (!is_if) {
return(pd)
}
Expand All @@ -47,10 +45,8 @@ indent_without_paren_if_else <- function(pd, indent_by) {
pd$indent[expr_after_if] <- indent_by
}



else_idx <- which(pd$token == "ELSE")
if (length(else_idx) == 0) {
if (length(else_idx) == 0L) {
return(pd)
}
expr_after_else_idx <- next_non_comment(pd, else_idx)
Expand Down Expand Up @@ -167,7 +163,7 @@ needs_indention <- function(pd,
needs_indention_one <- function(pd,
potential_trigger_pos,
other_trigger_tokens) {
before_first_break <- which(pd$lag_newlines > 0)[1] - 1L
before_first_break <- which(pd$lag_newlines > 0L)[1] - 1L
if (is.na(before_first_break)) {
return(FALSE)
}
Expand Down Expand Up @@ -240,7 +236,7 @@ pd_multi_line <- function(pd) {
#' @seealso choose_indention
#' @keywords internal
update_newlines <- function(pd) {
seq_pd <- seq_len(nrow(pd) - 1)
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1]
seq_pd <- seq_len(nrow(pd) - 1L)
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1L]
pd
}
Loading

0 comments on commit d829284

Please sign in to comment.