From 16940db8342a083cc56a6492f1b34a91a5513466 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 26 Feb 2018 11:10:05 +0100 Subject: [PATCH] Refactor KeySequence initialization --- qutebrowser/keyinput/keyutils.py | 54 ++++++++++++----------------- qutebrowser/keyinput/modeparsers.py | 4 +-- qutebrowser/misc/keyhintwidget.py | 2 +- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index 626808f4a..bd12ccb2c 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -264,16 +264,13 @@ class KeySequence: _MAX_LEN = 4 - def __init__(self, strings=None): + def __init__(self, *keys): self._sequences = [] - if strings is None: - strings = [] - - for sub in utils.chunk(strings, 4): - # Catch old API usage FIXME - assert all(isinstance(s, str) for s in sub) - sequence = QKeySequence(', '.join(sub)) + for sub in utils.chunk(keys, self._MAX_LEN): + sequence = QKeySequence(*sub) self._sequences.append(sequence) + if keys: + assert len(self) > 0 self._validate() def __str__(self): @@ -287,7 +284,7 @@ class KeySequence: modifier_mask = int(Qt.ShiftModifier | Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier | Qt.KeypadModifier | Qt.GroupSwitchModifier) - for key in itertools.chain.from_iterable(self._sequences): + for key in self._iter_keys(): yield KeyInfo( key=int(key) & ~modifier_mask, modifiers=Qt.KeyboardModifiers(int(key) & modifier_mask)) @@ -314,6 +311,13 @@ class KeySequence: def __len__(self): return sum(len(seq) for seq in self._sequences) + def __getitem__(self, item): + keys = list(self._iter_keys()) + return self.__class__(*keys[item]) + + def _iter_keys(self): + return itertools.chain.from_iterable(self._sequences) + def _validate(self): for info in self: assert info.key != Qt.Key_unknown @@ -347,39 +351,25 @@ class KeySequence: FIXME: create test cases! """ # pylint: disable=protected-access - new = self.__class__() - new._sequences = self._sequences[:] - modifiers = ev.modifiers() if (modifiers == Qt.ShiftModifier and ev.text() and unicodedata.category(ev.text()) != 'Lu'): modifiers = Qt.KeyboardModifiers() - if new._sequences and len(new._sequences[-1]) < self._MAX_LEN: - new._sequences[-1] = QKeySequence(*new._sequences[-1], - ev.key() | int(modifiers)) - else: - new._sequences.append(QKeySequence(ev.key() | int(modifiers))) + keys = list(self._iter_keys()) + keys.append(ev.key() | int(modifiers)) - new._validate() - return new - - def remove_last(self): - """Create a new KeySequence with the last key removed.""" - new = self.__class__() - new._sequences = self._sequeces[:] - if len(new._sequences[-1]) == 1: - del new._sequences[-1] - else: - new._sequences[-1] = QKeySequence(*new._sequences[-1][:-1]) - new._validate() - return new + return self.__class__(*keys) @classmethod def parse(cls, keystr): """Parse a keystring like or xyz and return a KeySequence.""" - parts = list(_parse_keystring(keystr)) - new = cls(parts) + new = cls() + strings = list(_parse_keystring(keystr)) + for sub in utils.chunk(strings, cls._MAX_LEN): + sequence = QKeySequence(', '.join(sub)) + new._sequences.append(sequence) assert len(new) > 0 + new._validate() return new diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index d22b27d22..7237a34de 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -183,7 +183,7 @@ class HintKeyParser(keyparser.CommandKeyParser): hintmanager.filter_hints(self._filtertext) return True elif self._last_press == LastPress.keystring and self._sequence: - self._sequence = self._sequence.remove_last() + self._sequence = self._sequence[:-1] self.keystring_updated.emit(str(self._sequence)) if not self._sequence and self._filtertext: # Switch back to hint filtering mode (this can happen only @@ -254,7 +254,7 @@ class HintKeyParser(keyparser.CommandKeyParser): `self._filtertext`. """ self._read_config() - self.bindings.update({keyutils.KeySequence(s): + self.bindings.update({keyutils.KeySequence.parse(s): 'follow-hint ' + s for s in strings}) if not preserve_filter: self._filtertext = '' diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 0aa52116e..967bdd541 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -107,7 +107,7 @@ class KeyHintView(QLabel): bindings_dict = config.key_instance.get_bindings_for(modename) bindings = [(k, v) for (k, v) in sorted(bindings_dict.items()) - if k.matches(keyutils.KeySequence(prefix)) and # FIXME + if k.matches(keyutils.KeySequence.parse(prefix)) and # FIXME not blacklisted(k) and (takes_count(v) or not countstr)]