Skip to content
Merged
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 NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# styler 1.6.2.9000 (Development version)

* Alignment detection respects stylerignore (#850).

# styler 1.6.2

Expand Down
10 changes: 9 additions & 1 deletion R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ token_is_on_aligned_line <- function(pd_flat) {
pd_flat$lag_newlines <- pd_flat$pos_id <- NULL
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) {
return(TRUE)
}
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
relevant_idx <- seq2(2, ifelse(last_line_is_closing_brace_only,
length(pd_by_line) - 1,
Expand Down Expand Up @@ -86,7 +90,11 @@ token_is_on_aligned_line <- function(pd_flat) {
}

pd_by_line <- alignment_drop_comments(pd_by_line) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)
if (length(pd_by_line) < 1) {
return(TRUE)
}
pd_by_line <- pd_by_line %>%
alignment_ensure_trailing_comma()
# now, pd only contains arguments separated by values, ideal for iterating
# over columns.
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/stylerignore/alignment-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ps(
interaction_constraints = p_uty(tgs = "train"),
monotone_constraints = p_uty(dfault = 0, tags = c("train", "control"), custom_check = function(x) { checkmate::check_integerish(x, lower = -1, upper = 1, any.missing = FALSE) }), # styler: off
normalize_type = p_fct(c("tee", "forest"), default = "tree", tags = "train"),
)
110 changes: 110 additions & 0 deletions tests/testthat/stylerignore/alignment-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/stylerignore/alignment-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ps(
interaction_constraints = p_uty(tgs = "train"),
monotone_constraints = p_uty(dfault = 0, tags = c("train", "control"), custom_check = function(x) { checkmate::check_integerish(x, lower = -1, upper = 1, any.missing = FALSE) }), # styler: off
normalize_type = p_fct(c("tee", "forest"), default = "tree", tags = "train"),
)
6 changes: 6 additions & 0 deletions tests/testthat/test-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ test_that("no token added or removed in complex case", {
transformer = style_text
), NA)
})

test_that("stylerignore sequences are respected in alignment detection", {
expect_warning(test_collection("stylerignore", "alignment",
transformer = style_text
), NA)
})