mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Project.scan invokes the callback with matches
This commit is contained in:
@@ -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]]
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user