From 432b5322409d935bcc771e726f472ca573d85457 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 6 Jul 2022 21:42:12 -0500 Subject: [PATCH] Only use nbsp for old style usage Fixes #1342. Closes #1343. --- NEWS.md | 2 ++ R/rd-usage.R | 29 ++++++++++++++++++----------- tests/testthat/test-rd-usage.R | 7 ++++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index b3f42b99..2fa44f13 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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`/ diff --git a/R/rd-usage.R b/R/rd-usage.R index 3f3163f8..c68f6524 100644 --- a/R/rd-usage.R +++ b/R/rd-usage.R @@ -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)) } @@ -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)) } @@ -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)) } diff --git a/tests/testthat/test-rd-usage.R b/tests/testthat/test-rd-usage.R index 4176be52..69f74937 100644 --- a/tests/testthat/test-rd-usage.R +++ b/tests/testthat/test-rd-usage.R @@ -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}")' + ) +})