Modernize command-panel package with package.cson

This commit is contained in:
Corey Johnson & Kevin Sawicki
2013-02-08 11:52:52 -08:00
parent 9043f8290f
commit 2d80d27ca7
27 changed files with 110 additions and 138 deletions

View File

@@ -13,12 +13,13 @@ class AtomPackage extends Package
super
@keymapsDirPath = fs.join(@path, 'keymaps')
load: ->
load: ({activateImmediately}={}) ->
try
@loadMetadata()
@loadKeymaps()
@loadStylesheets() if @autoloadStylesheets
if activationEvents = @getActivationEvents()
activationEvents = @getActivationEvents()
if activationEvents and not activateImmediately
@subscribeToActivationEvents(activationEvents)
else
@activatePackageMain()

View File

@@ -14,10 +14,10 @@ _.extend atom,
pendingBrowserProcessCallbacks: {}
loadedPackages: []
loadPackage: (name) ->
loadPackage: (name, options) ->
packagePath = _.find @getPackagePaths(), (packagePath) -> fs.base(packagePath) == name
pack = Package.build(packagePath)
pack?.load()
pack?.load(options)
loadPackages: ->
textMatePackages = []

View File

@@ -1,7 +1,7 @@
$ = require 'jquery'
module.exports =
eventLog: null
eventLog: {}
commandLoggerView: null
originalTrigger: null
@@ -29,7 +29,7 @@ module.exports =
deactivate: ->
$.fn.trigger = @originalTrigger if @originalTrigger?
@commandLoggerView = null
@eventLog = null
@eventLog = {}
serialize: ->
{@eventLog}

View File

@@ -7,6 +7,7 @@ describe "CommandLogger", ->
beforeEach ->
new RootView(require.resolve('fixtures/sample.js'))
commandLogger = atom.loadPackage('command-logger').packageMain
commandLogger.eventLog = {}
editor = rootView.getActiveEditor()
afterEach ->

View File

@@ -1,33 +0,0 @@
DeferredAtomPackage = require 'deferred-atom-package'
module.exports =
class CommandPanel extends DeferredAtomPackage
loadEvents: [
'command-panel:toggle'
'command-panel:toggle-preview'
'command-panel:find-in-file'
'command-panel:find-in-project'
'command-panel:repeat-relative-address'
'command-panel:repeat-relative-address-in-reverse'
'command-panel:set-selection-as-regex-address'
]
instanceClass: 'command-panel/src/command-panel-view'
onLoadEvent: (event, instance) ->
switch event.type
when 'command-panel:toggle'
instance.toggle()
when 'command-panel:toggle-preview'
instance.togglePreview()
when 'command-panel:find-in-file'
instance.attach("/")
when 'command-panel:find-in-project'
instance.attach("Xx/")
when 'command-panel:repeat-relative-address'
instance.repeatRelativeAddress()
when 'command-panel:repeat-relative-address-in-reverse'
instance.repeatRelativeAddressInReverse()
when 'command-panel:set-selection-as-regex-address'
instance.setSelectionAsLastRelativeAddress()

View File

@@ -6,7 +6,7 @@ class CommandInterpreter
constructor: (@project) ->
eval: (string, activeEditSession) ->
@parser ?= PEG.buildParser(fs.read(require.resolve 'command-panel/commands.pegjs'))
@parser ?= PEG.buildParser(fs.read(require.resolve 'command-panel/lib/commands.pegjs'))
compositeCommand = @parser.parse(string)
@lastRelativeAddress = compositeCommand if compositeCommand.isRelativeAddress()
compositeCommand.execute(@project, activeEditSession)

View File

@@ -1,28 +1,15 @@
{View, $$, $$$} = require 'space-pen'
CommandInterpreter = require 'command-panel/src/command-interpreter'
RegexAddress = require 'command-panel/src/commands/regex-address'
CompositeCommand = require 'command-panel/src/commands/composite-command'
PreviewList = require 'command-panel/src/preview-list'
CommandInterpreter = require './command-interpreter'
RegexAddress = require './commands/regex-address'
CompositeCommand = require './commands/composite-command'
PreviewList = require './preview-list'
Editor = require 'editor'
{SyntaxError} = require('pegjs').parser
_ = require 'underscore'
module.exports =
class CommandPanelView extends View
@activate: (rootView, state) ->
if state?
@instance = @deserialize(state, rootView)
else
@instance = new CommandPanelView(rootView)
@deserialize: (state, rootView) ->
commandPanel = new CommandPanelView(rootView, state.history)
commandPanel.attach(state.text, focus: false) if state.visible
commandPanel.miniEditor.focus() if state.miniEditorFocused
commandPanel
@content: (rootView) ->
@content: ->
@div class: 'command-panel tool-panel', =>
@div outlet: 'previewCount', class: 'preview-count'
@subview 'previewList', new PreviewList(rootView)
@@ -36,38 +23,42 @@ class CommandPanelView extends View
historyIndex: 0
maxSerializedHistorySize: 100
initialize: (@rootView, @history) ->
@commandInterpreter = new CommandInterpreter(@rootView.project)
initialize: (state={}) ->
@commandInterpreter = new CommandInterpreter(rootView.project)
@history ?= []
@historyIndex = @history.length
@command 'tool-panel:unfocus', => @rootView.focus()
@command 'tool-panel:unfocus', => rootView.focus()
@command 'core:close', => @detach(); false
@command 'core:confirm', => @execute()
@command 'core:move-up', => @navigateBackwardInHistory()
@command 'core:move-down', => @navigateForwardInHistory()
rootView.command 'command-panel:toggle', => @toggle()
rootView.command 'command-panel:toggle-preview', => @togglePreview()
rootView.command 'command-panel:find-in-file', => @attach('/')
rootView.command 'command-panel:find-in-project', => @attach('Xx/')
rootView.command 'command-panel:repeat-relative-address', => @repeatRelativeAddress()
rootView.command 'command-panel:repeat-relative-address-in-reverse', => @repeatRelativeAddressInReverse()
rootView.command 'command-panel:set-selection-as-regex-address', => @setSelectionAsLastRelativeAddress()
@previewList.hide()
@previewCount.hide()
@errorMessages.hide()
@prompt.iconSize(@miniEditor.getFontSize())
@history = state.history ? []
@historyIndex = @history.length
serialize: ->
text: @miniEditor.getText()
visible: @hasParent()
miniEditorFocused: @miniEditor.isFocused
history: @history[-@maxSerializedHistorySize..]
deactivate: -> @destroy()
destroy: ->
@previewList.destroy()
toggle: ->
if @miniEditor.isFocused
@detach()
@rootView.focus()
rootView.focus()
else
@attach() unless @hasParent()
@miniEditor.focus()
@@ -77,7 +68,7 @@ class CommandPanelView extends View
@previewList.hide()
@previewCount.hide()
@detach()
@rootView.focus()
rootView.focus()
else
@attach() unless @hasParent()
if @previewList.hasOperations()
@@ -90,13 +81,13 @@ class CommandPanelView extends View
@errorMessages.hide()
focus = options.focus ? true
@rootView.vertical.append(this)
rootView.vertical.append(this)
@miniEditor.focus() if focus
@miniEditor.setText(text)
@miniEditor.setCursorBufferPosition([0, Infinity])
detach: ->
@rootView.focus()
rootView.focus()
@previewList.hide()
@previewCount.hide()
super
@@ -108,7 +99,7 @@ class CommandPanelView extends View
@errorMessages.empty()
try
@commandInterpreter.eval(command, @rootView.getActiveEditSession()).done ({operationsToPreview, errorMessages}) =>
@commandInterpreter.eval(command, rootView.getActiveEditSession()).done ({operationsToPreview, errorMessages}) =>
@history.push(command)
@historyIndex = @history.length
@@ -141,12 +132,12 @@ class CommandPanelView extends View
@miniEditor.setText(@history[@historyIndex] or '')
repeatRelativeAddress: ->
@commandInterpreter.repeatRelativeAddress(@rootView.getActiveEditSession())
@commandInterpreter.repeatRelativeAddress(rootView.getActiveEditSession())
repeatRelativeAddressInReverse: ->
@commandInterpreter.repeatRelativeAddressInReverse(@rootView.getActiveEditSession())
@commandInterpreter.repeatRelativeAddressInReverse(rootView.getActiveEditSession())
setSelectionAsLastRelativeAddress: ->
selection = @rootView.getActiveEditor().getSelectedText()
selection = rootView.getActiveEditor().getSelectedText()
regex = _.escapeRegExp(selection)
@commandInterpreter.lastRelativeAddress = new CompositeCommand([new RegexAddress(regex)])

View File

@@ -0,0 +1,17 @@
CommandPanelView = require './command-panel-view'
module.exports =
commandPanelView: null
activate: (rootView, @state) ->
@commandPanelView = new CommandPanelView(@state)
deactivate: ->
@commandPanelView?.destroy()
@commandPanelView = null
serialize: ->
if @commandPanelView?
@commandPanelView.serialize()
else
@state

View File

@@ -1,15 +1,15 @@
{
var CompositeCommand = require('command-panel/src/commands/composite-command')
var Substitution = require('command-panel/src/commands/substitution');
var ZeroAddress = require('command-panel/src/commands/zero-address');
var EofAddress = require('command-panel/src/commands/eof-address');
var LineAddress = require('command-panel/src/commands/line-address');
var AddressRange = require('command-panel/src/commands/address-range');
var DefaultAddressRange = require('command-panel/src/commands/default-address-range');
var CurrentSelectionAddress = require('command-panel/src/commands/current-selection-address')
var RegexAddress = require('command-panel/src/commands/regex-address')
var SelectAllMatches = require('command-panel/src/commands/select-all-matches')
var SelectAllMatchesInProject = require('command-panel/src/commands/select-all-matches-in-project')
var CompositeCommand = require('command-panel/lib/commands/composite-command')
var Substitution = require('command-panel/lib/commands/substitution');
var ZeroAddress = require('command-panel/lib/commands/zero-address');
var EofAddress = require('command-panel/lib/commands/eof-address');
var LineAddress = require('command-panel/lib/commands/line-address');
var AddressRange = require('command-panel/lib/commands/address-range');
var DefaultAddressRange = require('command-panel/lib/commands/default-address-range');
var CurrentSelectionAddress = require('command-panel/lib/commands/current-selection-address')
var RegexAddress = require('command-panel/lib/commands/regex-address')
var SelectAllMatches = require('command-panel/lib/commands/select-all-matches')
var SelectAllMatchesInProject = require('command-panel/lib/commands/select-all-matches-in-project')
}
start = _ commands:( selectAllMatchesInProject / textCommand ) {

View File

@@ -1,4 +1,4 @@
Address = require 'command-panel/src/commands/address'
Address = require 'command-panel/lib/commands/address'
Range = require 'range'
module.exports =

View File

@@ -1,5 +1,5 @@
Command = require 'command-panel/src/commands/command'
Operation = require 'command-panel/src/operation'
Command = require './command'
Operation = require 'command-panel/lib/operation'
$ = require 'jquery'
module.exports =

View File

@@ -1,5 +1,3 @@
_ = require 'underscore'
module.exports =
class Command
isAddress: -> false

View File

@@ -1,5 +1,4 @@
Address = require 'command-panel/src/commands/address'
Range = require 'range'
Address = require './address'
module.exports =
class CurrentSelectionAddress extends Address

View File

@@ -1,5 +1,4 @@
Address = require 'command-panel/src/commands/address'
Range = require 'range'
Address = require './address'
module.exports =
class DefaultAddressRange extends Address

View File

@@ -1,4 +1,4 @@
Address = require 'command-panel/src/commands/address'
Address = require './address'
Range = require 'range'
module.exports =

View File

@@ -1,4 +1,4 @@
Address = require 'command-panel/src/commands/address'
Address = require './address'
Range = require 'range'
module.exports =

View File

@@ -1,4 +1,4 @@
Address = require 'command-panel/src/commands/address'
Address = require './address'
Range = require 'range'
module.exports =

View File

@@ -1,5 +1,5 @@
Command = require 'command-panel/src/commands/command'
Operation = require 'command-panel/src/operation'
Command = require './command'
Operation = require 'command-panel/lib/operation'
$ = require 'jquery'
module.exports =

View File

@@ -1,5 +1,5 @@
Command = require 'command-panel/src/commands/command'
Operation = require 'command-panel/src/operation'
Command = require './command'
Operation = require 'command-panel/lib/operation'
$ = require 'jquery'
module.exports =

View File

@@ -1,5 +1,5 @@
Command = require 'command-panel/src/commands/command'
Operation = require 'command-panel/src/operation'
Command = require './command'
Operation = require 'command-panel/lib/operation'
$ = require 'jquery'
module.exports =

View File

@@ -1,4 +1,4 @@
Address = require 'command-panel/src/commands/address'
Address = require './address'
Range = require 'range'
module.exports =

View File

@@ -1,5 +1,3 @@
{$$$} = require 'space-pen'
module.exports =
class Operation
constructor: ({@project, @buffer, bufferRange, @newText, @preserveSelection, @errorMessage}) ->

View File

@@ -0,0 +1,10 @@
'main': 'lib/command-panel'
'activationEvents': [
'command-panel:toggle'
'command-panel:toggle-preview'
'command-panel:find-in-file'
'command-panel:find-in-project'
'command-panel:repeat-relative-address'
'command-panel:repeat-relative-address-in-reverse'
'command-panel:set-selection-as-regex-address'
]

View File

@@ -1,4 +1,4 @@
CommandInterpreter = require 'command-panel/src/command-interpreter'
CommandInterpreter = require 'command-panel/lib/command-interpreter'
Project = require 'project'
Buffer = require 'buffer'
EditSession = require 'edit-session'

View File

@@ -1,19 +1,19 @@
RootView = require 'root-view'
CommandPanelView = require 'command-panel/src/command-panel-view'
CommandPanelView = require 'command-panel/lib/command-panel-view'
_ = require 'underscore'
describe "CommandPanel", ->
[rootView, editor, buffer, commandPanel, project, CommandPanel] = []
[editor, buffer, commandPanel, project, CommandPanel] = []
beforeEach ->
rootView = new RootView
new RootView
rootView.open(require.resolve 'fixtures/sample.js')
rootView.enableKeymap()
project = rootView.project
editor = rootView.getActiveEditor()
buffer = editor.activeEditSession.buffer
CommandPanel = atom.loadPackage('command-panel')
commandPanel = CommandPanel.getInstance()
commandPanelMain = atom.loadPackage('command-panel', activateImmediately: true).packageMain
commandPanel = commandPanelMain.commandPanelView
commandPanel.history = []
commandPanel.historyIndex = 0
@@ -21,7 +21,7 @@ describe "CommandPanel", ->
rootView.deactivate()
describe "serialization", ->
it "preserves the command panel's mini-editor text, visibility, focus, and history across reloads", ->
it "preserves the command panel's history across reloads", ->
rootView.attachToDom()
rootView.trigger 'command-panel:toggle'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
@@ -31,29 +31,20 @@ describe "CommandPanel", ->
expect(commandPanel.historyIndex).toBe(1)
rootView.trigger 'command-panel:toggle'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
commandPanel.miniEditor.insertText 'abc'
rootView2 = RootView.deserialize(rootView.serialize())
rootView.deactivate()
rootView2.attachToDom()
commandPanel = rootView2.activatePackage('command-panel', CommandPanel).getInstance()
expect(rootView2.find('.command-panel')).toExist()
expect(commandPanel.miniEditor.getText()).toBe 'abc'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
rootViewState = rootView.serialize()
rootView.deactivate()
RootView.deserialize(rootViewState).attachToDom()
atom.loadPackage('command-panel')
expect(rootView.find('.command-panel')).not.toExist()
rootView.trigger 'command-panel:toggle'
expect(rootView.find('.command-panel')).toExist()
commandPanel = rootView.find('.command-panel').view()
expect(commandPanel.history.length).toBe(1)
expect(commandPanel.history[0]).toBe('/.')
expect(commandPanel.historyIndex).toBe(1)
rootView2.focus()
expect(commandPanel.miniEditor.isFocused).toBeFalsy()
rootView3 = RootView.deserialize(rootView2.serialize())
rootView2.deactivate()
rootView3.attachToDom()
commandPanel = rootView3.activatePackage('command-panel', CommandPanel).getInstance()
expect(commandPanel.miniEditor.isFocused).toBeFalsy()
rootView3.deactivate()
it "only retains the configured max serialized history size", ->
rootView.attachToDom()
@@ -67,18 +58,18 @@ describe "CommandPanel", ->
expect(commandPanel.history[2]).toBe('/test3')
expect(commandPanel.historyIndex).toBe(3)
rootView2 = RootView.deserialize(rootView.serialize())
rootViewState = rootView.serialize()
rootView.deactivate()
rootView2.attachToDom()
RootView.deserialize(rootViewState).attachToDom()
atom.loadPackage('command-panel')
rootView.trigger 'command-panel:toggle'
commandPanel = rootView2.activatePackage('command-panel', CommandPanel).getInstance()
commandPanel = rootView.find('.command-panel').view()
expect(commandPanel.history.length).toBe(2)
expect(commandPanel.history[0]).toBe('/test2')
expect(commandPanel.history[1]).toBe('/test3')
expect(commandPanel.historyIndex).toBe(2)
rootView2.deactivate()
describe "when core:close is triggered on the command panel", ->
it "detaches the command panel, focuses the RootView and does not bubble the core:close event", ->
commandPanel.attach()