Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

drop_cached_children

Drop all children of a top level expression that are cached


Description

Note that we do not cache top-level comments. Because package code has a lot of roxygen comments and each of them is a top level expression, checking is very expensive. More expensive than styling, because comments are always terminals.

Usage

drop_cached_children(pd)

Arguments

pd

A top-level nest.

Details

Because we process in blocks of expressions for speed, a cached expression will always end up in a block that won't be styled again (usual case), unless it's on a line where multiple expressions sit and at least one is not styled (exception).

usual case: All other expressions in a block are cached

Cached expressions don't need to be transformed with transformers in parse_transform_serialize_r_block(), we simply return text for the top level token. For that reason, the nested parse table can, at the rows where these expressions are located, be shallow, i.e. it does not have to contain a child, because it will neither be transformed nor serialized anytime. This function drops all associated tokens except the top-level token for such expressions, which will result in large speed improvements in compute_parse_data_nested() because nesting is expensive and will not be done for cached expressions.

exception: Not all other expressions in a block are cached

As described in cache_find_block(), expressions on the same line are always put into one block. If any element of a block is not cached, the block will be styled as a whole. If the parse table was made shallow (and the top level) expression is still marked as non-terminal, text will never be used in the transformation process and eventually lost. Hence, we must change the top level expression to a terminal. It will act like a comment in the sense that it is a fixed text.

Because for the usual case, it does not even matter if the cached expression is a terminal or not (because it is not processed), we can safely set terminal = TRUE in general.

Implementation

Because the structure of the parse table is not always "top-level expression first, then children", this function creates a temporary parse table that has this property and then extract the ids and subset the original parse table so it is shallow in the right places.


styler

Non-Invasive Pretty Printing of R Code

v1.4.1
MIT + file LICENSE
Authors
Kirill Müller [aut], Lorenz Walthert [cre, aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.