mirror of
https://github.com/atom/atom.git
synced 2026-02-09 22:24:59 -05:00
Can match key patterns with the '-' character
Add a parser to parse keystroke patterns instead of splitting on '-' with a regex
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
$ = require 'jquery'
|
||||
_ = require 'underscore'
|
||||
Specificity = require 'specificity'
|
||||
fs = require 'fs'
|
||||
|
||||
PEG = require 'pegjs'
|
||||
|
||||
module.exports =
|
||||
class BindingSet
|
||||
selector: null
|
||||
commandForEvent: null
|
||||
keystrokePatternParser: null
|
||||
|
||||
constructor: (@selector, mapOrFunction) ->
|
||||
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
|
||||
@specificity = Specificity(@selector)
|
||||
@commandForEvent = @buildEventHandler(mapOrFunction)
|
||||
|
||||
@@ -18,13 +23,9 @@ class BindingSet
|
||||
mapOrFunction = @normalizeKeystrokePatterns(mapOrFunction)
|
||||
(event) =>
|
||||
for pattern, command of mapOrFunction
|
||||
return command if @eventMatchesPattern(event, pattern)
|
||||
return command if event.keystroke == pattern
|
||||
null
|
||||
|
||||
eventMatchesPattern: (event, pattern) ->
|
||||
pattern = pattern.replace(/^<|>$/g, '')
|
||||
event.keystroke == pattern
|
||||
|
||||
normalizeKeystrokePatterns: (map) ->
|
||||
normalizedMap = {}
|
||||
for pattern, event of map
|
||||
@@ -32,7 +33,7 @@ class BindingSet
|
||||
normalizedMap
|
||||
|
||||
normalizeKeystrokePattern: (pattern) ->
|
||||
keys = pattern.split('-')
|
||||
keys = @parser.parse(pattern)
|
||||
modifiers = keys[0...-1]
|
||||
modifiers.sort()
|
||||
[modifiers..., _.last(keys)].join('-')
|
||||
|
||||
3
src/app/keystroke-pattern.pegjs
Normal file
3
src/app/keystroke-pattern.pegjs
Normal file
@@ -0,0 +1,3 @@
|
||||
keystrokePattern = key:key additionalKeys:additionalKey* { return [key].concat(additionalKeys); }
|
||||
additionalKey = '-' key:key { return key; }
|
||||
key = '-' / chars:[^-]+ { return chars.join('') }
|
||||
Reference in New Issue
Block a user