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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
103 changes: 66 additions & 37 deletions R/rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -367,56 +369,83 @@ 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)
}

# For replacement functions, wrap only the LHS args
if (is_replacement && length(args) >= 1) {
wrap_args <- args[-length(args)]
# 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.
if (is_replacement) {
bare_name <- sub("<-$", "", name)
} else {
wrap_args <- args
bare_name <- name
}
bare <- paste0(bare_name, "(", paste(call_args, collapse = ", "), ")")
if (nchar(bare, type = "width") < 80) {
return(paste0(display_name, "(",
paste(call_args, collapse = ", "), ")", suffix))
}

if (is_replacement) {
close_suffix <- ") <- value"
} else {
close_suffix <- ")"
# 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)
}

#' 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")
}

Expand Down
69 changes: 69 additions & 0 deletions inst/tinytest/test_rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
9 changes: 7 additions & 2 deletions man/document.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
Expand Down
6 changes: 4 additions & 2 deletions man/format_usage.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
7 changes: 6 additions & 1 deletion man/generate_rd.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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().}
Expand Down
24 changes: 24 additions & 0 deletions man/wrap_usage_arg.Rd
Original file line number Diff line number Diff line change
@@ -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}
Loading