Skip to content

Commit 5957df1

Browse files
Merge pull request #743 from r-lib/b-742-examples-if
- Support for @examplesIf roxygen tag (#743).
2 parents 179a85a + c65e562 commit 5957df1

33 files changed

+533
-73
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
integrations`, minor other consistency edits (#741, #643, #618, #614, #677,
3636
#651, #667, #672, #687).
3737

38+
- `@exampleIsf` roxygen tag for conditional examples is now supported (#743).
39+
3840
- The environment variable `save_after_styling` is deprecated in favor of the R
3941
option `styler.save_after_styling` to control if a file is saved after styling
4042
with the RStudio Addin. Note than in RStudio >= 1.3.0, you can auto-save edits

R/roxygen-examples-add-remove.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ remove_roxygen_mask <- function(text) {
2929
#' #' @examples c(1, 2)
3030
#' @keywords internal
3131
remove_roxygen_header <- function(text) {
32-
text <- gsub("^\\s*@examples\\s*", "", text, perl = TRUE)
33-
starts_with_blank <- text[1] == "\n"
34-
c(text[1][!starts_with_blank], text[-1])
32+
gsub("^[\\s\t]*@examples(If)?(\\s|\t)*", "", text, perl = TRUE)
3533
}
3634

35+
#' Add the roxygen mask to code
36+
#'
37+
#' @param text Character vector with code.
38+
#' @param example_type Either 'examples' or 'examplesIf'.
39+
#' @keywords internal
3740
#' @importFrom purrr map2_chr
38-
add_roxygen_mask <- function(text) {
41+
add_roxygen_mask <- function(text, example_type) {
3942
space <- ifelse(text == "", "", " ")
4043
c(
41-
paste0("#' @examples", space[1], text[1]),
44+
paste0("#' @", example_type, space[1], text[1]),
4245
map2_chr(space[-1], text[-1], ~ paste0("#'", .x, .y))
4346
)
4447
}

R/roxygen-examples-find.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#' @importFrom rlang seq2
88
#' @keywords internal
99
identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
10-
starts <- grep("^#'\\s*@examples", text, perl = TRUE)
10+
starts <- grep("^#'(\\s|\t)*@examples(If\\s|\\s|\t|$)", text, perl = TRUE)
1111
if (length(starts) < 1L) {
1212
return(integer())
1313
}
14-
stop_candidates <- grep("^[^#]|^#'\\s*@", text, perl = TRUE)
14+
stop_candidates <- grep("(^[^#]|^#'[\\s\t]*@)", text, perl = TRUE)
1515
stops <- map(starts, match_stop_to_start, stop_candidates) %>%
1616
flatten_int()
1717
if (length(stops) < 1L) {

R/roxygen-examples-parse.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#' ))
1616
#' @keywords internal
1717
parse_roxygen <- function(roxygen) {
18-
connection <- textConnection(emulate_rd(roxygen))
18+
emulated <- emulate_rd(roxygen)
19+
connection <- textConnection(emulated$text)
1920
had_warning <- FALSE
2021
parsed <- withCallingHandlers(
2122
{
@@ -32,7 +33,7 @@ parse_roxygen <- function(roxygen) {
3233
}
3334
)
3435
close(connection)
35-
parsed
36+
list(text = parsed, example_type = emulated$example_type)
3637
}
3738

3839
#' Fix [tools::parse_Rd()] output
@@ -123,22 +124,30 @@ roxygen_remove_extra_brace <- function(parsed) {
123124
#' `remove_roxygen_mask()` when there are no characters to escape.
124125
#' @keywords internal
125126
emulate_rd <- function(roxygen) {
127+
example_type <- gsub("^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "examples\\2", roxygen[1])
126128
if (needs_rd_emulation(roxygen)) {
127129
roxygen <- c(
128-
"#' Example", "#' @examples",
129-
gsub("^#'\\s*@examples\\s*(.*)", "#' \\1", roxygen),
130+
"#' Example",
131+
gsub("^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "#' @examples \\4", roxygen),
130132
"x <- 1"
131133
)
132-
133-
roxygen2::roc_proc_text(
134+
text <- roxygen2::roc_proc_text(
134135
roxygen2::rd_roclet(),
135136
paste(roxygen, collapse = "\n")
136137
)[[1]]$get_section("examples") %>%
137138
as.character() %>%
138139
.[-1]
140+
text <- c(
141+
if (grepl("^#'(\\s|\t)*@examples(\\s|\t)*$", roxygen[2])) "",
142+
text
143+
)
139144
} else {
140-
remove_roxygen_mask(roxygen)
145+
text <- remove_roxygen_mask(roxygen)
141146
}
147+
list(
148+
text = text,
149+
example_type = example_type
150+
)
142151
}
143152

144153
#' Check if rd emulation is required with [roxygen2::roc_proc_text()]

R/roxygen-examples.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ style_roxygen_code_example <- function(example, transformers, base_indention) {
1717
flatten_chr()
1818
}
1919

20-
20+
#' Style a roxygen code example with exactly one `@example` or `@exampleIf`
21+
#' @inheritParams style_roxygen_code_example
22+
#' @param example_one A character vector, one element per line, that contains in
23+
#' total at most one example tag.
24+
#' @keywords internal
2125
style_roxygen_code_example_one <- function(example_one, transformers, base_indention) {
2226
bare <- parse_roxygen(example_one)
23-
one_dont <- split(bare, factor(cumsum(bare %in% dont_keywords())))
27+
one_dont <- split(bare$text, factor(cumsum(bare$text %in% dont_keywords())))
2428
map(one_dont, style_roxygen_code_example_segment,
2529
transformers = transformers,
2630
base_indention = base_indention
2731
) %>%
2832
flatten_chr() %>%
29-
add_roxygen_mask()
33+
add_roxygen_mask(bare$example_type)
3034
}
3135

3236
#' Style a roxygen code example segment

man/add_roxygen_mask.Rd

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

man/style_roxygen_code_example_one.Rd

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

tests/testthat/escaping/basic-escape-in.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ x <- '\001'
3636

3737
"\01"
3838
'\01'
39+
40+
#' things
41+
#'
42+
#' @examplesIf N
43+
#' call("\n")
44+
#' ano("\\.", further = X)
45+
NULL
46+
47+
#' things
48+
#'
49+
#' @examplesIf call("\n")
50+
#' ano("\\.", further = X)
51+
NULL

tests/testthat/escaping/basic-escape-in_tree

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

tests/testthat/escaping/basic-escape-out.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ x <- "\001"
3636

3737
"\0"
3838
"\0"
39+
40+
#' things
41+
#'
42+
#' @examplesIf N
43+
#' call("\n")
44+
#' ano("\\.", further = X)
45+
NULL
46+
47+
#' things
48+
#'
49+
#' @examplesIf call("\n")
50+
#' ano("\\.", further = X)
51+
NULL

0 commit comments

Comments
 (0)