From 02cecb9b3f43b898ee175e6f0e145254401978a2 Mon Sep 17 00:00:00 2001 From: tshino Date: Thu, 17 Mar 2022 22:36:10 +0900 Subject: [PATCH 1/3] Add 'record' option to 'wrap' command --- src/keyboard_macro.js | 4 ++++ src/util.js | 6 ++++++ test/suite/keyboard_macro.test.js | 19 +++++++++++++++++++ test/suite/util.test.js | 9 +++++++++ 4 files changed, 38 insertions(+) diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 47afc1d8..03ce8514 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -100,6 +100,10 @@ const KeyboardMacro = function({ awaitController }) { const push = function(spec) { if (recording) { + if (spec.record === 'side-effect') { + // side-effect mode + return; + } sequence.push(spec); } }; diff --git a/src/util.js b/src/util.js index cf7f1004..b8c16723 100644 --- a/src/util.js +++ b/src/util.js @@ -73,6 +73,12 @@ const util = (function() { } spec['await'] = args['await']; } + if ('record' in args) { + if (typeof(args.record) !== 'string') { + return null; + } + spec.record = args.record; + } return spec; }; diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index c224ac41..e8fa0c10 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -230,6 +230,9 @@ describe('KeybaordMacro', () => { keyboardMacro.onChangeRecordingState(null); keyboardMacro.cancelRecording(); }); + afterEach(async () => { + keyboardMacro.cancelRecording(); + }); it('should add specified command to sequence', async () => { keyboardMacro.startRecording(); keyboardMacro.push({ command: 'example:command1' }); @@ -244,6 +247,13 @@ describe('KeybaordMacro', () => { keyboardMacro.push({ command: 'example:command1' }); keyboardMacro.push({ command: 'example:command2', args: { opt1: 'opt1' } }); + assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), []); + }); + it('should do nothing if side-effect mode', async () => { + keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'example:command1', record: 'side-effect' }); + keyboardMacro.push({ command: 'example:command2', record: 'side-effect', args: { opt1: 'opt1' } }); + assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), []); }); }); @@ -963,5 +973,14 @@ describe('KeybaordMacro', () => { { command: 'internal:indirectWrap' } ]); }); + it('should invoke but not record the target command if side-effect mode', async () => { + keyboardMacro.startRecording(); + await keyboardMacro.wrapSync({ command: 'internal:log', record: 'side-effect' }); + keyboardMacro.finishRecording(); + + assert.deepStrictEqual(logs, [ 'begin', 'end' ]); + assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), []); + }); + // TODO: test for side-effect mode not to invoke onBegin/EndWrappedCommand }); }); diff --git a/test/suite/util.test.js b/test/suite/util.test.js index f78d2573..a2a19ec4 100644 --- a/test/suite/util.test.js +++ b/test/suite/util.test.js @@ -210,6 +210,9 @@ describe('util', () => { assert.strictEqual(makeCommandSpec({ foo: 'foo' }), null); assert.strictEqual(makeCommandSpec({ command: 123 }), null); assert.strictEqual(makeCommandSpec({ command: 'aaa', 'await': 123 }), null); + assert.strictEqual(makeCommandSpec({ command: 'aaa', 'await': [ 'xxx' ] }), null); + assert.strictEqual(makeCommandSpec({ command: 'aaa', record: 123 }), null); + assert.strictEqual(makeCommandSpec({ command: 'aaa', record: [ 'xxx' ] }), null); }); it('should return a new Object', () => { const input = { command: 'foo' }; @@ -239,6 +242,12 @@ describe('util', () => { { command: 'a', args: { b: 42 }, 'await': 'ccc' } ); }); + it('should accept \'record\' properties as well', () => { + assert.deepStrictEqual( + makeCommandSpec({ command: 'a', record: 'side-effect' }), + { command: 'a', record: 'side-effect' } + ); + }); it('should drop properties that are not relevant to a command spec', () => { assert.deepStrictEqual( makeCommandSpec({ command: 'a', foo: 'bar', baz: 'zoo' }), From 7ff07ff74d38f40c3c02678bf87721d7f9eab121 Mon Sep 17 00:00:00 2001 From: tshino Date: Thu, 17 Mar 2022 23:22:42 +0900 Subject: [PATCH 2/3] Enable side effect detection in side-effect mode --- src/keyboard_macro.js | 5 +++-- test/suite/keyboard_macro.test.js | 35 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 03ce8514..8bbd2d43 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -287,7 +287,8 @@ const KeyboardMacro = function({ awaitController }) { await playbackImpl(spec.args); return; } - if (onBeginWrappedCommandCallback) { + const sideEffectMode = spec.record === 'side-effect'; + if (!sideEffectMode && onBeginWrappedCommandCallback) { onBeginWrappedCommandCallback(); } try { @@ -296,7 +297,7 @@ const KeyboardMacro = function({ awaitController }) { push(spec); } } finally { - if (onEndWrappedCommandCallback) { + if (!sideEffectMode && onEndWrappedCommandCallback) { onEndWrappedCommandCallback(); } } diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index e8fa0c10..b55ffc6d 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -555,7 +555,7 @@ describe('KeybaordMacro', () => { { command: 'internal:log', args: 'world' } ]); }); - it('should call onBegin/EndWrappedCommand callback if it called during recording', async () => { + it('should call onBegin/EndWrappedCommand callback if called during recording', async () => { keyboardMacro.onBeginWrappedCommand(() => { logs.push('onbeginwrap'); }); keyboardMacro.onEndWrappedCommand(() => { logs.push('onendwrap'); }); const sequence = [ @@ -831,6 +831,8 @@ describe('KeybaordMacro', () => { }); }); afterEach(() => { + keyboardMacro.onBeginWrappedCommand(null); + keyboardMacro.onEndWrappedCommand(null); keyboardMacro.setPrintError(oldPrintError); }); it('should invoke and record specified command', async () => { @@ -848,6 +850,23 @@ describe('KeybaordMacro', () => { assert.deepStrictEqual(logs, []); }); + it('should call onBegin/EndWrappedCommand callback', async () => { + keyboardMacro.onBeginWrappedCommand(() => { logs.push('onbeginwrap'); }); + keyboardMacro.onEndWrappedCommand(() => { logs.push('onendwrap'); }); + keyboardMacro.startRecording(); + await keyboardMacro.wrapSync({ command: 'internal:log' }); + keyboardMacro.finishRecording(); + + assert.deepStrictEqual(logs, [ + 'onbeginwrap', + 'begin', + 'end', + 'onendwrap' + ]); + assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), [ + { command: 'internal:log' }, + ]); + }); it('should not crash even if the argument is invalid', async () => { keyboardMacro.startRecording(); await keyboardMacro.wrapSync({ command: '' }); @@ -981,6 +1000,18 @@ describe('KeybaordMacro', () => { assert.deepStrictEqual(logs, [ 'begin', 'end' ]); assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), []); }); - // TODO: test for side-effect mode not to invoke onBegin/EndWrappedCommand + it('should not call onBegin/EndWrappedCommand callback if side-effect mode', async () => { + keyboardMacro.onBeginWrappedCommand(() => { logs.push('onbeginwrap'); }); + keyboardMacro.onEndWrappedCommand(() => { logs.push('onendwrap'); }); + keyboardMacro.startRecording(); + await keyboardMacro.wrapSync({ command: 'internal:log', record: 'side-effect' }); + keyboardMacro.finishRecording(); + + assert.deepStrictEqual(logs, [ + 'begin', + 'end' + ]); + assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), []); + }); }); }); From 246bc307e215d4fe4171a04248169f312081d2b3 Mon Sep 17 00:00:00 2001 From: tshino Date: Fri, 18 Mar 2022 00:57:29 +0900 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b1f597..760e3cd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ All notable changes to the Keyboard Macro Bata extension will be documented in t - Added 'Keymap wrappers' section to [DESIGN.md](./DESIGN.md). - Update - Updated default keybindings based on VS Code 1.65.2. [#74](https://github.com/tshino/vscode-kb-macro/pull/74) +- Internal + - Added `record` option to the `kb-macro.wrap` command. [#76](https://github.com/tshino/vscode-kb-macro/pull/76) ### [0.11.3] - 2022-03-06 - Update