Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions R/.lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters: linters_with_defaults(
line_length_linter(120),
indentation_linter(4),
cyclocomp_linter = NULL,
object_name_linter = NULL,
object_usage_linter = NULL)
52 changes: 26 additions & 26 deletions R/help/getAliases.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS")
if (nzchar(add_lib_paths)) {
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
}

use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH")
use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE
if (use_renv_lib_path) {
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
}

.libPaths(.paths)
Expand All @@ -27,27 +27,27 @@ ord <- order(ip[, "Priority"])
ip <- ip[ord, ]

ret <- lapply(rownames(ip), function(row) {
libPath <- ip[row, "LibPath"]
pkg <- ip[row, "Package"]
filename <- file.path(libPath, pkg, "help", "aliases.rds")
info <- list(
package = pkg,
libPath = libPath,
aliasFile = filename,
aliases = NULL
)
if (file.exists(filename)) {
res <- tryCatch(
expr = as.list(readRDS(filename)),
error = conditionMessage
libPath <- ip[row, "LibPath"]
pkg <- ip[row, "Package"]
filename <- file.path(libPath, pkg, "help", "aliases.rds")
info <- list(
package = pkg,
libPath = libPath,
aliasFile = filename,
aliases = NULL
)
if (is.list(res)) {
info$aliases <- res
} else {
info$error <- res
if (file.exists(filename)) {
res <- tryCatch(
expr = as.list(readRDS(filename)),
error = conditionMessage
)
if (is.list(res)) {
info$aliases <- res
} else {
info$error <- res
}
}
}
info
info
})

names(ret) <- rownames(ip)
Expand Down
14 changes: 7 additions & 7 deletions R/help/helpServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS")
if (nzchar(add_lib_paths)) {
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
}

use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH")
use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE
if (use_renv_lib_path) {
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
}

.libPaths(.paths)
Expand Down
1 change: 0 additions & 1 deletion R/help/rdToHtml.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Converts an .Rd file to HTML (output is printed to stdout)
#'
#' Execute this with the following trailing commandline args:
Expand Down
16 changes: 8 additions & 8 deletions R/languageServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS")
if (nzchar(add_lib_paths)) {
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]]
.paths <- c(.paths, add_lib_paths)
}

use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH")
use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE
if (use_renv_lib_path) {
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
if (requireNamespace("renv", quietly = TRUE)) {
.paths <- c(.paths, renv::paths$cache())
} else {
warning("renv package is not installed. Please install renv to use renv library path.")
}
}

.libPaths(.paths)
message("R library paths: ", paste(.libPaths(), collapse = "\n"))

if (!requireNamespace("languageserver", quietly = TRUE)) {
q(save = "no", status = 10)
q(save = "no", status = 10)
}

debug <- Sys.getenv("VSCR_LSP_DEBUG")
Expand Down
24 changes: 12 additions & 12 deletions R/rmarkdown/templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ loadNamespace("yaml")
pkgs <- .packages(all.available = TRUE)
templates <- new.env()
template_dirs <- lapply(pkgs, function(pkg) {
dir <- system.file("rmarkdown/templates", package = pkg)
if (dir.exists(dir)) {
ids <- list.dirs(dir, full.names = FALSE, recursive = FALSE)
for (id in ids) {
file <- file.path(dir, id, "template.yaml")
if (file.exists(file)) {
data <- yaml::read_yaml(file)
data$id <- id
data$package <- pkg
templates[[paste0(pkg, "::", id)]] <- data
}
dir <- system.file("rmarkdown/templates", package = pkg)
if (dir.exists(dir)) {
ids <- list.dirs(dir, full.names = FALSE, recursive = FALSE)
for (id in ids) {
file <- file.path(dir, id, "template.yaml")
if (file.exists(file)) {
data <- yaml::read_yaml(file)
data$id <- id
data$package <- pkg
templates[[paste0(pkg, "::", id)]] <- data
}
}
}
}
})

template_list <- unname(as.list(templates))
Expand Down
147 changes: 74 additions & 73 deletions R/session/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ dir_init <- getwd()
# This function is run at the beginning of R's startup sequence
# Code that is meant to be run at the end of the startup should go in `init_last`
init_first <- function() {
# return early if not a vscode term session
if (
!interactive()
|| Sys.getenv("RSTUDIO") != ""
|| Sys.getenv("TERM_PROGRAM") != "vscode"
) {
return()
}
# return early if not a vscode term session
if (
!interactive()
|| Sys.getenv("RSTUDIO") != ""
|| Sys.getenv("TERM_PROGRAM") != "vscode"
) {
return()
}

# check requried packages
required_packages <- c("jsonlite", "rlang")
missing_packages <- required_packages[
!vapply(required_packages, requireNamespace,
logical(1L), quietly = TRUE)
]
# check requried packages
required_packages <- c("jsonlite", "rlang")
missing_packages <- required_packages[
!vapply(required_packages, requireNamespace,
logical(1L), quietly = TRUE
)
]

if (length(missing_packages)) {
message(
"VSCode R Session Watcher requires ",
toString(missing_packages), ". ",
"Please install manually in order to use VSCode-R."
)
} else {
# Initialize vsc utils after loading other default packages
assign(".First.sys", init_last, envir = globalenv())
}
if (length(missing_packages)) {
message(
"VSCode R Session Watcher requires ",
toString(missing_packages), ". ",
"Please install manually in order to use VSCode-R."
)
} else {
# Initialize vsc utils after loading other default packages
assign(".First.sys", init_last, envir = globalenv())
}
}

old.First.sys <- .First.sys
Expand All @@ -42,62 +43,62 @@ old.First.sys <- .First.sys
# Will be assigned to and called from the global environment,
# Will be run with wd being the user's working directory (!)
init_last <- function() {
old.First.sys()
old.First.sys()

# cleanup previous version
removeTaskCallback("vscode-R")
options(vscodeR = NULL)
.vsc.name <- "tools:vscode"
if (.vsc.name %in% search()) {
detach(.vsc.name, character.only = TRUE)
}
# cleanup previous version
removeTaskCallback("vscode-R")
options(vscodeR = NULL)
.vsc.name <- "tools:vscode"
if (.vsc.name %in% search()) {
detach(.vsc.name, character.only = TRUE)
}

# Source vsc utils in new environmeent
.vsc <- new.env()
source(file.path(dir_init, "vsc.R"), local = .vsc)
# Source vsc utils in new environmeent
.vsc <- new.env()
source(file.path(dir_init, "vsc.R"), local = .vsc)

# attach functions that are meant to be called by the user/vscode
exports <- local({
.vsc <- .vsc
.vsc.attach <- .vsc$attach
.vsc.view <- .vsc$show_dataview
.vsc.browser <- .vsc$show_browser
.vsc.viewer <- .vsc$show_viewer
.vsc.page_viewer <- .vsc$show_page_viewer
View <- .vsc.view
environment()
})
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
# attach functions that are meant to be called by the user/vscode
exports <- local({
.vsc <- .vsc
.vsc.attach <- .vsc$attach
.vsc.view <- .vsc$show_dataview
.vsc.browser <- .vsc$show_browser
.vsc.viewer <- .vsc$show_viewer
.vsc.page_viewer <- .vsc$show_page_viewer
View <- .vsc.view
environment()
})
attach(exports, name = .vsc.name, warn.conflicts = FALSE)

# overwrite S3 bindings from other packages
suppressWarnings({
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
# Overwrite print function for results of `?`
.vsc$.S3method(
"print",
"help_files_with_topic",
.vsc$print.help_files_with_topic
)
# Overwrite print function for results of `??`
.vsc$.S3method(
"print",
"hsearch",
.vsc$print.hsearch
)
}
# Further S3 overwrites can go here
# ...
})
# overwrite S3 bindings from other packages
suppressWarnings({
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
# Overwrite print function for results of `?`
.vsc$.S3method(
"print",
"help_files_with_topic",
.vsc$print.help_files_with_topic
)
# Overwrite print function for results of `??`
.vsc$.S3method(
"print",
"hsearch",
.vsc$print.hsearch
)
}
# Further S3 overwrites can go here
# ...
})

# remove this function from globalenv()
suppressWarnings(
rm(".First.sys", envir = globalenv())
)
# remove this function from globalenv()
suppressWarnings(
rm(".First.sys", envir = globalenv())
)

# Attach to vscode
exports$.vsc.attach()
# Attach to vscode
exports$.vsc.attach()

invisible()
invisible()
}

init_first()
Loading