Skip to content

Commit

Permalink
Only use nbsp for old style usage
Browse files Browse the repository at this point in the history
Fixes #1342. Closes #1343.
  • Loading branch information
hadley committed Jul 7, 2022
1 parent c9cffcb commit 432b532
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
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))
}
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}")'
)
})

0 comments on commit 432b532

Please sign in to comment.