@@ -250,34 +250,17 @@ parse_transform_serialize_r <- function(text,
250250 text_out
251251}
252252
253- # ' Removes the transformers if tokens in x
254- # ' @param transformers The style full style guide, i.e. the result of
255- # ' [create_style_guide()].
256- # ' @param token Named character vector: Names are rules and values are the token
257- # ' that trigger are required to be absent to trigger a removal.
258- # ' @param scope The low-level scope, e.g. 'token'.
259- # ' @param code tokenized code for which we check if `token` is in them.
260- # ' @importFrom purrr walk
261- transformers_subset_impl <- function (transformers , token , scope , code ) {
262- transformer_names <- names(token )
263- walk(seq_along(token ), function (i ) {
264- if (! any(token [[i ]] %in% code )) {
265- transformers [[scope ]][[transformer_names [i ]]] <- NULL
266- }
267- })
268- transformers
269- }
270253
271254# ' Remove transformers that are not needed
272255# '
273- # ' For every transformer, at least one token must be given to make subsetting.
274- # ' active.
256+ # ' The goal is to speed up styling by removing all rules that are only
257+ # ' applicable in contexts that don't occur often, e.g. for most code, we don't
258+ # ' expect ";" to be in it, so we don't need to apply [resolve_semicolon()] on
259+ # ' every *nest*.
275260# ' @param text Text to parse. Can also be the column `text` of the output of
276261# ' [compute_parse_data_nested()], where each element is a token (instead of a
277262# ' line).
278- # ' @return
279- # ' Returns `transformers`, but stripped away those rules that are not used when
280- # ' styling `text`.
263+ # ' @param transformers the transformers.
281264# ' @keywords internal
282265transformers_subset <- function (text , transformers ) {
283266 is_colon <- text == " ;"
@@ -286,12 +269,16 @@ transformers_subset <- function(text, transformers) {
286269 # here since text is output of compute_parse_data_nested.
287270 text <- c(text [! is_colon ], " 1;" )
288271 }
289- purrr :: reduce2(
290- transformers $ subset_transformers ,
291- names(transformers $ subset_transformers ),
292- transformers_subset_impl , unique(tokenize(text )$ token ),
293- .init = transformers
294- )
272+ token <- unique(tokenize(text )$ token )
273+ for (scope in c(" line_break" , " space" , " token" , " indention" )) {
274+ rules <- transformers $ subset_transformers [[scope ]]
275+ for (rule in names(rules )) {
276+ if (! any(rules [[rule ]] %in% token )) {
277+ transformers [[scope ]][rule ] <- NULL
278+ }
279+ }
280+ }
281+ transformers
295282}
296283
297284# ' Apply transformers to a parse table
0 commit comments