Skip to content
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
4 changes: 3 additions & 1 deletion .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Setup system dependencies
run: |
sudo apt install cmake libcurl4-openssl-dev libnode-dev pandoc
- name: Set up Python
uses: actions/setup-python@v6.0.0
with:
Expand Down
4 changes: 2 additions & 2 deletions 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.11.0
Version: 1.11.0.9001
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down Expand Up @@ -53,7 +53,6 @@ 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.3
Language: en-US
Config/testthat/edition: 3
Config/testthat/parallel: true
Expand Down Expand Up @@ -107,3 +106,4 @@ Collate:
'vertical.R'
'visit.R'
'zzz.R'
Config/roxygen2/version: 8.0.0
23 changes: 15 additions & 8 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
NULL



#' @keywords internal
style_active_file <- function() {
communicate_addins_style_transformers()
context <- get_rstudio_context()
transformer <- make_transformer(get_addins_style_transformer(),
transformer <- make_transformer(
get_addins_style_transformer(),
include_roxygen_examples = TRUE,
base_indention = 0L,
warn_empty = is_plain_r_file(context$path)
Expand Down Expand Up @@ -124,18 +124,23 @@ style_selection <- function() {
communicate_addins_style_transformers()
context <- get_rstudio_context()
text <- context$selection[[1L]]$text
if (!any(nzchar(text))) abort("No code selected")
if (!any(nzchar(text))) {
abort("No code selected")
}
out <- style_text(
text,
transformers = get_addins_style_transformer(),
base_indention = nchar(gsub("^( *).*", "\\1", text))
)
rstudioapi::modifyRange(
context$selection[[1L]]$range,
paste(c(
out,
if (context$selection[[1L]]$range$end[2L] == 1L) ""
), collapse = "\n"),
paste(
c(
out,
if (context$selection[[1L]]$range$end[2L] == 1L) ""
),
collapse = "\n"
),
id = context$id
)
if (save_after_styling_is_active() && context$path != "") {
Expand Down Expand Up @@ -169,7 +174,9 @@ set_style_transformers <- function() {
error = function(e) {
abort(paste0(
"The selected style transformers \"",
new_style, "\" is not valid: ", e$message
new_style,
"\" is not valid: ",
e$message
))
}
)
Expand Down
20 changes: 14 additions & 6 deletions R/communicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#' @inheritParams parse_tree_must_be_identical
#' @keywords internal
communicate_warning <- function(changed, transformers) {
if (any(changed, na.rm = TRUE) &&
!parse_tree_must_be_identical(transformers) &&
!getOption("styler.quiet", FALSE)
if (
any(changed, na.rm = TRUE) &&
!parse_tree_must_be_identical(transformers) &&
!getOption("styler.quiet", FALSE)
) {
cat("Please review the changes carefully!", fill = TRUE)
}
Expand All @@ -26,17 +27,24 @@ communicate_summary <- function(changed, ruler_width) {
cli::cat_rule(width = max(40L, ruler_width))
cat("Status\tCount\tLegend \n")
cli::cat_bullet(
"\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.",
"\t",
sum(!changed, na.rm = TRUE),
"\tFile unchanged.",
bullet = "tick",
bullet_col = "green"
)
cli::cat_bullet(
"\t", sum(changed, na.rm = TRUE), "\tFile changed.",
"\t",
sum(changed, na.rm = TRUE),
"\tFile changed.",
bullet = "info",
bullet_col = "cyan"
)
cli::cat_bullet(
bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error.",
bullet = "cross",
"\t",
sum(is.na(changed)),
"\tStyling threw an error.",
bullet_col = "red"
)
cli::cat_rule(width = max(40L, ruler_width))
Expand Down
42 changes: 30 additions & 12 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#' to be the last token in that case.
#' @inheritParams alignment_drop_comments
#' @keywords internal
alignment_ensure_no_closing_brace <- function(pd_by_line,
last_line_droped_early) {
alignment_ensure_no_closing_brace <- function(
pd_by_line,
last_line_droped_early
) {
if (last_line_droped_early) {
return(pd_by_line)
}
Expand All @@ -15,7 +17,10 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
pd_by_line[-length(pd_by_line)]
} else {
# only drop last elment of last line
pd_by_line[[length(pd_by_line)]] <- vec_slice(last, seq2(1L, nrow(last) - 1L))
pd_by_line[[length(pd_by_line)]] <- vec_slice(
last,
seq2(1L, nrow(last) - 1L)
)
pd_by_line
}
}
Expand Down Expand Up @@ -59,7 +64,10 @@ alignment_drop_comments <- function(pd_by_line) {
alignment_drop_last_expr <- function(pds_by_line) {
# TODO could be skipped if we know it's not a function dec
pd_last_line <- pds_by_line[[length(pds_by_line)]]
last_two_lines <- pd_last_line$token[c(nrow(pd_last_line) - 1L, nrow(pd_last_line))]
last_two_lines <- pd_last_line$token[c(
nrow(pd_last_line) - 1L,
nrow(pd_last_line)
)]
if (identical(last_two_lines, c("')'", "expr"))) {
pd_last_line <- vec_slice(pd_last_line, -nrow(pd_last_line))
}
Expand Down Expand Up @@ -110,7 +118,10 @@ alignment_col1_all_named <- function(relevant_pd_by_line) {
}
x$token[3L] == "expr" &&
any(c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") == x$token[1L]) &&
any(c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") == x$token[2L])
any(
c("EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE") ==
x$token[2L]
)
}) %>%
all()
}
Expand Down Expand Up @@ -151,13 +162,20 @@ alignment_serialize_line <- function(relevant_pd_by_line, column) {
#' @inheritParams alignment_drop_comments
#' @keywords internal
alignment_serialize <- function(pd_sub) {
out <- Map(function(terminal, text, child, spaces, newlines) {
if (terminal) {
paste0(text, rep_char(" ", spaces))
} else {
paste0(alignment_serialize(child), rep_char(" ", spaces))
}
}, pd_sub$terminal, pd_sub$text, pd_sub$child, pd_sub$spaces, pd_sub$newlines)
out <- Map(
function(terminal, text, child, spaces, newlines) {
if (terminal) {
paste0(text, rep_char(" ", spaces))
} else {
paste0(alignment_serialize(child), rep_char(" ", spaces))
}
},
pd_sub$terminal,
pd_sub$text,
pd_sub$child,
pd_sub$spaces,
pd_sub$newlines
)
if (anyNA(out)) {
return(NA)
}
Expand Down
6 changes: 4 additions & 2 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ token_is_on_aligned_line <- function(pd_flat) {
return(FALSE)
}
has_correct_spacing_around_comma <- map_lgl(
pd_by_line, alignment_has_correct_spacing_around_comma
pd_by_line,
alignment_has_correct_spacing_around_comma
)
if (!all(has_correct_spacing_around_comma)) {
return(FALSE)
}

has_correct_spacing_around_eq_sub <- map_lgl(
pd_by_line, alignment_has_correct_spacing_around_eq_sub
pd_by_line,
alignment_has_correct_spacing_around_eq_sub
)

if (!all(has_correct_spacing_around_eq_sub)) {
Expand Down
5 changes: 4 additions & 1 deletion R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ is_subset_expr <- function(pd) {
#' @keywords internal
is_shebang <- function(pd) {
is_first_comment <- pd$pos_id == 1L
is_first_comment[is_first_comment] <- startsWith(pd$text[is_first_comment], "#!")
is_first_comment[is_first_comment] <- startsWith(
pd$text[is_first_comment],
"#!"
)
is_first_comment
}

Expand Down
64 changes: 36 additions & 28 deletions R/indent.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,29 @@ indent_without_paren_if_else <- function(pd, indent_by) {
}
needs_indention_now <- pd$lag_newlines[
next_non_comment(pd, which(pd$token == "')'"))
] > 0L
] >
0L

if (needs_indention_now) {
pd$indent[expr_after_if] <- indent_by
}

else_idx <- which(pd$token == "ELSE")
if (length(else_idx) == 0L) {
els_idx <- which(pd$token == "ELSE")
if (length(els_idx) == 0L) {
return(pd)
}
expr_after_else_idx <- next_non_comment(pd, else_idx)
has_else_without_curly_or_else_chid <-
any(pd$token == "ELSE") &&
pd$child[[expr_after_else_idx]]$token[1L] != "'{'" &&
pd$child[[expr_after_else_idx]]$token[1L] != "IF"
expr_after_else_idx <- next_non_comment(pd, els_idx)
has_else_without_curly_or_else_chid <- any(pd$token == "ELSE") &&
pd$child[[expr_after_else_idx]]$token[1L] != "'{'" &&
pd$child[[expr_after_else_idx]]$token[1L] != "IF"

needs_indention_now <- pd$lag_newlines[
next_non_comment(pd, which(pd$token == "ELSE"))
] > 0L
] >
0L

if (has_else_without_curly_or_else_chid && needs_indention_now) {
pd$indent[seq(else_idx + 1L, nrow(pd))] <- indent_by
pd$indent[seq(els_idx + 1L, nrow(pd))] <- indent_by
}
pd
}
Expand Down Expand Up @@ -96,12 +97,12 @@ indent_without_paren_if_else <- function(pd, indent_by) {
#' @examples
#' styler:::parse_text("a[1]")
#' styler:::parse_text("a[[1\n]]")
compute_indent_indices <- function(pd,
token_opening,
token_closing = NULL) {
compute_indent_indices <- function(pd, token_opening, token_closing = NULL) {
npd <- nrow(pd)
potential_triggers <- which(pd$token %in% token_opening)
needs_indention <- needs_indention(pd, potential_triggers,
needs_indention <- needs_indention(
pd,
potential_triggers,
other_trigger_tokens = c("EQ_SUB", "EQ_FORMALS")
)
trigger <- potential_triggers[needs_indention][1L]
Expand All @@ -128,11 +129,16 @@ compute_indent_indices <- function(pd,
#' tokens in `pd`.
#' @inheritParams needs_indention_one
#' @keywords internal
needs_indention <- function(pd,
potential_triggers_pos,
other_trigger_tokens = NULL) {
map_lgl(potential_triggers_pos, needs_indention_one,
pd = pd, other_trigger_tokens = other_trigger_tokens
needs_indention <- function(
pd,
potential_triggers_pos,
other_trigger_tokens = NULL
) {
map_lgl(
potential_triggers_pos,
needs_indention_one,
pd = pd,
other_trigger_tokens = other_trigger_tokens
)
}

Expand Down Expand Up @@ -168,15 +174,18 @@ needs_indention <- function(pd,
#' "call(named = c,",
#' "named = b)"
#' ), strict = FALSE)
needs_indention_one <- function(pd,
potential_trigger_pos,
other_trigger_tokens) {
needs_indention_one <- function(
pd,
potential_trigger_pos,
other_trigger_tokens
) {
before_first_break <- which(pd$lag_newlines > 0L)[1L] - 1L
if (is.na(before_first_break)) {
return(FALSE)
}
row_idx_between_trigger_and_line_break <- seq2(
potential_trigger_pos, before_first_break
potential_trigger_pos,
before_first_break
)
multi_line_token <- pd_is_multi_line(
vec_slice(pd, row_idx_between_trigger_and_line_break)
Expand All @@ -186,10 +195,10 @@ needs_indention_one <- function(pd,
potential_trigger_pos
)

other_trigger_on_same_line <- (
pd$token[remaining_row_idx_between_trigger_and_line_break] %in%
other_trigger_tokens
)
other_trigger_on_same_line <- (pd$token[
remaining_row_idx_between_trigger_and_line_break
] %in%
other_trigger_tokens)
line_break_after_other_trigger <-
pd$lag_newlines[remaining_row_idx_between_trigger_and_line_break + 1L] > 0L

Expand All @@ -200,7 +209,6 @@ needs_indention_one <- function(pd,
}



#' Set the multi-line column
#'
#' Sets the column `multi_line` in `pd` by checking row-wise whether any child
Expand Down
Loading
Loading