Skip to content

Commit

Permalink
Substitute variables in r.rpath and r.rterm settings (#1444)
Browse files Browse the repository at this point in the history
* Substitute variables in r.rpath and r.rterm settings

* Update logging

* Add substituteVariable
  • Loading branch information
renkun-ken authored Nov 6, 2023
1 parent 70e3267 commit 4b2e340
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceRoot}/out/src/**/*.js"
"${workspaceFolder}/out/src/**/*.js"
],
"preLaunchTask": "watchAll"
},
Expand All @@ -27,7 +27,7 @@
"--disable-extensions"
],
"outFiles": [
"${workspaceRoot}/out/src/**/*.js"
"${workspaceFolder}/out/src/**/*.js"
],
"preLaunchTask": "watchAll"
},
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1422,32 +1422,32 @@
"r.rpath.windows": {
"type": "string",
"default": "",
"description": "Path to an R executable to launch R background processes (Windows). Must be \"vanilla\" R, not radian etc.!"
"markdownDescription": "Path to an R executable to launch R background processes (Windows). Must be \"vanilla\" R, not radian etc.! Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rpath.mac": {
"type": "string",
"default": "",
"description": "Path to an R executable to launch R background processes (macOS). Must be \"vanilla\" R, not radian etc.!"
"markdownDescription": "Path to an R executable to launch R background processes (macOS). Must be \"vanilla\" R, not radian etc.! Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rpath.linux": {
"type": "string",
"default": "",
"description": "Path to an R executable to launch R background processes (Linux). Must be \"vanilla\" R, not radian etc.!"
"markdownDescription": "Path to an R executable to launch R background processes (Linux). Must be \"vanilla\" R, not radian etc.! Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rterm.windows": {
"type": "string",
"default": "",
"description": "R path for interactive terminals (Windows). Can also be radian etc."
"markdownDescription": "R path for interactive terminals (Windows). Can also be radian etc. Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rterm.mac": {
"type": "string",
"default": "",
"description": "R path for interactive terminals (macOS). Can also be radian etc."
"markdownDescription": "R path for interactive terminals (macOS). Can also be radian etc. Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rterm.linux": {
"type": "string",
"default": "",
"description": "R path for interactive terminals (Linux). Can also be radian etc."
"markdownDescription": "R path for interactive terminals (Linux). Can also be radian etc. Some variables defined in <https://code.visualstudio.com/docs/editor/variables-reference> such as `${userHome}`, `${workspaceFolder}`, `${fileWorkspaceFolder}`, and `${fileDirname}` are supported."
},
"r.rterm.option": {
"type": "array",
Expand Down
5 changes: 1 addition & 4 deletions src/rTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ export async function runFromLineToEnd(): Promise<void> {

export async function makeTerminalOptions(): Promise<vscode.TerminalOptions> {
const workspaceFolderPath = getCurrentWorkspaceFolder()?.uri.fsPath;
const termPathMaybeRelative = await getRterm();
const termPath: string | undefined = (workspaceFolderPath && termPathMaybeRelative) ?
path.resolve(workspaceFolderPath, termPathMaybeRelative) :
termPathMaybeRelative;
const termPath = await getRterm();
const shellArgs: string[] = config().get('rterm.option') || [];
const termOptions: vscode.TerminalOptions = {
name: 'R Interactive',
Expand Down
41 changes: 36 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import { homedir } from 'os';
import { existsSync, PathLike, readFile } from 'fs-extra';
import * as fs from 'fs';
import winreg = require('winreg');
Expand All @@ -14,6 +15,31 @@ export function config(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('r');
}

function substituteVariable(str: string, key: string, getValue: () => string | undefined) {
if (str.includes(key)) {
const value = getValue();
if (value) {
return str.replaceAll(key, value);
}
}
return str;
}

function substituteVariables(str: string) {
let result = str;
result = substituteVariable(result, '${userHome}', () => homedir());
result = substituteVariable(result, '${workspaceFolder}', () => getCurrentWorkspaceFolder()?.uri.fsPath);
result = substituteVariable(result, '${fileWorkspaceFolder}', () => getActiveFileWorkspaceFolder()?.uri.fsPath);
result = substituteVariable(result, '${fileDirname}', () => {
const activeFilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
if (activeFilePath) {
return path.dirname(activeFilePath);
}
});

return result;
}

function getRfromEnvPath(platform: string) {
let splitChar = ':';
let fileExtension = '';
Expand Down Expand Up @@ -79,6 +105,7 @@ export async function getRpath(quote = false, overwriteConfig?: string): Promise
// try the os-specific config entry for the rpath:
const configEntry = getRPathConfigEntry();
rpath ||= config().get<string>(configEntry);
rpath &&= substituteVariables(rpath);

// read from path/registry:
rpath ||= await getRpathFromSystem();
Expand Down Expand Up @@ -106,7 +133,7 @@ export async function getRpath(quote = false, overwriteConfig?: string): Promise
export async function getRterm(): Promise<string | undefined> {
const configEntry = getRPathConfigEntry(true);
let rpath = config().get<string>(configEntry);

rpath &&= substituteVariables(rpath);
rpath ||= await getRpathFromSystem();

if (rpath !== '') {
Expand Down Expand Up @@ -147,15 +174,19 @@ export function checkIfFileExists(filePath: string): boolean {
return existsSync(filePath);
}

function getActiveFileWorkspaceFolder(): vscode.WorkspaceFolder | undefined {
const currentDocument = vscode.window.activeTextEditor;
if (currentDocument !== undefined) {
return vscode.workspace.getWorkspaceFolder(currentDocument.document.uri);
}
}

export function getCurrentWorkspaceFolder(): vscode.WorkspaceFolder | undefined {
if (vscode.workspace.workspaceFolders !== undefined) {
if (vscode.workspace.workspaceFolders.length === 1) {
return vscode.workspace.workspaceFolders[0];
} else if (vscode.workspace.workspaceFolders.length > 1) {
const currentDocument = vscode.window.activeTextEditor;
if (currentDocument !== undefined) {
return vscode.workspace.getWorkspaceFolder(currentDocument.document.uri);
}
return getActiveFileWorkspaceFolder() || vscode.workspace.workspaceFolders[0];
}
}

Expand Down

0 comments on commit 4b2e340

Please sign in to comment.