diff --git a/extension.js b/extension.js index 8d0e940..af90804 100644 --- a/extension.js +++ b/extension.js @@ -1,5 +1,4 @@ const vscode = require('vscode'); -const PromiseSeries = require('promise-series'); var activeContext; var disposables = []; @@ -14,33 +13,45 @@ function activate(context) { } exports.activate = activate; -function deactivate() { -} +function deactivate() {} exports.deactivate = deactivate; +function executeDelayCommand(action) { + return new Promise(resolve => { + setTimeout(() => resolve(), action.args && action.args.milliseconds); + }); +} + +function executeCommand(action) { + // support objects so that we can pass arguments from user settings to the commands + if (typeof action === 'object') { + if (action.command === '$delay') { + return executeDelayCommand(action); + } else { + return vscode.commands.executeCommand(action.command, action.args); + } + } else { + // support commands as strings (no args) + return vscode.commands.executeCommand(action); + } +} + function loadMacros(context) { const settings = vscode.workspace.getConfiguration('macros'); - const macros = Object.keys(settings).filter((prop) => { + const macros = Object.keys(settings).filter(prop => { return prop !== 'has' && prop !== 'get' && prop !== 'update'; }); - macros.forEach((name) => { - const disposable = vscode.commands.registerCommand(`macros.${name}`, function () { - const series = new PromiseSeries(); - settings[name].forEach((action) => { - series.add(() => { - // support objects so that we can pass arguments from user settings to the commands - if (typeof action === "object"){ - vscode.commands.executeCommand(action.command, action.args); - } - // support commands as strings (no args) - else{ - vscode.commands.executeCommand(action); - } - }) - }) - return series.run(); - }) + macros.forEach(name => { + const disposable = vscode.commands.registerCommand( + `macros.${name}`, + function() { + return settings[name].reduce( + (promise, action) => promise.then(() => executeCommand(action)), + Promise.resolve() + ); + } + ); context.subscriptions.push(disposable); disposables.push(disposable); }); diff --git a/package.json b/package.json index 9221b03..466c2ed 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,13 @@ "contributes": { "configuration": { "type": "object", - "title": "Macros configuration" + "title": "Macros configuration", + "properties": { + "macros": { + "type": "object", + "description": "The root element to define macros in." + } + } } }, "scripts": { @@ -31,8 +37,5 @@ "eslint": "^3.6.0", "@types/node": "^6.0.40", "@types/mocha": "^2.2.32" - }, - "dependencies": { - "promise-series": "^1.0.0" } } \ No newline at end of file