Skip to content

Commit

Permalink
Use more generic x-r-run over ide:run (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Apr 19, 2024
1 parent db4dd9a commit 2357de6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions R/rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ roclet_output.roclet_rd <- function(x, results, base_path, ..., is_first = FALSE

names <- unname(map_chr(results, ~ .$get_name()[[1]]))
if (length(names) > 0) {
hrefs <- paste0("ide:run:pkgload::dev_help('", names, "')")
commands <- paste0("pkgload::dev_help('", names, "')")
} else {
hrefs <- character()
commands <- character()
}

# Always check for roxygen2 header before overwriting NAMESPACE (#436),
# even when running for the first time
mapply(write_if_different, paths, contents, href = hrefs)
mapply(write_if_different, paths, contents, command = commands)

if (!is_first) {
# Automatically delete any files in man directory that were generated
Expand Down
8 changes: 5 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ nice_name <- function(x) {
x
}

write_if_different <- function(path, contents, href = NULL, check = TRUE) {
write_if_different <- function(path, contents, command = NULL, check = TRUE) {
if (!file.exists(dirname(path))) {
dir.create(dirname(path), showWarnings = FALSE)
}
Expand All @@ -87,8 +87,10 @@ write_if_different <- function(path, contents, href = NULL, check = TRUE) {
))
FALSE
} else {
if (!is.null(href)) {
name <- cli::style_hyperlink(name, href)
if (!is.null(command)) {
scheme <- "x-r-run"
url <- paste0(scheme, ":", command)
name <- cli::style_hyperlink(name, url)
}
cli::cli_inform("Writing {.path {name}}")

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@
Output
[1] FALSE

# write_if_different produces correct command hyperlink

Code
write_if_different(path, "a <- 2", command = "rlang::inform('hi')")
Message
Writing ']8;;x-r-run:rlang::inform('hi')test.R]8;;'
Output
[1] TRUE

10 changes: 10 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ test_that("write_if_different and end of line", {
expect_message(write_if_different(tmp, cnt_mix, check = FALSE), "Writing ")
expect_identical(readBin(tmp, "raw", 100), readBin(tmp_win, "raw", 100))
})

test_that("write_if_different produces correct command hyperlink", {
testthat::local_reproducible_output(hyperlinks = TRUE)

dir <- withr::local_tempdir()
path <- file.path(dir, "test.R")

write_lines(made_by("#"), path)
expect_snapshot(write_if_different(path, "a <- 2", command = "rlang::inform('hi')"))
})

0 comments on commit 2357de6

Please sign in to comment.