Project.scan invokes the callback with matches

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-12 11:12:31 -06:00
parent 2fe56ba077
commit d2d6efdcb5
2 changed files with 31 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'
Range = require 'range'
Buffer = require 'buffer'
EditSession = require 'edit-session'
EventEmitter = require 'event-emitter'
@@ -127,16 +127,18 @@ class Project
bufferWithPath: (path) ->
return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == path
scan: ({regex}, callback) ->
scan: (regex, iterator) ->
regex = new RegExp(regex.source, 'g')
command = "grep --null --perl-regexp --with-filename --line-number --recursive --regexp=#{regex.source} #{@getPath()}"
ChildProcess.exec command, bufferLines: false, stdout: (data) ->
for grepLine in data.split('\n') when grepLine.length
nullCharIndex = grepLine.indexOf('\0')
colonIndex = grepLine.indexOf(':')
path = grepLine.substring(0, nullCharIndex)
row = parseInt(grepLine.substring(nullCharIndex + 1, colonIndex)) - 1
line = grepLine.substring(colonIndex + 1)
console.log path, row, line
ChildProcess.exec command, bufferLines: true, stdout: (data) ->
for grepLine in data.split('\n') when grepLine.length
nullCharIndex = grepLine.indexOf('\0')
colonIndex = grepLine.indexOf(':')
path = grepLine.substring(0, nullCharIndex)
row = parseInt(grepLine.substring(nullCharIndex + 1, colonIndex)) - 1
line = grepLine.substring(colonIndex + 1)
while match = regex.exec(line)
range = new Range([row, match.index], [row, match.index + match[0].length])
iterator({path, match, range})
_.extend Project.prototype, EventEmitter