diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 97bace5eb..f05712fe3 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -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]] + + diff --git a/src/app/project.coffee b/src/app/project.coffee index 689a1beab..d10110582 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -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