Skip to content

Support left alignment in tribble #785

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 5 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
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ repos:
- id: deps-in-desc
exclude: >
(?x)^(
bench/.*
bench/.*|
tests/testthat/.*-in\.R|
tests/testthat/.*-out\.R
)$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
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.4.1.9001
Version: 1.4.1.9002
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down
37 changes: 30 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
# styler 1.4.1.9000 (Development version)

## Alignment detection

* code with left alignment after `=` in function calls is now recognized as
aligned and won't be reformatted (#774, #777).
```
# newly detected
# already detected previously
call(
x = 12345,
y2 = 17
y2 = 17
)

# already detected previously
# newly detected
call(
x = 12345,
y2 = 17
y2 = 17
)
```
Also see `vignette("detect-alignment")`:

* similarly, `tibble::tribble()`-like alignment for column > 1 is now detected
when left aligned (#785).
```
# previously detected
tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 422231
)

# newly detected
tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 422231
)
```
Also see `vignette("detect-alignment")`.

## Minor changes and fixes

* `#>` is recognized as an output marker and no space is added after `#` (#771).
* improve pkgdown author URLs (#775).
* `multi_line` attribute in parse table is now integer, not boolean (#782).

* improve pkgdown author URLs (#775).


# styler 1.4.1

* Fix interaction between cache and `base_indention`. This also fixes
Expand Down
12 changes: 11 additions & 1 deletion R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,21 @@ 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 (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) {
previous_line <- previous_line + nchar(by_line)
next
}
# check 2: match by = (no extra spaces around it allowed.)
# check 3: 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)
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/alignment/tribble-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)


tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

# has EQ_SUB which don't match, not tribble-like
mlr3misc:::rowwise_table(
x = 23, zy = 3,
y = 1, k = 1,
)
165 changes: 165 additions & 0 deletions tests/testthat/alignment/tribble-in_tree

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

30 changes: 30 additions & 0 deletions tests/testthat/alignment/tribble-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)


tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)

# has EQ_SUB which don't match, not tribble-like
mlr3misc:::rowwise_table(
x = 23, zy = 3,
y = 1, k = 1,
)
2 changes: 1 addition & 1 deletion tests/testthat/strict/strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test <- function() {
# Only with conservative settings:
call(
preserves, distance,
after, commas,
after, commas,
given_has, one
)

Expand Down
2 changes: 1 addition & 1 deletion touchstone/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"os": "ubuntu-18.04",
"r": "4.0.0",
"benchmarking_repo": "lorenzwalthert/here",
"benchmarking_ref": "10ae0b2935e6d0b157895dcec0fd5f2638793631",
"benchmarking_ref": "bf0167746da7fe4fb156082bad93c9e5cd3386bd",
"benchmarking_path": "touchstone/sources/here",
"rspm": "https://packagemanager.rstudio.com/all/__linux__/bionic/291"
}
31 changes: 29 additions & 2 deletions vignettes/detect-alignment.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ tibble::tribble(
"long string", "shrt" # columns can overlap ('~' above ',')
)

tibble::tribble(
~key_here, ~value_here,
"left", "right", # comments are allowed
"long string", "shrt" # columns can overlap ('~' above ',')
)


purrr::map(x, fun, # arguments on same line as opening brace are not considered
arg2 = 2,
ar = f(k, x)
Expand Down Expand Up @@ -126,8 +133,8 @@ fell(

# this works also with more than one column
fell(
x = 1, annoying = 3,
y = 23, # nothing in column 2 for row 2
x = 1, annoying = 3,
y = 23, # nothing in column 2 for row 2
zz = NULL, finally = "stuff"
)
```
Expand All @@ -153,6 +160,26 @@ gell(
)
```

**If the function call is tribble-like**

```{r}
# you can right align, then it's the last case discussed
tibble::tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 422231
)

# or you left align, then all tokens after commas must match in position
tibble::tribble(
~x, ~d,
"axa'fa", 1:6,
"b", 4:6
)
```



## Comments

not supported yet.
Expand Down