From 7022723e4ae9b0a433212e3110d1949f39da35e1 Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Fri, 4 Oct 2024 11:37:56 -0500 Subject: [PATCH] minimal viable addin registration and keybindings are as in clipal, related to #6 --- R/addin.R | 24 +++++++++++++----------- inst/rstudio/addins.dcf | 7 ++++++- man/pal_cli.Rd | 33 +++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/R/addin.R b/R/addin.R index 90c40eb..dbff3b4 100644 --- a/R/addin.R +++ b/R/addin.R @@ -1,15 +1,10 @@ -rs_update_selection <- function( - context = rstudioapi::getActiveDocumentContext(), - id = "cli" - ) { +rs_update_selection <- function(context, role) { # see if the user has run `pal()` successfully already - if (exists(paste0(".last_pal_", id))) { - pal <- get(paste0(".last_pal_", id)) - } else if (exists(".last_pal")) { - pal <- .last_pal + if (exists(paste0(".last_pal_", role))) { + pal <- get(paste0(".last_pal_", role)) } else { tryCatch( - pal <- pal(id), + pal <- pal(role), error = function(e) { rstudioapi::showDialog("Error", "Unable to create a pal. See `?pal()`.") } @@ -25,8 +20,7 @@ rs_update_selection <- function( } tryCatch({ - output_str <- pal_chat(selection_text, pal) - output_str <- rlang::call2(.pal) + output_str <- .pal_chat(pal, selection_text) rstudioapi::modifyRange( selection$range, @@ -41,3 +35,11 @@ rs_update_selection <- function( rstudioapi::showDialog("Error", paste("The pal ran into an issue: ", e$message)) }) } + +rs_pal_cli <- function(context = rstudioapi::getActiveDocumentContext()) { + rs_update_selection(context = context, role = "cli") +} + +rs_pal_testthat <- function(context = rstudioapi::getActiveDocumentContext()) { + rs_update_selection(context = context, role = "testthat") +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 54d419b..735e15d 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -1,4 +1,9 @@ Name: Convert to cli Description: Replaces selected erroring code with a version adapted to cli -Binding: rs_.pal_cli +Binding: rs_pal_cli +Interactive: false + +Name: Convert to testthat +Description: Replaces selected unit testing code with a version adapted to testthat 3 +Binding: rs_pal_testthat Interactive: false diff --git a/man/pal_cli.Rd b/man/pal_cli.Rd index 83600a8..4a72aa2 100644 --- a/man/pal_cli.Rd +++ b/man/pal_cli.Rd @@ -57,7 +57,7 @@ and are generated with the default model, Claude Sonnet 3.5. pal("cli") #> -#> -- A cli pal using claude-3-5-sonnet-20240620. +#> -- A cli pal using gpt-4o-mini. }\if{html}{\out{}} The \code{pal_chat()} function takes an R expression that raises a condition @@ -67,7 +67,8 @@ little bit of markup: \if{html}{\out{
}}\preformatted{pal_chat(\{ rlang::abort("`save_pred` can only be used if the initial results saved predictions.") \}) -#> cli::cli_abort("\{.code save_pred\} can only be used if the initial results saved predictions.") +#> cli::cli_abort("The \{.code save_pred\} function can only be used if the initial +#> results saved predictions.") }\if{html}{\out{
}} The function knows to look for the most recently defined pal, but you can @@ -87,8 +88,10 @@ Some strange vector collapsing and funky line breaking: rlang::abort(msg) \}) #> cli::cli_abort( -#> "The provided \{.arg grid\} has parameter columns that have not been -#> marked for tuning by \{.fn tune\}: \{extra_grid_params\}." +#> c( +#> "The provided \{.code grid\} has the following parameter columns that have +#> not been marked for tuning by \{.code tune()\}: \{extra_grid_params\}." +#> ) #> ) }\if{html}{\out{}} @@ -108,8 +111,8 @@ A message that probably best lives as two separate elements: #> c( #> "Some model parameters require finalization but there are recipe #> parameters that require tuning.", -#> "i" = "Use \{.fn extract_parameter_set_dials\} to set parameter ranges -#> manually and supply the output to the \{.arg param_info\} argument." +#> "i" = "Please use \{.code extract_parameter_set_dials()\} to set parameter +#> ranges manually and supply the output to the \{.arg param_info\} argument." #> ) #> ) }\if{html}{\out{}} @@ -127,7 +130,9 @@ Gnarly ad-hoc pluralization: rlang::inform(msg) \}) #> cli::cli_inform( -#> "Creating pre-processing data to finalize unknown parameter\{?s\}: \{unk_names\}" +#> c( +#> "Creating pre-processing data to finalize unknown parameter\{?s:\}. \{unk_names\}" +#> ) #> ) }\if{html}{\out{}} @@ -141,8 +146,10 @@ Some \code{paste0()} wonk: )) \}) #> cli::cli_abort( -#> "The workflow has arguments to be tuned that are missing some -#> parameter objects: \{.val \{pset$id[!params]\}\}" +#> c( +#> "The workflow has arguments to be tuned that are missing some +#> parameter objects: \{pset$id[!params]\}." +#> ) #> ) }\if{html}{\out{}} @@ -160,8 +167,10 @@ if (!fine) \{ \} \}) #> cli::cli_abort( -#> "Argument \{.code \{deparse(cl$x)\}\} should be \{?a/an\} \{cls\} or \{.code NULL\}\{? -#> in \{.fn \{where\}\}\}" +#> c( +#> "Argument \{.code \{deparse(cl$x)\}\} should be a \{.cls \{cls\}\} or \{.code NULL\}.", +#> "i" = "\{if (!is.null(where)) \{where\}\}" +#> ) #> ) }\if{html}{\out{}} @@ -170,7 +179,7 @@ Sprintf-style statements aren't an issue: \if{html}{\out{
}}\preformatted{pal_chat(\{ abort(sprintf("No such '\%s' function: `\%s()`.", package, name)) \}) -#> cli::cli_abort("No such \{.pkg \{package\}\} function: \{.fn \{name\}\}.") +#> cli::cli_abort("No such '\{.pkg \{package\}\}' function: \{.fn \{name\}\}().") }\if{html}{\out{
}} }