Skip to content

Commit 97e2984

Browse files
Merge 415258d into 51bb82f
2 parents 51bb82f + 415258d commit 97e2984

File tree

10 files changed

+301
-14
lines changed

10 files changed

+301
-14
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ repos:
4444
- id: deps-in-desc
4545
exclude: >
4646
(?x)^(
47-
bench/.*
47+
bench/.*|
48+
tests/testthat/.*-in\.R|
49+
tests/testthat/.*-out\.R
4850
)$
4951
- repo: https://github.com/pre-commit/pre-commit-hooks
5052
rev: v3.2.0

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: styler
33
Title: Non-Invasive Pretty Printing of R Code
4-
Version: 1.4.1.9001
4+
Version: 1.4.1.9002
55
Authors@R:
66
c(person(given = "Kirill",
77
family = "Müller",

NEWS.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
# styler 1.4.1.9000 (Development version)
22

3+
## Alignment detection
4+
35
* code with left alignment after `=` in function calls is now recognized as
46
aligned and won't be reformatted (#774, #777).
57
```
6-
# newly detected
8+
# already detected previously
79
call(
810
x = 12345,
9-
y2 = 17
11+
y2 = 17
1012
)
1113
12-
# already detected previously
14+
# newly detected
1315
call(
1416
x = 12345,
15-
y2 = 17
17+
y2 = 17
1618
)
1719
```
18-
Also see `vignette("detect-alignment")`:
20+
21+
* similarly, `tibble::tribble()`-like alignment for column > 1 is now detected
22+
when left aligned (#785).
23+
```
24+
# previously detected
25+
tribble(
26+
~x, ~d,
27+
"axa'fa", 1:6,
28+
"b", 422231
29+
)
1930
31+
# newly detected
32+
tribble(
33+
~x, ~d,
34+
"axa'fa", 1:6,
35+
"b", 422231
36+
)
37+
```
38+
Also see `vignette("detect-alignment")`.
39+
40+
## Minor changes and fixes
41+
2042
* `#>` is recognized as an output marker and no space is added after `#` (#771).
21-
* improve pkgdown author URLs (#775).
2243
* `multi_line` attribute in parse table is now integer, not boolean (#782).
23-
44+
* improve pkgdown author URLs (#775).
45+
46+
2447
# styler 1.4.1
2548

2649
* Fix interaction between cache and `base_indention`. This also fixes

R/detect-alignment.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,21 @@ token_is_on_aligned_line <- function(pd_flat) {
109109
}
110110

111111
is_aligned <- length(unique(current_col)) == 1L
112+
if (!is_aligned && !any(pd_flat$token == "EQ_SUB")) {
113+
# check 2: by character after comma, e.g. tribble. Must not have =
114+
current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*", "\\1", by_line, perl = TRUE))
115+
116+
if (column > 1) {
117+
# must add previous columns, as first column might not align
118+
current_col <- current_col + previous_line
119+
}
120+
is_aligned <- length(unique(current_col)) == 1L
121+
}
112122
if (is_aligned) {
113123
previous_line <- previous_line + nchar(by_line)
114124
next
115125
}
116-
# check 2: match by = (no extra spaces around it allowed.)
126+
# check 3: match by = (no extra spaces around it allowed.)
117127
# match left aligned after =
118128
start_after_eq <- regexpr("= [^ ]", by_line)
119129
names(start_after_eq) <- names(by_line)

tests/testthat/alignment/tribble-in.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
tribble(
2+
~x, ~d,
3+
"axa'fa", 1:6,
4+
"b", 4:6
5+
)
6+
7+
tribble(
8+
~x, ~d,
9+
"axa'fa", 1:6,
10+
"b", 4:6
11+
)
12+
13+
14+
tribble(
15+
~x, ~d,
16+
"axa'fa", 1:6,
17+
"b", 4:6
18+
)
19+
20+
tribble(
21+
~x, ~d,
22+
"axa'fa", 1:6,
23+
"b", 4:6
24+
)
25+
26+
# has EQ_SUB which don't match, not tribble-like
27+
mlr3misc:::rowwise_table(
28+
x = 23, zy = 3,
29+
y = 1, k = 1,
30+
)

tests/testthat/alignment/tribble-in_tree

Lines changed: 165 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
tribble(
2+
~x, ~d,
3+
"axa'fa", 1:6,
4+
"b", 4:6
5+
)
6+
7+
tribble(
8+
~x, ~d,
9+
"axa'fa", 1:6,
10+
"b", 4:6
11+
)
12+
13+
14+
tribble(
15+
~x, ~d,
16+
"axa'fa", 1:6,
17+
"b", 4:6
18+
)
19+
20+
tribble(
21+
~x, ~d,
22+
"axa'fa", 1:6,
23+
"b", 4:6
24+
)
25+
26+
# has EQ_SUB which don't match, not tribble-like
27+
mlr3misc:::rowwise_table(
28+
x = 23, zy = 3,
29+
y = 1, k = 1,
30+
)

tests/testthat/strict/strict-out.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ test <- function() {
8787
# Only with conservative settings:
8888
call(
8989
preserves, distance,
90-
after, commas,
90+
after, commas,
9191
given_has, one
9292
)
9393

touchstone/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"os": "ubuntu-18.04",
33
"r": "4.0.0",
44
"benchmarking_repo": "lorenzwalthert/here",
5-
"benchmarking_ref": "10ae0b2935e6d0b157895dcec0fd5f2638793631",
5+
"benchmarking_ref": "bf0167746da7fe4fb156082bad93c9e5cd3386bd",
66
"benchmarking_path": "touchstone/sources/here",
77
"rspm": "https://packagemanager.rstudio.com/all/__linux__/bionic/291"
88
}

vignettes/detect-alignment.Rmd

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ tibble::tribble(
5454
"long string", "shrt" # columns can overlap ('~' above ',')
5555
)
5656
57+
tibble::tribble(
58+
~key_here, ~value_here,
59+
"left", "right", # comments are allowed
60+
"long string", "shrt" # columns can overlap ('~' above ',')
61+
)
62+
63+
5764
purrr::map(x, fun, # arguments on same line as opening brace are not considered
5865
arg2 = 2,
5966
ar = f(k, x)
@@ -126,8 +133,8 @@ fell(
126133
127134
# this works also with more than one column
128135
fell(
129-
x = 1, annoying = 3,
130-
y = 23, # nothing in column 2 for row 2
136+
x = 1, annoying = 3,
137+
y = 23, # nothing in column 2 for row 2
131138
zz = NULL, finally = "stuff"
132139
)
133140
```
@@ -153,6 +160,26 @@ gell(
153160
)
154161
```
155162

163+
**If the function call is tribble-like**
164+
165+
```{r}
166+
# you can right align, then it's the last case discussed
167+
tibble::tribble(
168+
~x, ~d,
169+
"axa'fa", 1:6,
170+
"b", 422231
171+
)
172+
173+
# or you left align, then all tokens after commas must match in position
174+
tibble::tribble(
175+
~x, ~d,
176+
"axa'fa", 1:6,
177+
"b", 4:6
178+
)
179+
```
180+
181+
182+
156183
## Comments
157184

158185
not supported yet.

0 commit comments

Comments
 (0)