Make Workspace::scan work w/ multiple root directories

This commit is contained in:
Max Brunsfeld
2015-02-24 14:22:39 -08:00
parent a208e789d7
commit cbefdd6c5e
3 changed files with 100 additions and 12 deletions

View File

@@ -854,6 +854,64 @@ describe "Workspace", ->
runs ->
expect(results).toHaveLength 0
describe "when the project has multiple root directories", ->
[dir1, dir2, file1, file2] = []
beforeEach ->
[dir1] = atom.project.getPaths()
file1 = path.join(dir1, "a-dir", "oh-git")
dir2 = temp.mkdirSync("a-second-dir")
aDir2 = path.join(dir2, "a-dir")
file2 = path.join(aDir2, "a-file")
fs.mkdirSync(aDir2)
fs.writeFileSync(file2, "ccc aaaa")
atom.project.addPath(dir2)
it "searches matching files in all of the project's root directories", ->
resultPaths = []
waitsForPromise ->
atom.workspace.scan /aaaa/, ({filePath}) ->
resultPaths.push(filePath)
runs ->
expect(resultPaths.sort()).toEqual([file1, file2].sort())
describe "when an inclusion path starts with the basename of a root directory", ->
it "interprets the inclusion path as starting from that directory", ->
waitsForPromise ->
resultPaths = []
atom.workspace
.scan /aaaa/, paths: ["dir"], ({filePath}) ->
resultPaths.push(filePath) unless filePath in resultPaths
.then ->
expect(resultPaths).toEqual([file1])
waitsForPromise ->
resultPaths = []
atom.workspace
.scan /aaaa/, paths: [path.join("dir", "a-dir")], ({filePath}) ->
resultPaths.push(filePath) unless filePath in resultPaths
.then ->
expect(resultPaths).toEqual([file1])
waitsForPromise ->
resultPaths = []
atom.workspace
.scan /aaaa/, paths: [path.basename(dir2)], ({filePath}) ->
resultPaths.push(filePath) unless filePath in resultPaths
.then ->
expect(resultPaths).toEqual([file2])
waitsForPromise ->
resultPaths = []
atom.workspace
.scan /aaaa/, paths: [path.join(path.basename(dir2), "a-dir")], ({filePath}) ->
resultPaths.push(filePath) unless filePath in resultPaths
.then ->
expect(resultPaths).toEqual([file2])
describe "::replace(regex, replacementText, paths, iterator)", ->
[filePath, commentFilePath, sampleContent, sampleCommentContent] = []

View File

@@ -1,13 +1,17 @@
_ = require "underscore-plus"
path = require "path"
async = require "async"
{PathSearcher, PathScanner, search} = require 'scandal'
module.exports = (rootPath, regexSource, options) ->
module.exports = (rootPaths, regexSource, options) ->
callback = @async()
rootPath = rootPaths[0]
PATHS_COUNTER_SEARCHED_CHUNK = 50
pathsSearched = 0
searcher = new PathSearcher()
scanner = new PathScanner(rootPath, options)
searcher.on 'file-error', ({code, path, message}) ->
emit('scan:file-error', {code, path, message})
@@ -15,14 +19,41 @@ module.exports = (rootPath, regexSource, options) ->
searcher.on 'results-found', (result) ->
emit('scan:result-found', result)
scanner.on 'path-found', ->
pathsSearched++
if pathsSearched % PATHS_COUNTER_SEARCHED_CHUNK == 0
emit('scan:paths-searched', pathsSearched)
flags = "g"
flags += "i" if options.ignoreCase
regex = new RegExp(regexSource, flags)
search regex, scanner, searcher, ->
emit('scan:paths-searched', pathsSearched)
callback()
async.each(
rootPaths,
(rootPath, next) ->
options2 = _.extend {}, options,
inclusions: processPaths(rootPath, options.inclusions)
exclusions: processPaths(rootPath, options.exclusions)
scanner = new PathScanner(rootPath, options2)
scanner.on 'path-found', ->
pathsSearched++
if pathsSearched % PATHS_COUNTER_SEARCHED_CHUNK == 0
emit('scan:paths-searched', pathsSearched)
search regex, scanner, searcher, ->
emit('scan:paths-searched', pathsSearched)
next()
callback
)
processPaths = (rootPath, paths) ->
return paths unless paths?.length > 0
rootPathBase = path.basename(rootPath)
results = []
for givenPath in paths
segments = givenPath.split(path.sep)
firstSegment = segments.shift()
results.push(givenPath)
if firstSegment is rootPathBase
if segments.length is 0
results.push(path.join("**", "*"))
else
results.push(path.join(segments...))
results

View File

@@ -870,8 +870,7 @@ class Workspace extends Model
exclusions: atom.config.get('core.ignoredNames')
follow: atom.config.get('core.followSymlinks')
# TODO: need to support all paths in @getPaths()
task = Task.once require.resolve('./scan-handler'), atom.project.getPaths()[0], regex.source, searchOptions, ->
task = Task.once require.resolve('./scan-handler'), atom.project.getPaths(), regex.source, searchOptions, ->
deferred.resolve()
task.on 'scan:result-found', (result) ->