Skip to content

Commit 52b4bba

Browse files
authored
Merge pull request #922 from r-lib/feature/r6
Implement R6 support. Fixes #388.
2 parents abd1b47 + b52ac49 commit 52b4bba

18 files changed

+1788
-13
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(block_to_rd,default)
4+
S3method(block_to_rd,roxy_block)
5+
S3method(block_to_rd,roxy_block_r6class)
36
S3method(c,rd)
47
S3method(default_export,NULL)
58
S3method(default_export,default)
@@ -56,6 +59,7 @@ S3method(object_defaults,data)
5659
S3method(object_defaults,default)
5760
S3method(object_defaults,import)
5861
S3method(object_defaults,package)
62+
S3method(object_defaults,r6class)
5963
S3method(object_defaults,rcclass)
6064
S3method(object_defaults,s3generic)
6165
S3method(object_defaults,s3method)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ You can override the default either by calling (e.g.) `roxygenise(load_code = "s
157157
* `global_options` is no longer passed to all roclet methods. Instead use
158158
`roxy_meta_get()` to retrieve values stored in the options (#918).
159159

160+
* `@description` and `@detail` tags automatically generated from the leading
161+
description block now have correct line numbers (#917).
162+
163+
* roxygen2 now supports documentation for R6 classes. See the
164+
"Rd (documentation) tags" vignette for details (#922).
165+
160166
* `rd_section()` and `roxy_tag_rd()` are now exported so that you can more
161167
easily extend `rd_roclet()` with your own tags that genereate output in
162168
`.Rd` files.

R/block.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ block_find_object <- function(block, env) {
130130
)
131131
block$object <- object
132132

133+
class(block) <- unique(c(
134+
paste0("roxy_block_", class(object)),
135+
class(block)
136+
))
137+
133138
# Add in defaults generated from the object
134139
defaults <- object_defaults(object)
135140
defaults <- c(defaults, list(roxy_tag("backref", block$file, block$file)))

R/object-from-call.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ object_from_call <- function(call, env, block, file) {
4141

4242
object_from_name <- function(name, env, block) {
4343
value <- get(name, env)
44-
if (methods::is(value, "refObjectGenerator")) {
44+
if (inherits(value, "R6ClassGenerator")) {
45+
type <- "r6class"
46+
} else if (methods::is(value, "refObjectGenerator")) {
4547
value <- methods::getClass(as.character(value@className), where = env)
4648
type <- "rcclass"
4749
} else if (methods::is(value, "classGeneratorFunction")) {
@@ -268,6 +270,7 @@ object_topic <- function(value, alias, type) {
268270
s4class = paste0(value@className, "-class"),
269271
s4generic = value@generic,
270272
rcclass = paste0(value@className, "-class"),
273+
r6class = alias,
271274
rcmethod = value@name,
272275
s3generic = alias,
273276
s3method = alias,

R/object-r6.R

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#' @export
2+
object_defaults.r6class <- function(x) {
3+
list(
4+
roxy_tag("docType", NULL, NULL),
5+
roxy_tag(".r6data", NULL, extract_r6_data(x$value))
6+
)
7+
}
8+
9+
extract_r6_data <- function(x) {
10+
list(
11+
self = extract_r6_self_data(x),
12+
super = extract_r6_super_data(x)
13+
)
14+
}
15+
16+
extract_r6_self_data <- function(x) {
17+
rbind(
18+
extract_r6_methods(x),
19+
extract_r6_fields(x),
20+
extract_r6_bindings(x)
21+
)
22+
}
23+
24+
omit_r6_methods <- function() {
25+
"clone"
26+
}
27+
28+
extract_r6_methods <- function(x) {
29+
method_nms <- setdiff(names(x$public_methods), omit_r6_methods())
30+
method_loc <- map_int(
31+
x$public_methods[method_nms],
32+
function(m) {
33+
ref <- utils::getSrcref(m)
34+
if (is.null(ref)) stop("R6 class without source references")
35+
utils::getSrcLocation(ref)
36+
}
37+
)
38+
method_fnm <- map_chr(
39+
x$public_methods[method_nms],
40+
function(m) {
41+
utils::getSrcFilename(utils::getSrcref(m))
42+
}
43+
)
44+
method_formals <- map(x$public_methods[method_nms], formals)
45+
46+
data.frame(
47+
stringsAsFactors = FALSE,
48+
type = if (length(method_loc)) "method" else character(),
49+
name = unname(method_nms),
50+
file = unname(method_fnm),
51+
line = unname(method_loc),
52+
formals = I(unname(method_formals))
53+
)
54+
}
55+
56+
extract_r6_fields <- function(x) {
57+
field_nms <- names(x$public_fields)
58+
data.frame(
59+
stringsAsFactors = FALSE,
60+
type = rep("field", length(field_nms)),
61+
name = as.character(field_nms),
62+
file = rep(NA, length(field_nms)),
63+
line = rep(NA, length(field_nms)),
64+
formals = I(replicate(length(field_nms), NULL))
65+
)
66+
}
67+
68+
extract_r6_bindings <- function(x) {
69+
bind_nms <- names(x$active)
70+
data.frame(
71+
stringsAsFactors = FALSE,
72+
type = if (length(bind_nms)) "active" else character(),
73+
name = as.character(bind_nms),
74+
file = rep(NA, length(bind_nms)),
75+
line = rep(NA, length(bind_nms)),
76+
formals = I(replicate(length(bind_nms), NULL))
77+
)
78+
}
79+
80+
extract_r6_super_data <- function(x) {
81+
if (is.null(x$inherit)) return()
82+
super <- x$get_inherit()
83+
super_data <- extract_r6_super_data(super)
84+
85+
method_nms <- setdiff(names(super$public_methods), omit_r6_methods())
86+
field_nms <- names(super$public_fields)
87+
active_nms <- names(super$active)
88+
classname <- super$classname %||% NA_character_
89+
pkg <- environmentName(topenv(super$parent_env))
90+
91+
cls <- rbind(
92+
data.frame(
93+
stringsAsFactors = FALSE,
94+
package = pkg,
95+
classname = classname
96+
),
97+
super_data$classes
98+
)
99+
100+
types <- rep(
101+
c("method", "field", "active"),
102+
c(length(method_nms), length(field_nms), length(active_nms))
103+
)
104+
rsort <- function(x) sort(x, decreasing = TRUE)
105+
names <-c(rsort(method_nms), rsort(field_nms), rsort(active_nms))
106+
mth <- rbind(
107+
data.frame(
108+
stringsAsFactors = FALSE,
109+
package = rep(pkg, length(names)),
110+
classname = rep(classname , length(names)),
111+
type = types,
112+
name = names
113+
),
114+
super_data$members
115+
)
116+
117+
list(classes = cls, members = mth)
118+
}

0 commit comments

Comments
 (0)