-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathrd-params.R
66 lines (53 loc) · 2.3 KB
/
rd-params.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#' @export
roxy_tag_parse.roxy_tag_param <- function(x) {
tag_two_part(x, "an argument name", "a description")
}
#' @export
roxy_tag_rd.roxy_tag_param <- function(x, base_path, env) {
value <- setNames(x$val$description, x$val$name)
rd_section(x$tag, value)
}
#' @export
merge.rd_section_param <- function(x, y, ...) {
stopifnot(identical(class(x), class(y)))
# When parameters appear in both x and y, keep values from y
# This happens for example when inherit_dot_params adds a "..." param after
# inherit_params has done the same.
to_add <- setdiff(names(x$value), names(y$value))
rd_section(x$type, c(x$value[to_add], y$value))
}
#' @export
format.rd_section_param <- function(x, ...) {
names <- names(x$value)
# add space to multiple arguments so they can wrap
names <- gsub(",", ", ", names)
items <- paste0("\\item{", names, "}{", x$value, "}", collapse = "\n\n")
rd_macro("arguments", items, space = TRUE)
}
# Other helpers -----------------------------------------------------------
# Postprocessing to reset ordering of parameter documentation
topics_fix_params_order <- function(topics) {
for (topic in topics$topics) {
# Compute correct ordering of parameter documentation
# Check what's needed...
needed <- topic$get_value("formals")
# (Workaround for dupes that can occur but perhaps shouldn't,
# cf. https://github.com/r-lib/roxygen2/commit/83d125dce50a072534988787d49ffe206d19b232#commitcomment-6742169)
needed <- unique(needed)
# ...and what's documented (here we look only at the first parameter
# in a multi-parameter documentation)
documented <- get_documented_params(topic, only_first = TRUE)
# We operate on indexes to make sure that no documentation is lost during
# the reordering
documented_indexes <- seq_along(documented)
# We compute the indexes in the current documentation in the required order
# and append everything that's missing in the order found
required_order <- match(needed, documented)
required_order <- required_order[!is.na(required_order)]
required_order <- c(required_order, setdiff(documented_indexes, required_order))
# Overwrite all param fields to fix order
param <- topic$get_value("param")[required_order]
topic$add(rd_section("param", param), overwrite = TRUE)
}
invisible()
}