From f312e36dccb4e9e0544a9d6ad5270bbaad76be7f Mon Sep 17 00:00:00 2001 From: moshee Date: Tue, 18 Nov 2014 15:34:46 -0800 Subject: [PATCH] Make use of editor transactions. Resolve #5. --- lib/pipe.coffee | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/pipe.coffee b/lib/pipe.coffee index 5294840..3781a56 100644 --- a/lib/pipe.coffee +++ b/lib/pipe.coffee @@ -26,13 +26,19 @@ module.exports = commandString = "cd '#{atom.project.rootDirectory.path}' && #{commandString}" properties = { reversed: true, invalidate: 'never' } - for range in editor.getSelectedBufferRanges() - marker = editor.markBufferRange range, properties - processRange marker, editor, commandString + ranges = editor.getSelectedBufferRanges() + wg = new WaitGroup -> + editor.commitTransaction() + view.focus() - view.focus() + wg.add(ranges.length) + + editor.beginTransaction() + for range, i in ranges + marker = editor.markBufferRange range, properties + processRange marker, editor, commandString, wg -processRange = (marker, editor, commandString) -> +processRange = (marker, editor, commandString, wg) -> stdout = '' stderr = '' @@ -47,6 +53,20 @@ processRange = (marker, editor, commandString) -> proc.on 'close', (code) -> text = stderr || stdout editor.setTextInBufferRange(marker.getBufferRange(), text) + wg.done() proc.stdin.write(editor.getTextInBufferRange(marker.getBufferRange())) proc.stdin.end() + +class WaitGroup + constructor: (cb) -> + @n = 0 + @cb = cb + + add: (n) -> + @n += n + + done: -> + @n -= 1 + if @n <= 0 + @cb() -- 2.39.2