Project.scan handles paths with newlines

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-19 10:27:42 -06:00
parent a61064eeeb
commit 7a85e07cf1
2 changed files with 25 additions and 9 deletions

View File

@@ -138,6 +138,11 @@ describe "Project", ->
it "works on evil filenames", ->
project.setPath(require.resolve('fixtures/evil-files'))
paths = []
waitsForPromise ->
project.scan /evil/, ({path, match, range}) ->
#console.log path
paths.push(path)
runs ->
expect(paths[0]).toMatch /file with spaces.txt$/
expect(paths[1]).toMatch /goddam\nnewlines$/m

View File

@@ -138,16 +138,27 @@ class Project
scan: (regex, iterator) ->
regex = new RegExp(regex.source, 'g')
command = "#{require.resolve('ack')} --all-types --match \"#{regex.source}\" \"#{@getPath()}\""
ChildProcess.exec command , bufferLines: true, stdout: (data) ->
for grepLine in data.split('\n') when grepLine.length
pathEndIndex = grepLine.indexOf('\0')
lineNumberEndIndex = grepLine.indexOf('\0', pathEndIndex + 1)
path = grepLine.substring(0, pathEndIndex)
row = parseInt(grepLine.substring(pathEndIndex + 1, lineNumberEndIndex)) - 1
line = grepLine.substring(lineNumberEndIndex + 1)
command = "#{require.resolve('ack')} --all --match \"#{regex.source}\" \"#{@getPath()}\""
bufferedData = ""
promise = ChildProcess.exec command , bufferLines: true, stdout: (data) ->
bufferedData += data
currentIndex = 0
while currentIndex < bufferedData.length
pathEndIndex = bufferedData.indexOf('\0', currentIndex)
break unless pathEndIndex >= 0
lineNumberEndIndex = bufferedData.indexOf('\0', pathEndIndex + 1)
lineEndIndex = bufferedData.indexOf('\n', lineNumberEndIndex + 1)
path = bufferedData.substring(currentIndex, pathEndIndex)
row = parseInt(bufferedData.substring(pathEndIndex + 1, lineNumberEndIndex)) - 1
line = bufferedData.substring(lineNumberEndIndex + 1, lineEndIndex)
while match = regex.exec(line)
range = new Range([row, match.index], [row, match.index + match[0].length])
iterator({path, match, range})
currentIndex = lineEndIndex + 1
bufferedData = bufferedData.substring(currentIndex)
_.extend Project.prototype, EventEmitter