Skip to content

Commit 2b886c4

Browse files
preserve trailing blank line
1 parent 82e3db8 commit 2b886c4

File tree

7 files changed

+99
-2
lines changed

7 files changed

+99
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
detected (#881).
3838
* Add vignette on distributing style guide (#846, #861).
3939
* ensure a trailing blank line also if the input is cached (#867).
40+
* Preserve trailing blank line in roxygen examples (#880).
4041
* Fix argument name `filetype` in Example for `style_dir()` (#855).
4142
* An error is now thrown on styling if input unicode characters can't be
4243
correctly parsed for Windows and R < 4.2 (#883).

R/roxygen-examples.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ style_roxygen_example_snippet <- function(code_snippet,
102102
mask <- decomposed$mask
103103
}
104104
code_snippet <- post_parse_roxygen(code_snippet)
105+
append_empty <- !is_dont &&
106+
length(code_snippet) > 1L &&
107+
last(code_snippet) == ""
105108

106109
cache_is_active <- cache_is_activated()
107110
is_cached <- is_cached(
@@ -116,10 +119,13 @@ style_roxygen_example_snippet <- function(code_snippet,
116119
parse_transform_serialize_r(transformers,
117120
base_indention = base_indention, warn_empty = FALSE
118121
)
119-
} else {
120-
code_snippet <- ensure_last_n_empty(code_snippet, n = 0)
121122
}
122123

124+
code_snippet <- ensure_last_n_empty(
125+
code_snippet,
126+
n = ifelse(append_empty, 1L, 0L)
127+
)
128+
123129
if (!is_cached && cache_is_active) {
124130
cache_write(
125131
code_snippet, transformers,

tests/testthat/roxygen-examples-complete/17-two-no-non-comment-out.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#' }
55
#' # before this comment is a left-over space
66
#' another_function <- function() NULL
7+
#'
78
#' @examples
89
#' my_fun <- function() {
910
#' print("hello world!")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' this
2+
#'
3+
#' mey
4+
#' @examples
5+
#' 2 + 1
6+
#'
7+
NULL
8+
9+
10+
#' this
11+
#'
12+
#' mey
13+
#' @examples
14+
#' 2 + 1
15+
#'
16+
#'
17+
#'
18+
#'
19+
#'
20+
#'
21+
#'
22+
NULL
23+
24+
25+
#' this
26+
#'
27+
#' mey
28+
#' @examples
29+
#' 2 + 1
30+
NULL

tests/testthat/roxygen-examples-complete/26-empty-trailing-lines-in_tree

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' this
2+
#'
3+
#' mey
4+
#' @examples
5+
#' 2 + 1
6+
#'
7+
NULL
8+
9+
10+
#' this
11+
#'
12+
#' mey
13+
#' @examples
14+
#' 2 + 1
15+
#'
16+
NULL
17+
18+
19+
#' this
20+
#'
21+
#' mey
22+
#' @examples
23+
#' 2 + 1
24+
NULL

tests/testthat/test-roxygen-examples-complete.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ test_that("analogous to test-roxygen-examples-complete", {
131131
"roxygen-examples-complete", "^25",
132132
transformer = style_text
133133
), NA)
134+
135+
expect_warning(test_collection(
136+
"roxygen-examples-complete", "^26",
137+
transformer = style_text
138+
), NA)
134139
})

0 commit comments

Comments
 (0)