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
54 changes: 54 additions & 0 deletions R/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,59 @@ homedir <- Sys.getenv(
dir_watcher <- Sys.getenv("VSCODE_WATCHER_DIR", file.path(homedir, ".vscode-R"))
request_file <- file.path(dir_watcher, "request.log")
request_lock_file <- file.path(dir_watcher, "request.lock")
settings_file <- file.path(dir_watcher, "settings.json")
user_options <- names(options())

load_settings <- function() {
if (!file.exists(settings_file)) {
return(FALSE)
}

mapping <- quote(list(
vsc.use_httpgd = plot$useHttpgd,
vsc.show_object_size = workspaceViewer$showObjectSize,
vsc.rstudioapi = session$emulateRStudioAPI,
vsc.str.max.level = session$levelOfObjectDetail,
vsc.globalenv = session$watchGlobalEnvironment,
vsc.plot = session$viewers$viewColumn$plot,
vsc.browser = session$viewers$viewColumn$browser,
vsc.viewer = session$viewers$viewColumn$viewer,
vsc.page_viewer = session$viewers$viewColumn$pageViewer,
vsc.view = session$viewers$viewColumn$view,
vsc.helpPanel = session$viewers$viewColumn$helpPanel
))

vsc_settings <- tryCatch(jsonlite::read_json(settings_file), error = function(e) {
message("Error occurs when reading VS Code settings: ", conditionMessage(e))
})

if (is.null(vsc_settings)) {
return(FALSE)
}

ops <- eval(mapping, vsc_settings)

# exclude options set by user on startup
ops <- ops[!(names(ops) %in% user_options)]

# translate VS Code setting values to R option values
r_options <- lapply(ops, function(x) {
if (is.character(x) && length(x) == 1) {
switch(EXPR = x,
"Disable" = FALSE,
"Minimal" = 0,
"Detailed" = 2,
x
)
} else {
x
}
})

options(r_options)
}

load_settings()

if (is.null(getOption("help_type"))) {
options(help_type = "html")
Expand Down Expand Up @@ -396,6 +449,7 @@ if (show_view) {
}

attach <- function() {
load_settings()
if (rstudioapi_enabled()) {
rstudioapi_util_env$update_addin_registry(addin_registry)
}
Expand Down
152 changes: 151 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1326,11 +1326,156 @@
"default": true,
"description": "Enable R session watcher. Required for workspace viewer and most features to work with an R session. Restart required to take effect."
},
"r.session.watchGlobalEnvironment": {
"type": "boolean",
"default": true,
"markdownDescription": "Watch the global environment to provide hover, autocompletions, and workspace viewer information. Changes the option `vsc.globalenv` in R. Requires `#r.sessionWatcher#` to be set to `true`."
},
"r.session.levelOfObjectDetail": {
"type": "string",
"markdownDescription": "How much of the object to show on hover, autocompletion, and in the workspace viewer? Changes the option `vsc.str.max.level` in R. Requires `#r.sessionWatcher#` to be set to `true`.",
"default": "Minimal",
"enum": [
"Minimal",
"Detailed"
],
"enumDescriptions": [
"Display literal values and object types only.",
"Display list content, data frame column values, and example values."
]
},
"r.session.emulateRStudioAPI": {
"type": "boolean",
"default": false,
"markdownDescription": "Emulate the RStudio API for addin support and other {rstudioapi} calls. Changes the option `vsc.rstudioapi` in R. Requires `#r.sessionWatcher#` to be set to `true`."
},
"r.session.viewers.viewColumn": {
"type": "object",
"markdownDescription": "Which view column should R-related webviews be displayed? Requires `#r.sessionWatcher#` to be set to `true`.",
"default": {
"plot": "Two",
"browser": "Active",
"viewer": "Two",
"pageViewer": "Active",
"view": "Two",
"helpPanel": "Two"
},
"properties": {
"plot": {
"type": "string",
"description": "Which view column to show the plot file on graphics update? \n\nChanges the option 'vsc.plot' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display plots in editor group 2.",
"Display plots in the active editor.",
"Display plots beside the active editor.",
"Do not use the VSCode-R plot viewer."
],
"default": "Two"
},
"browser": {
"type": "string",
"description": "Which view column to show the WebView triggered by browser (e.g. shiny apps)? \n\nChanges the option 'vsc.browser' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display browser in editor group 2.",
"Display browser in the active editor.",
"Display browser beside the active editor.",
"Do not use the VSCode-R browser."
],
"default": "Active"
},
"viewer": {
"type": "string",
"description": "Which view column to show the WebView triggered by viewer (e.g. htmlwidgets)? \n\nChanges the option 'vsc.viewer' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display viewer in editor group 2.",
"Display viewer in the active editor.",
"Display viewer beside the active editor.",
"Do not use the VSCode-R viewer."
],
"default": "Two"
},
"pageViewer": {
"type": "string",
"description": "Which view column to show the WebView triggered by the page viewer (e.g. profvis)? \n\nChanges the option 'vsc.page_viewer' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display page viewer in editor group 2.",
"Display page viewer in the active editor.",
"Display page viewer beside the active editor.",
"Do not use the VSCode-R page viewer."
],
"default": "Active"
},
"view": {
"type": "string",
"description": "Which view column to show the WebView triggered by View()? \n\nChanges the option 'vsc.plot' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display view output in editor group 2.",
"Display view output in the active editor.",
"Display view output beside the active editor.",
"Do not use the VSCode-R view command."
],
"default": "Two"
},
"helpPanel": {
"type": "string",
"description": "Which view column to show the WebView triggered by the help panel? \n\nChanges the option 'vsc.help_panel' in R.",
"enum": [
"Two",
"Active",
"Beside",
"Disable"
],
"enumDescriptions": [
"Display help panel in editor group 2.",
"Display help panel in the active editor.",
"Display help panel beside the active editor.",
"Do not use the VSCode-R help panel."
],
"default": "Two"
}
},
"additionalProperties": false
},
"r.rtermSendDelay": {
"type": "integer",
"default": 8,
"description": "Delay in milliseconds before sending each line to rterm (only applies if r.bracketedPaste is false)"
},
"r.workspaceViewer.showObjectSize": {
"type": "boolean",
"default": false,
"markdownDescription": "Show object size when hovering over a workspace viewer item. Changes the option `vsc.show_object_size` in R."
},
"r.workspaceViewer.removeHiddenItems": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -1361,6 +1506,11 @@
"default": false,
"description": "Default boolean value for automatically sharing R browser ports with guests."
},
"r.plot.useHttpgd": {
"type": "boolean",
"default": false,
"markdownDescription": "Use the httpgd-based plot viewer instead of the base VSCode-R plot viewer. Changes the option `vsc.use_httpgd` in R."
},
"r.plot.defaults.colorTheme": {
"type": "string",
"default": "original",
Expand Down Expand Up @@ -1482,4 +1632,4 @@
"ws": "^7.4.6",
"vscode-languageclient": "^7.0.0"
}
}
}
12 changes: 12 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export function deploySessionWatcher(extensionPath: string): void {
const initPath = path.join(extensionPath, 'R', 'init.R');
const linkPath = path.join(homeExtDir(), 'init.R');
fs.writeFileSync(linkPath, `local(source("${initPath.replace(/\\/g, '\\\\')}", chdir = TRUE, local = TRUE))\n`);

writeSettings();
workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('r')) {
writeSettings();
}
});
}

export function startRequestWatcher(sessionStatusBarItem: StatusBarItem): void {
Expand Down Expand Up @@ -103,6 +110,11 @@ export function removeSessionFiles(): void {
console.info('[removeSessionFiles] Done');
}

function writeSettings() {
const settingPath = path.join(homeExtDir(), 'settings.json');
fs.writeFileSync(settingPath, JSON.stringify(config()));
}

function updateSessionWatcher() {
console.info(`[updateSessionWatcher] PID: ${pid}`);
console.info('[updateSessionWatcher] Create globalEnvWatcher');
Expand Down