Skip to content

Commit 6c89bf4

Browse files
allow ordinary comments in roxygen comments
1 parent 66ce587 commit 6c89bf4

8 files changed

+367
-3
lines changed

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-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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ style_roxygen_code_example <- function(example, transformers, base_indention) {
2525
style_roxygen_code_example_one <- function(example_one, transformers, base_indention) {
2626
bare <- parse_roxygen(example_one)
2727
one_dont <- split(bare$text, factor(cumsum(bare$text %in% dont_keywords())))
28-
map(one_dont, style_roxygen_code_example_segment,
28+
styled <- map(one_dont, style_roxygen_code_example_segment,
2929
transformers = transformers,
3030
base_indention = base_indention
3131
) %>%
3232
flatten_chr() %>%
3333
add_roxygen_mask(bare$example_type)
34+
35+
ordinary_comment <- grep("^#[^']", example_one, value = TRUE)
36+
if (length(ordinary_comment) == 0L) {
37+
return(styled)
38+
}
39+
without_mask <- remove_roxygen_mask(styled)
40+
for (idx in seq_along(ordinary_comment)) {
41+
to_replace <- which(ordinary_comment[idx] == without_mask)[1]
42+
styled[to_replace] <- ordinary_comment[idx]
43+
}
44+
styled
3445
}
3546

3647
#' Style a roxygen code example segment

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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

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

Lines changed: 119 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)