Skip to content

Commit e0cbe12

Browse files
committed
restore: R/session/ files removed in previous commit
1 parent 41c34b9 commit e0cbe12

5 files changed

Lines changed: 1651 additions & 0 deletions

File tree

R/session/init.R

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# This file is executed with its containing directory as wd
2+
3+
# Remember the working directory (should be extension subfolder that contains this script)
4+
dir_init <- getwd()
5+
6+
7+
# This function is run at the beginning of R's startup sequence
8+
# Code that is meant to be run at the end of the startup should go in `init_last`
9+
init_first <- function() {
10+
# return early if not a vscode term session
11+
if (
12+
!interactive()
13+
|| Sys.getenv("RSTUDIO") != ""
14+
|| Sys.getenv("TERM_PROGRAM") != "vscode"
15+
) {
16+
return()
17+
}
18+
19+
# check required packages
20+
required_packages <- c("jsonlite", "rlang")
21+
missing_packages <- required_packages[
22+
!vapply(required_packages, requireNamespace,
23+
logical(1L), quietly = TRUE
24+
)
25+
]
26+
27+
if (length(missing_packages)) {
28+
message(
29+
"VSCode R Session Watcher requires ",
30+
toString(missing_packages), ". ",
31+
"Please install manually in order to use VSCode-R."
32+
)
33+
} else {
34+
# Initialize vsc utils after loading other default packages
35+
assign(".First.sys", init_last, envir = globalenv())
36+
}
37+
}
38+
39+
old.First.sys <- .First.sys
40+
41+
# Overwrite for `.First.sys`
42+
# Is used to make sure that all default packages are loaded first
43+
# Will be assigned to and called from the global environment,
44+
# Will be run with wd being the user's working directory (!)
45+
init_last <- function() {
46+
old.First.sys()
47+
48+
# cleanup previous version
49+
removeTaskCallback("vscode-R")
50+
options(vscodeR = NULL)
51+
.vsc.name <- "tools:vscode"
52+
if (.vsc.name %in% search()) {
53+
detach(.vsc.name, character.only = TRUE)
54+
}
55+
56+
# Source vsc utils in new environmeent
57+
.vsc <- new.env()
58+
source(file.path(dir_init, "vsc.R"), local = .vsc)
59+
60+
# attach functions that are meant to be called by the user/vscode
61+
exports <- local({
62+
.vsc <- .vsc
63+
.vsc.attach <- .vsc$attach
64+
.vsc.view <- .vsc$show_dataview
65+
.vsc.browser <- .vsc$show_browser
66+
.vsc.viewer <- .vsc$show_viewer
67+
.vsc.page_viewer <- .vsc$show_page_viewer
68+
View <- .vsc.view
69+
environment()
70+
})
71+
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
72+
73+
# overwrite S3 bindings from other packages
74+
suppressWarnings({
75+
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
76+
# Overwrite print function for results of `?`
77+
.vsc$.S3method(
78+
"print",
79+
"help_files_with_topic",
80+
.vsc$print.help_files_with_topic
81+
)
82+
# Overwrite print function for results of `??`
83+
.vsc$.S3method(
84+
"print",
85+
"hsearch",
86+
.vsc$print.hsearch
87+
)
88+
}
89+
# Further S3 overwrites can go here
90+
# ...
91+
})
92+
93+
# remove this function from globalenv()
94+
suppressWarnings(
95+
rm(".First.sys", envir = globalenv())
96+
)
97+
98+
# Attach to vscode
99+
exports$.vsc.attach()
100+
101+
invisible()
102+
}
103+
104+
init_first()

R/session/profile.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Source the original .Rprofile
2+
local({
3+
try_source <- function(file) {
4+
if (file.exists(file)) {
5+
source(file)
6+
TRUE
7+
} else {
8+
FALSE
9+
}
10+
}
11+
12+
r_profile <- Sys.getenv("R_PROFILE_USER_OLD")
13+
Sys.setenv(
14+
R_PROFILE_USER_OLD = "",
15+
R_PROFILE_USER = r_profile
16+
)
17+
18+
if (nzchar(r_profile)) {
19+
try_source(r_profile)
20+
} else {
21+
try_source(".Rprofile") || try_source(file.path("~", ".Rprofile"))
22+
}
23+
24+
invisible()
25+
})
26+
27+
# Run vscode initializer
28+
local({
29+
init_file <- Sys.getenv("VSCODE_INIT_R")
30+
if (nzchar(init_file)) {
31+
source(init_file, chdir = TRUE, local = TRUE)
32+
}
33+
})

0 commit comments

Comments
 (0)