Skip to content

Commit 5f60ba6

Browse files
author
Tal Hadad
committed
allow attaching in a later stage
1 parent 4bae0a7 commit 5f60ba6

3 files changed

Lines changed: 53 additions & 50 deletions

File tree

R/session/init.R

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,59 +44,13 @@ old.First.sys <- .First.sys
4444
init_last <- function() {
4545
old.First.sys()
4646

47-
# cleanup previous version
48-
removeTaskCallback("vscode-R")
49-
options(vscodeR = NULL)
50-
.vsc.name <- "tools:vscode"
51-
if (.vsc.name %in% search()) {
52-
detach(.vsc.name, character.only = TRUE)
53-
}
54-
55-
# Source vsc utils in new environmeent
56-
.vsc <- new.env()
57-
source(file.path(dir_init, "vsc.R"), local = .vsc)
58-
59-
# attach functions that are meant to be called by the user/vscode
60-
exports <- local({
61-
.vsc <- .vsc
62-
.vsc.attach <- .vsc$attach
63-
.vsc.view <- .vsc$show_dataview
64-
.vsc.browser <- .vsc$show_browser
65-
.vsc.viewer <- .vsc$show_viewer
66-
.vsc.page_viewer <- .vsc$show_page_viewer
67-
View <- .vsc.view
68-
environment()
69-
})
70-
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
71-
72-
# overwrite S3 bindings from other packages
73-
suppressWarnings({
74-
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
75-
# Overwrite print function for results of `?`
76-
.vsc$.S3method(
77-
"print",
78-
"help_files_with_topic",
79-
.vsc$print.help_files_with_topic
80-
)
81-
# Overwrite print function for results of `??`
82-
.vsc$.S3method(
83-
"print",
84-
"hsearch",
85-
.vsc$print.hsearch
86-
)
87-
}
88-
# Further S3 overwrites can go here
89-
# ...
90-
})
47+
source(file.path(dir_init, "init_late.R"), chdir = TRUE)
9148

9249
# remove this function from globalenv()
9350
suppressWarnings(
9451
rm(".First.sys", envir = globalenv())
9552
)
9653

97-
# Attach to vscode
98-
#exports$.vsc.attach()
99-
10054
invisible()
10155
}
10256

R/session/init_late.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
dir_init <- getwd()
2+
3+
4+
# cleanup previous version
5+
removeTaskCallback("vscode-R")
6+
options(vscodeR = NULL)
7+
.vsc.name <- "tools:vscode"
8+
if (.vsc.name %in% search()) {
9+
detach(.vsc.name, character.only = TRUE)
10+
}
11+
12+
# Source vsc utils in new environmeent
13+
.vsc <- new.env()
14+
source(file.path(dir_init, "vsc.R"), local = .vsc)
15+
16+
# attach functions that are meant to be called by the user/vscode
17+
exports <- local({
18+
.vsc <- .vsc
19+
.vsc.attach <- .vsc$attach
20+
.vsc.view <- .vsc$show_dataview
21+
.vsc.browser <- .vsc$show_browser
22+
.vsc.viewer <- .vsc$show_viewer
23+
.vsc.page_viewer <- .vsc$show_page_viewer
24+
View <- .vsc.view
25+
environment()
26+
})
27+
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
28+
29+
# overwrite S3 bindings from other packages
30+
suppressWarnings({
31+
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
32+
# Overwrite print function for results of `?`
33+
.vsc$.S3method(
34+
"print",
35+
"help_files_with_topic",
36+
.vsc$print.help_files_with_topic
37+
)
38+
# Overwrite print function for results of `??`
39+
.vsc$.S3method(
40+
"print",
41+
"hsearch",
42+
.vsc$print.hsearch
43+
)
44+
}
45+
# Further S3 overwrites can go here
46+
# ...
47+
})

src/session.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ export function deploySessionWatcher(extensionPath: string): void {
9393
console.info(`[deploySessionWatcher] extensionPath: ${extensionPath}`);
9494
resDir = path.join(extensionPath, 'dist', 'resources');
9595

96-
const initPath = path.join(extensionPath, 'R', 'session', 'init.R');
97-
const linkPath = path.join(homeExtDir(), 'init.R');
98-
fs.writeFileSync(linkPath, `local(source("${initPath.replace(/\\/g, '\\\\')}", chdir = TRUE, local = TRUE))\n`);
96+
for (const initFileName of ['init.R', 'init_late.R']) {
97+
const initPath = path.join(extensionPath, 'R', 'session', initFileName);
98+
const linkPath = path.join(homeExtDir(), initFileName);
99+
fs.writeFileSync(linkPath, `local(source("${initPath.replace(/\\/g, '\\\\')}", chdir = TRUE, local = TRUE))\n`);
100+
}
99101

100102
writeSettings();
101103
workspace.onDidChangeConfiguration(event => {

0 commit comments

Comments
 (0)