Skip to content

Commit 9cbce34

Browse files
better explanations for alignment
1 parent 236c957 commit 9cbce34

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

vignettes/detect-alignment.Rmd

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,38 @@ call(
3232
3333
```
3434

35-
because no alignment detection was built in. With `strict = FALSE`, the spacing
35+
because no alignment detection was built in.^[With `strict = FALSE`, the spacing
3636
would have been kept, however, `strict = FALSE` has a number of other
37-
implications because it is in general less invasive (e.g. it would not add
38-
braces to the following expression, whereas `strict = TRUE` would):
37+
implications because it is in general less invasive. For example, it would not
38+
add braces and line breaks to "if (TRUE) return()".]
39+
40+
styler >= 1.1.1.9003 detects aforementioned alignment for function calls. This
41+
vignette describes how aligned code is defined in styler and gives some examples
42+
so users can format their aligned code to match the definition styler uses to
43+
ensure their code is not unintentionally reformatted.
44+
45+
An important definition used in the remainder is the one of a **column**. All
46+
arguments of a function call that have the same position but are placed on
47+
different lines form a column. The below call shows a call with two columns and
48+
two rows. Columns separate arguments of the function call, so the separator is
49+
the comma. The first row is named because all arguments are named, the second is
50+
unnamed:
3951

4052
```{r}
41-
if (TRUE)
42-
call(another, arg)
53+
call(
54+
# column 1 | column 2 |
55+
abkj = f(2), 7,
56+
more_ = "a", 2 # more
57+
)
4358
```
4459

45-
Back to the initial topic, styler >= 1.1.1.9003 detects the aforementioned
46-
alignment. This vignette describes how an aligned code is defined by styler and
47-
gives some examples so users can format their aligned code to match the
48-
definition styler uses to ensure their code is not unintentionally reformatted.
60+
## Function calls
4961

50-
## function calls
62+
We generally distinguish the case of named and unnamed columns.
5163

5264
### Non-technical
5365

54-
If all arguments in the first column are named: Make commas match position
66+
**If all arguments in the first column are named**: Make commas match position
5567
vertically and right align everything between commas
5668

5769
```{r}
@@ -63,20 +75,29 @@ fell(
6375
y = 23,
6476
zz = NULL
6577
)
78+
79+
# this works also with more than one column
80+
fell(
81+
x = 1, annoying = 3,
82+
y = 23, # nothing in column 2 for row 2
83+
zz = NULL, finally = ""
84+
)
6685
```
6786

68-
If not all arguments of the first column are named: Make all but the first
69-
column's commas match position vertically and right align everything between the
70-
commas, except before the first comma on a line, give priority to correctly
71-
indent.
87+
**If not all arguments of the first column are named:**^[In the below example,
88+
the first argument of the first column is named (`p = 2`), the second argument
89+
of the first column is not (`31`)] Make all but the first column's commas match
90+
position vertically and right align everything between the commas, except before
91+
the first comma on a line, give priority to correctly indent (i.e. left align).
7292

7393
```{r}
74-
# not all arguments of first column (f = NULL and 13231) named, hence, only
94+
# not all arguments of first column named, hence, only
7595
# commas of all but the first column must agree.
7696
gell(
77-
2, g = gg(x), n = 3 * 3,
78-
13231, fds = -1, gz = f / 3,
97+
p = 2, g = gg(x), n = 3 * 3, #
98+
31, fds = -1, gz = f / 3,
7999
)
100+
80101
```
81102

82103
By *align everything in between the commas*, we mean put zero space before a
@@ -92,15 +113,31 @@ map(x, f,
92113
)
93114
```
94115

116+
**Examples**
117+
118+
```{r}
119+
# tribble
120+
tibble::tribble(
121+
~key_here, ~value_here,
122+
"left", "right", # comments are allowed
123+
"long string", "shrt" # columns can overlap ('~' above ',')
124+
)
125+
126+
purrr::map(x, fun, # arguments on same line as opening brace are not considered
127+
arg2 = 2,
128+
ar = f(x)
129+
)
130+
```
131+
95132
## technical
96133

97134
Function calls are aligned if **all** of the following conditions hold (for all
98135
but the very first line (i.e. `call(` below):
99136

100-
* first column has same number of lag spaces. This basically means that the
101-
indention is identical for all columns (except for the closing brace if it is
102-
on its own line). The below example has one column, because the maximal number
103-
of commas on one line is one.
137+
* all rows in first column has same number of lag spaces. This basically means
138+
that the indention is identical for all columns (except for the closing brace
139+
if it is on its own line). The below example has one column, because the
140+
maximal number of commas on one line is one.
104141

105142
```{r}
106143
# holds
@@ -135,16 +172,16 @@ call(
135172

136173
* All commas from all columns are aligned. This means that for every column, all
137174
commas must be on the same positions as the commas from the other lines. If
138-
not all arguments are named in the first column, this column is not considered.
139-
The reason to exclude the first column is that, as in the example below, it is
140-
possible that some arguments are named while others are not. Then, it is not
141-
generally possible to keep the first rule (i.e. indention identical across
142-
lines) as well as ensuring that the comma does not have any spaces before it
143-
and that the comma is aligned with the other lines. This is shown below with
144-
the line `f(x, y),`. For this reason, the requirements exclude the first
145-
column in such cases. The *holds* example shows that is is possible (but not
146-
required) for named arguments to also have the commas separating the first and
147-
second column aligned.
175+
not all arguments are named in the first column, this column is not
176+
considered. The reason to exclude the first column is that, as in the example
177+
below, it is possible that some arguments are named while others are not.
178+
Then, it is not generally possible to keep the first rule (i.e. indention
179+
identical across lines) as well as ensuring that the comma does not have any
180+
spaces before it and that the comma is aligned with the other lines. This is
181+
shown below with the line `f(x, y),`. For this reason, the requirements
182+
exclude the first column in such cases. The *holds* example shows that is is
183+
possible (but not required) for named arguments to also have the commas
184+
separating the first and second column aligned.
148185

149186
```{r}
150187
# holds
@@ -173,11 +210,11 @@ rge(
173210
)
174211
```
175212

176-
## comments
213+
## Comments
177214

178215
not supported yet.
179216

180-
## assignment
217+
## Assignment
181218

182219
not supported yet.
183220

0 commit comments

Comments
 (0)