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

@@ -109,12 +109,23 @@ describe "Project", ->
describe ".scan(options, callback)", ->
describe "when called with a regex", ->
fit "calls the callback with all regex matches in all files in the project", ->
it "calls the callback with all regex matches in all files in the project", ->
matches = []
project.scan regex: /a+/, ({path, match, range}) ->
matches.push({path, match, range})
expect(matches[0]).toEqual
path: project.resolve('a')
match: 'aaa'
range: [[0, 0], [0, 3]]
waitsForPromise ->
project.scan /a+/, ({path, match, range}) ->
console.log "ITERATOR", path, match, range
matches.push({path, match, range})
runs ->
expect(matches[0]).toEqual
path: project.resolve('a')
match: ['aaa']
range: [[0, 0], [0, 3]]
expect(matches[1]).toEqual
path: project.resolve('a')
match: ['aa']
range: [[1, 3], [1, 5]]

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