mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Load grammars from web worker
This commit is contained in:
@@ -3,6 +3,7 @@ _ = 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
|
||||
@@ -13,7 +14,15 @@ _.extend atom,
|
||||
pendingBrowserProcessCallbacks: {}
|
||||
|
||||
loadPackages: ->
|
||||
pack.load() for pack in @getPackages()
|
||||
{packages, asyncTextMatePackages} = _.groupBy @getPackages(), (pack) ->
|
||||
if pack instanceof TextMatePackage and pack.name isnt 'text.tmbundle'
|
||||
'asyncTextMatePackages'
|
||||
else
|
||||
'packages'
|
||||
|
||||
pack.load() for pack in packages
|
||||
if asyncTextMatePackages.length
|
||||
new LoadTextMatePackagesTask(asyncTextMatePackages).start()
|
||||
|
||||
getPackages: ->
|
||||
@getPackageNames().map((name) -> Package.build(name)).filter (pack) -> pack?
|
||||
|
||||
@@ -402,6 +402,11 @@ class Editor extends View
|
||||
@gutter.widthChanged = (newWidth) =>
|
||||
@scrollView.css('left', newWidth + 'px')
|
||||
|
||||
rootView?.on 'grammars-loaded', =>
|
||||
@reloadGrammar()
|
||||
for session in @editSessions
|
||||
session.reloadGrammar() unless session is @activeEditSession
|
||||
|
||||
@scrollView.on 'scroll', =>
|
||||
if @scrollView.scrollLeft() == 0
|
||||
@gutter.removeClass('drop-shadow')
|
||||
|
||||
5
src/app/load-text-mate-packages-handler.coffee
Normal file
5
src/app/load-text-mate-packages-handler.coffee
Normal file
@@ -0,0 +1,5 @@
|
||||
TextMatePackage = require 'text-mate-package'
|
||||
|
||||
module.exports =
|
||||
loadPackage: (name) ->
|
||||
callTaskMethod('packageLoaded', new TextMatePackage(name).readGrammars())
|
||||
22
src/app/load-text-mate-packages-task.coffee
Normal file
22
src/app/load-text-mate-packages-task.coffee
Normal file
@@ -0,0 +1,22 @@
|
||||
Task = require 'Task'
|
||||
|
||||
module.exports =
|
||||
class LoadTextMatePackagesTask extends Task
|
||||
|
||||
constructor: (@packages) ->
|
||||
super('load-text-mate-packages-handler')
|
||||
|
||||
started: ->
|
||||
@loadNextPackage()
|
||||
|
||||
loadNextPackage: ->
|
||||
unless @packages.length
|
||||
rootView.trigger 'grammars-loaded'
|
||||
return
|
||||
|
||||
@package = @packages.shift()
|
||||
@callWorkerMethod('loadPackage', @package.name)
|
||||
|
||||
packageLoaded: (grammars) ->
|
||||
@package.loadGrammars(grammars)
|
||||
@loadNextPackage()
|
||||
@@ -7,13 +7,13 @@ OnigScanner = require 'onig-scanner'
|
||||
|
||||
module.exports =
|
||||
class TextMateGrammar
|
||||
@loadFromPath: (path) ->
|
||||
grammar = null
|
||||
@readFromPath: (path) ->
|
||||
grammarContent = null
|
||||
plist.parseString fs.read(path), (e, data) ->
|
||||
throw new Error(e) if e
|
||||
grammar = new TextMateGrammar(data[0])
|
||||
throw new Error("Failed to load grammar at path `#{path}`") unless grammar
|
||||
grammar
|
||||
grammarContent = data[0]
|
||||
throw new Error("Failed to load grammar at path `#{path}`") unless grammarContent
|
||||
grammarContent
|
||||
|
||||
name: null
|
||||
fileTypes: null
|
||||
|
||||
@@ -22,28 +22,41 @@ class TextMatePackage extends Package
|
||||
super
|
||||
@preferencesPath = fs.join(@path, "Preferences")
|
||||
@syntaxesPath = fs.join(@path, "Syntaxes")
|
||||
@grammars = []
|
||||
|
||||
load: ->
|
||||
try
|
||||
for grammar in @getGrammars()
|
||||
syntax.addGrammar(grammar)
|
||||
|
||||
for { selector, properties } in @getScopedProperties()
|
||||
syntax.addProperties(selector, properties)
|
||||
@loadGrammars()
|
||||
catch e
|
||||
console.warn "Failed to load package named '#{@name}'", e.stack
|
||||
this
|
||||
|
||||
getGrammars: ->
|
||||
return @grammars if @grammars
|
||||
getGrammars: -> @grammars
|
||||
|
||||
readGrammars: ->
|
||||
grammars = []
|
||||
for grammarPath in fs.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 = []
|
||||
if fs.exists(@syntaxesPath)
|
||||
for grammarPath in fs.list(@syntaxesPath)
|
||||
try
|
||||
@grammars.push TextMateGrammar.loadFromPath(grammarPath)
|
||||
catch e
|
||||
console.warn "Failed to load grammar at path '#{grammarPath}'", e.stack
|
||||
@grammars
|
||||
@addGrammar(rawGrammar) for rawGrammar in rawGrammars
|
||||
@loadScopedProperties()
|
||||
|
||||
loadScopedProperties: ->
|
||||
for { selector, properties } in @getScopedProperties()
|
||||
syntax.addProperties(selector, properties)
|
||||
|
||||
getScopedProperties: ->
|
||||
scopedProperties = []
|
||||
|
||||
Reference in New Issue
Block a user