diff --git a/CHANGELOG.md b/CHANGELOG.md index ff925560..37e5b005 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to the Keyboard Macro Bata extension will be documented in t ### [Unreleased] - New - Added Sublime Text Keymap support. [#18](https://github.com/tshino/vscode-kb-macro/issues/18) + - Added 'repeat' argument support to the playback command. [#19](https://github.com/tshino/vscode-kb-macro/pull/19) - Update - Updated keymap wrapper for Vz Keymap; Removed unnecessary wrappers for Vz Keymap's built-in macro feature. - Fix diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 20db3064..ea684a09 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -121,13 +121,18 @@ const KeyboardMacro = function({ awaitController }) { return ok; }; - const playback = makeGuardedCommand(async function() { + const playback = makeGuardedCommand(async function(args) { if (!recording) { + args = (args && typeof(args) === 'object') ? args : {}; + const repeat = typeof(args.repeat) === 'number' ? args.repeat : 1; const commands = sequence.get(); - for (const spec of commands) { - const ok = await invokeCommandSync(spec, 'playback'); - if (!ok) { - break; + let ok = true; + for (let k = 0; k < repeat && ok; k++) { + for (const spec of commands) { + ok = await invokeCommandSync(spec, 'playback'); + if (!ok) { + break; + } } } } diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index 8005d847..2bf2c2e2 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -224,6 +224,25 @@ describe('KeybaordMacro', () => { 'end' ]); }); + it('should repeat 5 times (repeat argument)', async () => { + keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'internal:log' }); + keyboardMacro.finishRecording(); + + await keyboardMacro.playback({ repeat: 5 }); + assert.deepStrictEqual(logs, [ + 'begin', + 'end', + 'begin', + 'end', + 'begin', + 'end', + 'begin', + 'end', + 'begin', + 'end' + ]); + }); it('should abort playback if command execution failed', async () => { keyboardMacro.startRecording(); keyboardMacro.push({ command: 'internal:log' }); @@ -234,6 +253,15 @@ describe('KeybaordMacro', () => { await keyboardMacro.playback(); assert.deepStrictEqual(logs, [ 'begin', 'end' ]); }); + it('should abort playback with repeat argument if command execution failed', async () => { + keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'internal:log' }); + keyboardMacro.push({ command: 'INVALID' }); + keyboardMacro.finishRecording(); + + await keyboardMacro.playback({ repeat: 5 }); + assert.deepStrictEqual(logs, [ 'begin', 'end' ]); + }); it('should prevent reentry', async () => { keyboardMacro.startRecording(); keyboardMacro.push({ command: 'internal:log' });