Skip to content

Detect alignment more generally #786

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

Merged
merged 4 commits into from
Apr 30, 2021
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
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.4.1.9002
Version: 1.4.1.9003
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down
26 changes: 18 additions & 8 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@
)
```

* similarly, `tibble::tribble()`-like alignment for column > 1 is now detected
when left aligned (#785).
* similarly, left aligned after comma is now detected (#785).
```
# previously detected
call(
x = 12345, "It's old",
y2 = 17, "before"
)

tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 422231
~x, ~y,
"another", 1:3,
"b", 1211234
)

# newly detected
call(
x = 2, p = "another",
y = "hhjkjkbew", x = 3
)


tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 422231
~x, ~y,
"another", 1:3,
"b", 1211234
)
```
Also see `vignette("detect-alignment")`.
Expand Down
6 changes: 3 additions & 3 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ alignment_ensure_trailing_comma <- function(pd_by_line) {
#' excluding first and last column.
#' @importFrom purrr map_lgl
#' @keywords internal
alignment_col1_is_named <- function(relevant_pd_by_line) {
alignment_col1_all_named <- function(relevant_pd_by_line) {
map_lgl(relevant_pd_by_line, function(x) {
if (nrow(x) < 3) {
return(FALSE)
Expand All @@ -90,7 +90,7 @@ alignment_col1_is_named <- function(relevant_pd_by_line) {

#' Serialize all lines for a given column
#' @param column The index of the column to serialize.
#' @inheritParams alignment_col1_is_named
#' @inheritParams alignment_col1_all_named
#' @importFrom purrr map
#' @keywords internal
alignment_serialize_column <- function(relevant_pd_by_line, column) {
Expand All @@ -101,7 +101,7 @@ alignment_serialize_column <- function(relevant_pd_by_line, column) {
#'
#'
#' @inheritParams alignment_serialize_column
#' @inheritParams alignment_col1_is_named
#' @inheritParams alignment_col1_all_named
#' @keywords internal
alignment_serialize_line <- function(relevant_pd_by_line, column) {
# TODO
Expand Down
12 changes: 8 additions & 4 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) {

n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
previous_line <- 0
start_eval <- ifelse(all(alignment_col1_is_named(pd_by_line)), 1, 2)
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
for (column in seq2(1, max(n_cols))) {
by_line <- alignment_serialize_column(pd_by_line, column) %>%
compact() %>%
Expand All @@ -109,15 +109,19 @@ token_is_on_aligned_line <- function(pd_flat) {
}

is_aligned <- length(unique(current_col)) == 1L
if (!is_aligned && !any(pd_flat$token == "EQ_SUB")) {
# check 2: by character after comma, e.g. tribble. Must not have =
current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*", "\\1", by_line, perl = TRUE))
if (!is_aligned) {
# check 2: left aligned after ,
current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*.*$", "\\1", by_line, perl = TRUE))

if (column > 1) {
# must add previous columns, as first column might not align
current_col <- current_col + previous_line
}
is_aligned <- length(unique(current_col)) == 1L
if (is_aligned) {
# if left aligned after ,
start_eval <- 2
}
}
if (is_aligned) {
previous_line <- previous_line + nchar(by_line)
Expand Down

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

12 changes: 12 additions & 0 deletions tests/testthat/alignment/named-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,15 @@ xgle(
1212, 232, f(n = 2),
1, 2, "kFlya"
)

# left aligned after ,
call(
x = 2, y = "another",
y = "hhjkjkbew", x = 3
)

call(
k = ff("pk"), k = 3,
b = f(-g), 22 + 1,
44, 323
)
127 changes: 98 additions & 29 deletions tests/testthat/alignment/named-in_tree

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

12 changes: 12 additions & 0 deletions tests/testthat/alignment/named-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,15 @@ xgle(
1212, 232, f(n = 2),
1, 2, "kFlya"
)

# left aligned after ,
call(
x = 2, y = "another",
y = "hhjkjkbew", x = 3
)

call(
k = ff("pk"), k = 3,
b = f(-g), 22 + 1,
44, 323
)
2 changes: 1 addition & 1 deletion tests/testthat/alignment/tribble-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ tribble(
# has EQ_SUB which don't match, not tribble-like
mlr3misc:::rowwise_table(
x = 23, zy = 3,
y = 1, k = 1,
y = 1, k = 1,
)
Loading