Skip to content

Commit

Permalink
Merge pull request #880 from r-lib/f-empty-example-lines
Browse files Browse the repository at this point in the history
Keep at most one empty line at the end of each example
  • Loading branch information
lorenzwalthert authored Dec 24, 2021
2 parents 82e3db8 + 1744cb3 commit be6449c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
detected (#881).
* Add vignette on distributing style guide (#846, #861).
* ensure a trailing blank line also if the input is cached (#867).
* Preserve trailing blank line in roxygen examples to simplify concatenation of examples (#880).
* Fix argument name `filetype` in Example for `style_dir()` (#855).
* An error is now thrown on styling if input unicode characters can't be
correctly parsed for Windows and R < 4.2 (#883).
Expand Down
10 changes: 8 additions & 2 deletions R/roxygen-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ style_roxygen_example_snippet <- function(code_snippet,
mask <- decomposed$mask
}
code_snippet <- post_parse_roxygen(code_snippet)
append_empty <- !is_dont &&
length(code_snippet) > 1L &&
last(code_snippet) == ""

cache_is_active <- cache_is_activated()
is_cached <- is_cached(
Expand All @@ -116,10 +119,13 @@ style_roxygen_example_snippet <- function(code_snippet,
parse_transform_serialize_r(transformers,
base_indention = base_indention, warn_empty = FALSE
)
} else {
code_snippet <- ensure_last_n_empty(code_snippet, n = 0)
}

code_snippet <- ensure_last_n_empty(
code_snippet,
n = ifelse(append_empty, 1L, 0L)
)

if (!is_cached && cache_is_active) {
cache_write(
code_snippet, transformers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
#' op<- pd_flat$token %in% "}"
#' }
#' op <-pd_flat$token %in% "'['"
#' }
#' \donttest{
#' op <- pd_flat$token %in% "']'"
#' }
Expand All @@ -74,4 +73,5 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
#' }
#' \donttest{
#' op <- pd_flat$token %in%"]"
#' }
NULL

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' }
#' # before this comment is a left-over space
#' another_function <- function() NULL
#'
#' @examples
#' my_fun <- function() {
#' print("hello world!")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' this
#'
#' mey
#' @examples
#' 2 + 1
#'
NULL


#' this
#'
#' mey
#' @examples
#' 2 + 1
#'
#'
#'
#'
#'
#'
#'
NULL


#' this
#'
#' mey
#' @examples
#' 2 + 1
NULL

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' this
#'
#' mey
#' @examples
#' 2 + 1
#'
NULL


#' this
#'
#' mey
#' @examples
#' 2 + 1
#'
NULL


#' this
#'
#' mey
#' @examples
#' 2 + 1
NULL
5 changes: 5 additions & 0 deletions tests/testthat/test-roxygen-examples-complete.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,9 @@ test_that("analogous to test-roxygen-examples-complete", {
"roxygen-examples-complete", "^25",
transformer = style_text
), NA)

expect_warning(test_collection(
"roxygen-examples-complete", "^26",
transformer = style_text
), NA)
})

0 comments on commit be6449c

Please sign in to comment.