Skip to content

Commit

Permalink
make pal addition and removal developer-facing
Browse files Browse the repository at this point in the history
`pal_add()` -> `.pal_add()`
`pal_remove()` -> `.pal_remove()`
  • Loading branch information
simonpcouch committed Oct 11, 2024
1 parent 0e7e0dd commit 6a7149d
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 56 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by roxygen2: do not edit by hand

S3method(print,pal_response)
export(.pal_add)
export(.pal_addin)
export(.pal_init)
export(pal_add)
import(rlang)
importFrom(elmer,content_image_file)
importFrom(glue,glue)
13 changes: 3 additions & 10 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Creating custom pals
#'
#' @description
#' Users can create custom pals using the `pal_add()` function; after passing
#' Users can create custom pals using the `.pal_add()` function; after passing
#' the function a role and prompt, the pal will be available on the command
#' palette.
#'
Expand All @@ -15,20 +15,13 @@
#' [cli pal][pal_cli] `"replace"`s the selection, while the
#' [roxygen pal][pal_roxygen] `"prefixes"` the selected code with documentation.
#'
#' @details
#' `pal_add()` will register the add-in as coming from the pal package
#' itself—because of this, custom pals will be deleted when the pal
#' package is reinstalled. Include `pal_add()` code in your `.Rprofile` or
#' make a pal extension package using `pal_add(package = TRUE)` to create
#' persistent custom pals.
#'
#' @returns
#' `NULL`, invisibly. Called for its side effect: a pal with role `role`
#' is registered with the pal package.
#'
#' @name pal_add_remove
#' @export
pal_add <- function(
.pal_add <- function(
role,
prompt = NULL,
interface = c("replace", "prefix", "suffix")
Expand All @@ -45,7 +38,7 @@ pal_add <- function(
}

#' @rdname pal_add_remove
pal_remove <- function(role) {
.pal_remove <- function(role) {
check_string(role)
if (!role %in% list_pals()) {
cli::cli_abort("No active pal with the given {.arg role}.")
Expand Down
6 changes: 3 additions & 3 deletions R/pal-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#' **Users typically should not need to call this function.**
#'
#' * Create new pals that will automatically be registered with this function
#' with [pal_add()].
#' with [.pal_add()].
#' * The [pal addin][.pal_addin()] will initialize needed pals on-the-fly.
#'
#' @param role The identifier for a pal prompt. By default one
#' of `r glue::glue_collapse(paste0("[", glue::double_quote(default_roles), "]", "[pal_", default_roles, "]"), ", ", last = " or ")`,
#' though custom pals can be added with [pal_add()].
#' though custom pals can be added with [.pal_add()].
#' @param fn A `new_*()` function, likely from the elmer package. Defaults
#' to [elmer::chat_claude()]. To set a persistent alternative default,
#' set the `.pal_fn` option; see examples below.
Expand Down Expand Up @@ -52,7 +52,7 @@
if (!role %in% list_pals()) {
cli::cli_abort(c(
"No pals with role {.arg {role}} registered.",
"i" = "See {.fn pal_add}."
"i" = "See {.fn .pal_add}."
))
}

Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
prompt <- paste0(readLines(prompts[idx]), collapse = "\n")
interface <- roles_and_interfaces[[idx]][2]

pal_add(role = role, prompt = prompt, interface = interface)
.pal_add(role = role, prompt = prompt, interface = interface)
}
}

Expand Down
8 changes: 4 additions & 4 deletions man/dot-pal_init.Rd

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

19 changes: 6 additions & 13 deletions man/pal_add_remove.Rd

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

2 changes: 1 addition & 1 deletion man/pal_cli.Rd

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

2 changes: 1 addition & 1 deletion man/pal_roxygen.Rd

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

2 changes: 1 addition & 1 deletion man/pal_testthat.Rd

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

24 changes: 12 additions & 12 deletions tests/testthat/_snaps/pal-add-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,48 @@
# pal addition with bad inputs

Code
pal_add(role = identity, prompt = "hey")
.pal_add(role = identity, prompt = "hey")
Condition
Error in `pal_add()`:
Error in `.pal_add()`:
! `role` must be a single string, not a function.

---

Code
pal_add(role = "sillyhead", prompt = "hey", interface = "no")
.pal_add(role = "sillyhead", prompt = "hey", interface = "no")
Condition
Error in `pal_add()`:
Error in `.pal_add()`:
! `interface` should be one of "replace", "prefix", or "suffix".

---

Code
pal_add(role = "sillyhead", prompt = "hey", interface = "suffix")
.pal_add(role = "sillyhead", prompt = "hey", interface = "suffix")
Condition
Error in `pal_add()`:
Error in `.pal_add()`:
! Suffixing not implemented yet.

---

Code
pal_add(role = "sillyhead", prompt = "hey", interface = NULL)
.pal_add(role = "sillyhead", prompt = "hey", interface = NULL)
Condition
Error in `pal_add()`:
Error in `.pal_add()`:
! `interface` should be one of "replace", "prefix", or "suffix".

# pal remove with bad inputs

Code
pal_remove(role = identity)
.pal_remove(role = identity)
Condition
Error in `pal_remove()`:
Error in `.pal_remove()`:
! `role` must be a single string, not a function.

---

Code
pal_remove(role = "notAnActivePal")
.pal_remove(role = "notAnActivePal")
Condition
Error in `pal_remove()`:
Error in `.pal_remove()`:
! No active pal with the given `role`.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/pal-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@
Condition
Error in `.pal_init()`:
! No pals with role `beep bop boop` registered.
i See `pal_add()`.
i See `.pal_add()`.

16 changes: 8 additions & 8 deletions tests/testthat/test-pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("pal addition and removal works", {
withr::local_options(.pal_fn = NULL, .pal_args = NULL)

boop_prompt <- "just reply with beep bop boop regardless of input"
pal_add("boopery", boop_prompt)
.pal_add("boopery", boop_prompt)

expect_equal(.pal_prompt_boopery, boop_prompt)
expect_true(is_function(.pal_rs_boopery))
Expand All @@ -18,7 +18,7 @@ test_that("pal addition and removal works", {
res <- pal_boopery$chat("hey there")
expect_true(grepl("bop", res))

pal_remove("boopery")
.pal_remove("boopery")

expect_false(".pal_last_boopery" %in% names(pal_env()))
expect_false(".pal_prompt_boopery" %in% names(pal_env()))
Expand All @@ -28,33 +28,33 @@ test_that("pal addition and removal works", {
test_that("pal addition with bad inputs", {
expect_snapshot(
error = TRUE,
pal_add(role = identity, prompt = "hey")
.pal_add(role = identity, prompt = "hey")
)

# TODO: decide on `prompt` interface and test


expect_snapshot(
error = TRUE,
pal_add(role = "sillyhead", prompt = "hey", interface = "no")
.pal_add(role = "sillyhead", prompt = "hey", interface = "no")
)
expect_snapshot(
error = TRUE,
pal_add(role = "sillyhead", prompt = "hey", interface = "suffix")
.pal_add(role = "sillyhead", prompt = "hey", interface = "suffix")
)
expect_snapshot(
error = TRUE,
pal_add(role = "sillyhead", prompt = "hey", interface = NULL)
.pal_add(role = "sillyhead", prompt = "hey", interface = NULL)
)
})

test_that("pal remove with bad inputs", {
expect_snapshot(
error = TRUE,
pal_remove(role = identity)
.pal_remove(role = identity)
)
expect_snapshot(
error = TRUE,
pal_remove(role = "notAnActivePal")
.pal_remove(role = "notAnActivePal")
)
})

0 comments on commit 6a7149d

Please sign in to comment.