From 8dd60109b92d9e9e9ac35c4aa2127c42feecf181 Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Thu, 10 Oct 2024 16:29:23 -0500 Subject: [PATCH] use common prefix for `pal_env()` objects `.last_pal_` -> `.pal_last_` `rs_pal_` -> `.pal_rs_` `system_prompt_` -> `.pal_prompt_` --- R/gadget.R | 4 ++-- R/pal-add-remove.R | 8 ++++---- R/pal-class.R | 2 +- R/pal-package.R | 2 +- R/pal.R | 2 +- R/rstudioapi.R | 14 +++++++------- R/utils.R | 12 ++++++------ man/pal.Rd | 2 +- tests/testthat/_snaps/utils.md | 8 ++++---- tests/testthat/test-pal-add-remove.R | 16 ++++++++-------- tests/testthat/test-utils.R | 8 ++++---- 11 files changed, 39 insertions(+), 39 deletions(-) diff --git a/R/gadget.R b/R/gadget.R index f890b10..84709e0 100644 --- a/R/gadget.R +++ b/R/gadget.R @@ -1,6 +1,6 @@ .pal <- function() { pal_fn <- .pal_app() - if (is.null(pal_fn) || identical(pal_fn, "rs_pal_")) { + if (is.null(pal_fn) || identical(pal_fn, ".pal_rs__")) { return(NULL) } try_fetch( @@ -39,7 +39,7 @@ server = TRUE ) shiny::observeEvent(input$done, { - shiny::stopApp(returnValue = paste0("rs_pal_", input$pal)) + shiny::stopApp(returnValue = paste0(".pal_rs__", input$pal)) }) shiny::onStop(function() { shiny::stopApp(returnValue = NULL) diff --git a/R/pal-add-remove.R b/R/pal-add-remove.R index 9b6ccd6..01f1746 100644 --- a/R/pal-add-remove.R +++ b/R/pal-add-remove.R @@ -53,11 +53,11 @@ pal_remove <- function(role) { env_unbind( pal_env(), - c(paste0("system_prompt_", role), paste0("rs_pal_", role)) + c(paste0(".pal_prompt_", role), paste0(".pal_rs__", role)) ) - if (paste0(".last_pal_", role) %in% names(pal_env())) { - env_unbind(pal_env(), paste0(".last_pal_", role)) + if (paste0(".pal_last_", role) %in% names(pal_env())) { + env_unbind(pal_env(), paste0(".pal_last_", role)) } invisible() @@ -96,5 +96,5 @@ parse_interface <- function(interface, role, call = caller_env()) { } ) - paste0("rs_pal_", role) + paste0(".pal_rs__", role) } diff --git a/R/pal-class.R b/R/pal-class.R index 947fd6c..7e0369c 100644 --- a/R/pal-class.R +++ b/R/pal-class.R @@ -8,7 +8,7 @@ Pal <- R6::R6Class( args <- modifyList(default_args, args) args$system_prompt <- get( - paste0("system_prompt_", role), + paste0(".pal_prompt_", role), envir = search_envs()[["pkg:pal"]] ) diff --git a/R/pal-package.R b/R/pal-package.R index c680fd3..1e5245b 100644 --- a/R/pal-package.R +++ b/R/pal-package.R @@ -6,7 +6,7 @@ ## usethis namespace: end NULL -utils::globalVariables(c(".last_pal", "modifyList")) +utils::globalVariables(c(".pal_last", "modifyList")) # address "not imported from" R CMD check error #' @importFrom elmer content_image_file diff --git a/R/pal.R b/R/pal.R index 94095fc..244fee5 100644 --- a/R/pal.R +++ b/R/pal.R @@ -19,7 +19,7 @@ #' #' @details #' Upon successfully creating a pal, this function will assign the -#' result to the search path as `.last_pal`. +#' result to the search path as `.pal_last`. #' #' If you have an Anthropic API key (or another API key and the `pal_*()` #' options) set and this package installed, you are ready to using the add-in diff --git a/R/rstudioapi.R b/R/rstudioapi.R index a9a0be7..e981403 100644 --- a/R/rstudioapi.R +++ b/R/rstudioapi.R @@ -1,8 +1,8 @@ # replace selection with refactored code rs_replace_selection <- function(context, role) { # check if pal exists - if (exists(paste0(".last_pal_", role))) { - pal <- get(paste0(".last_pal_", role)) + if (exists(paste0(".pal_last_", role))) { + pal <- get(paste0(".pal_last_", role)) } else { tryCatch( pal <- pal(role), @@ -114,8 +114,8 @@ stream_selection <- function(selection, context, pal, n_lines_orig) { # prefix selection with new code ----------------------------------------------- rs_prefix_selection <- function(context, role) { # check if pal exists - if (exists(paste0(".last_pal_", role))) { - pal <- get(paste0(".last_pal_", role)) + if (exists(paste0(".pal_last_", role))) { + pal <- get(paste0(".pal_last_", role)) } else { tryCatch( pal <- pal(role), @@ -154,14 +154,14 @@ rs_prefix_selection <- function(context, role) { } # pal-specific helpers --------------------------------------------------------- -rs_pal_cli <- function(context = rstudioapi::getActiveDocumentContext()) { +.pal_rs__cli <- function(context = rstudioapi::getActiveDocumentContext()) { rs_replace_selection(context = context, role = "cli") } -rs_pal_testthat <- function(context = rstudioapi::getActiveDocumentContext()) { +.pal_rs__testthat <- function(context = rstudioapi::getActiveDocumentContext()) { rs_replace_selection(context = context, role = "testthat") } -rs_pal_roxygen <- function(context = rstudioapi::getActiveDocumentContext()) { +.pal_rs__roxygen <- function(context = rstudioapi::getActiveDocumentContext()) { rs_prefix_selection(context = context, role = "roxygen") } diff --git a/R/utils.R b/R/utils.R index fafe655..0f9cba0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,20 +1,20 @@ # helpers for the pal environment ---------------------------------------------- .stash_last_pal <- function(x) { pal_env <- pal_env() - pal_env[[paste0(".last_pal_", x$role)]] <- x - pal_env[[".last_pal"]] <- x + pal_env[[paste0(".pal_last_", x$role)]] <- x + pal_env[[".pal_last"]] <- x invisible(NULL) } .stash_binding <- function(role, fn) { pal_env <- pal_env() - pal_env[[paste0("rs_pal_", role)]] <- fn + pal_env[[paste0(".pal_rs__", role)]] <- fn invisible(NULL) } .stash_prompt <- function(prompt, role) { pal_env <- pal_env() - pal_env[[paste0("system_prompt_", role)]] <- prompt + pal_env[[paste0(".pal_prompt_", role)]] <- prompt invisible(NULL) } @@ -31,8 +31,8 @@ pal_env <- function() { list_pals <- function() { pal_env <- pal_env() pal_env_names <- names(pal_env) - prompt_names <- grep("system_prompt_", names(pal_env), value = TRUE) - gsub("system_prompt_", "", prompt_names) + prompt_names <- grep(".pal_prompt_", names(pal_env), value = TRUE) + gsub(".pal_prompt_", "", prompt_names) } # ad-hoc check functions ------------------------------------------------------- diff --git a/man/pal.Rd b/man/pal.Rd index 95b8e1a..a158393 100644 --- a/man/pal.Rd +++ b/man/pal.Rd @@ -34,7 +34,7 @@ suggest Cmd + Ctrl + P), select the pal, and watch your code be rewritten. } \details{ Upon successfully creating a pal, this function will assign the -result to the search path as \code{.last_pal}. +result to the search path as \code{.pal_last}. If you have an Anthropic API key (or another API key and the \verb{pal_*()} options) set and this package installed, you are ready to using the add-in diff --git a/tests/testthat/_snaps/utils.md b/tests/testthat/_snaps/utils.md index 0b7e9a6..d7e4d86 100644 --- a/tests/testthat/_snaps/utils.md +++ b/tests/testthat/_snaps/utils.md @@ -1,7 +1,7 @@ -# .last_pal is up to date with most recent pal +# .pal_last is up to date with most recent pal Code - .last_pal + .pal_last Message -- A cli pal using claude-3-5-sonnet-20240620. @@ -9,7 +9,7 @@ --- Code - .last_pal_cli + .pal_last_cli Message -- A cli pal using claude-3-5-sonnet-20240620. @@ -17,7 +17,7 @@ --- Code - .last_pal + .pal_last Message -- A cli pal using gpt-4o-mini. diff --git a/tests/testthat/test-pal-add-remove.R b/tests/testthat/test-pal-add-remove.R index e9c3d01..4a146d3 100644 --- a/tests/testthat/test-pal-add-remove.R +++ b/tests/testthat/test-pal-add-remove.R @@ -6,23 +6,23 @@ test_that("pal addition and removal works", { boop_prompt <- "just reply with beep bop boop regardless of input" pal_add("boopery", boop_prompt) - expect_equal(system_prompt_boopery, boop_prompt) - expect_true(is_function(rs_pal_boopery)) - expect_true("system_prompt_boopery" %in% names(pal_env())) - expect_true("rs_pal_boopery" %in% names(pal_env())) + expect_equal(.pal_prompt_boopery, boop_prompt) + expect_true(is_function(.pal_rs__boopery)) + expect_true(".pal_prompt_boopery" %in% names(pal_env())) + expect_true(".pal_rs__boopery" %in% names(pal_env())) pal_boopery <- pal("boopery") expect_snapshot(pal_boopery) - expect_true(".last_pal_boopery" %in% names(pal_env())) + expect_true(".pal_last_boopery" %in% names(pal_env())) res <- pal_boopery$chat("hey there") expect_true(grepl("bop", res)) pal_remove("boopery") - expect_false(".last_pal_boopery" %in% names(pal_env())) - expect_false("system_prompt_boopery" %in% names(pal_env())) - expect_false("rs_pal_boopery" %in% names(pal_env())) + expect_false(".pal_last_boopery" %in% names(pal_env())) + expect_false(".pal_prompt_boopery" %in% names(pal_env())) + expect_false(".pal_rs__boopery" %in% names(pal_env())) }) test_that("pal addition with bad inputs", { diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 4de12f1..2113d2b 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -1,13 +1,13 @@ -test_that(".last_pal is up to date with most recent pal", { +test_that(".pal_last is up to date with most recent pal", { skip_if(identical(Sys.getenv("ANTHROPIC_API_KEY"), "")) skip_if(identical(Sys.getenv("OPENAI_API_KEY"), "")) skip_if_not_installed("withr") withr::local_options(.pal_fn = NULL, .pal_args = NULL) pal("cli") - expect_snapshot(.last_pal) - expect_snapshot(.last_pal_cli) + expect_snapshot(.pal_last) + expect_snapshot(.pal_last_cli) pal("cli", "chat_openai", model = "gpt-4o-mini") - expect_snapshot(.last_pal) + expect_snapshot(.pal_last) })