Skip to content

Commit baf7dbf

Browse files
better functional capsuling
1 parent 29dd6ef commit baf7dbf

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

R/roxygen-examples-add-remove.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@ remove_roxygen_header <- function(text) {
3434

3535
#' Add the roxygen mask to code
3636
#'
37+
#' This function compares `text` with `initial_text` to make sure a mask is only
38+
#' added to roxygen comments, not ordinary comments
3739
#' @param text Character vector with code.
3840
#' @param example_type Either 'examples' or 'examplesIf'.
3941
#' @keywords internal
4042
#' @importFrom purrr map2_chr
41-
add_roxygen_mask <- function(text, example_type) {
43+
add_roxygen_mask <- function(text, initial_text, example_type) {
4244
space <- ifelse(text == "", "", " ")
43-
c(
45+
out <- c(
4446
paste0("#' @", example_type, space[1], text[1]),
4547
map2_chr(space[-1], text[-1], ~ paste0("#'", .x, .y))
4648
)
49+
50+
ordinary_comment <- grep("^#[^']", initial_text, value = TRUE)
51+
if (length(ordinary_comment) == 0L) {
52+
return(out)
53+
}
54+
without_mask <- remove_roxygen_mask(out)
55+
for (idx in seq_along(ordinary_comment)) {
56+
to_replace <- which(ordinary_comment[idx] == without_mask)[1]
57+
out[to_replace] <- ordinary_comment[idx]
58+
without_mask[to_replace] <- NA
59+
}
60+
out
4761
}

R/roxygen-examples.R

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,12 @@ style_roxygen_code_example <- function(example, transformers, base_indention) {
2525
style_roxygen_code_example_one <- function(example_one, transformers, base_indention) {
2626
bare <- parse_roxygen(example_one)
2727
one_dont <- split(bare$text, factor(cumsum(bare$text %in% dont_keywords())))
28-
styled <- map(one_dont, style_roxygen_code_example_segment,
28+
map(one_dont, style_roxygen_code_example_segment,
2929
transformers = transformers,
3030
base_indention = base_indention
3131
) %>%
3232
flatten_chr() %>%
33-
add_roxygen_mask(bare$example_type)
34-
35-
ordinary_comment <- grep("^#[^']", example_one, value = TRUE)
36-
if (length(ordinary_comment) == 0L) {
37-
return(styled)
38-
}
39-
without_mask <- remove_roxygen_mask(styled)
40-
for (idx in seq_along(ordinary_comment)) {
41-
to_replace <- which(ordinary_comment[idx] == without_mask)[1]
42-
styled[to_replace] <- ordinary_comment[idx]
43-
without_mask[to_replace] <- NA
44-
}
45-
styled
33+
add_roxygen_mask(example_one, bare$example_type)
4634
}
4735

4836
#' Style a roxygen code example segment

man/add_roxygen_mask.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)