Skip to content

Commit 027d5b1

Browse files
Merge pull request #830 from lorenzwalthert/issue-828
- Allow comments in roxygen comment blocks (#830).
2 parents a8ec068 + 2444974 commit 027d5b1

11 files changed

+449
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: styler
33
Title: Non-Invasive Pretty Printing of R Code
4-
Version: 1.5.1.9000
4+
Version: 1.5.1.9001
55
Authors@R:
66
c(person(given = "Kirill",
77
family = "Müller",

NEWS.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
12
# styler 1.5.1.9000 (Development version)
23

4+
* Files with `.Rmarkdown` extension are now recognized as an R markdown files in `style_file()` and friends (#824).
5+
36
* Don't break line before comments in pipes (#822).
47

5-
* Files with `.Rmarkdown` extension are now recognised as an R markdown files in `style_file()` and friends (#824)
8+
* Ordinary comments (starting with `#`) within a roxygen code example block
9+
(starting with `#'`) are now recognized and preserved (#830).
10+
11+
* Break the line between `%>%` and `{` inside and outside function calls (#825).
612

713
# styler 1.5.1
814

R/roxygen-examples-add-remove.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,30 @@ 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.
40+
#' @param initial_text The roxygen code example to style with mask and
41+
#' potentially ordinary comments.
3842
#' @param example_type Either 'examples' or 'examplesIf'.
3943
#' @keywords internal
4044
#' @importFrom purrr map2_chr
41-
add_roxygen_mask <- function(text, example_type) {
45+
add_roxygen_mask <- function(text, initial_text, example_type) {
4246
space <- ifelse(text == "", "", " ")
43-
c(
47+
out <- c(
4448
paste0("#' @", example_type, space[1], text[1]),
4549
map2_chr(space[-1], text[-1], ~ paste0("#'", .x, .y))
4650
)
51+
52+
ordinary_comment <- grep("^#[^']", initial_text, value = TRUE)
53+
if (length(ordinary_comment) == 0L) {
54+
return(out)
55+
}
56+
without_mask <- remove_roxygen_mask(out)
57+
for (idx in seq_along(ordinary_comment)) {
58+
to_replace <- which(ordinary_comment[idx] == without_mask)[1]
59+
out[to_replace] <- ordinary_comment[idx]
60+
without_mask[to_replace] <- NA
61+
}
62+
out
4763
}

R/roxygen-examples-parse.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ emulate_rd <- function(roxygen) {
131131
gsub("^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "#' @examples \\4", roxygen),
132132
"x <- 1"
133133
)
134+
roxygen <- gsub("(^#)[^']", "#' #", roxygen)
135+
134136
text <- roxygen2::roc_proc_text(
135137
roxygen2::rd_roclet(),
136138
paste(roxygen, collapse = "\n")

R/roxygen-examples.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ style_roxygen_code_example_one <- function(example_one, transformers, base_inden
3030
base_indention = base_indention
3131
) %>%
3232
flatten_chr() %>%
33-
add_roxygen_mask(bare$example_type)
33+
add_roxygen_mask(example_one, bare$example_type)
3434
}
3535

3636
#' Style a roxygen code example segment

man/add_roxygen_mask.Rd

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

man/style_pkg.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#' Example
2+
# Random comment
3+
#' Roxygen
4+
#' @examples
5+
#' 1 + 1
6+
NULL
7+
8+
9+
#' Example
10+
# Random comment
11+
#' Roxygen
12+
#' @examplesIf
13+
#' 1 + 1
14+
NULL
15+
16+
#' Example
17+
# Random comment
18+
#' Roxygen
19+
#' @examples
20+
#' 1 + 1
21+
# comment
22+
# more
23+
NULL
24+
25+
#' Example
26+
# Random comment
27+
#' Roxygen
28+
#' @examples
29+
# There
30+
#' 1 + 1
31+
# comment
32+
# more
33+
NULL
34+
35+
#' Example
36+
#' Random comment
37+
#' Roxygen
38+
#' @examples
39+
# There
40+
#' 1 + 1
41+
# comment
42+
# more
43+
NULL
44+
45+
#' Example
46+
# Random comment
47+
#' Roxygen
48+
#' @examples
49+
# There
50+
#' \dontrun{
51+
#' 1 + 1
52+
#' }
53+
# comment
54+
# more
55+
NULL
56+
57+
#' Example
58+
# Random comment
59+
#' Roxygen
60+
#' @examples
61+
# There
62+
#' \dontrun{
63+
#' 1 + 1
64+
#' } # comment
65+
# more
66+
NULL
67+
68+
#' Example
69+
# Random comment
70+
#' Roxygen
71+
#' @examples
72+
# 'There
73+
#' \dontrun{
74+
#' 1 + 1
75+
#' }
76+
# comment
77+
# more
78+
NULL
79+
80+
#' Example
81+
# Random comment
82+
#' Roxygen
83+
#' @examples
84+
# There
85+
#' \dontrun{
86+
# comment
87+
#' 1 + 1
88+
#' }
89+
# more
90+
NULL
91+
92+
#' Example
93+
# Random comment
94+
#' Roxygen
95+
#' @examples
96+
# There
97+
#' \dontrun{
98+
#' call(
99+
# comment
100+
#' 1 + 1
101+
#' )
102+
#' }
103+
# more
104+
NULL
105+
106+
# nolint start
107+
#' @examplesIf TRUE
108+
# nolint end
109+
#' df %>% func()
110+
func <- function() NULL
111+
112+
113+
#' Hi
114+
# Comment
115+
#' @examples
116+
#' 1 + 1
117+
# this
118+
# this
119+
#this
120+
# thi3
121+
#' c()
122+
NULL
123+
124+
#' Hi
125+
# Comment
126+
#' @examples
127+
#' 1 + 1
128+
# this
129+
# this
130+
#this
131+
# thi3
132+
#' c()
133+
NULL

tests/testthat/roxygen-examples-complete/25-ordinary-comment-in-example-in_tree

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

0 commit comments

Comments
 (0)