Skip to content

Commit e7ad5f7

Browse files
Merge e46ca2e into c29ed37
2 parents c29ed37 + e46ca2e commit e7ad5f7

8 files changed

+144
-16
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
* `Warning: Unknown or uninitialised column:` was fixed (#885).
3636
* Unaligned expressions with quoted key (e.g. `c("x" = 2)`) are now correctly
3737
detected (#881).
38+
* function calls with unequal number of token on different lines are no longer
39+
deemed aligned if there are arbitrary spaces around the tokens on the lines
40+
with most tokens (#902).
3841
* ensure a trailing blank line also if the input is cached (#867).
3942
* Preserve trailing blank line in roxygen examples to simplify concatenation of
4043
examples (#880).

R/detect-alignment.R

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ token_is_on_aligned_line <- function(pd_flat) {
8484
if (any(starting_with_comma)) {
8585
return(FALSE)
8686
}
87-
pd_is_multi_line <- map_lgl(pd_by_line, ~ any(.x$multi_line > 0L, na.rm = TRUE))
87+
pd_is_multi_line <- map_lgl(
88+
pd_by_line,
89+
~ any(.x$multi_line > 0L, na.rm = TRUE)
90+
)
8891
if (any(pd_is_multi_line)) {
8992
return(FALSE)
9093
}
@@ -98,9 +101,9 @@ token_is_on_aligned_line <- function(pd_flat) {
98101
alignment_ensure_trailing_comma()
99102
# now, pd only contains arguments separated by values, ideal for iterating
100103
# over columns.
101-
102104
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
103105
previous_line <- 0
106+
current_col <- 0
104107
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
105108
for (column in seq2(1, max(n_cols))) {
106109
by_line <- alignment_serialize_column(pd_by_line, column) %>%
@@ -109,33 +112,47 @@ token_is_on_aligned_line <- function(pd_flat) {
109112
trimws(which = "right")
110113
# check 1: match by comma
111114
# might have fewer lines in subsequent columns.
112-
current_col <- nchar(by_line)
115+
max_previous_col <- max(current_col)
116+
117+
# first col has no leading ,
118+
current_col <- nchar(by_line) - as.integer(column > 1)
113119
if (column > 1) {
114-
previous_line <- previous_line[intersect(names(previous_line), names(by_line))]
120+
previous_line <- previous_line[
121+
intersect(names(previous_line), names(by_line))
122+
]
115123
# must add previous columns, as first column might not align
116124
current_col <- current_col + previous_line
117125
}
118126

119127
is_aligned <- length(unique(current_col)) == 1L
120-
if (!is_aligned) {
128+
if (!is_aligned || length(current_col) < 2) {
121129
# check 2: left aligned after ,
122-
current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*.*$", "\\1", by_line, perl = TRUE))
130+
current_col <- "^(,[\\s\\t]*)[^ ]*.*$" %>%
131+
gsub("\\1", by_line, perl = TRUE) %>%
132+
nchar() %>%
133+
magrittr::subtract(1)
123134

124135
if (column > 1) {
125136
# must add previous columns, as first column might not align
126-
current_col <- current_col + previous_line
137+
current_col <- previous_line + current_col
127138
}
128-
is_aligned <- length(unique(current_col)) == 1L
139+
if (length(current_col) > 1) {
140+
is_aligned <- length(unique(current_col)) == 1L
141+
} else {
142+
is_aligned <- current_col - max_previous_col == 1
143+
current_col <- max_previous_col + current_col
144+
}
145+
129146
if (is_aligned) {
130147
# if left aligned after ,
131148
start_eval <- 2
132149
}
133150
}
134151
if (is_aligned) {
135-
previous_line <- previous_line + nchar(by_line)
152+
previous_line <- current_col
136153
next
137154
}
138-
# check 3: match by = (no extra spaces around it allowed.)
155+
# check 3: match by = (no extra spaces around it allowed)
139156
# match left aligned after =
140157
start_after_eq <- regexpr("= [^ ]", by_line)
141158
names(start_after_eq) <- names(by_line)
@@ -145,14 +162,17 @@ token_is_on_aligned_line <- function(pd_flat) {
145162
if (length(start_after_eq) == 0) {
146163
return(FALSE)
147164
}
148-
# when match via comma unsuccessful, matching by = must yield at least one =
165+
# when match via , unsuccessful, matching by = must yield at least one =
149166
if (column == 1) {
150167
current_col <- start_after_eq
151168
} else {
152169
current_col <- start_after_eq +
153170
previous_line[intersect(names(previous_line), names(start_after_eq))]
154171
}
155-
is_aligned <- length(unique(current_col)) == 1 && length(start_after_eq) > 1
172+
is_aligned <- all(
173+
length(unique(current_col)) == 1,
174+
length(start_after_eq) > 1
175+
)
156176
if (!is_aligned) {
157177
return(FALSE)
158178
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
c(
2+
"x", "z",
3+
"cgjhg", "thi", "z"
4+
)
5+
6+
7+
c(
8+
"x", "z",
9+
"cgjhg", "thi", "z"
10+
)
11+
12+
13+
c(
14+
"x", "y", "z", "m", "n", "o", "p",
15+
"c", "d"
16+
)

tests/testthat/alignment/cols-with-one-row-in_tree

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
c(
2+
"x", "z",
3+
"cgjhg", "thi", "z"
4+
)
5+
6+
7+
c(
8+
"x", "z",
9+
"cgjhg", "thi", "z"
10+
)
11+
12+
13+
c(
14+
"x", "y", "z", "m", "n", "o", "p",
15+
"c", "d"
16+
)

tests/testthat/alignment/named-in.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ call(
5757
# algorithm: aligned. human: aligned.
5858
call(
5959
x = 1,
60-
xy = 2, n = 33, z = "333"
60+
xy = 2, n = 33, z = "333"
6161
)
6262

6363
# algorithm: aligned. human: aligned.

tests/testthat/alignment/named-in_tree

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/alignment/named-out.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ call(
5757
# algorithm: aligned. human: aligned.
5858
call(
5959
x = 1,
60-
xy = 2, n = 33, z = "333"
60+
xy = 2, n = 33, z = "333"
6161
)
6262

6363
# algorithm: aligned. human: aligned.

0 commit comments

Comments
 (0)