Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve wrapping #1318

Merged
merged 4 commits into from
Apr 6, 2022
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# roxygen2 (development version)

* Arguments containing syntactically significant whitespace (e.g anonymous
functions) are now wrapped correctly (#1281).

* Arguments containing non-syntactic values surrounded by back ticks are now
wrapped correctly (#1257).

* DOIs in the `URL` field of the `DESCRIPTION` are now converted to Rd's special
`\doi{}` tag (@ThierryO, #1296).

Expand Down
2 changes: 1 addition & 1 deletion R/rd-usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ wrap_usage <- function(name, format_name, formals, suffix = NULL, width = 80L) {

# Do we need any wrapping?
bare <- args_call(name, args)
if (nchar(bare, type = "width") < width) {
if (!str_detect(bare, "\n") && nchar(bare, type = "width") < width) {
out <- args_call(format_name(name), args)
} else if (roxy_meta_get("old_usage", FALSE)) {
x <- args_call(format_name(name), args)
Expand Down
3 changes: 2 additions & 1 deletion src/wrapUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ std::vector<std::string> splitByWhitespace(std::string string) {
}

} else if (*cur == ' ' || *cur == '\t' || *cur == '\n') {
if (*cur == '\n') acc += *cur; // newlines are significant whitespace
out.push_back(acc);
acc = "";
} else if (*cur == '"' || *cur == '\'') {
} else if (*cur == '"' || *cur == '\'' || *cur == '`') {
in_string = *cur;
acc += *cur;
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/_snaps/rd-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
c = "aaaaaaaaaaaaaaaa"
) <- value

function_name(
x,
y,
xy = "abcdef",
xyz = c(`word word word word` = "abcdef", `word word word` = "abcdef",
`word word word` = "abcdef", `word word word` = "abcdef")
)

function_name(
f = function(x) {
1
2
}
)


# old wrapping style doesn't change unexpectedly

Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-rd-usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ test_that("new wrapping style doesn't change unexpectedly", {
c = 'aaaaaaaaaaaaaaaa',
value) {}
}), "\n\n")

cat(call_to_usage({
function_name <- function(x, y, xy = "abcdef",
xyz = c(`word word word word` = "abcdef", `word word word` = "abcdef",
`word word word` = "abcdef", `word word word` = "abcdef")) {}
}), "\n\n")

cat(call_to_usage({
function_name <- function(
f = function(x) {
1
2
}) {}
}), "\n\n")
})
})

Expand Down