Skip to content

Commit 6138a93

Browse files
authored
Only link to topics that exist (#1820)
Fixes #963
1 parent 0af212e commit 6138a93

5 files changed

Lines changed: 40 additions & 22 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* R6 improvements:
66
* The "Super classes" section now omits the `pkg::` prefix for parent classes from the same package, making the inheritance chain easier to read (#1567).
77
* R6 classes with only active bindings and `cloneable = FALSE` no longer error during documentation (#1610).
8+
* Inherited method links now only link to parent classes that have documentation, avoiding broken links when parent classes are undocumented (#963, #1155).
89
* `initialize()` method parameters now automatically inherit documentation from `@field` tags with the same name, reducing the need to duplicate descriptions. Explicit `@param` tags still take precedence (#1004).
910
* New `needs_roxygenize()` provides a lightweight check that man pages are up-to-date by comparing modification times of `.Rd` files with their source files (#1411).
1011
* All generated links now use the same code path. This will lead to some minor differences when you re-document, but overall the links will now be more consistent (#1792).

R/rd-r6-methods-inherited.R

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,41 @@ format.rd_r6_inherited <- function(x, ...) {
2121
"><summary>Inherited methods</summary>"
2222
)
2323

24+
# Which parent classes are documented?
25+
cls <- unique(x$classname)
26+
pkgs <- x$package[match(cls, x$classname)]
27+
ht <- map2_lgl(cls, pkgs, has_topic)
28+
topic_ok <- ht[match(x$classname, cls)]
29+
30+
# Build display components for each inherited method
2431
anchor <- sprintf("method-%s-%s", x$classname, x$name)
25-
href <- sprintf("../../%s/html/%s.html#%s", x$package, x$classname, anchor)
26-
label <- sprintf("%s::%s$%s()", x$package, x$classname, x$name)
32+
self_pkg <- roxy_meta_get("current_package") %||% ""
33+
same_pkg <- x$package == self_pkg
34+
prefix <- ifelse(same_pkg, "", paste0(x$package, "::"))
35+
label <- sprintf("%s%s$%s()", prefix, x$classname, x$name)
2736

28-
items <- paste0(
29-
"<li>",
30-
sprintf(
31-
'<span class="pkg-link" data-pkg="%s" data-topic="%s" data-id="%s">',
32-
x$package,
33-
x$classname,
34-
x$name
35-
),
36-
sprintf("<a href='%s'><code>%s</code></a>", href, label),
37-
"</span>",
38-
"</li>"
37+
# Linked version: clickable reference to parent class docs
38+
href <- sprintf("../../%s/html/%s.html#%s", x$package, x$classname, anchor)
39+
data_attrs <- paste(
40+
sprintf('data-pkg="%s"', x$package),
41+
sprintf('data-topic="%s"', x$classname),
42+
sprintf('data-id="%s"', x$name)
3943
)
44+
linked <- sprintf(
45+
'<span class="pkg-link" %s><a href=\'%s\'><code>%s</code></a></span>',
46+
data_attrs,
47+
href,
48+
label
49+
)
50+
51+
# Plain version: just the method name, no link
52+
plain <- sprintf("<code>%s</code>", label)
53+
items <- paste0("<li>", ifelse(topic_ok, linked, plain), "</li>")
4054

41-
rd_if_html(paste(c(details, "<ul>", items, "</ul>", "</details>"), collapse = "\n"))
55+
rd_if_html(paste(
56+
c(details, "<ul>", items, "</ul>", "</details>"),
57+
collapse = "\n"
58+
))
4259
}
4360

4461
r6_extract_inherited_methods <- function(r6data) {

tests/testthat/_snaps/rd-r6-class.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
}
7979
\if{html}{\out{<details open><summary>Inherited methods</summary>
8080
<ul>
81-
<li><span class="pkg-link" data-pkg="R_GlobalEnv" data-topic="A" data-id="only_a"><a href='../../R_GlobalEnv/html/A.html#method-A-only_a'><code>R_GlobalEnv::A$only_a()</code></a></span></li>
81+
<li><code>R_GlobalEnv::A$only_a()</code></li>
8282
</ul>
8383
</details>}}
8484
\if{html}{\out{<hr>}}

tests/testthat/_snaps/rd-r6-methods-inherited.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Output
66
\if{html}{\out{<details open><summary>Inherited methods</summary>
77
<ul>
8-
<li><span class="pkg-link" data-pkg="pkg" data-topic="A" data-id="foo"><a href='../../pkg/html/A.html#method-A-foo'><code>pkg::A$foo()</code></a></span></li>
9-
<li><span class="pkg-link" data-pkg="pkg" data-topic="A" data-id="bar"><a href='../../pkg/html/A.html#method-A-bar'><code>pkg::A$bar()</code></a></span></li>
8+
<li><code>pkg::A$foo()</code></li>
9+
<li><code>pkg::A$bar()</code></li>
1010
</ul>
1111
</details>}}
1212

tests/testthat/_snaps/rd-r6.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@
249249
}
250250
\if{html}{\out{<details open><summary>Inherited methods</summary>
251251
<ul>
252-
<li><span class="pkg-link" data-pkg="testR6" data-topic="A" data-id="meth2"><a href='../../testR6/html/A.html#method-A-meth2'><code>testR6::A$meth2()</code></a></span></li>
253-
<li><span class="pkg-link" data-pkg="testR6" data-topic="A" data-id="meth3"><a href='../../testR6/html/A.html#method-A-meth3'><code>testR6::A$meth3()</code></a></span></li>
252+
<li><code>A$meth2()</code></li>
253+
<li><code>A$meth3()</code></li>
254254
</ul>
255255
</details>}}
256256
\if{html}{\out{<hr>}}
@@ -365,9 +365,9 @@
365365
}
366366
\if{html}{\out{<details open><summary>Inherited methods</summary>
367367
<ul>
368-
<li><span class="pkg-link" data-pkg="testR6" data-topic="A" data-id="meth3"><a href='../../testR6/html/A.html#method-A-meth3'><code>testR6::A$meth3()</code></a></span></li>
369-
<li><span class="pkg-link" data-pkg="testR6" data-topic="B" data-id="meth1"><a href='../../testR6/html/B.html#method-B-meth1'><code>testR6::B$meth1()</code></a></span></li>
370-
<li><span class="pkg-link" data-pkg="testR6" data-topic="B" data-id="meth4"><a href='../../testR6/html/B.html#method-B-meth4'><code>testR6::B$meth4()</code></a></span></li>
368+
<li><code>A$meth3()</code></li>
369+
<li><code>B$meth1()</code></li>
370+
<li><code>B$meth4()</code></li>
371371
</ul>
372372
</details>}}
373373
\if{html}{\out{<hr>}}

0 commit comments

Comments
 (0)