Skip to content
Open
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
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export default function variables(string: string, recursive?: boolean): Promise<string>;
25 changes: 21 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = variables;
const os = require("os");
const process = require("process");
const vscode = require("vscode");
module.exports = function variables(string, recursive = false) {
async function variables(string, recursive = false) {
const workspaceFolders = vscode.workspace.workspaceFolders;
const activeFile = vscode.window.activeTextEditor?.document;
const absoluteFilePath = activeFile?.uri.fsPath;
Expand Down Expand Up @@ -50,6 +51,8 @@ module.exports = function variables(string, recursive = false) {
string = string.replace(/\${selectedText}/g, function () {
return (vscode.window.activeTextEditor?.document.getText(new vscode.Range(vscode.window.activeTextEditor.selection.start, vscode.window.activeTextEditor.selection.end)) ?? "");
});
// ${cwd} - current working directory
string = string.replace(/\${cwd}/g, absoluteFilePath?.split("/")?.slice(0, -1)?.join("/") ?? "");
// ${execPath} - location of Code.exe
string = string.replace(/\${execPath}/g, process.execPath);
// ${pathSeparator} - / on macOS or linux, \ on Windows
Expand All @@ -64,9 +67,23 @@ module.exports = function variables(string, recursive = false) {
string = string.replace(/\${config:(.*?)}/g, function (variable, _) {
return vscode.workspace.getConfiguration().get(_, "");
});
if (string.match(/\${command:(.*?)}/)) {
// async
while (string.match(/\${command:(.*?)}/)) {
const command = string.match(/\${command:(.*?)}/)[1];
try {
const result = await vscode.commands.executeCommand(command);
string = string.replace(/\${command:(.*?)}/, result !== undefined ? result + "" : "");
}
catch (error) {
string = string.replace(/\${command:(.*?)}/, "");
}
}
}
if (recursive &&
string.match(/\${(workspaceFolder|workspaceFolder:(.*?)|workspaceFolderBase:(.*?)|workspaceFolderBasename|fileWorkspaceFolder|relativeFile|fileBasename|fileBasenameNoExtension|fileExtname|fileDirname|cwd|pathSeparator|lineNumber|selectedText|env:(.*?)|config:(.*?)|userHome)}/)) {
string = variables(string, recursive);
string.match(/\${(workspaceFolder|workspaceFolder:(.*?)|workspaceFolderBase:(.*?)|workspaceFolderBasename|fileWorkspaceFolder|relativeFile|fileBasename|fileBasenameNoExtension|fileExtname|fileDirname|cwd|pathSeparator|lineNumber|selectedText|env:(.*?)|config:(.*?)|command:(.*?)|userHome)}/)) {
string = await variables(string, recursive);
}
return string;
};
}
// export = variables;
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as os from "os";
import * as process from "process";
import * as vscode from "vscode";
module.exports = async function variables(string: string, recursive = false) {

export default async function variables(string: string, recursive = false) {
const workspaceFolders = vscode.workspace.workspaceFolders;
const activeFile = vscode.window.activeTextEditor?.document;
const absoluteFilePath = activeFile?.uri.fsPath;
Expand Down Expand Up @@ -160,4 +161,4 @@ module.exports = async function variables(string: string, recursive = false) {
string = await variables(string, recursive);
}
return string;
};
}