mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-12-29 09:34:58 +03:00
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
This commit is contained in:
parent
42301210c4
commit
130e5cd3ec
58
src/packages/fuzzy-finder/lib/load-paths-handler.coffee
Normal file
58
src/packages/fuzzy-finder/lib/load-paths-handler.coffee
Normal file
@ -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()
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user