Skip to content

Commit

Permalink
Make rstudioapi calls work better outside RStudio
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Aug 14, 2024
1 parent e3c704a commit 3f6f1bd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ that will passed on to `proj_list()`

* `file_outline()` result is now a simpler data frame. The cli links are now created in the print method. (which makes more sense for truncation)

* More safety around rstudioapi. (make `open_rs_doc()` work in Positron.)

* `use_todo("global::todo")` no longer works out of the box. You need to set `options(reuseme.global_todo = fs::path("Documents"))` explicitly (in .Rprofile) for example to make sure
reuseme can write in a directory.

Expand Down
33 changes: 22 additions & 11 deletions R/open.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,26 @@ open_rs_doc <- function(path, line = -1L, col = -1L, move_cursor = TRUE) {
cli::cli_abort("Internal error, you can't specify col only.")
}

doc_id <- rstudioapi::documentOpen(path = path, line = line, col = col, moveCursor = move_cursor)
if (is.null(doc_id)) {
# FIXME why is this code like this?
file_pos_string <- path
if (line != -1L) pos_string <- paste0(pos_string, ":", line)
if (col != -1L) pos_string <- paste0(pos_string, ":", col)
cli::cli_bullets()
}
invisible(doc_id)
if (is_rstudio() && rstudioapi::hasFun("documentOpen")) {
doc_id <- rstudioapi::documentOpen(path = path, line = line, col = col, moveCursor = move_cursor)
if (is.null(doc_id)) {
# FIXME why is this code like this?
file_pos_string <- path
if (line != -1L) pos_string <- paste0(pos_string, ":", line)
if (col != -1L) pos_string <- paste0(pos_string, ":", col)
cli::cli_bullets()
}
return(invisible(doc_id))
}

# Fallback if rstudioapi not available
utils::file.edit(path)
if (line != -1L || col != -1L) {
cli::cli_inform(c(
"Jump to {.file {path:{line}:{col}}}"
))
}
invisible(path)
}

active_rs_proj <- function() {
Expand All @@ -46,7 +57,7 @@ active_rs_doc <- function() {
if (!interactive() && !is_rstudio()) {
return("Non-existing doc")
}
if (!is_rstudio()) {
if (!is_rstudio(f = "documentPath")) {
cli::cli_abort("Not in RStudio.")
}
unsaved_doc <- tryCatch(rstudioapi::documentPath(), error = function(e) TRUE)
Expand Down Expand Up @@ -393,7 +404,7 @@ normalize_proj_and_path <- function(path, call = caller_env()) {
#' @returns NULL, called for its side effects.
#' @export
active_rs_doc_nav <- function(path = active_rs_doc()) {
if (!is_rstudio() || !interactive()) {
if (!is_rstudio(f = "filesPaneNavigate") || !interactive()) {
cli::cli_abort("Must use in RStudio interactive sessions.")
}
if (is.null(path)) {
Expand Down
2 changes: 1 addition & 1 deletion R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ construct_outline_link <- function(.data) {
if (is.null(dir_common) || !nzchar(dir_common)) {
dir_common <- "Don't remove anything if not null"
}
.data$rs_version <- ifelse(!is_rstudio("2023.12.0.274") && is_rstudio(), ".", "")
.data$rs_version <- ifelse(!is_rstudio("2023.12.0.274") && is_rstudio(f = "documentOpen"), ".", "")
.data$has_inline_markup <- dplyr::coalesce(stringr::str_detect(.data$outline_el, "\\{|\\}"), FALSE)
.data$is_saved_doc <- is_saved_doc
# Only show `complete_todo()` links for TODO.R files or active file in interactive sessions
Expand Down
3 changes: 2 additions & 1 deletion R/todo.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ complete_todo <- function(line, file, regexp, rm_line = NULL) {
check_number_whole(line)
line_original <- line
# to defer warning.
if (interactive() && is_rstudio()) {
if (interactive() && is_rstudio(f = "documentSaveAll")) {
# VSCode has a way to save All
rstudioapi::documentSaveAll()
}
warn_change_of_line <- FALSE
Expand Down
8 changes: 6 additions & 2 deletions R/utils-proj.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ check_proj <- function(x,
)
}
# to mock.
is_rstudio <- function(v = NULL) {
rstudioapi::isAvailable(version_needed = v)
is_rstudio <- function(v = NULL, f = NULL) {
avail <- rstudioapi::isAvailable(version_needed = v)
if (avail && !is.null(f)) {
avail <- rstudioapi::hasFun(f)
}
avail
}

0 comments on commit 3f6f1bd

Please sign in to comment.