From 8fe9e31c08df5e82e1878b90640cfb1bdfc4b9f4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Mar 2013 11:27:54 -0700 Subject: [PATCH] Don't use a web worker for loading all paths Instead use fs.realpath() and fs.stat() to walk the project tree asynchronously. --- .../lib/load-paths-handler.coffee | 23 ----------- .../fuzzy-finder/lib/load-paths-task.coffee | 38 +++++++++++++------ src/stdlib/fs-utils.coffee | 27 +++++++++++++ 3 files changed, 54 insertions(+), 34 deletions(-) delete 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 deleted file mode 100644 index 12ac60605..000000000 --- a/src/packages/fuzzy-finder/lib/load-paths-handler.coffee +++ /dev/null @@ -1,23 +0,0 @@ -fs = require 'fs-utils' -_ = require 'underscore' - -module.exports = - loadPaths: (rootPath, ignoredNames, excludeGitIgnoredPaths) -> - if excludeGitIgnoredPaths - Git = require 'git' - repo = Git.open(rootPath, refreshOnWindowFocus: false) - - paths = [] - isIgnored = (path) -> - for segment in path.split('/') - return true if _.contains(ignoredNames, segment) - repo?.isPathIgnored(fs.join(rootPath, path)) - onFile = (path) -> - paths.push(path) unless isIgnored(path) - onDirectory = (path) -> - not isIgnored(path) - fs.traverseTree(rootPath, onFile, onDirectory) - - repo?.destroy() - - callTaskMethod('pathsLoaded', paths) diff --git a/src/packages/fuzzy-finder/lib/load-paths-task.coffee b/src/packages/fuzzy-finder/lib/load-paths-task.coffee index f2ff851b6..a674e958f 100644 --- a/src/packages/fuzzy-finder/lib/load-paths-task.coffee +++ b/src/packages/fuzzy-finder/lib/load-paths-task.coffee @@ -1,17 +1,33 @@ -Task = require 'task' +_ = require 'underscore' +fs = require 'fs-utils' module.exports = -class LoadPathsTask extends Task - constructor: (@callback) -> - super('fuzzy-finder/lib/load-paths-handler') +class LoadPathsTask + aborted: false - started: -> + constructor: (@callback) -> + + start: -> + rootPath = project.getPath() ignoredNames = config.get('fuzzyFinder.ignoredNames') ? [] ignoredNames = ignoredNames.concat(config.get('core.ignoredNames') ? []) - excludeGitIgnoredPaths = config.get('core.hideGitIgnoredFiles') - rootPath = project.getPath() - @callWorkerMethod('loadPaths', rootPath, ignoredNames, excludeGitIgnoredPaths) + ignoreGitIgnoredFiles = config.get('core.hideGitIgnoredFiles') - pathsLoaded: (paths) -> - @done() - @callback(paths) + paths = [] + isIgnored = (path) -> + for segment in path.split('/') + return true if _.contains(ignoredNames, segment) + ignoreGitIgnoredFiles and git?.isPathIgnored(fs.join(rootPath, path)) + onFile = (path) -> + return if @aborted + path = path.substring(rootPath.length + 1) + paths.push(path) unless isIgnored(path) + onDirectory = (path) => + not @aborted and not isIgnored(path.substring(rootPath.length + 1)) + onDone = => + @callback(paths) unless @aborted + + fs.traverseTreeAsync(rootPath, onFile, onDirectory, onDone) + + abort: -> + @aborted = true diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index ad1260e83..f129027a0 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -168,6 +168,33 @@ module.exports = traverse(rootPath, '', onFile, onDirectory) + traverseTreeAsync: (rootPath, onFile, onDirectory, onDone) -> + pathCounter = 0 + startPath = -> pathCounter++ + endPath = -> onDone() if --pathCounter is 0 + + traverse = (rootPath, onFile, onDirectory) => + startPath() + fs.readdir rootPath, (error, files) => + if error or files.length is 0 + endPath() + return + + for file in files + path = @join(rootPath, file) + do (path) => + startPath() + fs.stat path, (error, stats) => + unless error + if stats.isFile() + onFile(path) + else if stats.isDirectory() + traverse(path, onFile, onDirectory) if onDirectory(path) + endPath() + endPath() + + traverse(rootPath, onFile, onDirectory) + md5ForPath: (path) -> contents = fs.readFileSync(path) require('crypto').createHash('md5').update(contents).digest('hex')