Support activation events in package manifest

These events will load the package's main module
when first triggered.  The go-to-line package has been
migrated to use this new option.
This commit is contained in:
Kevin Sawicki
2013-02-06 20:12:45 -08:00
committed by Corey Johnson & Kevin Sawicki
parent d884bf58e8
commit 7c41b15e00
5 changed files with 36 additions and 16 deletions

View File

@@ -1,5 +1,6 @@
Package = require 'package'
fs = require 'fs'
_ = require 'underscore'
module.exports =
class AtomPackage extends Package
@@ -16,12 +17,34 @@ class AtomPackage extends Package
@loadMetadata()
@loadKeymaps()
@loadStylesheets() if @autoloadStylesheets
if packageMain = @getPackageMain()
rootView?.activatePackage(@name, packageMain)
if activationEvents = @getActivationEvents()
@subscribeToActivationEvents(activationEvents)
else
@activatePackageMain()
catch e
console.warn "Failed to load package named '#{@name}'", e.stack
this
subscribeToActivationEvents: (activationEvents) ->
if _.isArray(activationEvents)
activateHandler = =>
@activatePackageMain()
for event in activationEvents
rootView.off event, activateHandler
for event in activationEvents
rootView.command event, activateHandler
else
activateHandler = =>
@activatePackageMain()
for event, selector of activationEvents
rootView.off event, selector, activateHandler
for event, selector of activationEvents
rootView.command event, selector, activateHandler
activatePackageMain: ->
if packageMain = @getPackageMain()
rootView?.activatePackage(@name, packageMain)
getPackageMain: ->
mainPath = require.resolve(@metadata.main) if @metadata.main
if mainPath
@@ -29,6 +52,8 @@ class AtomPackage extends Package
else if require.resolve(@path)
this
getActivationEvents: -> @metadata.activationEvents
loadMetadata: ->
if metadataPath = fs.resolveExtension(fs.join(@path, "package"), ['cson', 'json'])
@metadata = fs.readObject(metadataPath)

View File

@@ -1,12 +0,0 @@
DeferredAtomPackage = require 'deferred-atom-package'
module.exports =
class GoToLinePackage extends DeferredAtomPackage
loadEvents:
'editor:go-to-line': '.editor'
instanceClass: 'go-to-line/lib/go-to-line-view'
onLoadEvent: (event, instance) ->
instance.toggle(event.currentTargetView())

View File

@@ -6,7 +6,7 @@ Point = require 'point'
module.exports =
class GoToLineView extends View
@activate: (rootView) -> new GoToLineView(rootView)
@activate: (rootView) -> new GoToLineView(rootView).attach()
@content: ->
@div class: 'go-to-line overlay from-top mini', =>
@@ -14,6 +14,7 @@ class GoToLineView extends View
@div class: 'message', outlet: 'message'
initialize: (@rootView) ->
@rootView.command 'editor:go-to-line', '.editor', => @toggle()
@miniEditor.on 'focusout', => @detach()
@on 'core:confirm', => @confirm()
@on 'core:cancel', => @detach()

View File

@@ -0,0 +1,3 @@
'activationEvents':
'editor:go-to-line': '.editor'
'main': 'go-to-line/lib/go-to-line-view'

View File

@@ -6,8 +6,11 @@ describe 'GoToLine', ->
beforeEach ->
rootView = new RootView(require.resolve('fixtures/sample.js'))
rootView.enableKeymap()
goToLine = atom.loadPackage("go-to-line").getInstance()
atom.loadPackage("go-to-line")
editor = rootView.getActiveEditor()
editor.trigger 'editor:go-to-line'
goToLine = rootView.find('.go-to-line').view()
editor.trigger 'editor:go-to-line'
editor.setCursorBufferPosition([1,0])
afterEach ->