Replace LoadTextMatePackagesTask with async grammar loading

This commit is contained in:
Nathan Sobo
2013-03-20 18:55:28 -06:00
parent 8e57ee2776
commit ab934cfbfb
9 changed files with 61 additions and 88 deletions

View File

@@ -63,24 +63,6 @@ describe "the `atom` global", ->
window.loadPackage("package-with-module")
expect(stylesheetElementForId(stylesheetPath).length).toBe 1
describe ".loadPackages()", ->
beforeEach ->
spyOn(syntax, 'addGrammar')
it "aborts the worker when all packages have been loaded", ->
LoadTextMatePackagesTask = require 'load-text-mate-packages-task'
spyOn(LoadTextMatePackagesTask.prototype, 'abort').andCallThrough()
eventHandler = jasmine.createSpy('eventHandler')
syntax.on 'grammars-loaded', eventHandler
config.get("core.disabledPackages").push('textmate-package.tmbundle', 'package-with-snippets')
atom.loadPackages()
waitsFor "all packages to load", 5000, -> eventHandler.callCount > 0
runs ->
expect(LoadTextMatePackagesTask.prototype.abort).toHaveBeenCalled()
expect(LoadTextMatePackagesTask.prototype.abort.calls.length).toBe 1
describe "package lifecycle", ->
describe "activation", ->
it "calls activate on the package main with its previous state", ->

View File

@@ -107,7 +107,7 @@ window.loadTextMatePackages = ->
config.packageDirPaths.unshift(fixturePackagesPath)
window.textMatePackages = []
for path in atom.getPackagePaths() when TextMatePackage.testName(path)
window.textMatePackages.push window.loadPackage(fs.base(path))
window.textMatePackages.push window.loadPackage(fs.base(path), sync: true)
window.loadTextMatePackages()

View File

@@ -3,7 +3,6 @@ _ = require 'underscore'
Package = require 'package'
TextMatePackage = require 'text-mate-package'
Theme = require 'theme'
LoadTextMatePackagesTask = require 'load-text-mate-packages-task'
messageIdCounter = 1
originalSendMessageToBrowserProcess = atom.sendMessageToBrowserProcess
@@ -46,7 +45,7 @@ _.extend atom,
textPackagePath = _.find @getPackagePaths(), (path) -> fs.base(path) is 'text.tmbundle'
pack = Package.build(textPackagePath)
@loadedPackages.push(pack)
pack.load()
pack.load(sync: true)
loadPackages: ->
textMatePackages = []
@@ -54,12 +53,7 @@ _.extend atom,
for path in paths
pack = Package.build(path)
@loadedPackages.push(pack)
if pack instanceof TextMatePackage
textMatePackages.push(pack)
else
pack.load()
new LoadTextMatePackagesTask(textMatePackages).start() if textMatePackages.length > 0
pack.load()
activatePackages: ->
pack.activate() for pack in @loadedPackages

View File

@@ -1,5 +0,0 @@
TextMatePackage = require 'text-mate-package'
module.exports =
loadPackage: (path) ->
callTaskMethod('packageLoaded', new TextMatePackage(path).readGrammars())

View File

@@ -1,26 +0,0 @@
Task = require 'task'
module.exports =
class LoadTextMatePackagesTask extends Task
constructor: (@packages) ->
super('load-text-mate-packages-handler')
started: ->
@loadNextPackage()
loadNextPackage: ->
unless @packages.length
@done()
syntax.trigger 'grammars-loaded'
return
@package = @packages.shift()
@loadPackage(@package.path)
loadPackage: (path) ->
@callWorkerMethod('loadPackage', path)
packageLoaded: (grammars) ->
@package.loadGrammars(grammars)
@loadNextPackage()

View File

@@ -10,6 +10,16 @@ class TextMateGrammar
@readFromPath: (path) ->
fs.readPlist(path)
@load: (path, done) ->
fs.readObjectAsync path, (err, object) ->
if err
done(err)
else
done(null, new TextMateGrammar(object))
@loadSync: (path) ->
new TextMateGrammar(fs.readObject(path))
name: null
fileTypes: null
scopeName: null

View File

@@ -5,6 +5,7 @@ plist = require 'plist'
_ = require 'underscore'
TextMateGrammar = require 'text-mate-grammar'
CSON = require 'cson'
async = require 'async'
module.exports =
class TextMatePackage extends Package
@@ -17,38 +18,39 @@ class TextMatePackage extends Package
@syntaxesPath = fsUtils.join(@path, "Syntaxes")
@grammars = []
load: ->
try
load: ({sync}={}) ->
if sync
@loadGrammarsSync()
else
@loadGrammars()
catch e
console.warn "Failed to load package at '#{@path}'", e.stack
this
@loadScopedProperties()
legalGrammarExtensions: ['plist', 'tmLanguage', 'tmlanguage', 'cson', 'json']
loadGrammars: (done) ->
fsUtils.isDirectoryAsync @syntaxesPath, (isDirectory) =>
if isDirectory
fsUtils.listAsync @syntaxesPath, @legalGrammarExtensions, (err, paths) =>
return console.log("Error loading grammars of TextMate package '#{@path}':", err.stack, err) if err
async.eachSeries paths, @loadGrammarAtPath, done
loadGrammarAtPath: (path, done) =>
TextMateGrammar.load path, (err, grammar) =>
return console.log("Error loading grammar at path '#{path}':", err.stack ? err) if err
@addGrammar(grammar)
loadGrammarsSync: ->
for path in fsUtils.list(@syntaxesPath, @legalGrammarExtensions) ? []
@addGrammar(TextMateGrammar.loadSync(path))
addGrammar: (grammar) ->
@grammars.push(grammar)
syntax.addGrammar(grammar)
activate: -> # no-op
getGrammars: -> @grammars
readGrammars: ->
grammars = []
for grammarPath in fsUtils.list(@syntaxesPath)
try
grammars.push(TextMateGrammar.readFromPath(grammarPath))
catch e
console.warn "Failed to load grammar at path '#{grammarPath}'", e.stack
grammars
addGrammar: (rawGrammar) ->
grammar = new TextMateGrammar(rawGrammar)
@grammars.push(grammar)
syntax.addGrammar(grammar)
loadGrammars: (rawGrammars) ->
rawGrammars = @readGrammars() unless rawGrammars?
@grammars = []
@addGrammar(rawGrammar) for rawGrammar in rawGrammars
@loadScopedProperties()
loadScopedProperties: ->
for { selector, properties } in @getScopedProperties()
syntax.addProperties(selector, properties)
@@ -70,10 +72,10 @@ class TextMatePackage extends Package
getTextMatePreferenceObjects: ->
preferenceObjects = []
if fs.exists(@preferencesPath)
for preferencePath in fs.list(@preferencesPath)
if fsUtils.exists(@preferencesPath)
for preferencePath in fsUtils.list(@preferencesPath)
try
preferenceObjects.push(fs.readObject(preferencePath))
preferenceObjects.push(fsUtils.readObject(preferencePath))
catch e
console.warn "Failed to parse preference at path '#{preferencePath}'", e.stack
preferenceObjects

View File

@@ -10,7 +10,7 @@ describe "GitHub Flavored Markdown grammar", ->
grammar = syntax.addGrammar.argsForCall[0][0]
it "parses the grammar", ->
expect(grammar).toBeTruthy()
expect(grammar).toBeDefined()
expect(grammar.scopeName).toBe "source.gfm"
it "tokenizes horizontal rules", ->

View File

@@ -69,6 +69,15 @@ module.exports =
catch e
false
isDirectoryAsync: (path, done) ->
return done(false) unless path?.length > 0
fs.exists path, (exists) ->
if exists
fs.stat path, (err, stat) ->
done(stat?.isDirectory() ? false)
else
done(false)
# Returns true if the file specified by path exists and is a
# regular file.
isFile: (path) ->
@@ -291,6 +300,13 @@ module.exports =
else
@readPlist(path)
readObjectAsync: (path, done) ->
cson = require 'cson'
if cson.isObjectPath(path)
cson.readObjectAsync(path, done)
else
@readPlistAsync(path, done)
watchPath: (path, callback) ->
path = @absolute(path)
watchCallback = (eventType, eventPath) =>