Skip to content

Commit e46ca2e

Browse files
style
1 parent 3d33d16 commit e46ca2e

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

R/detect-alignment.R

Lines changed: 20 additions & 7 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
}
@@ -110,17 +113,24 @@ token_is_on_aligned_line <- function(pd_flat) {
110113
# check 1: match by comma
111114
# might have fewer lines in subsequent columns.
112115
max_previous_col <- max(current_col)
113-
current_col <- nchar(by_line) - as.integer(column > 1) # fir col has no leading ,
116+
117+
# first col has no leading ,
118+
current_col <- nchar(by_line) - as.integer(column > 1)
114119
if (column > 1) {
115-
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+
]
116123
# must add previous columns, as first column might not align
117124
current_col <- current_col + previous_line
118125
}
119126

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

125135
if (column > 1) {
126136
# must add previous columns, as first column might not align
@@ -142,7 +152,7 @@ token_is_on_aligned_line <- function(pd_flat) {
142152
previous_line <- current_col
143153
next
144154
}
145-
# check 3: match by = (no extra spaces around it allowed.)
155+
# check 3: match by = (no extra spaces around it allowed)
146156
# match left aligned after =
147157
start_after_eq <- regexpr("= [^ ]", by_line)
148158
names(start_after_eq) <- names(by_line)
@@ -152,14 +162,17 @@ token_is_on_aligned_line <- function(pd_flat) {
152162
if (length(start_after_eq) == 0) {
153163
return(FALSE)
154164
}
155-
# when match via comma unsuccessful, matching by = must yield at least one =
165+
# when match via , unsuccessful, matching by = must yield at least one =
156166
if (column == 1) {
157167
current_col <- start_after_eq
158168
} else {
159169
current_col <- start_after_eq +
160170
previous_line[intersect(names(previous_line), names(start_after_eq))]
161171
}
162-
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+
)
163176
if (!is_aligned) {
164177
return(FALSE)
165178
}

0 commit comments

Comments
 (0)