Skip to content

Commit

Permalink
add suffixing (closes #54)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 30, 2024
1 parent 37e5136 commit c96aa9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
5 changes: 0 additions & 5 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ parse_interface <- function(interface, role, call = caller_env()) {
)
}

if (interface == "suffix") {
# TODO: implement suffixing
cli::cli_abort("Suffixing not implemented yet.", call = call)
}

.stash_binding(
role,
function(context = rstudioapi::getActiveDocumentContext()) {
Expand Down
42 changes: 42 additions & 0 deletions R/rstudioapi.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,45 @@ rs_prefix_selection <- function(context, role) {
)
}

# suffix selection with new code -----------------------------------------------
rs_suffix_selection <- function(context, role) {
# check if pal exists
if (exists(paste0(".pal_last_", role))) {
pal <- get(paste0(".pal_last_", role))
} else {
tryCatch(
pal <- .init_pal(role),
error = function(e) {
rstudioapi::showDialog("Error", "Unable to create a pal. See `?.init_pal()`.")
return(NULL)
}
)
}

selection <- rstudioapi::primary_selection(context)

if (selection[["text"]] == "") {
rstudioapi::showDialog("Error", "No code selected. Please highlight some code first.")
return(NULL)
}

# add one blank line after the selection
rstudioapi::modifyRange(selection$range, paste0(selection[["text"]], "\n"), context$id)

# make the "current selection" that blank line
last_line <- selection$range
last_line$start[["row"]] <- selection$range$end[["row"]] + 1
last_line$end[["row"]] <- selection$range$end[["row"]] + 1
last_line$start[["column"]] <- 1
last_line$end[["column"]] <- 100000
selection$range <- last_line
rstudioapi::setCursorPosition(selection$range$start)

# start streaming into it--will be interactively appended to if need be
tryCatch(
stream_selection(selection, context, pal, n_lines_orig = 1),
error = function(e) {
rstudioapi::showDialog("Error", paste("The pal ran into an issue: ", e$message))
}
)
}
8 changes: 0 additions & 8 deletions tests/testthat/_snaps/pal-add-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
Error in `.pal_add()`:
! `interface` should be one of "replace", "prefix", or "suffix".

---

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

---

Code
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ test_that("pal addition with bad inputs", {
error = TRUE,
.pal_add(role = "sillyhead", prompt = "hey", interface = "no")
)
expect_snapshot(
error = TRUE,
.pal_add(role = "sillyhead", prompt = "hey", interface = "suffix")
)
expect_snapshot(
error = TRUE,
.pal_add(role = "sillyhead", prompt = "hey", interface = NULL)
Expand Down

0 comments on commit c96aa9a

Please sign in to comment.