-
Notifications
You must be signed in to change notification settings - Fork 52
Use rdtools #337
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
Open
hadley
wants to merge
9
commits into
main
Choose a base branch
from
use-rdtools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Use rdtools #337
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78acf59
Use rdtools
hadley 65b9150
Merge commit '552a11f852a6af7fdb7812fad97d18d4cc3222f8'
hadley b054e09
Use `rdtools::pkg_macros()`
hadley 91090e7
Oops
hadley aedea80
No longer support paths
hadley b343044
Remove outdated RStudio code
hadley 4f6e406
Fix macros buglet
hadley 211fe6e
Final polishing
hadley 7760934
Use `topic_rd_path()`
hadley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
| #' In-development help for package loaded with devtools | ||
| #' | ||
| #' `dev_help()` searches for source documentation provided in packages | ||
| #' loaded by devtools. To improve performance, the `.Rd` files are | ||
| #' parsed to create to index once, then cached. Use | ||
| #' `dev_topic_index_reset()` to clear that index. You can manually | ||
| #' retrieve the index for a local package with `dev_topic_index()`. | ||
| #' loaded by devtools. The topic index is maintained by the rdtools | ||
| #' package, which caches it per package. | ||
| #' | ||
| #' @param topic name of help to search for. | ||
| #' @param dev_packages A character vector of package names to search within. | ||
|
|
@@ -38,12 +36,6 @@ dev_help <- function( | |
|
|
||
| loc <- dev_topic_find(topic, dev_packages) | ||
|
|
||
| if (!is.null(loc$path) && !fs::file_exists(loc$path)) { | ||
| # Documentation topic might have moved, so reset topic index and try again | ||
| dev_topic_index_reset(loc$pkg) | ||
| loc <- dev_topic_find(topic, dev_packages) | ||
| } | ||
|
|
||
| if (is.null(loc$path) || !fs::file_exists(loc$path)) { | ||
| cli::cli_abort("Can't find development topic {.arg {topic}}.") | ||
| } | ||
|
|
@@ -60,29 +52,6 @@ dev_help <- function( | |
| ) | ||
| } | ||
|
|
||
| has_rd_macros <- function(dir) { | ||
| desc <- file.path(dir, "DESCRIPTION") | ||
| if (!file.exists(desc)) { | ||
| return(FALSE) | ||
| } | ||
|
|
||
| tryCatch( | ||
| expr = { | ||
| desc <- read.dcf(desc) | ||
| "RdMacros" %in% colnames(desc) | ||
| }, | ||
| error = function(...) FALSE | ||
| ) | ||
| } | ||
|
|
||
| load_rd_macros <- function(dir) { | ||
| macros <- tools::loadPkgRdMacros(dir) | ||
| tools::loadRdMacros( | ||
| file.path(R.home("share"), "Rd", "macros", "system.Rd"), | ||
| macros = macros | ||
| ) | ||
| } | ||
|
|
||
| #' @export | ||
| print.dev_topic <- function(x, ...) { | ||
| cli::cli_inform(c( | ||
|
|
@@ -91,54 +60,31 @@ print.dev_topic <- function(x, ...) { | |
|
|
||
| type <- arg_match0(x$type %||% "text", c("text", "html")) | ||
|
|
||
| # Use rstudio's previewRd() if possible | ||
| if (type == "html" && rstudioapi_available()) { | ||
| # If the package has Rd macros, this needs a version of rstudio | ||
| # that loads them, see rstudio/rstudio#12111 | ||
| version_needed <- if (has_rd_macros(dirname(dirname(x$path)))) { | ||
| "2022.12.0.256" | ||
| } | ||
|
|
||
| if (rstudioapi::hasFun("previewRd", version_needed = version_needed)) { | ||
| return(rstudioapi::callFun("previewRd", x$path)) | ||
| } | ||
| # Use RStudio's previewRd() if possible | ||
| rstudio_preview <- type == "html" && | ||
| is_installed("rstudioapi") && | ||
| rstudioapi::isAvailable() && | ||
| rstudioapi::hasFun("previewRd") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe keep the |
||
| if (rstudio_preview) { | ||
| return(rstudioapi::callFun("previewRd", x$path)) | ||
| } | ||
|
|
||
| # otherwise render and serve | ||
| file <- fs::path_ext_set(fs::path_file(x$path), type) | ||
|
|
||
| # This directory structure is necessary for RStudio to open the | ||
| # .html file in the help pane (see rstudio/rstudio#11336) | ||
| doc_path <- fs::path("doc", "html", file) | ||
| path <- fs::path(tempdir(), ".R", doc_path) | ||
| fs::dir_create(fs::path_dir(path), recurse = TRUE) | ||
| path <- fs::path(tempdir(), file) | ||
|
|
||
| if (type == "text") { | ||
| topic_write_text(x, path) | ||
| title <- paste(x$pkg, basename(x$path), sep = ":") | ||
| file.show(path, title = title) | ||
| } else if (type == "html") { | ||
| topic_write_html(x, path) | ||
|
|
||
| if (is_rstudio()) { | ||
| # This localhost URL is also part of getting RStudio to open in | ||
| # the help pane | ||
| port <- httpdPort() | ||
| url <- sprintf("http://localhost:%i/%s", port, doc_path) | ||
| } else { | ||
| url <- path | ||
| } | ||
|
|
||
| utils::browseURL(url) | ||
| utils::browseURL(path) | ||
| } | ||
| } | ||
|
|
||
| on_load( | ||
| httpdPort %<~% env_get(rlang::ns_env("tools"), "httpdPort") | ||
| ) | ||
|
|
||
| topic_write_text <- function(x, path) { | ||
| macros <- load_rd_macros(dirname(dirname(x$path))) | ||
| macros <- rdtools::pkg_macros(x$pkg) | ||
|
|
||
| tools::Rd2txt( | ||
| x$path, | ||
|
|
@@ -150,7 +96,7 @@ topic_write_text <- function(x, path) { | |
| } | ||
|
|
||
| topic_write_html <- function(x, path) { | ||
| macros <- load_rd_macros(dirname(dirname(x$path))) | ||
| macros <- rdtools::pkg_macros(x$pkg) | ||
|
|
||
| tools::Rd2HTML( | ||
| x$path, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,40 @@ | ||
| # Tools for indexing package documentation by alias, and for finding | ||
| # the rd file for a given topic (alias). | ||
|
|
||
| rd_files <- function(path) { | ||
| path <- pkg_path(path) | ||
| path_man <- package_file("man", path = path) | ||
| files <- dir(path_man, pattern = "\\.[Rr]d$", full.names = TRUE) | ||
| names(files) <- basename(files) | ||
| sort_ci(files) | ||
| } | ||
| # Tools for finding the Rd file for a given topic (alias). The alias index | ||
| # is maintained by rdtools, which caches it per package until it is reset | ||
| # with `dev_topic_index_reset()` (e.g. by `devtools::document()`). | ||
|
|
||
| #' @rdname dev_help | ||
| #' @export | ||
| dev_topic_find <- function(topic, dev_packages = NULL) { | ||
| topic <- dev_topic_parse(topic, dev_packages) | ||
|
|
||
| for (pkg_name in topic$pkg_names) { | ||
| path <- dev_topic_path(topic$topic, path = ns_path(pkg_name)) | ||
| if (!is.null(path)) { | ||
| return(list(path = path, pkg = pkg_name)) | ||
| } | ||
| } | ||
|
|
||
| NULL | ||
| } | ||
|
|
||
| dev_topic_parse <- function(topic, packages = NULL) { | ||
| stopifnot(is_string(topic)) | ||
|
|
||
| # Only treat `::`/`:::` as a namespace qualifier when preceded by a valid | ||
| # package name at the start. Otherwise `::` may appear inside an S7 method | ||
| # alias like `fn,pkg::Class-method`. | ||
| m <- regmatches(topic, regexec("^([a-zA-Z][a-zA-Z0-9.]*):::?(.+)$", topic))[[ | ||
| 1 | ||
| ]] | ||
| if (length(m) == 3) { | ||
| pkgs <- m[[2]] | ||
| topic <- m[[3]] | ||
| } else { | ||
| pkgs <- packages %||% dev_packages() | ||
| } | ||
| parsed <- rdtools::topic_split(topic) | ||
| topic <- parsed$topic | ||
|
|
||
| list( | ||
| topic = topic, | ||
| pkg_names = pkgs | ||
| ) | ||
| } | ||
| # Only search in-development packages, so that a qualified topic like | ||
| # `stats::mean` falls through to `utils::help()`. | ||
| packages <- parsed$package %||% dev_packages %||% dev_packages() | ||
| packages <- intersect(packages, dev_packages()) | ||
|
|
||
|
|
||
| dev_topic_path <- function(topic, path = ".") { | ||
| # Don't interpret the division operator as a path (#198) | ||
| if (is_string(topic, "/")) { | ||
| found <- rdtools::topic_find(topic, packages = packages) | ||
| if (is.null(found)) { | ||
| return(NULL) | ||
| } | ||
|
|
||
| path <- pkg_path(path) | ||
|
|
||
| # First see if a man file of that name exists | ||
| man <- package_file("man", topic, path = path) | ||
| if (file.exists(man)) { | ||
| return(man) | ||
| } | ||
|
|
||
| # Next, look in index | ||
| index <- dev_topic_index(path) | ||
| if (topic %in% names(index)) { | ||
| return(package_file("man", last(index[[topic]]), path = path)) | ||
| } | ||
|
|
||
| # Finally, try adding .Rd to name | ||
| man_rd <- package_file("man", paste0(topic, ".Rd"), path = path) | ||
| if (file.exists(man_rd)) { | ||
| return(man_rd) | ||
| } | ||
|
|
||
| NULL | ||
| path <- rdtools::topic_rd_path(topic, found$package) | ||
| list(path = path, pkg = found$package) | ||
| } | ||
|
|
||
|
|
||
| # Cache ------------------------------------------------------------------- | ||
|
|
||
| dev_topic_indices <- new.env(parent = emptyenv()) | ||
|
|
||
| #' @rdname dev_help | ||
| #' @param path Path to package. | ||
| #' @export | ||
| dev_topic_index <- function(path = ".") { | ||
| path <- pkg_path(path) | ||
| package <- pkg_name(path) | ||
|
|
||
| if (!exists(pkg_name(path), dev_topic_indices)) { | ||
| dev_topic_indices[[package]] <- build_topic_index(path) | ||
| } | ||
| dev_topic_indices[[package]] | ||
| rdtools::pkg_topics(pkg_path(path)) | ||
| } | ||
|
|
||
| #' @rdname dev_help | ||
| #' @param pkg_name Name of package. | ||
| #' @export | ||
| dev_topic_index_reset <- function(pkg_name) { | ||
| if (exists(pkg_name, dev_topic_indices)) { | ||
| rm(list = pkg_name, envir = dev_topic_indices) | ||
| } | ||
|
|
||
| rdtools::pkg_cache_reset(pkg_name) | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| # Topic index ------------------------------------------------------------- | ||
|
|
||
| build_topic_index <- function(path = ".") { | ||
| path <- pkg_path(path) | ||
|
|
||
| macros <- load_rd_macros(path) | ||
| rds <- rd_files(path) | ||
|
|
||
| # Pass `permissive = TRUE` to suppress warnings about unknown | ||
| # macros (#119). It is unlikely that a macro generates `name` or | ||
| # `alias` commands, so we shouldn't be missing any topics from | ||
| # unknown macros. | ||
| aliases <- function(path) { | ||
| parsed <- tools::parse_Rd(path, macros = macros, permissive = TRUE) | ||
| tags <- vapply(parsed, function(x) attr(x, "Rd_tag")[[1]], character(1)) | ||
| unlist(parsed[tags == "\\alias"]) | ||
| } | ||
|
|
||
| invert(lapply(rds, aliases)) | ||
| } | ||
|
|
||
| invert <- function(L) { | ||
| if (length(L) == 0) { | ||
| return(L) | ||
| } | ||
| t1 <- unlist(L) | ||
| names(t1) <- rep(names(L), lapply(L, length)) | ||
| tapply(names(t1), t1, c) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mention the year? I saw in removed code that the minimum version is from 2022?