Skip to content

Commit

Permalink
transition to streamy (closes #73)
Browse files Browse the repository at this point in the history
simonpcouch committed Jan 24, 2025
1 parent 1cd8173 commit 79d394a
Showing 4 changed files with 49 additions and 217 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ Imports:
miniUI (>= 0.1.1.1),
rlang (>= 1.1.4),
rstudioapi (>= 0.17.1),
shiny (>= 1.9.1)
shiny (>= 1.9.1),
streamy
Suggests:
gt,
knitr,
@@ -34,5 +35,5 @@ Suggests:
tibble,
withr
Remotes:
tidyverse/ellmer
simonpcouch/streamy
VignetteBuilder: knitr
12 changes: 9 additions & 3 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
@@ -78,9 +78,15 @@ parse_interface <- function(interface, role, call = caller_env()) {
.stash_binding(
role,
function(context = rstudioapi::getActiveDocumentContext()) {
do.call(
paste0("rs_", interface, "_selection"),
args = list(context = context, role = role)
selection <- get_primary_selection(context)
pal <- retrieve_pal(role)$clone()
streamy::stream(
generator =
# TODO: this is gnarly--revisit when pals are just Chats
# or ensure selection$text isn't substituted
pal[[".__enclos_env__"]][["private"]]$.stream(selection$text),
context = context,
interface = interface
)
}
)
212 changes: 0 additions & 212 deletions R/rstudioapi.R

This file was deleted.

37 changes: 37 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -31,6 +31,43 @@ list_pals <- function() {
gsub(".pal_prompt_", "", prompt_names)
}

retrieve_pal <- function(role) {
if (exists(paste0(".pal_last_", role))) {
pal <- get(paste0(".pal_last_", role))
} else {
tryCatch(
pal <- .init_pal(role),
error = function(cnd) {
# rethrow condition message directly rather than setting
# `cli::cli_abort(parent)` so that rstudioapi::showDialog is able
# to handle the formatting (#62)
stop(condition_message(cnd), call. = FALSE)
return(NULL)
}
)
}

pal
}

condition_message <- function(cnd) {
if ("message" %in% names(cnd)) {
return(cnd$message)
}

cnd_message(cnd, inherit = FALSE, prefix = FALSE)
}

get_primary_selection <- function(context) {
selection <- rstudioapi::primary_selection(context)

if (selection[["text"]] == "") {
stop("No code selected. Please highlight some code first.", call. = FALSE)
}

selection
}

# ad-hoc check helpers -------
check_role <- function(role,
allow_default = !is.null(getOption(".pal_on_load")),

0 comments on commit 79d394a

Please sign in to comment.