@@ -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 }
0 commit comments