From 7a85e07cf1e9ebd025d75d3db1deb1d3afa0f4ee Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 19 Jul 2012 10:27:42 -0600 Subject: [PATCH] Project.scan handles paths with newlines --- spec/app/project-spec.coffee | 7 ++++++- src/app/project.coffee | 27 +++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 8a79e3931..a645d29c6 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -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 diff --git a/src/app/project.coffee b/src/app/project.coffee index d8700c5c1..ca9c677b3 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -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