Skip to content

Commit

Permalink
reorganize
Browse files Browse the repository at this point in the history
main methods at the top, helpers below, in the order in which they appear in main methods
  • Loading branch information
simonpcouch committed Oct 30, 2024
1 parent 376b209 commit 2f94b3a
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions R/rstudioapi.R
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}

0 comments on commit 2f94b3a

Please sign in to comment.