From 130e5cd3ec98987bfabdf4080c619bcadb90084d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 6 May 2013 15:27:37 -0700 Subject: [PATCH] Use task handler to load fuzzy finder paths This use completely async calls from a process task and also respects the repository-defined ignore rules. Closes #509 and #514 --- .../lib/load-paths-handler.coffee | 58 +++++++++++++++++++ .../fuzzy-finder/lib/load-paths-task.coffee | 40 ++++--------- 2 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 src/packages/fuzzy-finder/lib/load-paths-handler.coffee diff --git a/src/packages/fuzzy-finder/lib/load-paths-handler.coffee b/src/packages/fuzzy-finder/lib/load-paths-handler.coffee new file mode 100644 index 000000000..1a316ba41 --- /dev/null +++ b/src/packages/fuzzy-finder/lib/load-paths-handler.coffee @@ -0,0 +1,58 @@ +fs = require 'fs' +path = require 'path' +_ = require 'underscore' +Git = require 'git' + +class PathLoader + asyncCallsInProgress: 0 + pathsChunkSize: 100 + paths: [] + repo: null + rootPath: null + ignoredNames: null + + constructor: (@rootPath, @ignoredNames) -> + @repo = Git.open(@rootPath) + @ignoredNames.sort() + + isIgnored: (loadedPath) -> + @repo?.isPathIgnored(loadedPath) or _.indexOf(@ignoredNames, path.basename(loadedPath), true) isnt -1 + + asyncCallStarting: -> + @asyncCallsInProgress++ + + asyncCallDone: -> + if --@asyncCallsInProgress is 0 + @repo?.release() + callTaskMethod('pathsLoaded', @paths) + callTaskMethod('pathLoadingComplete') + + pathLoaded: (path) -> + @paths.push(path) unless @isIgnored(path) + if @paths.length is @pathsChunkSize + callTaskMethod('pathsLoaded', @paths) + @paths = [] + + loadPath: (path) -> + @asyncCallStarting() + fs.stat path, (error, stats) => + unless error? + if stats.isDirectory() + @loadFolder(path) unless @isIgnored(path) + else if stats.isFile() + @pathLoaded(path) + @asyncCallDone() + + loadFolder: (folderPath) -> + @asyncCallStarting() + fs.readdir folderPath, (error, children=[]) => + @loadPath(path.join(folderPath, childName)) for childName in children + @asyncCallDone() + + load: -> + @loadFolder(@rootPath) + +module.exports = + loadPaths: (rootPath, ignoredNames) -> + pathLoader = new PathLoader(rootPath, ignoredNames) + pathLoader.load() diff --git a/src/packages/fuzzy-finder/lib/load-paths-task.coffee b/src/packages/fuzzy-finder/lib/load-paths-task.coffee index 8253da523..dfe10d5c6 100644 --- a/src/packages/fuzzy-finder/lib/load-paths-task.coffee +++ b/src/packages/fuzzy-finder/lib/load-paths-task.coffee @@ -1,37 +1,19 @@ -_ = require 'underscore' -BufferedProcess = require 'buffered-process' +Task = require 'task' module.exports = -class LoadPathsTask +class LoadPathsTask extends Task constructor: (@callback) -> + super(require.resolve('./load-paths-handler')) - start: -> - rootPath = project.getPath() + started: -> + @paths = [] ignoredNames = config.get('fuzzyFinder.ignoredNames') ? [] ignoredNames = ignoredNames.concat(config.get('core.ignoredNames') ? []) + @callWorkerMethod('loadPaths', project.getPath(), ignoredNames) - command = require.resolve 'nak' - args = ['--list', rootPath] - args.unshift('--addVCSIgnores') if config.get('core.excludeVcsIgnoredPaths') - args.unshift('--ignore', ignoredNames.join(',')) if ignoredNames.length > 0 - args.unshift('--follow') - args.unshift('--hidden') + pathsLoaded: (paths) -> + @paths.push(paths...) - paths = [] - exit = (code) => - if code is 0 - @callback(paths) - else - console.error "Path loading process exited with status #{code}" - @callback([]) - stdout = (data) -> - paths.push(_.compact(data.split('\n'))...) - stderr = (data) -> - console.error "Error in LoadPathsTask:\n#{data}" - - @process = new BufferedProcess({command, args, stdout, stderr, exit}) - - abort: -> - if @process? - @process.kill() - @process = null + pathLoadingComplete: -> + @callback(@paths) + @done()