Add a Key config type
Make sure any key we get from the config is normalized).
This commit is contained in:
parent
0d062b28bf
commit
383968d948
@ -1811,8 +1811,8 @@ bindings.key_mappings:
|
|||||||
<Ctrl-[>: <Escape>
|
<Ctrl-[>: <Escape>
|
||||||
type:
|
type:
|
||||||
name: Dict
|
name: Dict
|
||||||
keytype: String
|
keytype: Key
|
||||||
valtype: String
|
valtype: Key
|
||||||
desc: >-
|
desc: >-
|
||||||
This setting can be used to map keys to other keys.
|
This setting can be used to map keys to other keys.
|
||||||
|
|
||||||
@ -2071,7 +2071,7 @@ bindings.commands:
|
|||||||
'prompt', 'caret', 'register']
|
'prompt', 'caret', 'register']
|
||||||
valtype:
|
valtype:
|
||||||
name: Dict
|
name: Dict
|
||||||
keytype: String # key
|
keytype: Key
|
||||||
valtype: Command
|
valtype: Command
|
||||||
desc: >-
|
desc: >-
|
||||||
Keybindings mapping keys to commands in different modes.
|
Keybindings mapping keys to commands in different modes.
|
||||||
|
@ -1016,7 +1016,7 @@ class Dict(BaseType):
|
|||||||
super().__init__(none_ok)
|
super().__init__(none_ok)
|
||||||
# If the keytype is not a string, we'll get problems with showing it as
|
# If the keytype is not a string, we'll get problems with showing it as
|
||||||
# json in to_str() as json converts keys to strings.
|
# json in to_str() as json converts keys to strings.
|
||||||
assert isinstance(keytype, String), keytype
|
assert isinstance(keytype, (String, Key)), keytype
|
||||||
self.keytype = keytype
|
self.keytype = keytype
|
||||||
self.valtype = valtype
|
self.valtype = valtype
|
||||||
self.fixed_keys = fixed_keys
|
self.fixed_keys = fixed_keys
|
||||||
@ -1458,3 +1458,16 @@ class TimestampTemplate(BaseType):
|
|||||||
value, "Invalid format string: {}".format(error))
|
value, "Invalid format string: {}".format(error))
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class Key(BaseType):
|
||||||
|
|
||||||
|
"""A name of a key."""
|
||||||
|
|
||||||
|
def to_py(self, value):
|
||||||
|
self._basic_py_validation(value, str)
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
if utils.is_special_key(value):
|
||||||
|
value = '<{}>'.format(utils.normalize_keystr(value[1:-1]))
|
||||||
|
return value
|
||||||
|
@ -347,8 +347,7 @@ class BaseKeyParser(QObject):
|
|||||||
def _parse_key_command(self, modename, key, cmd):
|
def _parse_key_command(self, modename, key, cmd):
|
||||||
"""Parse the keys and their command and store them in the object."""
|
"""Parse the keys and their command and store them in the object."""
|
||||||
if utils.is_special_key(key):
|
if utils.is_special_key(key):
|
||||||
keystr = utils.normalize_keystr(key[1:-1])
|
self.special_bindings[key[1:-1]] = cmd
|
||||||
self.special_bindings[keystr] = cmd
|
|
||||||
elif self._supports_chains:
|
elif self._supports_chains:
|
||||||
self.bindings[key] = cmd
|
self.bindings[key] = cmd
|
||||||
elif self._warn_on_keychains:
|
elif self._warn_on_keychains:
|
||||||
|
@ -1801,6 +1801,20 @@ class TestTimestampTemplate:
|
|||||||
klass().to_py('%')
|
klass().to_py('%')
|
||||||
|
|
||||||
|
|
||||||
|
class TestKey:
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def klass(self):
|
||||||
|
return configtypes.Key
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('val, expected', [
|
||||||
|
('gC', 'gC'),
|
||||||
|
('<Control-x>', '<ctrl+x>')
|
||||||
|
])
|
||||||
|
def test_to_py_valid(self, klass, val, expected):
|
||||||
|
assert klass().to_py(val) == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('first, second, equal', [
|
@pytest.mark.parametrize('first, second, equal', [
|
||||||
(re.compile('foo'), RegexEq('foo'), True),
|
(re.compile('foo'), RegexEq('foo'), True),
|
||||||
(RegexEq('bar'), re.compile('bar'), True),
|
(RegexEq('bar'), re.compile('bar'), True),
|
||||||
|
Loading…
Reference in New Issue
Block a user