Skip to content

Commit 35d8f6a

Browse files
somehow simplify and extend
This needs refactoring
1 parent 2242bf9 commit 35d8f6a

File tree

7 files changed

+121
-9
lines changed

7 files changed

+121
-9
lines changed

R/detect-alignment.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ token_is_on_aligned_line <- function(pd_flat) {
9898
alignment_ensure_trailing_comma()
9999
# now, pd only contains arguments separated by values, ideal for iterating
100100
# over columns.
101-
102101
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
103102
previous_line <- 0
103+
current_col <- 0
104104
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
105105
for (column in seq2(1, max(n_cols))) {
106106
by_line <- alignment_serialize_column(pd_by_line, column) %>%
@@ -109,30 +109,37 @@ token_is_on_aligned_line <- function(pd_flat) {
109109
trimws(which = "right")
110110
# check 1: match by comma
111111
# might have fewer lines in subsequent columns.
112-
current_col <- nchar(by_line)
112+
max_previous_col <- max(current_col)
113+
current_col <- nchar(by_line) - as.integer(column > 1) # fir col has no leading ,
113114
if (column > 1) {
114115
previous_line <- previous_line[intersect(names(previous_line), names(by_line))]
115116
# must add previous columns, as first column might not align
116117
current_col <- current_col + previous_line
117118
}
118119

119120
is_aligned <- length(unique(current_col)) == 1L
120-
if (!is_aligned) {
121+
if (!is_aligned || length(current_col) < 2) {
121122
# check 2: left aligned after ,
122123
current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*.*$", "\\1", by_line, perl = TRUE)) - 1
123124

124125
if (column > 1) {
125126
# must add previous columns, as first column might not align
126127
current_col <- previous_line + current_col
127128
}
128-
is_aligned <- length(unique(current_col)) == 1L
129+
if (length(current_col) > 1) {
130+
is_aligned <- length(unique(current_col)) == 1L
131+
} else {
132+
is_aligned <- current_col - max_previous_col == 1
133+
current_col <- max_previous_col + current_col
134+
}
135+
129136
if (is_aligned) {
130137
# if left aligned after ,
131138
start_eval <- 2
132139
}
133140
}
134141
if (is_aligned) {
135-
previous_line <- previous_line + nchar(by_line)
142+
previous_line <- current_col
136143
next
137144
}
138145
# check 3: match by = (no extra spaces around it allowed.)
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)