Skip to content

Commit 9f08512

Browse files
committed
Allow extensions to register custom help handlers
1 parent 1377f0d commit 9f08512

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

crates/ark/src/modules/positron/help.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ help <- function(topic, package = NULL) {
4444
# found.
4545
#' @export
4646
.ps.help.showHelpTopic <- function(topic) {
47+
help_handler <- tryCatch(
48+
{
49+
# Before we do anything to find the help page, we evaluate the topic expression
50+
# to see if the object can be found in the current environment and if it has a
51+
# custom help handler (eg. reticulate objects).
52+
object <- eval(
53+
parse(text = topic),
54+
envir = new.env(parent = globalenv())
55+
)
56+
# call_ark_method() returns NULL if no method is found for the object
57+
# ark_positron_help_get_handler() must return a function that's called for
58+
# its side effects (potentially showing documentation) and returning `TRUE`
59+
# if it could handle the request. We could also make it
60+
# actually show help imediatelly, but that makes it hard to separate
61+
# non-existant methods, from methods that return `NULL` and actual errors.
62+
# This also allows methods to skip matching objects for which they don't want
63+
# to support, by simply returning a `NULL` handler.
64+
call_ark_method(
65+
"ark_positron_help_get_handler",
66+
object
67+
)
68+
},
69+
error = function(e) {
70+
NULL
71+
}
72+
)
73+
74+
if (!is.null(help_handler)) {
75+
return(help_handler(topic))
76+
}
77+
4778
info <- split_topic(topic)
4879
topic <- info$topic
4980
package <- info$package
@@ -230,6 +261,11 @@ getHtmlHelpContentsDevImpl <- function(x) {
230261
.ps.Call("ps_browse_url", as.character(url))
231262
}
232263

264+
#' @export
265+
.ps.help.browse_external_url <- function(url) {
266+
.ps.Call("ps_help_browse_external_url", as.character(url))
267+
}
268+
233269
# @param rd_file Path to an `.Rd` file.
234270
# @returns The result of converting that `.Rd` to HTML and concatenating to a
235271
# string.

crates/ark/src/modules/positron/methods.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ ark_methods_table$ark_positron_variable_get_children <- new.env(
2525
ark_methods_table$ark_positron_variable_has_viewer <- new.env(
2626
parent = emptyenv()
2727
)
28+
ark_methods_table$ark_positron_help_get_handler <- new.env(
29+
parent = emptyenv()
30+
)
2831
lockEnvironment(ark_methods_table, TRUE)
2932

3033
ark_methods_allowed_packages <- c("torch", "reticulate", "duckplyr")

0 commit comments

Comments
 (0)