From e2252df35e88de70d4af5961b2e3d564a69ccda6 Mon Sep 17 00:00:00 2001 From: tshino Date: Mon, 3 Jan 2022 15:38:38 +0900 Subject: [PATCH 1/3] Add 'Repeat Playback' command --- package.json | 5 +++++ src/extension.js | 1 + src/keyboard_macro.js | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 220f98e4..b1ad802a 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "onCommand:kb-macro.cancelRecording", "onCommand:kb-macro.playback", "onCommand:kb-macro.abortPlayback", + "onCommand:kb-macro.repeatPlayback", "onCommand:kb-macro.wrap" ], "main": "./src/extension.js", @@ -95,6 +96,10 @@ { "command": "kb-macro.abortPlayback", "title": "Keyboard Macro: Abort Playback" + }, + { + "command": "kb-macro.repeatPlayback", + "title": "Keyboard Macro: Repeat Playback" } ], "keybindings": [ diff --git a/src/extension.js b/src/extension.js index e297a84d..7961200c 100644 --- a/src/extension.js +++ b/src/extension.js @@ -33,6 +33,7 @@ function activate(context) { registerCommand('finishRecording', keyboardMacro.finishRecording); registerCommand('playback', keyboardMacro.playback); registerCommand('abortPlayback', keyboardMacro.abortPlayback); + registerCommand('repeatPlayback', keyboardMacro.repeatPlayback); registerCommand('wrap', keyboardMacro.wrap); keyboardMacro.registerInternalCommand('internal:performType', internalCommands.performType); diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 0534f265..1c5fb820 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -136,7 +136,7 @@ const KeyboardMacro = function({ awaitController }) { return ok; }; - const playback = makeGuardedCommand(async function(args) { + const playbackImpl = async function(args) { if (recording) { return; } @@ -163,7 +163,8 @@ const KeyboardMacro = function({ awaitController }) { changePlaybackState(false, PlaybackStateReason.Finish); } } - }); + }; + const playback = makeGuardedCommand(playbackImpl); const abortPlayback = async function() { if (playing) { @@ -171,6 +172,25 @@ const KeyboardMacro = function({ awaitController }) { } }; + const repeatPlayback = makeGuardedCommand(async function() { + if (recording) { + return; + } + const input = await vscode.window.showInputBox({ + prompt: 'Input the number of times to repeat the macro', + validateInput: function(value) { + if (value !== '' && !/^[1-9]\d*$/.test(value)) { + return 'Input a positive integer number'; + } + } + }); + if (input) { + await playbackImpl({ + repeat: Number(input) + }); + } + }); + const makeCommandSpec = function(args) { if (!args || !args.command) { return null; @@ -220,6 +240,7 @@ const KeyboardMacro = function({ awaitController }) { push, playback, abortPlayback, + repeatPlayback, wrap, // testing purpose only From c6666ee9806e7e02ff288f2aba8aafe6aa1a7884 Mon Sep 17 00:00:00 2001 From: tshino Date: Mon, 3 Jan 2022 18:28:35 +0900 Subject: [PATCH 2/3] Add tests for keyboard_macro.js --- src/keyboard_macro.js | 12 +++++++----- test/suite/keyboard_macro.test.js | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 1c5fb820..5ae402cf 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -172,17 +172,18 @@ const KeyboardMacro = function({ awaitController }) { } }; + const validatePositiveIntegerInput = function(value) { + if (value !== '' && !/^[1-9]\d*$/.test(value)) { + return 'Input a positive integer number'; + } + }; const repeatPlayback = makeGuardedCommand(async function() { if (recording) { return; } const input = await vscode.window.showInputBox({ prompt: 'Input the number of times to repeat the macro', - validateInput: function(value) { - if (value !== '' && !/^[1-9]\d*$/.test(value)) { - return 'Input a positive integer number'; - } - } + validateInput: validatePositiveIntegerInput }); if (input) { await playbackImpl({ @@ -240,6 +241,7 @@ const KeyboardMacro = function({ awaitController }) { push, playback, abortPlayback, + validatePositiveIntegerInput, repeatPlayback, wrap, diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index 90a4bff3..89a36e1e 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -414,6 +414,27 @@ describe('KeybaordMacro', () => { assert.strictEqual(logs.length < 2 * 3 * 3, true); }); }); + describe('validatePositiveIntegerInput', () => { + const validatePositiveIntegerInput = keyboardMacro.validatePositiveIntegerInput; + it('should return undefined if input is a valid string representing positive integer', () => { + assert.strictEqual(validatePositiveIntegerInput('1'), undefined); + assert.strictEqual(validatePositiveIntegerInput('42'), undefined); + assert.strictEqual(validatePositiveIntegerInput('9999'), undefined); + }); + it('should return undefined if input is empty', () => { + assert.strictEqual(validatePositiveIntegerInput(''), undefined); + }); + it('should return diagnostic message if input does not reprensent an integer', () => { + assert.strictEqual(typeof validatePositiveIntegerInput('abc'), 'string'); + assert.strictEqual(typeof validatePositiveIntegerInput('123m'), 'string'); + assert.strictEqual(typeof validatePositiveIntegerInput('1.5'), 'string'); + assert.strictEqual(typeof validatePositiveIntegerInput('1e7'), 'string'); + assert.strictEqual(typeof validatePositiveIntegerInput(' '), 'string'); + }); + it('should return diagnostic message if input reprensents a negative integer', () => { + assert.strictEqual(typeof validatePositiveIntegerInput('-123'), 'string'); + }); + }); describe('wrap', () => { const logs = []; beforeEach(async () => { From c5851a121fa66846fefc31aaeef9aba71c79e196 Mon Sep 17 00:00:00 2001 From: tshino Date: Mon, 3 Jan 2022 18:50:06 +0900 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d0946c..f2e737ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to the Keyboard Macro Bata extension will be documented in this file. ### [Unreleased] +- New + - Added a new `Keybaord Macro: Repeat Playback` command, which lets the user input a number and then repeats the macro specified times. [#29](https://github.com/tshino/vscode-kb-macro/pull/29) - Update - Updated keymap wrapper for Awesome Emacs Keymap (v0.37.1). - Fix