Skip to content

Commit

Permalink
insert native pipe now honours RStudio and project setting
Browse files Browse the repository at this point in the history
  • Loading branch information
msberends committed Jun 20, 2024
1 parent 29b7115 commit ed35dc1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: certetoolbox
Title: A Certe R Package for Miscellaneous Functions
Version: 1.13.1
Version: 1.13.2
Authors@R: c(
person(given = c("Erwin", "E.", "A."),
family = "Hassing",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ importFrom(readr,write_tsv)
importFrom(rstudioapi,document_position)
importFrom(rstudioapi,document_range)
importFrom(rstudioapi,getActiveDocumentContext)
importFrom(rstudioapi,getActiveProject)
importFrom(rstudioapi,insertText)
importFrom(rstudioapi,modifyRange)
importFrom(rstudioapi,readRStudioPreference)
importFrom(rstudioapi,showPrompt)
importFrom(stringr,str_detect)
importFrom(stringr,str_match)
Expand Down
30 changes: 26 additions & 4 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,24 @@ addin_insert_like <- function() {
}
}

#' @importFrom rstudioapi getActiveDocumentContext insertText modifyRange document_range document_position
#' @importFrom rstudioapi getActiveDocumentContext insertText modifyRange document_range document_position readRStudioPreference getActiveProject
#' @importFrom yaml read_yaml
addin_insert_pipe <- function() {
# we want Shift + Ctrl/Cmd + M to iterate over |>, %>%, %$%

# general RStudio option to test for 'native pipe' checkbox
uses_native_pipe <- readRStudioPreference("insert_native_pipe_operator", TRUE)

Check warning on line 68 in R/addins.R

View workflow job for this annotation

GitHub Actions / lintr / lintr

file=R/addins.R,line=68,col=23,[object_usage_linter] no visible global function definition for 'readRStudioPreference'

Check warning on line 68 in R/addins.R

View workflow job for this annotation

GitHub Actions / lintr / lintr

file=R/addins.R,line=68,col=23,[object_usage_linter] no visible global function definition for 'readRStudioPreference'

Check warning on line 68 in R/addins.R

View workflow job for this annotation

GitHub Actions / lintr / lintr

file=R/addins.R,line=68,col=23,[object_usage_linter] no visible global function definition for 'readRStudioPreference'

Check warning on line 68 in R/addins.R

View workflow job for this annotation

GitHub Actions / lintr / lintr

file=R/addins.R,line=68,col=23,[object_usage_linter] no visible global function definition for 'readRStudioPreference'

Check warning on line 68 in R/addins.R

View workflow job for this annotation

GitHub Actions / lintr / lintr

file=R/addins.R,line=68,col=23,[object_usage_linter] no visible global function definition for 'readRStudioPreference'
if (!is.null(getActiveProject())) {
# project-specific option could be different, read the Rproj file as YAML
project_file <- list.files(".", pattern = "[.]Rproj$")[1L]
if (!is.na(project_file)) {
project_properties <- read_yaml(project_file)
if ("UseNativePipeOperator" %in% names(project_properties)) {
uses_native_pipe <- isTRUE(project_properties$UseNativePipeOperator)
}
}
}

context <- getActiveDocumentContext()
current_row <- context$selection[[1]]$range$end[1]
current_col <- context$selection[[1]]$range$end[2]
Expand All @@ -71,7 +85,11 @@ addin_insert_pipe <- function() {
"[|][>]", "|",
"[%][>][%]", "|",
"[%][\\][$%]", ")")) {
insertText(" |> ")
if (uses_native_pipe == TRUE) {
insertText(" |> ")
} else {
insertText(" %>% ")
}
return(invisible())
}

Expand All @@ -90,12 +108,16 @@ addin_insert_pipe <- function() {
id = context$id)
}

if (pos_preceded_by(" |> ")) {
if (pos_preceded_by(" |> ") && uses_native_pipe == TRUE) {
replace_pos(" |> ", with = " %>% ")
} else if (pos_preceded_by(" %>% ")) {
replace_pos(" %>% ", with = " %$% ")
} else if (pos_preceded_by(" %$% ")) {
} else if (pos_preceded_by(" %$% ") && uses_native_pipe == TRUE) {
replace_pos(" %$% ", with = " |> ")
} else if (pos_preceded_by(" %$% ") && uses_native_pipe == FALSE) {
replace_pos(" %$% ", with = " %>% ")
} else if (uses_native_pipe == FALSE) {
insertText(" %>% ")
} else {
insertText(" |> ")
}
Expand Down

0 comments on commit ed35dc1

Please sign in to comment.