Get rid of BaseKeyparser.Type

This commit is contained in:
Florian Bruhin 2018-02-27 10:35:19 +01:00
parent 72e30cc12c
commit ba012c6ba8
5 changed files with 17 additions and 52 deletions

View File

@ -19,8 +19,6 @@
"""Base class for vim-like key sequence parser."""
import enum
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtGui import QKeySequence
@ -43,10 +41,6 @@ class BaseKeyParser(QObject):
definitive: Keychain matches exactly.
none: No more matches possible.
Types: type of a key binding.
chain: execute() was called via a chain-like key binding
special: execute() was called via a special key binding
do_log: Whether to log keypresses or not.
passthrough: Whether unbound keys should be passed through with this
handler.
@ -76,8 +70,6 @@ class BaseKeyParser(QObject):
do_log = True
passthrough = False
Type = enum.Enum('Type', ['chain', 'special'])
def __init__(self, win_id, parent=None, supports_count=None,
supports_chains=False):
super().__init__(parent)
@ -157,7 +149,7 @@ class BaseKeyParser(QObject):
self._sequence))
count = int(self._count) if self._count else None
self.clear_keystring()
self.execute(binding, self.Type.chain, count)
self.execute(binding, count)
elif match == QKeySequence.PartialMatch:
self._debug_log("No match for '{}' (added {})".format(
self._sequence, txt))
@ -248,13 +240,11 @@ class BaseKeyParser(QObject):
# "because keychains are not supported there."
# .format(key, modename))
def execute(self, cmdstr, keytype, count=None):
def execute(self, cmdstr, count=None):
"""Handle a completed keychain.
Args:
cmdstr: The command to execute as a string.
# FIXME do we still need this?
keytype: Type.chain or Type.special
count: The count if given.
"""
raise NotImplementedError

View File

@ -39,7 +39,7 @@ class CommandKeyParser(BaseKeyParser):
super().__init__(win_id, parent, supports_count, supports_chains)
self._commandrunner = runners.CommandRunner(win_id)
def execute(self, cmdstr, _keytype, count=None):
def execute(self, cmdstr, count=None):
try:
self._commandrunner.run(cmdstr, count)
except cmdexc.Error as e:

View File

@ -232,19 +232,6 @@ class HintKeyParser(keyparser.CommandKeyParser):
return match != QKeySequence.NoMatch
# FIXME why is this needed?
# def execute(self, cmdstr, keytype, count=None):
# """Handle a completed keychain."""
# if not isinstance(keytype, self.Type):
# raise TypeError("Type {} is no Type member!".format(keytype))
# if keytype == self.Type.chain:
# hintmanager = objreg.get('hintmanager', scope='tab',
# window=self._win_id, tab='current')
# hintmanager.handle_partial_key(cmdstr)
# else:
# # execute as command
# super().execute(cmdstr, keytype, count)
def update_bindings(self, strings, preserve_filter=False):
"""Update bindings when the hint strings changed.

View File

@ -181,15 +181,13 @@ class TestSpecialKeys:
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
keyparser.handle(fake_keyevent_factory(Qt.Key_X, modifier))
keyparser.execute.assert_called_once_with(
'message-info ctrla', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info ctrla', None)
def test_valid_key_count(self, fake_keyevent_factory, keyparser):
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
keyparser.handle(fake_keyevent_factory(Qt.Key_5, text='5'))
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier, text='A'))
keyparser.execute.assert_called_once_with(
'message-info ctrla', keyparser.Type.chain, 5)
keyparser.execute.assert_called_once_with('message-info ctrla', 5)
def test_invalid_key(self, fake_keyevent_factory, keyparser):
keyparser.handle(fake_keyevent_factory(
@ -212,8 +210,7 @@ class TestSpecialKeys:
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
keyparser.handle(fake_keyevent_factory(Qt.Key_B, modifier))
keyparser.execute.assert_called_once_with(
'message-info ctrla', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info ctrla', None)
def test_binding_and_mapping(self, config_stub, fake_keyevent_factory,
keyparser):
@ -221,8 +218,7 @@ class TestSpecialKeys:
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
keyparser.execute.assert_called_once_with(
'message-info ctrla', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info ctrla', None)
class TestKeyChain:
@ -240,8 +236,7 @@ class TestKeyChain:
modifier = Qt.ControlModifier
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
keyparser.handle(fake_keyevent_factory(Qt.Key_X, modifier))
keyparser.execute.assert_called_once_with(
'message-info ctrla', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info ctrla', None)
assert not keyparser._sequence
def test_invalid_special_key(self, fake_keyevent_factory, keyparser):
@ -255,14 +250,12 @@ class TestKeyChain:
handle_text((Qt.Key_X, 'x'),
# Then start the real chain
(Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
keyparser.execute.assert_called_with(
'message-info ba', keyparser.Type.chain, None)
keyparser.execute.assert_called_with('message-info ba', None)
assert not keyparser._sequence
def test_0_press(self, handle_text, keyparser):
handle_text((Qt.Key_0, '0'))
keyparser.execute.assert_called_once_with(
'message-info 0', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info 0', None)
assert not keyparser._sequence
def test_ambiguous_keychain(self, handle_text, keyparser):
@ -276,8 +269,7 @@ class TestKeyChain:
def test_mapping(self, config_stub, handle_text, keyparser):
handle_text((Qt.Key_X, 'x'))
keyparser.execute.assert_called_once_with(
'message-info a', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info a', None)
def test_binding_and_mapping(self, config_stub, handle_text, keyparser):
"""with a conflicting binding/mapping, the binding should win."""
@ -296,22 +288,20 @@ class TestCount:
def test_no_count(self, handle_text, keyparser):
"""Test with no count added."""
handle_text((Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
keyparser.execute.assert_called_once_with(
'message-info ba', keyparser.Type.chain, None)
keyparser.execute.assert_called_once_with('message-info ba', None)
assert not keyparser._sequence
def test_count_0(self, handle_text, keyparser):
handle_text((Qt.Key_0, '0'), (Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
calls = [mock.call('message-info 0', keyparser.Type.chain, None),
mock.call('message-info ba', keyparser.Type.chain, None)]
calls = [mock.call('message-info 0', None),
mock.call('message-info ba', None)]
keyparser.execute.assert_has_calls(calls)
assert not keyparser._sequence
def test_count_42(self, handle_text, keyparser):
handle_text((Qt.Key_4, '4'), (Qt.Key_2, '2'), (Qt.Key_B, 'b'),
(Qt.Key_A, 'a'))
keyparser.execute.assert_called_once_with(
'message-info ba', keyparser.Type.chain, 42)
keyparser.execute.assert_called_once_with('message-info ba', 42)
assert not keyparser._sequence
def test_count_42_invalid(self, handle_text, keyparser):
@ -323,8 +313,7 @@ class TestCount:
# Valid call with ccc gets the correct count
handle_text((Qt.Key_2, '2'), (Qt.Key_3, '3'), (Qt.Key_C, 'c'),
(Qt.Key_C, 'c'), (Qt.Key_C, 'c'))
keyparser.execute.assert_called_once_with(
'message-info ccc', keyparser.Type.chain, 23)
keyparser.execute.assert_called_once_with('message-info ccc', 23)
assert not keyparser._sequence

View File

@ -56,8 +56,7 @@ class TestsNormalKeyParser:
# Then start the real chain
keyparser.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
keyparser.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
keyparser.execute.assert_called_with(
'message-info ba', keyparser.Type.chain, None)
keyparser.execute.assert_called_with('message-info ba', None)
assert not keyparser._sequence
def test_partial_keychain_timeout(self, keyparser, config_stub,