Skip to content

Commit

Permalink
fix(fuzzyhelp): broken addin
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy committed Mar 28, 2024
1 parent a237c82 commit 7b056e5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
48 changes: 37 additions & 11 deletions R/fuzzyhelp.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ create_server <- function(method = c("fzf", "lv")) {
#' @param background Whether to run a shiny gadget in a background process.
#' The default value is `TRUE` and can be changed by
#' `option(fuzzyhelp.background = FALSE)`.
#' @inheritParams shiny::runGadget
#'
#' @note
#' The default fuzzy match algorithm is a simplified version of
Expand All @@ -397,13 +398,14 @@ create_server <- function(method = c("fzf", "lv")) {
fuzzyhelp <- function(
query = "",
method = getOption("fuzzyhelp.method", "fzf"),
background = getOption("fuzzyhelp.background", TRUE)) {
background = getOption("fuzzyhelp.background", TRUE),
viewer = shiny::paneViewer()) {
app <- create_ui(query)
server <- create_server(method)

# Create new gadget on foreground
if (!background) {
shiny::runGadget(app, server)
shiny::runGadget(app, server, viewer = viewer)
return(invisible(NULL))
}

Expand All @@ -412,32 +414,56 @@ fuzzyhelp <- function(
.env$fuzzyhelp_url <- tempfile()
}

# Re-use existing gadget
# View existing gadget
if (fuzzyhelp_bg_view(viewer, .env$fuzzyhelp_url)) {
return(.env$fuzzyhelp)
}

# Create new gadget on background
if (rstudioapi::isAvailable()) {
.env$fuzzyhelp <- fuzzyhelp_bg_start(app, server, identity)
for (i in 1:10) { # TODO: implement exponential backoff
if (!fuzzyhelp_bg_view(shiny::paneViewer(), .env$huzzyhelp_url)) {
message("Waiting for shiny app to start")
Sys.sleep(0.5)
}
}
} else {
.env$fuzzyhelp <- fuzzyhelp_bg_start(app, server, viewer)
}
return(.env$fuzzyhelp)
}

fuzzyhelp_bg_view <- function(viewer, url) {
if (
!is.null(.env$fuzzyhelp) &&
is.null(.env$fuzzyhelp$get_exit_status()) &&
file.exists(.env$fuzzyhelp_url)
) {
url <- readLines(.env$fuzzyhelp_url)[1L]
if (url != "") {
shiny::paneViewer()(url)
return(.env$fuzzyhelp)
viewer(url)
return(TRUE)
}
}
return(FALSE)
}

# Create new gadget on background
.env$fuzzyhelp <- callr::r_bg(
function(..., .env) {
fuzzyhelp_bg_start <- function(app, server, viewer) {
callr::r_bg(
function(..., .env, base_viewer) {
viewer <- function(url) {
writeLines(url, .env$fuzzyhelp_url)
shiny::paneViewer()(url)
base_viewer(url)
}
shiny::runGadget(..., viewer = viewer)
},
args = list(app = app, server = server, .env = .env),
args = list(app = app, server = server, .env = .env, base_viewer = viewer),
env = Sys.getenv(),
package = TRUE
)
}

return(.env$fuzzyhelp)
fuzzyhelp_addin <- function() {
fuzzyhelp(background = TRUE)
}
2 changes: 1 addition & 1 deletion inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Name: Fuzzy Search on R Help
Description: Search and view help files with fuzzy queries.
Binding: fuzzyhelp
Binding: fuzzyhelp_addin
Interactive: true
7 changes: 6 additions & 1 deletion man/fuzzyhelp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b056e5

Please sign in to comment.