Skip to content

Commit

Permalink
Merge 353acd1 into a8ec068
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Aug 7, 2021
2 parents a8ec068 + 353acd1 commit fa83546
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 7 deletions.
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

# styler 1.5.1.9000 (Development version)

* Files with `.Rmarkdown` extension are now recognized as an R markdown files in `style_file()` and friends (#824).

* Don't break line before comments in pipes (#822).

* Files with `.Rmarkdown` extension are now recognised as an R markdown files in `style_file()` and friends (#824)
* Ordinary comments (starting with `#`) within a roxygen code example block
(starting with `#'`) are now recognized and preserved (#830).

* Break the line between `%>%` and `{` inside and outside function calls (#825).

# styler 1.5.1

Expand Down
20 changes: 18 additions & 2 deletions R/roxygen-examples-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,30 @@ remove_roxygen_header <- function(text) {

#' Add the roxygen mask to code
#'
#' This function compares `text` with `initial_text` to make sure a mask is only
#' added to roxygen comments, not ordinary comments
#' @param text Character vector with code.
#' @param initial_text The roxygen code example to style with mask and
#' potentially ordinary comments.
#' @param example_type Either 'examples' or 'examplesIf'.
#' @keywords internal
#' @importFrom purrr map2_chr
add_roxygen_mask <- function(text, example_type) {
add_roxygen_mask <- function(text, initial_text, example_type) {
space <- ifelse(text == "", "", " ")
c(
out <- c(
paste0("#' @", example_type, space[1], text[1]),
map2_chr(space[-1], text[-1], ~ paste0("#'", .x, .y))
)

ordinary_comment <- grep("^#[^']", initial_text, value = TRUE)
if (length(ordinary_comment) == 0L) {
return(out)
}
without_mask <- remove_roxygen_mask(out)
for (idx in seq_along(ordinary_comment)) {
to_replace <- which(ordinary_comment[idx] == without_mask)[1]
out[to_replace] <- ordinary_comment[idx]
without_mask[to_replace] <- NA
}
out
}
2 changes: 2 additions & 0 deletions R/roxygen-examples-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ emulate_rd <- function(roxygen) {
gsub("^#'(\\s|\t)*@examples(If)?(\\s|\t)*(.*)", "#' @examples \\4", roxygen),
"x <- 1"
)
roxygen <- gsub("(^#)[^']", "#' #", roxygen)

text <- roxygen2::roc_proc_text(
roxygen2::rd_roclet(),
paste(roxygen, collapse = "\n")
Expand Down
2 changes: 1 addition & 1 deletion R/roxygen-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ style_roxygen_code_example_one <- function(example_one, transformers, base_inden
base_indention = base_indention
) %>%
flatten_chr() %>%
add_roxygen_mask(bare$example_type)
add_roxygen_mask(example_one, bare$example_type)
}

#' Style a roxygen code example segment
Expand Down
8 changes: 6 additions & 2 deletions man/add_roxygen_mask.Rd

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

2 changes: 1 addition & 1 deletion man/style_pkg.Rd

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,133 @@
#' Example
# Random comment
#' Roxygen
#' @examples
#' 1 + 1
NULL


#' Example
# Random comment
#' Roxygen
#' @examplesIf
#' 1 + 1
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
#' 1 + 1
# comment
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# There
#' 1 + 1
# comment
# more
NULL

#' Example
#' Random comment
#' Roxygen
#' @examples
# There
#' 1 + 1
# comment
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# There
#' \dontrun{
#' 1 + 1
#' }
# comment
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# There
#' \dontrun{
#' 1 + 1
#' } # comment
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# 'There
#' \dontrun{
#' 1 + 1
#' }
# comment
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# There
#' \dontrun{
# comment
#' 1 + 1
#' }
# more
NULL

#' Example
# Random comment
#' Roxygen
#' @examples
# There
#' \dontrun{
#' call(
# comment
#' 1 + 1
#' )
#' }
# more
NULL

# nolint start
#' @examplesIf TRUE
# nolint end
#' df %>% func()
func <- function() NULL


#' Hi
# Comment
#' @examples
#' 1 + 1
# this
# this
#this
# thi3
#' c()
NULL

#' Hi
# Comment
#' @examples
#' 1 + 1
# this
# this
#this
# thi3
#' c()
NULL

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

Loading

0 comments on commit fa83546

Please sign in to comment.