Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'repeat' argument to playback command #19

Merged
merged 3 commits into from
Dec 19, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions src/keyboard_macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/suite/keyboard_macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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' });
Expand Down