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:
Ryan Roden-Corrent 2016-05-18 22:05:48 -04:00
parent 5992b81850
commit b1440a1804
5 changed files with 28 additions and 8 deletions

View File

@ -24,7 +24,7 @@ from PyQt5.QtCore import Qt, QTimer, pyqtSlot
from qutebrowser.browser import webview
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.completion.models import base
@ -57,7 +57,7 @@ class CommandCompletionModel(base.BaseCompletionModel):
cmd_to_keys = defaultdict(list)
for key, cmd in keyconf.get_bindings_for('normal').items():
# put special bindings last
if key.startswith('<') and key.endswith('>'):
if utils.is_special_key(key):
cmd_to_keys[cmd].append(key)
else:
cmd_to_keys[cmd].insert(0, key)

View File

@ -335,7 +335,7 @@ class BaseKeyParser(QObject):
def _parse_key_command(self, modename, key, cmd):
"""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])
self.special_bindings[keystr] = cmd
elif self._supports_chains:

View File

@ -95,10 +95,9 @@ class KeyHintView(QLabel):
return
keyconf = objreg.get('key-config')
is_special = lambda k: k.startswith('<') and k.endswith('>')
bindings = [(key, cmd) for (key, cmd)
bindings = [(k, v) for (k, v)
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:
return

View File

@ -440,9 +440,14 @@ class KeyParseError(Exception):
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):
"""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
keystr = keystr[1:-1]
elif len(keystr) == 1:
@ -489,7 +494,7 @@ def _parse_single_key(keystr):
def parse_keystring(keystr):
"""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)]
else:
return [_parse_single_key(char) for char in keystr]

View File

@ -984,3 +984,19 @@ class TestGetSetClipboard:
def test_supports_selection(self, clipboard_mock, selection):
clipboard_mock.supportsSelection.return_value = 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