]> arthur.barton.de Git - atom-ax-pipe.git/blobdiff - lib/pipe.coffee
Don't use editor.{begin|commit}Transaction
[atom-ax-pipe.git] / lib / pipe.coffee
index 5294840bd507d0833bc6a85f296f20a40320056f..7c97563e4eb1e2028090ad4a35a7b6e7fa1547ad 100644 (file)
@@ -6,7 +6,7 @@ history = []
 
 module.exports =
   activate: ->
-    atom.workspaceView.command 'pipe:run', => @run()
+    atom.commands.add 'atom-workspace', "pipe:run", => @run()
 
   run: ->
     editor = atom.workspace.getActiveEditor()
@@ -26,13 +26,17 @@ 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 ->
+        view.focus()
 
-      view.focus()
+      wg.add(ranges.length)
+
+      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 +51,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()