From 6fdd007dbb8da0cf95dffcce2c0c16a708e464d9 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Wed, 21 Sep 2016 21:34:05 -0400 Subject: [PATCH] Simplify mode-checking in command. Rather than maintaining separate _modes and _not_modes lists, just build a single _modes list in the constructor. --- qutebrowser/commands/command.py | 33 +++++++++++++++------------- tests/end2end/features/hints.feature | 2 +- tests/end2end/features/misc.feature | 2 +- tests/end2end/test_insert_mode.py | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index ff4b2768a..16d9037d3 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -82,7 +82,6 @@ class Command: no_replace_variables: Don't replace variables like {url} _qute_args: The saved data from @cmdutils.argument _modes: The modes the command can be executed in. - _not_modes: The modes the command can not be executed in. _count: The count set for the command. _instance: The object to bind 'self' to. _scope: The scope to get _instance for in the object registry. @@ -101,10 +100,14 @@ class Command: for m in modes: if not isinstance(m, usertypes.KeyMode): raise TypeError("Mode {} is no KeyMode member!".format(m)) - if not_modes is not None: + self._modes = modes + elif not_modes is not None: for m in not_modes: if not isinstance(m, usertypes.KeyMode): raise TypeError("Mode {} is no KeyMode member!".format(m)) + self._modes = [m for m in usertypes.KeyMode if m not in not_modes] + else: + self._modes = list(usertypes.KeyMode) if scope != 'global' and instance is None: raise ValueError("Setting scope without setting instance makes " "no sense!") @@ -114,8 +117,6 @@ class Command: self.hide = hide self.deprecated = deprecated self._instance = instance - self._modes = modes - self._not_modes = not_modes self._scope = scope self._star_args_optional = star_args_optional self.debug = debug @@ -155,17 +156,7 @@ class Command: """ mode_manager = objreg.get('mode-manager', scope='window', window=win_id) - curmode = mode_manager.mode - if self._modes is not None and curmode not in self._modes: - mode_names = '/'.join(mode.name for mode in self._modes) - raise cmdexc.PrerequisitesError( - "{}: This command is only allowed in {} mode.".format( - self.name, mode_names)) - elif self._not_modes is not None and curmode in self._not_modes: - mode_names = '/'.join(mode.name for mode in self._not_modes) - raise cmdexc.PrerequisitesError( - "{}: This command is not allowed in {} mode.".format( - self.name, mode_names)) + self.validate_mode(mode_manager.mode) used_backend = usertypes.arg2backend[objreg.get('args').backend] if self.backend is not None and used_backend != self.backend: @@ -525,3 +516,15 @@ class Command: log.commands.debug('Calling {}'.format( debug_utils.format_call(self.handler, posargs, kwargs))) self.handler(*posargs, **kwargs) + + def validate_mode(self, mode): + """Raise cmdexc.PrerequisitesError unless allowed in the given mode. + + Args: + mode: The usertypes.KeyMode to check. + """ + if mode not in self._modes: + mode_names = '/'.join(m.name for m in self._modes) + raise cmdexc.PrerequisitesError( + "{}: This command is only allowed in {} mode, not {}.".format( + self.name, mode_names, mode.name)) diff --git a/tests/end2end/features/hints.feature b/tests/end2end/features/hints.feature index 7da850b50..08e6d718a 100644 --- a/tests/end2end/features/hints.feature +++ b/tests/end2end/features/hints.feature @@ -2,7 +2,7 @@ Feature: Using hints Scenario: Using :follow-hint outside of hint mode (issue 1105) When I run :follow-hint - Then the error "follow-hint: This command is only allowed in hint mode." should be shown + Then the error "follow-hint: This command is only allowed in hint mode, not normal." should be shown Scenario: Using :follow-hint with an invalid index. When I open data/hints/html/simple.html diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index f06fae098..8a4c3f09e 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -589,7 +589,7 @@ Feature: Various utility commands. And I run :repeat-command with count 2 And I wait until the scroll position changed to 0/0 Then the page should not be scrolled - And the error "prompt-accept: This command is only allowed in prompt/yesno mode." should be shown + And the error "prompt-accept: This command is only allowed in prompt/yesno mode, not normal." should be shown @qtwebengine_createWindow Scenario: :repeat-command with mode-switching command diff --git a/tests/end2end/test_insert_mode.py b/tests/end2end/test_insert_mode.py index d08f1b161..cd57968ca 100644 --- a/tests/end2end/test_insert_mode.py +++ b/tests/end2end/test_insert_mode.py @@ -93,7 +93,7 @@ def test_auto_leave_insert_mode(quteproc): quteproc.send_cmd(':paste-primary') expected_message = ('paste-primary: This command is only allowed in ' - 'insert mode.') + 'insert mode, not caret.') quteproc.mark_expected(category='message', loglevel=logging.ERROR, message=expected_message)