@@ -39,7 +39,7 @@ add_cache_block <- function(pd_nested) {
3939 if (cache_is_activated()) {
4040 pd_nested $ block <- cache_find_block(pd_nested )
4141 } else {
42- pd_nested $ block <- rep(1 , nrow (pd_nested ))
42+ pd_nested $ block <- rep(1 , length (pd_nested $ block ))
4343 }
4444 pd_nested
4545}
@@ -101,16 +101,27 @@ drop_cached_children <- function(pd) {
101101 } else {
102102 pd
103103 }
104-
105104}
106105
106+ # ' Find the pos ids to keep
107+ # '
108+ # ' To make a parse table shallow, we must know which ids to keep.
109+ # ' `split(cumsum(pd_parent_first$parent < 1))` above puts comments with negative
110+ # ' parents in the same block as proceeding expressions. `find_pos_id_to_keep()`
111+ # ' must hence alyways keep comments. We did not use
112+ # ' `split(cumsum(pd_parent_first$parent < 1))` because then every comment is an
113+ # ' expression on its own and processing takes much longer for typical roxygen
114+ # ' annotated code
115+ # ' @param pd A temporary top level nest where the first expression is always a
116+ # ' top level expression, potentially cached.
117+ # ' @keywords internal
107118find_pos_id_to_keep <- function (pd ) {
108- if (pd $ is_cached [1 ]) {
109- pd $ pos_id [1 ]
110- } else {
111- pd $ pos_id
112- }
119+ if (pd $ is_cached [1 ]) {
120+ pd $ pos_id [c(TRUE , pd [- 1L , " token" ] == " COMMENT" )]
121+ } else {
122+ pd $ pos_id
113123 }
124+ }
114125
115126
116127# ' Turn off styling for parts of the code
@@ -235,9 +246,10 @@ add_terminal_token_before <- function(pd_flat) {
235246add_attributes_caching <- function (pd_flat , transformers ) {
236247 pd_flat $ block <- pd_flat $ is_cached <- rep(NA , nrow(pd_flat ))
237248 if (cache_is_activated()) {
238- pd_flat $ is_cached [pd_flat $ parent == 0 ] <- map_lgl(
249+ is_parent <- pd_flat $ parent == 0
250+ pd_flat $ is_cached [is_parent ] <- map_lgl(
239251 pd_flat $ text [pd_flat $ parent == 0 ],
240- is_cached , transformers , cache_dir_default()
252+ is_cached , transformers
241253 )
242254 is_comment <- pd_flat $ token == " COMMENT"
243255 pd_flat $ is_cached [is_comment ] <- rep(FALSE , sum(is_comment ))
0 commit comments