Files
atom/src/app/binding-set.coffee
Nathan Sobo 2c211ba504 Remove ability to associate a binding set with a function
This was cool, but it's really hard to optimize the keymap with this feature because we never know if a keystroke will match against a binding set with a function, which will force us to always consider this binding set against every key event.
2012-11-02 13:39:21 -06:00

49 lines
1.6 KiB
CoffeeScript

$ = require 'jquery'
_ = require 'underscore'
fs = require 'fs'
Specificity = require 'specificity'
PEG = require 'pegjs'
module.exports =
class BindingSet
selector: null
commandsByKeystrokes: null
commandForEvent: null
parser: null
constructor: (@selector, commandsByKeystrokes, @index) ->
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
@specificity = Specificity(@selector)
@commandsByKeystrokes = @normalizeCommandsByKeystrokes(commandsByKeystrokes)
commandForEvent: (event) ->
for keystrokes, command of @commandsByKeystrokes
return command if event.keystrokes == keystrokes
null
matchesKeystrokePrefix: (event) ->
eventKeystrokes = event.keystrokes.split(' ')
for keystrokes, command of @commandsByKeystrokes
bindingKeystrokes = keystrokes.split(' ')
continue unless eventKeystrokes.length < bindingKeystrokes.length
return true if _.isEqual(eventKeystrokes, bindingKeystrokes[0...eventKeystrokes.length])
false
normalizeCommandsByKeystrokes: (commandsByKeystrokes) ->
normalizedCommandsByKeystrokes = {}
for keystrokes, command of commandsByKeystrokes
normalizedCommandsByKeystrokes[@normalizeKeystrokes(keystrokes)] = command
normalizedCommandsByKeystrokes
normalizeKeystrokes: (keystrokes) ->
normalizedKeystrokes = keystrokes.split(/\s+/).map (keystroke) =>
@normalizeKeystroke(keystroke)
normalizedKeystrokes.join(' ')
normalizeKeystroke: (keystroke) ->
keys = @parser.parse(keystroke)
modifiers = keys[0...-1]
modifiers.sort()
[modifiers..., _.last(keys)].join('-')