-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
44 lines (43 loc) · 2.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const vscode = require('vscode');
const process = require('process');
const path = require('path');
module.exports = function variables(string, recursive = false) {
let workspaces = vscode.workspace.workspaceFolders;
let workspace = vscode.workspace.workspaceFolders.length ? vscode.workspace.workspaceFolders[0] : null;
let activeFile = vscode.window.activeTextEditor?.document;
let absoluteFilePath = activeFile?.uri.fsPath
string = string.replace(/\${workspaceFolder}/g, workspace?.uri.fsPath);
string = string.replace(/\${workspaceFolderBasename}/g, workspace?.name);
string = string.replace(/\${file}/g, absoluteFilePath);
let activeWorkspace = workspace;
let relativeFilePath = absoluteFilePath;
for (let workspace of workspaces) {
if (absoluteFilePath.replace(workspace.uri.fsPath, '') !== absoluteFilePath) {
activeWorkspace = workspace;
relativeFilePath = absoluteFilePath.replace(workspace.uri.fsPath, '').substr(path.sep.length);
break;
}
}
let parsedPath = path.parse(absoluteFilePath);
string = string.replace(/\${fileWorkspaceFolder}/g, activeWorkspace?.uri.fsPath);
string = string.replace(/\${relativeFile}/g, relativeFilePath);
string = string.replace(/\${relativeFileDirname}/g, relativeFilePath.substr(0, relativeFilePath.lastIndexOf(path.sep)));
string = string.replace(/\${fileBasename}/g, parsedPath.base);
string = string.replace(/\${fileBasenameNoExtension}/g, parsedPath.name);
string = string.replace(/\${fileExtname}/g, parsedPath.ext);
string = string.replace(/\${fileDirname}/g, parsedPath.dir.substr(parsedPath.dir.lastIndexOf(path.sep) + 1));
string = string.replace(/\${cwd}/g, parsedPath.dir);
string = string.replace(/\${pathSeparator}/g, path.sep);
string = string.replace(/\${lineNumber}/g, vscode.window.activeTextEditor.selection.start.line + 1);
string = string.replace(/\${selectedText}/g, vscode.window.activeTextEditor.document.getText(new vscode.Range(vscode.window.activeTextEditor.selection.start, vscode.window.activeTextEditor.selection.end)));
string = string.replace(/\${env:(.*?)}/g, function (variable) {
return process.env[variable.match(/\${env:(.*?)}/)[1]] || '';
});
string = string.replace(/\${config:(.*?)}/g, function (variable) {
return vscode.workspace.getConfiguration().get(variable.match(/\${config:(.*?)}/)[1], '');
});
if (recursive && string.match(/\${(workspaceFolder|workspaceFolderBasename|fileWorkspaceFolder|relativeFile|fileBasename|fileBasenameNoExtension|fileExtname|fileDirname|cwd|pathSeparator|lineNumber|selectedText|env:(.*?)|config:(.*?))}/)) {
string = variables(string, recursive);
}
return string;
}