]> arthur.barton.de Git - atom-ax-pipe.git/blobdiff - lib/pipe.coffee
Make use of editor transactions. Resolve #5.
[atom-ax-pipe.git] / lib / pipe.coffee
index 5294840bd507d0833bc6a85f296f20a40320056f..3781a56ee719479af896dd1939568251a6478967 100644 (file)
@@ -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()