Skip to content

Commit

Permalink
Substitute variables in r.rpath and r.rterm settings
Browse files Browse the repository at this point in the history
  • Loading branch information
renkun-ken committed Nov 4, 2023
1 parent 70e3267 commit 5ae28a1
Show file tree
Hide file tree
Showing 4 changed files with 55 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
51 changes: 46 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,40 @@ export function config(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('r');
}

function substituteVariables(str: string) {
let result = str;

if (str.includes('${userHome}')) {
const userHome = homedir();
if (userHome) {
result = str.replaceAll('${userHome}', userHome);
}
}

if (str.includes('${workspaceFolder}')) {
const workspaceFolderPath = getCurrentWorkspaceFolder()?.uri.fsPath;
if (workspaceFolderPath) {
result = str.replaceAll('${workspaceFolder}', workspaceFolderPath);
}
}

if (str.includes('${fileWorkspaceFolder}')) {
const workspaceFolderPath = getActiveFileWorkspaceFolder()?.uri.fsPath;
if (workspaceFolderPath) {
result = str.replaceAll('${fileWorkspaceFolder}', workspaceFolderPath);
}
}

if (str.includes('${fileDirname}')) {
const activeFilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
if (activeFilePath) {
result = str.replaceAll('${fileDirname}', path.dirname(activeFilePath));
}
}

return result;
}

function getRfromEnvPath(platform: string) {
let splitChar = ':';
let fileExtension = '';
Expand Down Expand Up @@ -79,6 +114,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 +142,8 @@ 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);
console.log(rpath);
rpath ||= await getRpathFromSystem();

if (rpath !== '') {
Expand Down Expand Up @@ -147,15 +184,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 5ae28a1

Please sign in to comment.