From ad3f36357a7c59a3259e50ebf096fa0dc56bcc5e Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:06:56 -0500 Subject: [PATCH 1/3] Wrap overlong \usage lines roxygen2-style (#29) format_usage() previously wrapped only between arguments, so a single argument with a long default (a multi-value c(...) or Sys.getenv()) produced \usage lines wider than 90 characters and an R CMD check NOTE. Adopt roxygen2's algorithm: single line when the bare call is under 80 characters, otherwise one argument per line at two-space indent, with each overlong argument broken at spaces outside quoted strings to the 90-character Rd limit (new wrap_usage_arg() helper, fuzz-verified byte-identical to roxygen2's wrapUsage across 1100+ cases). The width decision is made on the bare name so \method markup and the replacement-function suffix keep headroom, also matching roxygen2. --- R/rd.R | 100 +++++++++++++++++++++++++--------------- inst/tinytest/test_rd.R | 69 +++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 37 deletions(-) diff --git a/R/rd.R b/R/rd.R index 77ad9c2..de9e49a 100644 --- a/R/rd.R +++ b/R/rd.R @@ -335,8 +335,10 @@ escape_rd <- function(text) { #' Format Usage Line #' #' Formats the function usage, wrapping to multiple lines if needed. -#' Follows roxygen2 style: each argument on its own line if total > 80 chars. -#' S3 methods use the \\method syntax. +#' Follows roxygen2: single line when the bare call is under 80 characters, +#' otherwise one argument per line at two-space indent, with each overlong +#' argument wrapped to the 90-character Rd limit. S3 methods use the +#' \\method syntax. #' #' @param name Function name. #' @param args Character vector of arguments with defaults. @@ -367,56 +369,80 @@ format_usage <- function(name, args, pkg_generics = character()) { # For replacement functions, last arg is 'value' which goes on the right side if (is_replacement && length(args) >= 1) { - lhs_args <- args[-length(args)] - single_line <- paste0(display_name, "(", paste(lhs_args, collapse = ", "), ") <- value") + call_args <- args[-length(args)] + suffix <- " <- value" } else { - single_line <- paste0(display_name, "(", paste(args, collapse = ", "), ")") + call_args <- args + suffix <- "" } - # If short enough, use single line - if (nchar(single_line) <= 80) { - return(single_line) + # Width decision is made on the bare name, like roxygen2: \method markup + # and the replacement suffix are excluded, leaving headroom under the + # 90-character Rd limit for the markup they add. + bare_name <- if (is_replacement) sub("<-$", "", name) else name + bare <- paste0(bare_name, "(", paste(call_args, collapse = ", "), ")") + if (nchar(bare, type = "width") < 80) { + return(paste0(display_name, "(", + paste(call_args, collapse = ", "), ")", suffix)) } - # For replacement functions, wrap only the LHS args - if (is_replacement && length(args) >= 1) { - wrap_args <- args[-length(args)] - } else { - wrap_args <- args - } + # One argument per line at two-space indent, each wrapped to 90 chars + wrapped <- vapply(paste0(" ", call_args), wrap_usage_arg, + character(1), USE.NAMES = FALSE) + paste0(display_name, "(\n", paste(wrapped, collapse = ",\n"), "\n)", + suffix) +} - if (is_replacement) { - close_suffix <- ") <- value" - } else { - close_suffix <- ")" +#' Wrap a Single Usage Argument +#' +#' Greedy space-fill wrap replicating roxygen2's wrapUsage(): once a line +#' exceeds the width, the last space outside a quoted string becomes a +#' newline, and continuation lines get a fixed indent. Quoted strings are +#' never broken. +#' +#' @param x Argument string, including its two-space usage indent. +#' @param width Maximum line width. +#' @param indent Number of spaces prefixed to continuation lines. +#' @return Wrapped string, possibly containing newlines. +#' @keywords internal +wrap_usage_arg <- function(x, width = 90L, indent = 4L) { + if (nchar(x) < width) { + return(x) } - # Wrap to multiple lines, packing multiple args per line - # Continuation lines indented to align after opening paren - open <- paste0(display_name, "(") - cont_indent <- paste(rep(" ", nchar(open)), collapse = "") + # Quote tracking is deliberately naive (a quote char always toggles, + # backslash escapes are not special) to stay byte-identical to + # roxygen2's wrapUsage(). + pad <- strsplit(strrep(" ", indent), "")[[1]] + chars <- strsplit(x, "")[[1]] lines <- character() - current <- open + line <- character() + last_space <- 0L + quote_char <- "" + + for (ch in chars) { + if (quote_char != "") { + if (ch == quote_char) { + quote_char <- "" + } + } else if (ch == "\"" || ch == "'" || ch == "`") { + quote_char <- ch + } - for (i in seq_along(wrap_args)) { - arg <- wrap_args[i] - if (i < length(wrap_args)) { - suffix <- ", " - } else { - suffix <- "" + line <- c(line, ch) + if (ch == " " && quote_char == "") { + last_space <- length(line) } - piece <- paste0(arg, suffix) - if (nchar(current) + nchar(piece) > 80 && current != open) { - lines <- c(lines, sub(",? $", ",", current)) - current <- paste0(cont_indent, piece) - } else { - current <- paste0(current, piece) + if (length(line) >= width && last_space > 0L) { + lines <- c(lines, paste(line[seq_len(last_space - 1L)], + collapse = "")) + line <- c(pad, line[seq_along(line) > last_space]) + last_space <- 0L } } - current <- paste0(current, close_suffix) - lines <- c(lines, current) + lines <- c(lines, paste(line, collapse = "")) paste(lines, collapse = "\n") } diff --git a/inst/tinytest/test_rd.R b/inst/tinytest/test_rd.R index 9659565..3002542 100644 --- a/inst/tinytest/test_rd.R +++ b/inst/tinytest/test_rd.R @@ -243,3 +243,72 @@ test_package_rd_no_details <- function() { unlink(pkg, recursive = TRUE) } test_package_rd_no_details() + +# --- Issue #29: \usage lines must stay within the 90-char Rd limit --- + +# Short usage stays on a single line +expect_equal(tinyrox:::format_usage("add", c("x", "y")), "add(x, y)") + +# Long defaults wrap roxygen2-style: one arg per line, overlong args broken +long_args <- c( + 'provider = c("anthropic", "anthropic_claude", "openai", "moonshot", "openai_codex", "ollama")', + 'access_token = Sys.getenv("ANTHROPIC_CLAUDE_ACCESS_TOKEN", "")' +) +usage29 <- tinyrox:::format_usage("choose_provider", long_args) +expect_equal(usage29, paste( + "choose_provider(", + ' provider = c("anthropic", "anthropic_claude", "openai", "moonshot", "openai_codex",', + ' "ollama"),', + ' access_token = Sys.getenv("ANTHROPIC_CLAUDE_ACCESS_TOKEN", "")', + ")", + sep = "\n")) +expect_true(all(nchar(strsplit(usage29, "\n")[[1]]) <= 90)) + +# 80-char boundary matches roxygen2: bare call of 79 chars stays single +# line, 80 chars wraps +arg79 <- strrep("a", 76) +expect_equal(tinyrox:::format_usage("f", arg79), paste0("f(", arg79, ")")) +arg80 <- strrep("a", 77) +expect_equal(tinyrox:::format_usage("f", arg80), paste0("f(\n ", arg80, "\n)")) + +# S3 method width decision uses the bare name, not the \method markup +s3_args <- c("x", strrep("b", 60)) +s3_usage <- tinyrox:::format_usage("print.myclass", s3_args) +expect_equal(s3_usage, + paste0("\\method{print}{myclass}(x, ", strrep("b", 60), ")")) + +# Replacement function wraps with the suffix after the closing paren +repl_args <- c("x", strrep("y", 80), "value") +repl_usage <- tinyrox:::format_usage("dim<-", repl_args) +expect_equal(repl_usage, + paste0("dim(\n x,\n ", strrep("y", 80), "\n) <- value")) + +# wrap_usage_arg: quoted strings are never broken +expect_equal( + tinyrox:::wrap_usage_arg(' x = c("a b c d e f", "g h i", "j k l")', + width = 20L, indent = 4L), + ' x =\n c("a b c d e f",\n "g h i",\n "j k l")') + +# wrap_usage_arg: short input returned unchanged +expect_equal(tinyrox:::wrap_usage_arg(" x = 1"), " x = 1") + +# End-to-end: generated Rd has no line wider than 90 chars +tags29 <- list( + title = "Choose a provider", + description = "Chooses a provider", + details = NULL, + params = list(provider = "Provider id.", access_token = "Token."), + return = "NULL, invisibly.", + examples = NULL, + seealso = NULL, + references = NULL, + aliases = character(), + keywords = character(), + family = NULL, + name = "choose_provider", + noRd = FALSE +) +rd29 <- tinyrox:::generate_rd(tags29, + list(names = c("provider", "access_token"), + usage = long_args)) +expect_true(all(nchar(strsplit(rd29, "\n")[[1]]) <= 90)) From 4ad2b3e37150c39d78de04e9c6ac3bd7bbf33d4e Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:07:21 -0500 Subject: [PATCH 2/3] rformat + document --- R/rd.R | 13 ++++++++----- man/document.Rd | 9 +++++++-- man/format_usage.Rd | 6 ++++-- man/generate_rd.Rd | 7 ++++++- man/wrap_usage_arg.Rd | 24 ++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 man/wrap_usage_arg.Rd diff --git a/R/rd.R b/R/rd.R index de9e49a..10d4961 100644 --- a/R/rd.R +++ b/R/rd.R @@ -379,7 +379,11 @@ format_usage <- function(name, args, pkg_generics = character()) { # Width decision is made on the bare name, like roxygen2: \method markup # and the replacement suffix are excluded, leaving headroom under the # 90-character Rd limit for the markup they add. - bare_name <- if (is_replacement) sub("<-$", "", name) else name + if (is_replacement) { + bare_name <- sub("<-$", "", name) + } else { + bare_name <- name + } bare <- paste0(bare_name, "(", paste(call_args, collapse = ", "), ")") if (nchar(bare, type = "width") < 80) { return(paste0(display_name, "(", @@ -389,8 +393,7 @@ format_usage <- function(name, args, pkg_generics = character()) { # One argument per line at two-space indent, each wrapped to 90 chars wrapped <- vapply(paste0(" ", call_args), wrap_usage_arg, character(1), USE.NAMES = FALSE) - paste0(display_name, "(\n", paste(wrapped, collapse = ",\n"), "\n)", - suffix) + paste0(display_name, "(\n", paste(wrapped, collapse = ",\n"), "\n)", suffix) } #' Wrap a Single Usage Argument @@ -435,8 +438,8 @@ wrap_usage_arg <- function(x, width = 90L, indent = 4L) { } if (length(line) >= width && last_space > 0L) { - lines <- c(lines, paste(line[seq_len(last_space - 1L)], - collapse = "")) + lines <- c(lines, + paste(line[seq_len(last_space - 1L)], collapse = "")) line <- c(pad, line[seq_along(line) > last_space]) last_space <- 0L } diff --git a/man/document.Rd b/man/document.Rd index 9ee0e4c..f4a7449 100644 --- a/man/document.Rd +++ b/man/document.Rd @@ -3,8 +3,13 @@ \alias{document} \title{Generate Documentation for an R Package} \usage{ -document(path = ".", namespace = c("overwrite", "append", "none"), - cran_check = TRUE, silent = FALSE, prune_rd = TRUE) +document( + path = ".", + namespace = c("overwrite", "append", "none"), + cran_check = TRUE, + silent = FALSE, + prune_rd = TRUE +) } \arguments{ \item{path}{Path to package root directory. Default is current directory.} diff --git a/man/format_usage.Rd b/man/format_usage.Rd index e39b691..1acdb82 100644 --- a/man/format_usage.Rd +++ b/man/format_usage.Rd @@ -18,7 +18,9 @@ Formatted usage string. } \description{ Formats the function usage, wrapping to multiple lines if needed. -Follows roxygen2 style: each argument on its own line if total > 80 chars. -S3 methods use the \\method syntax. +Follows roxygen2: single line when the bare call is under 80 characters, +otherwise one argument per line at two-space indent, with each overlong +argument wrapped to the 90-character Rd limit. S3 methods use the +\\method syntax. } \keyword{internal} diff --git a/man/generate_rd.Rd b/man/generate_rd.Rd index 8eab0a2..7c5ae09 100644 --- a/man/generate_rd.Rd +++ b/man/generate_rd.Rd @@ -3,7 +3,12 @@ \alias{generate_rd} \title{Generate Rd File Content} \usage{ -generate_rd(tags, formals = NULL, source_file = NULL, pkg_generics = character()) +generate_rd( + tags, + formals = NULL, + source_file = NULL, + pkg_generics = character() +) } \arguments{ \item{tags}{Parsed tags from parse_tags().} diff --git a/man/wrap_usage_arg.Rd b/man/wrap_usage_arg.Rd new file mode 100644 index 0000000..dd28aff --- /dev/null +++ b/man/wrap_usage_arg.Rd @@ -0,0 +1,24 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{wrap_usage_arg} +\alias{wrap_usage_arg} +\title{Wrap a Single Usage Argument} +\usage{ +wrap_usage_arg(x, width = 90L, indent = 4L) +} +\arguments{ +\item{x}{Argument string, including its two-space usage indent.} + +\item{width}{Maximum line width.} + +\item{indent}{Number of spaces prefixed to continuation lines.} +} +\value{ +Wrapped string, possibly containing newlines. +} +\description{ +Greedy space-fill wrap replicating roxygen2's wrapUsage(): once a line +exceeds the width, the last space outside a quoted string becomes a +newline, and continuation lines get a fixed indent. Quoted strings are +never broken. +} +\keyword{internal} From 3c1838c075bb028ea11db0cb00344aa0df7ae39d Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:12:00 -0500 Subject: [PATCH 3/3] Bump version to 0.4.0.1 --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d8391ea..866a8d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tinyrox Title: Minimal R Documentation Generator -Version: 0.4.0 +Version: 0.4.0.1 Date: 2026-06-24 Authors@R: c( person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index c44705b..8f09767 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# tinyrox 0.4.0.1 + +* `\usage` lines now stay within the 90-character Rd limit (#29). Overlong signatures wrap roxygen2-style: one argument per line, breaking inside a long default when needed, so `R CMD check` no longer NOTEs "Rd line widths". + # tinyrox 0.4.0 Consolidates the 0.3.3.x development cycle.