Simplify mode-checking in command.
Rather than maintaining separate _modes and _not_modes lists, just build a single _modes list in the constructor.
This commit is contained in:
parent
b49aa2d535
commit
6fdd007dbb
@ -82,7 +82,6 @@ class Command:
|
|||||||
no_replace_variables: Don't replace variables like {url}
|
no_replace_variables: Don't replace variables like {url}
|
||||||
_qute_args: The saved data from @cmdutils.argument
|
_qute_args: The saved data from @cmdutils.argument
|
||||||
_modes: The modes the command can be executed in.
|
_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.
|
_count: The count set for the command.
|
||||||
_instance: The object to bind 'self' to.
|
_instance: The object to bind 'self' to.
|
||||||
_scope: The scope to get _instance for in the object registry.
|
_scope: The scope to get _instance for in the object registry.
|
||||||
@ -101,10 +100,14 @@ class Command:
|
|||||||
for m in modes:
|
for m in modes:
|
||||||
if not isinstance(m, usertypes.KeyMode):
|
if not isinstance(m, usertypes.KeyMode):
|
||||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
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:
|
for m in not_modes:
|
||||||
if not isinstance(m, usertypes.KeyMode):
|
if not isinstance(m, usertypes.KeyMode):
|
||||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
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:
|
if scope != 'global' and instance is None:
|
||||||
raise ValueError("Setting scope without setting instance makes "
|
raise ValueError("Setting scope without setting instance makes "
|
||||||
"no sense!")
|
"no sense!")
|
||||||
@ -114,8 +117,6 @@ class Command:
|
|||||||
self.hide = hide
|
self.hide = hide
|
||||||
self.deprecated = deprecated
|
self.deprecated = deprecated
|
||||||
self._instance = instance
|
self._instance = instance
|
||||||
self._modes = modes
|
|
||||||
self._not_modes = not_modes
|
|
||||||
self._scope = scope
|
self._scope = scope
|
||||||
self._star_args_optional = star_args_optional
|
self._star_args_optional = star_args_optional
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
@ -155,17 +156,7 @@ class Command:
|
|||||||
"""
|
"""
|
||||||
mode_manager = objreg.get('mode-manager', scope='window',
|
mode_manager = objreg.get('mode-manager', scope='window',
|
||||||
window=win_id)
|
window=win_id)
|
||||||
curmode = mode_manager.mode
|
self.validate_mode(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))
|
|
||||||
|
|
||||||
used_backend = usertypes.arg2backend[objreg.get('args').backend]
|
used_backend = usertypes.arg2backend[objreg.get('args').backend]
|
||||||
if self.backend is not None and used_backend != self.backend:
|
if self.backend is not None and used_backend != self.backend:
|
||||||
@ -525,3 +516,15 @@ class Command:
|
|||||||
log.commands.debug('Calling {}'.format(
|
log.commands.debug('Calling {}'.format(
|
||||||
debug_utils.format_call(self.handler, posargs, kwargs)))
|
debug_utils.format_call(self.handler, posargs, kwargs)))
|
||||||
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))
|
||||||
|
@ -2,7 +2,7 @@ Feature: Using hints
|
|||||||
|
|
||||||
Scenario: Using :follow-hint outside of hint mode (issue 1105)
|
Scenario: Using :follow-hint outside of hint mode (issue 1105)
|
||||||
When I run :follow-hint
|
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.
|
Scenario: Using :follow-hint with an invalid index.
|
||||||
When I open data/hints/html/simple.html
|
When I open data/hints/html/simple.html
|
||||||
|
@ -589,7 +589,7 @@ Feature: Various utility commands.
|
|||||||
And I run :repeat-command with count 2
|
And I run :repeat-command with count 2
|
||||||
And I wait until the scroll position changed to 0/0
|
And I wait until the scroll position changed to 0/0
|
||||||
Then the page should not be scrolled
|
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
|
@qtwebengine_createWindow
|
||||||
Scenario: :repeat-command with mode-switching command
|
Scenario: :repeat-command with mode-switching command
|
||||||
|
@ -93,7 +93,7 @@ def test_auto_leave_insert_mode(quteproc):
|
|||||||
quteproc.send_cmd(':paste-primary')
|
quteproc.send_cmd(':paste-primary')
|
||||||
|
|
||||||
expected_message = ('paste-primary: This command is only allowed in '
|
expected_message = ('paste-primary: This command is only allowed in '
|
||||||
'insert mode.')
|
'insert mode, not caret.')
|
||||||
quteproc.mark_expected(category='message',
|
quteproc.mark_expected(category='message',
|
||||||
loglevel=logging.ERROR,
|
loglevel=logging.ERROR,
|
||||||
message=expected_message)
|
message=expected_message)
|
||||||
|
Loading…
Reference in New Issue
Block a user