diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 15ef5e7b5..97bace5eb 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -106,3 +106,15 @@ describe "Project", -> project.getFilePaths().done (paths) -> expect(paths).not.toContain('a') expect(paths).toContain('b') + + describe ".scan(options, callback)", -> + describe "when called with a regex", -> + fit "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]] diff --git a/spec/fixtures/dir/a b/spec/fixtures/dir/a index e69de29bb..9bda1cbd0 100644 --- a/spec/fixtures/dir/a +++ b/spec/fixtures/dir/a @@ -0,0 +1,2 @@ +aaa bbb +cc aa cc diff --git a/spec/fixtures/dir/b b/spec/fixtures/dir/b index e69de29bb..f2c4fd35c 100644 --- a/spec/fixtures/dir/b +++ b/spec/fixtures/dir/b @@ -0,0 +1 @@ +aaa ccc diff --git a/src/app/project.coffee b/src/app/project.coffee index 40ddfc523..689a1beab 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -6,6 +6,7 @@ Buffer = require 'buffer' EditSession = require 'edit-session' EventEmitter = require 'event-emitter' Directory = require 'directory' +ChildProcess = require 'child-process' module.exports = class Project @@ -126,4 +127,16 @@ class Project bufferWithPath: (path) -> return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == path + scan: ({regex}, callback) -> + 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 + _.extend Project.prototype, EventEmitter diff --git a/src/stdlib/child-process.coffee b/src/stdlib/child-process.coffee index e8885a7a2..f22cc09a9 100644 --- a/src/stdlib/child-process.coffee +++ b/src/stdlib/child-process.coffee @@ -14,6 +14,7 @@ class ChildProccess options.stderr = @bufferLines(options.stderr) if options.stderr $native.exec command, options, (exitStatus, stdout, stderr) -> + console.log exitStatus if exitStatus != 0 error = new Error("Exec failed (#{exitStatus}) command '#{command}'") error.exitStatus = exitStatus