diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6c1b610f5..a7fc7fb48 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ default_language_version: repos: - repo: https://github.com/lorenzwalthert/precommit - rev: b2a735b + rev: v0.2.2.9015 hooks: - id: style-files args: ['--ignore-start="^# styler: on$"', '--ignore-stop="^# styler: off$"'] @@ -84,7 +84,7 @@ repos: tests/testthat/.*\.R(md)? )$ - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.1.0 + rev: v4.2.0 hooks: - id: check-added-large-files args: ['--maxkb=200'] @@ -99,7 +99,7 @@ repos: tests/testthat/reference-objects/.*| )$ - repo: https://github.com/lorenzwalthert/gitignore-tidy - rev: d3e947d61fba8c6adde2707d3baa6ae36b989d5b + rev: 9dd648a2cbce0c0ce09255695b63ea4823699bec hooks: - id: tidy-gitignore - repo: local diff --git a/NEWS.md b/NEWS.md index 16a7a6a8f..7696690ab 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,10 @@ * If there are only empty lines in a code chunk, they are all removed (#936). * `{` is not put on a new line after `=` and in `function() {` for some edge cases (#939). +* Parsing of {roxygen2} example comments now also works for edge cases when + there is no literal code immediately following after the end of the example + section (#940). + # styler 1.7.0 diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R index 941414b14..dfa021d92 100644 --- a/R/roxygen-examples-find.R +++ b/R/roxygen-examples-find.R @@ -11,7 +11,10 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) { if (length(starts) < 1L) { return(integer()) } - stop_candidates <- grep("(^[^#]|^#'[\\s\t]*@)", text, perl = TRUE) + stop_candidates <- which(or( + grepl("(^[^#]|^#'[\\s\t]*@)", text, perl = TRUE), + grepl("^ *\t*$", text) & grepl("^#' *", lead(text)) + )) stops <- map(starts, match_stop_to_start, stop_candidates) %>% flatten_int() if (length(stops) < 1L) { diff --git a/R/roxygen-examples.R b/R/roxygen-examples.R index 8c3698876..b21ed4da9 100644 --- a/R/roxygen-examples.R +++ b/R/roxygen-examples.R @@ -9,7 +9,7 @@ #' @importFrom purrr map flatten_chr #' @keywords internal style_roxygen_code_example <- function(example, transformers, base_indention) { - example <- split(example, cumsum(grepl("^#' *@examples", example))) # TODO can this handle @examples 1 + example <- split(example, cumsum(grepl("^#' *@examples", example))) purrr::map( example, style_roxygen_code_example_one, transformers = transformers, base_indention = base_indention diff --git a/R/transform-files.R b/R/transform-files.R index 764c90f91..f4983cfae 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -111,9 +111,9 @@ make_transformer <- function(transformers, ) %>% when( include_roxygen_examples ~ - parse_transform_serialize_roxygen(., - transformers = transformers, base_indention = base_indention - ), + parse_transform_serialize_roxygen(., + transformers = transformers, base_indention = base_indention + ), ~. ) if (should_use_cache) { diff --git a/tests/testthat/roxygen-examples-complete/17-two-no-non-comment-out.R b/tests/testthat/roxygen-examples-complete/17-two-no-non-comment-out.R index e5063feec..21f1dd477 100644 --- a/tests/testthat/roxygen-examples-complete/17-two-no-non-comment-out.R +++ b/tests/testthat/roxygen-examples-complete/17-two-no-non-comment-out.R @@ -5,6 +5,7 @@ #' # before this comment is a left-over space #' another_function <- function() NULL #' + #' @examples #' my_fun <- function() { #' print("hello world!") diff --git a/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-in.R b/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-in.R new file mode 100644 index 000000000..9e26e054b --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-in.R @@ -0,0 +1,13 @@ +#' This +#' +#' +#' is stuff +#' +#' @examples +#' 1+1 + + +#' nolint start +#' @examplesIf long_condition_line +#' 32 / 3 +#' nolint end diff --git a/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-out.R b/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-out.R new file mode 100644 index 000000000..d82d10a2c --- /dev/null +++ b/tests/testthat/roxygen-examples-complete/27-no-code-block-after-example-out.R @@ -0,0 +1,13 @@ +#' This +#' +#' +#' is stuff +#' +#' @examples +#' 1 + 1 +#' + +#' nolint start +#' @examplesIf long_condition_line +#' 32 / 3 +#' nolint end diff --git a/tests/testthat/test-roxygen-examples-complete.R b/tests/testthat/test-roxygen-examples-complete.R index 861feefbe..dba323734 100644 --- a/tests/testthat/test-roxygen-examples-complete.R +++ b/tests/testthat/test-roxygen-examples-complete.R @@ -136,4 +136,9 @@ test_that("analogous to test-roxygen-examples-complete", { "roxygen-examples-complete", "^26", transformer = style_text ), NA) + + expect_warning(test_collection( + "roxygen-examples-complete", "^27", + transformer = style_text + ), NA) })