Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# roxygen2 (development version)

* Automated usage no longer mangles nbsp in default arguments (#1342).

* Give useful error if `@describeIn` can't combine docs (#1366).

* Code evaluated in inline markdown code chunks and `@eval`/`@evalRd`/
Expand Down
29 changes: 18 additions & 11 deletions R/rd-usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ usage_args <- function(args) {
map_chr(args, arg_to_text)
}

args_string <- function(x) {
sep <- ifelse(x != "", "\u{A0}=\u{A0}", "")
args_string <- function(x, space = " ") {
sep <- ifelse(x != "", paste0(space, "=", space), "")
arg_names <- escape(auto_backtick(names(x)))
paste0(arg_names, sep, escape(x))
}
Expand All @@ -127,24 +127,31 @@ args_call <- function(call, args) {
#' @param suffix Optional suffix, used for replacement functions
#' @noRd
wrap_usage <- function(name, format_name, formals, suffix = NULL, width = 80L) {
args <- args_string(usage_args(formals))
if (roxy_meta_get("old_usage", FALSE)) {
# Use nbsp to keep argument name & default value on same line
args <- args_string(usage_args(formals), "\u{A0}")
x <- args_call(format_name(name), args)
out <- wrapUsage(x, width = as.integer(width), indent = 2)

out <- gsub("\u{A0}", " ", out, useBytes = TRUE)
Encoding(out) <- "UTF-8"

return(rd(paste0(out, suffix)))
}

# Do we need any wrapping?
args <- args_string(usage_args(formals))
bare <- args_call(name, args)

if (!str_detect(bare, "\n") && nchar(bare, type = "width") < width) {
# Don't need to wrap
out <- args_call(format_name(name), args)
} else if (roxy_meta_get("old_usage", FALSE)) {
x <- args_call(format_name(name), args)
out <- wrapUsage(x, width = as.integer(width), indent = 2)
} else {
# Wrap each argument and put on own line
args <- paste0(" ", args)
args <- map_chr(args, wrapUsage, width = 90, indent = 4)
out <- paste0(format_name(name), "(\n", paste0(args, collapse = ",\n"), "\n)")
}

out <- gsub("\u{A0}", " ", out, useBytes = TRUE)
Encoding(out) <- "UTF-8"

rd(paste0(out, suffix))
}

Expand All @@ -153,5 +160,5 @@ wrap_usage <- function(name, format_name, formals, suffix = NULL, width = 80L) {
# used for testing
call_to_usage <- function(code, env = pkg_env()) {
obj <- call_to_object(!!enexpr(code), env)
gsub("\u{A0}", " ", as.character(object_usage(obj)))
as.character(object_usage(obj))
}
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/rd-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
x,
y,
xy = "abcdef",
xyz = c(`word word word word` = "abcdef", `word word word` = "abcdef",
`word word word` = "abcdef", `word word word` = "abcdef")
xyz = c(`word word word word` = "abcdef", `word word word` = "abcdef", `word word word`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only looked good by coincidence before, because the C++ wrapping code counts bytes, not characters, and \u{A0} is two bytes.

= "abcdef", `word word word` = "abcdef")
)

function_name(
Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/test-rd-usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,9 @@ test_that("old wrapping style doesn't change unexpectedly", {
})
})


test_that("preserves non-breaking-space", {
expect_equal(
call_to_usage(f <- function(a = "\u{A0}") {}),
'f(a = "\u{A0}")'
)
})