@@ -25,11 +25,7 @@ flatten_operators <- function(pd_nested) {
25
25
# ' @keywords internal
26
26
flatten_operators_one <- function (pd_nested ) {
27
27
pd_token_left <- c(special_token , " PIPE" , math_token , " '$'" )
28
- pd_token_right <- c(
29
- special_token , " PIPE" , " LEFT_ASSIGN" ,
30
- if (parser_version_get() > 1L ) " EQ_ASSIGN" ,
31
- " '+'" , " '-'" , " '~'"
32
- )
28
+ pd_token_right <- c(special_token , " PIPE" , " LEFT_ASSIGN" , " EQ_ASSIGN" , " '+'" , " '-'" , " '~'" )
33
29
pd_nested %> %
34
30
flatten_pd(pd_token_left , left = TRUE ) %> %
35
31
flatten_pd(pd_token_right , left = FALSE )
@@ -106,143 +102,3 @@ wrap_expr_in_expr <- function(pd) {
106
102
indents = pd $ indent [1L ]
107
103
)
108
104
}
109
-
110
-
111
- # ____________________________________________________________________________
112
- # Relocate EQ_ASSIGN ####
113
-
114
- # ' Relocate the expressions containing the token `EQ_ASSIGN` within the nested
115
- # ' parse table
116
- # '
117
- # ' Although syntactically identical, [utils::getParseData()] does not produce
118
- # ' the same hierarchy of the parse table (parent and id relationship) for `<-`
119
- # ' and `=` (See 'Examples').
120
- # ' This is considered to be a bug and causes problems because the
121
- # ' nested parse table constructed with [compute_parse_data_nested()] is not
122
- # ' consistent if `EQ_ASSIGN` occurs in the expression to style. In particular,
123
- # ' `EQ_ASSIGN` and the tokens to its left and right are located too high up in
124
- # ' the hierarchy of the nested parse data. Hence, this function wraps the
125
- # ' sub-expression into an expression, similar to [wrap_expr_in_curly()].
126
- # ' Since `wrap_expr_in_curly()` is called from within a visitor
127
- # ' (and `relocate_eq_assign()` not), we need to
128
- # ' wrap the the implementation [relocate_eq_assign_nest()] that operates on
129
- # ' *nests* into a visitor call.
130
- # ' @param pd A parse table.
131
- # ' @examples
132
- # ' styler:::get_parse_data("a <- b <- 3")
133
- # ' styler:::get_parse_data("a = b = 3")
134
- # ' styler:::get_parse_data(
135
- # ' "x = 5
136
- # ' if(x >= 5)
137
- # ' y = TRUE else
138
- # ' y = FALSE",
139
- # ' )
140
- # ' styler:::get_parse_data(
141
- # ' "x <- 5
142
- # ' if(x >= 5)
143
- # ' y <- TRUE else
144
- # ' y <- FALSE",
145
- # ' )
146
- # ' @keywords internal
147
- relocate_eq_assign <- function (pd ) {
148
- if (parser_version_get() < 2L ) {
149
- post_visit_one(pd , relocate_eq_assign_nest )
150
- } else {
151
- pd
152
- }
153
- }
154
-
155
-
156
- # ' Relocate all assignment expressions that contain `EQ_ASSIGN` within a *nest*
157
- # '
158
- # ' Implements the relocation of an `EQ_ASSIGN` and associated tokens
159
- # ' within a *nest* (nested parse table at one level of nesting).
160
- # ' Note that one assignment expression (such as "a = b = c") can include
161
- # ' multiple assignment operators, an assignment involves just one assignment
162
- # ' operator.
163
- # ' For the relocation of assignment expressions that contain `EQ_ASSIGN` within
164
- # ' a *nest*, we need to first find the expressions that contain `=` and then
165
- # ' split the *nest* into parse tables each containing one such assignment
166
- # ' expression and then relocate each of them separately.
167
- # ' We can't do all of them together because:
168
- # '
169
- # ' * An assignment can contain more than just three tokens, e.g. (a <- b <- c).
170
- # ' * Two assignments can be in the same nest although they don't belong to the
171
- # ' same assignment (if-else statement).
172
- # '
173
- # ' Please refer to the section 'Examples' in [relocate_eq_assign()] for details.
174
- # ' @param pd A parse table.
175
- # '
176
- # ' @keywords internal
177
- relocate_eq_assign_nest <- function (pd ) {
178
- idx_eq_assign <- which(pd $ token == " EQ_ASSIGN" )
179
- if (length(idx_eq_assign ) > 0L ) {
180
- block_id <- find_block_id(pd )
181
- blocks <- vec_split(pd , block_id )
182
- pd <- map_dfr(blocks [[2L ]], relocate_eq_assign_one )
183
- }
184
- pd
185
- }
186
-
187
- # ' Find the block to which a token belongs
188
- # '
189
- # ' Two assignment tokens `EQ_ASSIGN` belong to the same block if they are not
190
- # ' separated by more than one token. Token between `EQ_ASSIGN` tokens belong
191
- # ' to the `EQ_ASSIGN` token occurring before them, except the token right before
192
- # ' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. Note that this
193
- # ' notion is unrelated to the column *block* in the parse table, which is used
194
- # ' to [parse_transform_serialize_r()] code blocks and leave out the ones that
195
- # ' are cached.
196
- # ' @param pd A parse table.
197
- # ' @keywords internal
198
- find_block_id <- function (pd ) {
199
- idx_eq_assign <- which(pd $ token == " EQ_ASSIGN" )
200
- eq_belongs_to_block <- c(0L , diff(idx_eq_assign ) > 2L )
201
-
202
- empty_seq <- rep(0L , nrow(pd ))
203
- empty_seq [idx_eq_assign - 1L ] <- eq_belongs_to_block
204
- block_id <- cumsum(empty_seq )
205
- block_id
206
- }
207
-
208
- # ' Relocate an assignment expression
209
- # '
210
- # ' Relocates an assignment expression within a parse table containing one
211
- # ' assignment expression. Note that one assignment can include multiple
212
- # ' assignment operators such as "a = b = c".
213
- # ' @param pd A parse table with one assignment expression to relocate.
214
- # ' @keywords internal
215
- relocate_eq_assign_one <- function (pd ) {
216
- idx_eq_assign <- which(pd $ token == " EQ_ASSIGN" )
217
- eq_ind <- seq2(idx_eq_assign [1L ] - 1L , last(idx_eq_assign ) + 1L )
218
- # initialize because wrap_expr_in_expr -> create_tokens -> requires it
219
- pd $ indent <- 0L
220
- eq_expr <- vec_slice(pd , eq_ind ) %> %
221
- wrap_expr_in_expr() %> %
222
- add_line_col_to_wrapped_expr() %> %
223
- remove_attributes(c(
224
- " multi_line" , " indention_ref_pos_id" ,
225
- " newlines" , " indent" , " spaces" , " lag_newlines"
226
- ))
227
- eq_expr $ id <- NA
228
- eq_expr $ parent <- NA
229
- pd $ indent <- NULL
230
- non_eq_expr <- vec_slice(pd , - eq_ind )
231
- pd <- vec_rbind(eq_expr , non_eq_expr ) %> %
232
- arrange_pos_id()
233
- pd
234
- }
235
-
236
- # ' Adds line and col information to an expression from its child
237
- # '
238
- # ' @param pd A parse table.
239
-
240
- # ' @keywords internal
241
- add_line_col_to_wrapped_expr <- function (pd ) {
242
- if (nrow(pd ) > 1L ) abort(" pd must be a wrapped expression that has one row." )
243
- pd $ line1 <- pd $ child [[1L ]]$ line1 [1L ]
244
- pd $ line2 <- last(pd $ child [[1L ]]$ line2 )
245
- pd $ col1 <- pd $ child [[1L ]]$ col1 [1L ]
246
- pd $ col2 <- last(pd $ child [[1L ]]$ col2 )
247
- pd
248
- }
0 commit comments