From 2f94b3ace6fb5b5469d68a5ea3c5ceb860acfb8a Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Wed, 30 Oct 2024 16:23:54 -0500 Subject: [PATCH] reorganize main methods at the top, helpers below, in the order in which they appear in main methods --- R/rstudioapi.R | 85 +++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/R/rstudioapi.R b/R/rstudioapi.R index 69d08bd..a4cf900 100644 --- a/R/rstudioapi.R +++ b/R/rstudioapi.R @@ -1,4 +1,4 @@ -# replace selection with refactored code +# replace selection with refactored code --------------------------------------- rs_replace_selection <- function(context, role) { pal <- retrieve_pal(role) selection <- get_primary_selection(context) @@ -16,6 +16,48 @@ rs_replace_selection <- function(context, role) { stream_selection(selection, context, pal, n_lines_orig, selection_remainder) } +# prefix selection with new code ----------------------------------------------- +rs_prefix_selection <- function(context, role) { + pal <- retrieve_pal(role) + selection <- get_primary_selection(context) + + # add one blank line before the selection + rstudioapi::modifyRange(selection$range, paste0("\n", selection[["text"]]), context$id) + + # make the "current selection" that blank line + first_line <- selection$range + first_line$start[["column"]] <- 1 + first_line$end[["row"]] <- selection$range$start[["row"]] + first_line$end[["column"]] <- 100000 + selection$range <- first_line + rstudioapi::setCursorPosition(selection$range$start) + + # start streaming into it--will be interactively appended to if need be + stream_selection(selection, context, pal, n_lines_orig = 1) +} + +# suffix selection with new code ----------------------------------------------- +rs_suffix_selection <- function(context, role) { + pal <- retrieve_pal(role) + selection <- get_primary_selection(context) + + # 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 + stream_selection(selection, context, pal, n_lines_orig = 1) +} + +# helpers ---------------------------------------------------------------------- retrieve_pal <- function(role) { if (exists(paste0(".pal_last_", role))) { pal <- get(paste0(".pal_last_", role)) @@ -141,44 +183,3 @@ stream_selection_impl <- function(selection, context, pal, n_lines_orig, remaind rstudioapi::setCursorPosition(selection$range$start) } - -# prefix selection with new code ----------------------------------------------- -rs_prefix_selection <- function(context, role) { - pal <- retrieve_pal(role) - selection <- get_primary_selection(context) - - # add one blank line before the selection - rstudioapi::modifyRange(selection$range, paste0("\n", selection[["text"]]), context$id) - - # make the "current selection" that blank line - first_line <- selection$range - first_line$start[["column"]] <- 1 - first_line$end[["row"]] <- selection$range$start[["row"]] - first_line$end[["column"]] <- 100000 - selection$range <- first_line - rstudioapi::setCursorPosition(selection$range$start) - - # start streaming into it--will be interactively appended to if need be - stream_selection(selection, context, pal, n_lines_orig = 1) -} - -# suffix selection with new code ----------------------------------------------- -rs_suffix_selection <- function(context, role) { - pal <- retrieve_pal(role) - selection <- get_primary_selection(context) - - # 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 - stream_selection(selection, context, pal, n_lines_orig = 1) -}