diff --git a/spec/app/atom-spec.coffee b/spec/app/atom-spec.coffee index e89ae779e..3b765e1f8 100644 --- a/spec/app/atom-spec.coffee +++ b/spec/app/atom-spec.coffee @@ -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", -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index bfb1ffdef..9ac81274d 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -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() diff --git a/src/app/atom.coffee b/src/app/atom.coffee index 604c15821..581cbff63 100644 --- a/src/app/atom.coffee +++ b/src/app/atom.coffee @@ -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 diff --git a/src/app/load-text-mate-packages-handler.coffee b/src/app/load-text-mate-packages-handler.coffee deleted file mode 100644 index c6dd9a18a..000000000 --- a/src/app/load-text-mate-packages-handler.coffee +++ /dev/null @@ -1,5 +0,0 @@ -TextMatePackage = require 'text-mate-package' - -module.exports = - loadPackage: (path) -> - callTaskMethod('packageLoaded', new TextMatePackage(path).readGrammars()) diff --git a/src/app/load-text-mate-packages-task.coffee b/src/app/load-text-mate-packages-task.coffee deleted file mode 100644 index f2fef932c..000000000 --- a/src/app/load-text-mate-packages-task.coffee +++ /dev/null @@ -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() diff --git a/src/app/text-mate-grammar.coffee b/src/app/text-mate-grammar.coffee index 22a83612e..94a6c0ff4 100644 --- a/src/app/text-mate-grammar.coffee +++ b/src/app/text-mate-grammar.coffee @@ -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 diff --git a/src/app/text-mate-package.coffee b/src/app/text-mate-package.coffee index bb7f6ed99..e45a256f8 100644 --- a/src/app/text-mate-package.coffee +++ b/src/app/text-mate-package.coffee @@ -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 diff --git a/src/packages/gfm/spec/gfm-spec.coffee b/src/packages/gfm/spec/gfm-spec.coffee index 7460dfea5..484835ae9 100644 --- a/src/packages/gfm/spec/gfm-spec.coffee +++ b/src/packages/gfm/spec/gfm-spec.coffee @@ -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", -> diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index 724b5e7bb..5a912717c 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -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) =>