Sort candidate binding sets in a stable way in Keymap to preserve load order for a valid cascade

This commit is contained in:
Nathan Sobo
2012-06-20 22:45:14 -06:00
parent e1309f7c66
commit 68cb9992fc
2 changed files with 13 additions and 6 deletions

View File

@@ -12,7 +12,7 @@ class BindingSet
commandForEvent: null
parser: null
constructor: (@selector, mapOrFunction) ->
constructor: (@selector, mapOrFunction, @index) ->
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
@specificity = Specificity(@selector)
@commandsByKeystrokes = {}

View File

@@ -26,15 +26,15 @@ class Keymap
atom.open(path) if path
bindKeys: (selector, bindings) ->
@bindingSets.unshift(new BindingSet(selector, bindings))
index = @bindingSets.length
@bindingSets.unshift(new BindingSet(selector, bindings, index))
bindingsForElement: (element) ->
keystrokeMap = {}
currentNode = $(element)
while currentNode.length
bindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
bindingSets.sort (a, b) -> b.specificity - a.specificity
bindingSets = @bindingSetsForNode(currentNode)
_.defaults(keystrokeMap, set.commandsByKeystrokes) for set in bindingSets
currentNode = currentNode.parent()
@@ -46,8 +46,7 @@ class Keymap
@queuedKeystrokes = null
currentNode = $(event.target)
while currentNode.length
candidateBindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
candidateBindingSets.sort (a, b) -> b.specificity - a.specificity
candidateBindingSets = @bindingSetsForNode(currentNode)
for bindingSet in candidateBindingSets
command = bindingSet.commandForEvent(event)
if command
@@ -63,6 +62,14 @@ class Keymap
!isMultiKeystroke
bindingSetsForNode: (node) ->
bindingSets = @bindingSets.filter (set) -> node.is(set.selector)
bindingSets.sort (a, b) ->
if b.specificity == a.specificity
b.index - a.index
else
b.specificity - a.specificity
triggerCommandEvent: (keyEvent, commandName) ->
commandEvent = $.Event(commandName)
commandEvent.keyEvent = keyEvent