diff options
author | Florian Bruhin <me@the-compiler.org> | 2019-10-09 17:29:51 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2019-10-09 18:08:03 +0200 |
commit | 038c6e495b6f65e29f8d742846e21bdb12b12759 (patch) | |
tree | 5500a93b51ebdf0e21c3e050e08cc36aff7f266e /qutebrowser | |
parent | 7b2b977432257857ed196b034c459ec8d2d0a146 (diff) | |
download | qutebrowser-038c6e495b6f65e29f8d742846e21bdb12b12759.tar.gz qutebrowser-038c6e495b6f65e29f8d742846e21bdb12b12759.zip |
Pass commandrunner object to mode parsers
Apart from making things easier to test, this also means there is only one
shared CommandRunner instead of there being one per parser.
Diffstat (limited to 'qutebrowser')
-rw-r--r-- | qutebrowser/commands/runners.py | 22 | ||||
-rw-r--r-- | qutebrowser/keyinput/modeman.py | 85 | ||||
-rw-r--r-- | qutebrowser/keyinput/modeparsers.py | 30 |
3 files changed, 90 insertions, 47 deletions
diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 7462eff51..c12c2668e 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -303,7 +303,21 @@ class CommandParser: return split_args -class CommandRunner(QObject): +class AbstractCommandRunner(QObject): + + """Abstract base class for CommandRunner.""" + + def run(self, text, count=None, *, safely=False): + raise NotImplementedError + + @pyqtSlot(str, int) + @pyqtSlot(str) + def run_safely(self, text, count=None): + """Run a command and display exceptions in the statusbar.""" + self.run(text, count, safely=True) + + +class CommandRunner(AbstractCommandRunner): """Parse and run qutebrowser commandline commands. @@ -371,9 +385,3 @@ class CommandRunner(QObject): if record_macro and cur_mode == usertypes.KeyMode.normal: macro_recorder = objreg.get('macro-recorder') macro_recorder.record_command(text, count) - - @pyqtSlot(str, int) - @pyqtSlot(str) - def run_safely(self, text, count=None): - """Run a command and display exceptions in the statusbar.""" - self.run(text, count, safely=True) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index f4b2bf9ac..eb11926c3 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -25,6 +25,7 @@ import attr from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QEvent from PyQt5.QtWidgets import QApplication +from qutebrowser.commands import runners from qutebrowser.keyinput import modeparsers from qutebrowser.config import config from qutebrowser.api import cmdutils @@ -64,34 +65,68 @@ class NotInModeError(Exception): def init(win_id, parent): """Initialize the mode manager and the keyparsers for the given win_id.""" - KM = usertypes.KeyMode # noqa: N806 modeman = ModeManager(win_id, parent) objreg.register('mode-manager', modeman, scope='window', window=win_id) + + commandrunner = runners.CommandRunner(win_id) + keyparsers = { - KM.normal: - modeparsers.NormalKeyParser(win_id, modeman), - KM.hint: - modeparsers.HintKeyParser(win_id, modeman), - KM.insert: - modeparsers.PassthroughKeyParser(win_id, 'insert', modeman), - KM.passthrough: - modeparsers.PassthroughKeyParser(win_id, 'passthrough', modeman), - KM.command: - modeparsers.PassthroughKeyParser(win_id, 'command', modeman), - KM.prompt: - modeparsers.PassthroughKeyParser(win_id, 'prompt', modeman), - KM.yesno: - modeparsers.PromptKeyParser(win_id, modeman), - KM.caret: - modeparsers.CaretKeyParser(win_id, modeman), - KM.set_mark: - modeparsers.RegisterKeyParser(win_id, KM.set_mark, modeman), - KM.jump_mark: - modeparsers.RegisterKeyParser(win_id, KM.jump_mark, modeman), - KM.record_macro: - modeparsers.RegisterKeyParser(win_id, KM.record_macro, modeman), - KM.run_macro: - modeparsers.RegisterKeyParser(win_id, KM.run_macro, modeman), + usertypes.KeyMode.normal: + modeparsers.NormalKeyParser(win_id=win_id, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.hint: + modeparsers.HintKeyParser(win_id=win_id, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.insert: + modeparsers.PassthroughKeyParser(win_id=win_id, + mode='insert', + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.passthrough: + modeparsers.PassthroughKeyParser(win_id=win_id, + mode='passthrough', + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.command: + modeparsers.PassthroughKeyParser(win_id=win_id, + mode='command', + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.prompt: + modeparsers.PassthroughKeyParser(win_id=win_id, + mode='prompt', + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.yesno: + modeparsers.PromptKeyParser(win_id=win_id, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.caret: + modeparsers.CaretKeyParser(win_id=win_id, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.set_mark: + modeparsers.RegisterKeyParser(win_id=win_id, + mode=usertypes.KeyMode.set_mark, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.jump_mark: + modeparsers.RegisterKeyParser(win_id=win_id, + mode=usertypes.KeyMode.jump_mark, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.record_macro: + modeparsers.RegisterKeyParser(win_id=win_id, + mode=usertypes.KeyMode.record_macro, + commandrunner=commandrunner, + parent=modeman), + usertypes.KeyMode.run_macro: + modeparsers.RegisterKeyParser(win_id=win_id, + mode=usertypes.KeyMode.run_macro, + commandrunner=commandrunner, + parent=modeman), } objreg.register('keyparsers', keyparsers, scope='window', window=win_id) modeman.destroyed.connect( diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index cbfb9a500..bbb393a8f 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -29,7 +29,7 @@ import enum from PyQt5.QtCore import pyqtSlot, Qt from PyQt5.QtGui import QKeySequence -from qutebrowser.commands import runners, cmdexc +from qutebrowser.commands import cmdexc from qutebrowser.config import config from qutebrowser.keyinput import basekeyparser, keyutils from qutebrowser.utils import usertypes, log, message, objreg, utils @@ -47,9 +47,9 @@ class CommandKeyParser(basekeyparser.BaseKeyParser): _commandrunner: CommandRunner instance. """ - def __init__(self, win_id, parent=None): + def __init__(self, win_id, commandrunner, parent=None): super().__init__(win_id, parent) - self._commandrunner = runners.CommandRunner(win_id) + self._commandrunner = commandrunner def execute(self, cmdstr, count=None): try: @@ -66,8 +66,8 @@ class NormalKeyParser(CommandKeyParser): _partial_timer: Timer to clear partial keypresses. """ - def __init__(self, win_id, parent=None): - super().__init__(win_id, parent) + def __init__(self, win_id, commandrunner, parent=None): + super().__init__(win_id, commandrunner, parent) self._read_config('normal') self._partial_timer = usertypes.Timer(self, 'partial-match') self._partial_timer.setSingleShot(True) @@ -144,7 +144,7 @@ class PassthroughKeyParser(CommandKeyParser): passthrough = True supports_count = False - def __init__(self, win_id, mode, parent=None): + def __init__(self, win_id, mode, commandrunner, parent=None): """Constructor. Args: @@ -152,7 +152,7 @@ class PassthroughKeyParser(CommandKeyParser): parent: Qt parent. warn: Whether to warn if an ignored key was bound. """ - super().__init__(win_id, parent) + super().__init__(win_id, commandrunner, parent) self._read_config(mode) self._mode = mode @@ -166,8 +166,8 @@ class PromptKeyParser(CommandKeyParser): supports_count = False - def __init__(self, win_id, parent=None): - super().__init__(win_id, parent) + def __init__(self, win_id, commandrunner, parent=None): + super().__init__(win_id, commandrunner, parent) self._read_config('yesno') def __repr__(self): @@ -185,8 +185,8 @@ class HintKeyParser(CommandKeyParser): supports_count = False - def __init__(self, win_id, parent=None): - super().__init__(win_id, parent) + def __init__(self, win_id, commandrunner, parent=None): + super().__init__(win_id, commandrunner, parent) self._filtertext = '' self._last_press = LastPress.none self._read_config('hint') @@ -298,8 +298,8 @@ class CaretKeyParser(CommandKeyParser): passthrough = True - def __init__(self, win_id, parent=None): - super().__init__(win_id, parent) + def __init__(self, win_id, commandrunner, parent=None): + super().__init__(win_id, commandrunner, parent) self._read_config('caret') @@ -314,8 +314,8 @@ class RegisterKeyParser(CommandKeyParser): supports_count = False - def __init__(self, win_id, mode, parent=None): - super().__init__(win_id, parent) + def __init__(self, win_id, mode, commandrunner, parent=None): + super().__init__(win_id, commandrunner, parent) self._mode = mode self._read_config('register') |