From 68cb9992fc8640e2d135faa7721a0f334c4e8c75 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 20 Jun 2012 22:45:14 -0600 Subject: [PATCH] Sort candidate binding sets in a stable way in Keymap to preserve load order for a valid cascade --- src/app/binding-set.coffee | 2 +- src/app/keymap.coffee | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/app/binding-set.coffee b/src/app/binding-set.coffee index aaf08fda9..3882a7236 100644 --- a/src/app/binding-set.coffee +++ b/src/app/binding-set.coffee @@ -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 = {} diff --git a/src/app/keymap.coffee b/src/app/keymap.coffee index 6a8652400..1faa80063 100644 --- a/src/app/keymap.coffee +++ b/src/app/keymap.coffee @@ -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