Skip to content
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

Fix alignment detection in tribble #777

Merged
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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* `#>` is recognized as an output marker and no space is added after `#` (#771).
* code with left alignment after `=` in function calls is now recognized as
aligned and won't be reformatted (#774).
aligned and won't be reformatted (#774, #777).
```
# newly detected
call(
Expand Down
25 changes: 18 additions & 7 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,32 @@ token_is_on_aligned_line <- function(pd_flat) {

is_aligned <- length(unique(current_col)) == 1L
if (is_aligned) {
previous_line <- nchar(by_line)
previous_line <- previous_line + nchar(by_line)
next
}
# check 2: match by = (no extra spaces around it allowed.)
# 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]

# when match via comma unsuccessful, matching by = must yield at least one =
is_aligned <- length(unique(start_after_eq + previous_line)) == 1 && length(start_after_eq) > 1
previous_line <- nchar(by_line) + previous_line
if (column >= start_eval && !is_aligned) {
# when not all are named, we need colum 1 for previous_line
return(FALSE)
if (column >= start_eval) {
if (length(start_after_eq) == 0) {
return(FALSE)
}
# when match via comma unsuccessful, matching by = must yield at least one =
if (column == 1) {
current_col <- start_after_eq
} else {
current_col <- start_after_eq +
previous_line[intersect(names(previous_line), names(start_after_eq))]
}
is_aligned <- length(unique(current_col)) == 1 && length(start_after_eq) > 1
if (!is_aligned) {
return(FALSE)
}
}
previous_line <- nchar(by_line) + previous_line
}
TRUE
}
5 changes: 5 additions & 0 deletions tests/testthat/alignment/named-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ gell(
p = 2, g = gg(x), n = 3 * 3, #
31, fds = -1, gz = f / 3 + 1,
)

xgle(
1212, 232, f(n = 2),
1, 2, "kFlya"
)
133 changes: 81 additions & 52 deletions tests/testthat/alignment/named-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/alignment/named-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,8 @@ gell(
p = 2, g = gg(x), n = 3 * 3, #
31, fds = -1, gz = f / 3 + 1,
)

xgle(
1212, 232, f(n = 2),
1, 2, "kFlya"
)
3 changes: 3 additions & 0 deletions tests/testthat/test-detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ test_that("does apply spacing rules only if not aligned", {
expect_warning(test_collection("alignment",
transformer = style_text
), NA)

text <- "tribble(\n ~x, ~y,\n 11, list(a = 1),\n 2, list(bjj = 2)\n)"
expect_warning(style_text(text), NA)
})