Skip to content

Commit

Permalink
Naïve implementation. Doesn't give correct line numbers. [atom#5]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Dec 22, 2016
1 parent a2abc00 commit d8dd6af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
20 changes: 20 additions & 0 deletions spec/path-searcher-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
escapeStringRegexp = require 'escape-string-regexp'
fs = require 'fs'
path = require 'path'
PathSearcher = require '../src/path-searcher'
Expand Down Expand Up @@ -133,6 +134,25 @@ describe "PathSearcher", ->
expect(results.matches[0].matchText).toBe 'items'
expect(results.matches[0].range).toEqual [[1, 22], [1, 27]]

it "finds multiline matches", ->
searcher.on 'results-found', resultsHandler = jasmine.createSpy()
pattern = ("\\s*#{escapeStringRegexp line}" for line in [
'var pivot = items.shift(), current, left = [], right = [];',
'while(items.length > 0) {'
]).join('\\n')
searcher.searchPath new RegExp(pattern, 'g'), filePath, finishedHandler = jasmine.createSpy()

waitsFor ->
finishedHandler.callCount > 0

runs ->
expect(resultsHandler.callCount).toBe 1

results = resultsHandler.mostRecentCall.args[0]
expect(results.filePath).toBe filePath
expect(results.matches.length).toBe 1


describe "With windows line endings", ->
beforeEach ->
filePath = path.join(rootPath, 'sample-with-windows-line-endings.js')
Expand Down
33 changes: 22 additions & 11 deletions src/path-searcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,29 @@ class PathSearcher extends EventEmitter
# * `error` {Error}; null when there is no error
searchPath: (regex, filePath, doneCallback) ->
matches = null
lineNumber = 0
reader = new ChunkedLineReader(filePath)
error = null
lineNumber = 0
lineBreaks = /\\[nr]|\n|\r/

if regex.source.match lineBreaks
reader = fs.createReadStream filePath
reader.on 'data', (chunk) =>
string = chunk.toString()
stringMatches = @searchLine regex, string, lineNumber++ # TODO: I know this will not give a correct line number!
if stringMatches?
matches ?= []
matches.push(match) for match in stringMatches
else
reader = new ChunkedLineReader(filePath)

reader.on 'data', (chunk) =>
lines = chunk.toString().replace(TRAILING_LINE_END_REGEX, '').split(LINE_END_REGEX)
for line in lines
lineMatches = @searchLine(regex, line, lineNumber++)

if lineMatches?
matches ?= []
matches.push(match) for match in lineMatches

reader.on 'error', (e) =>
error = e
Expand All @@ -158,15 +178,6 @@ class PathSearcher extends EventEmitter
@emit('results-not-found', filePath)
doneCallback(output, error)

reader.on 'data', (chunk) =>
lines = chunk.toString().replace(TRAILING_LINE_END_REGEX, '').split(LINE_END_REGEX)
for line in lines
lineMatches = @searchLine(regex, line, lineNumber++)

if lineMatches?
matches ?= []
matches.push(match) for match in lineMatches


return

Expand Down

0 comments on commit d8dd6af

Please sign in to comment.