Implement utils.is_special_key.
The check `key.startswith('<') and key.endswith('>') is repeated many times in code to check for a special key. Replace all these with a call to the same function.
This commit is contained in:
parent
5992b81850
commit
b1440a1804
@ -24,7 +24,7 @@ from PyQt5.QtCore import Qt, QTimer, pyqtSlot
|
|||||||
|
|
||||||
from qutebrowser.browser import webview
|
from qutebrowser.browser import webview
|
||||||
from qutebrowser.config import config, configdata
|
from qutebrowser.config import config, configdata
|
||||||
from qutebrowser.utils import objreg, log, qtutils
|
from qutebrowser.utils import objreg, log, qtutils, utils
|
||||||
from qutebrowser.commands import cmdutils
|
from qutebrowser.commands import cmdutils
|
||||||
from qutebrowser.completion.models import base
|
from qutebrowser.completion.models import base
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ class CommandCompletionModel(base.BaseCompletionModel):
|
|||||||
cmd_to_keys = defaultdict(list)
|
cmd_to_keys = defaultdict(list)
|
||||||
for key, cmd in keyconf.get_bindings_for('normal').items():
|
for key, cmd in keyconf.get_bindings_for('normal').items():
|
||||||
# put special bindings last
|
# put special bindings last
|
||||||
if key.startswith('<') and key.endswith('>'):
|
if utils.is_special_key(key):
|
||||||
cmd_to_keys[cmd].append(key)
|
cmd_to_keys[cmd].append(key)
|
||||||
else:
|
else:
|
||||||
cmd_to_keys[cmd].insert(0, key)
|
cmd_to_keys[cmd].insert(0, key)
|
||||||
|
@ -335,7 +335,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 key.startswith('<') and key.endswith('>'):
|
if utils.is_special_key(key):
|
||||||
keystr = utils.normalize_keystr(key[1:-1])
|
keystr = utils.normalize_keystr(key[1:-1])
|
||||||
self.special_bindings[keystr] = cmd
|
self.special_bindings[keystr] = cmd
|
||||||
elif self._supports_chains:
|
elif self._supports_chains:
|
||||||
|
@ -95,10 +95,9 @@ class KeyHintView(QLabel):
|
|||||||
return
|
return
|
||||||
|
|
||||||
keyconf = objreg.get('key-config')
|
keyconf = objreg.get('key-config')
|
||||||
is_special = lambda k: k.startswith('<') and k.endswith('>')
|
bindings = [(k, v) for (k, v)
|
||||||
bindings = [(key, cmd) for (key, cmd)
|
|
||||||
in keyconf.get_bindings_for(modename).items()
|
in keyconf.get_bindings_for(modename).items()
|
||||||
if key.startswith(prefix) and not is_special(key)]
|
if k.startswith(prefix) and not utils.is_special_key(k)]
|
||||||
|
|
||||||
if not bindings:
|
if not bindings:
|
||||||
return
|
return
|
||||||
|
@ -440,9 +440,14 @@ class KeyParseError(Exception):
|
|||||||
super().__init__("Could not parse {!r}: {}".format(keystr, error))
|
super().__init__("Could not parse {!r}: {}".format(keystr, error))
|
||||||
|
|
||||||
|
|
||||||
|
def is_special_key(keystr):
|
||||||
|
"""True if keystr is a 'special' keystring (e.g. <ctrl-x> or <space>)."""
|
||||||
|
return keystr.startswith('<') and keystr.endswith('>')
|
||||||
|
|
||||||
|
|
||||||
def _parse_single_key(keystr):
|
def _parse_single_key(keystr):
|
||||||
"""Convert a single key string to a (Qt.Key, Qt.Modifiers, text) tuple."""
|
"""Convert a single key string to a (Qt.Key, Qt.Modifiers, text) tuple."""
|
||||||
if keystr.startswith('<') and keystr.endswith('>'):
|
if is_special_key(keystr):
|
||||||
# Special key
|
# Special key
|
||||||
keystr = keystr[1:-1]
|
keystr = keystr[1:-1]
|
||||||
elif len(keystr) == 1:
|
elif len(keystr) == 1:
|
||||||
@ -489,7 +494,7 @@ def _parse_single_key(keystr):
|
|||||||
|
|
||||||
def parse_keystring(keystr):
|
def parse_keystring(keystr):
|
||||||
"""Parse a keystring like <Ctrl-x> or xyz and return a KeyInfo list."""
|
"""Parse a keystring like <Ctrl-x> or xyz and return a KeyInfo list."""
|
||||||
if keystr.startswith('<') and keystr.endswith('>'):
|
if is_special_key(keystr):
|
||||||
return [_parse_single_key(keystr)]
|
return [_parse_single_key(keystr)]
|
||||||
else:
|
else:
|
||||||
return [_parse_single_key(char) for char in keystr]
|
return [_parse_single_key(char) for char in keystr]
|
||||||
|
@ -984,3 +984,19 @@ class TestGetSetClipboard:
|
|||||||
def test_supports_selection(self, clipboard_mock, selection):
|
def test_supports_selection(self, clipboard_mock, selection):
|
||||||
clipboard_mock.supportsSelection.return_value = selection
|
clipboard_mock.supportsSelection.return_value = selection
|
||||||
assert utils.supports_selection() == selection
|
assert utils.supports_selection() == selection
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('keystr, expected', [
|
||||||
|
('<Control-x>', True),
|
||||||
|
('<Meta-x>', True),
|
||||||
|
('<Ctrl-Alt-y>', True),
|
||||||
|
('x', False),
|
||||||
|
('X', False),
|
||||||
|
('<Escape>', True),
|
||||||
|
('foobar', False),
|
||||||
|
('foo>', False),
|
||||||
|
('<foo', False),
|
||||||
|
('<<', False),
|
||||||
|
])
|
||||||
|
def test_is_special_key(keystr, expected):
|
||||||
|
assert utils.is_special_key(keystr) == expected
|
||||||
|
Loading…
Reference in New Issue
Block a user