From a6c629899e17098d96159e262172529786ec7354 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 19 Jun 2017 16:34:15 +0200 Subject: [PATCH] Split CommandRunner into runner/parser --- qutebrowser/commands/runners.py | 26 +++++++++++++++++++------- qutebrowser/completion/completer.py | 8 +++----- qutebrowser/mainwindow/mainwindow.py | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 5c20bc91c..2dc509e65 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -81,19 +81,17 @@ def replace_variables(win_id, arglist): return args -class CommandRunner(QObject): +class CommandParser: - """Parse and run qutebrowser commandline commands. + """Parse qutebrowser commandline commands. Attributes: - _win_id: The window this CommandRunner is associated with. + _partial_match: Whether to allow partial command matches. """ - def __init__(self, win_id, partial_match=False, parent=None): - super().__init__(parent) + def __init__(self, partial_match=False): self._partial_match = partial_match - self._win_id = win_id def _get_alias(self, text, default=None): """Get an alias from the config. @@ -257,6 +255,20 @@ class CommandRunner(QObject): # already. return split_args + +class CommandRunner(QObject): + + """Parse and run qutebrowser commandline commands. + + Attributes: + _win_id: The window this CommandRunner is associated with. + """ + + def __init__(self, win_id, partial_match=False, parent=None): + super().__init__(parent) + self._parser = CommandParser(partial_match=partial_match) + self._win_id = win_id + def run(self, text, count=None): """Parse a command from a line of text and run it. @@ -271,7 +283,7 @@ class CommandRunner(QObject): window=self._win_id) cur_mode = mode_manager.mode - for result in self.parse_all(text): + for result in self._parser.parse_all(text): if result.cmd.no_replace_variables: args = result.args else: diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 7824de075..58be86d17 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -34,16 +34,14 @@ class Completer(QObject): Attributes: _cmd: The statusbar Command object this completer belongs to. _ignore_change: Whether to ignore the next completion update. - _win_id: The window ID this completer is in. _timer: The timer used to trigger the completion update. _last_cursor_pos: The old cursor position so we avoid double completion updates. _last_text: The old command text so we avoid double completion updates. """ - def __init__(self, cmd, win_id, parent=None): + def __init__(self, cmd, parent=None): super().__init__(parent) - self._win_id = win_id self._cmd = cmd self._ignore_change = False self._timer = QTimer() @@ -153,8 +151,8 @@ class Completer(QObject): if not text or not text.strip(): # Only ":", empty part under the cursor with nothing before/after return [], '', [] - runner = runners.CommandRunner(self._win_id) - result = runner.parse(text, fallback=True, keep=True) + parser = runners.CommandParser() + result = parser.parse(text, fallback=True, keep=True) parts = [x for x in result.cmdline if x] pos = self._cmd.cursorPosition() - len(self._cmd.prefix()) pos = min(pos, len(text)) # Qt treats 2-byte UTF-16 chars as 2 chars diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py index 6e1fab563..8b39322f5 100644 --- a/qutebrowser/mainwindow/mainwindow.py +++ b/qutebrowser/mainwindow/mainwindow.py @@ -304,7 +304,7 @@ class MainWindow(QWidget): def _init_completion(self): self._completion = completionwidget.CompletionView(self.win_id, self) cmd = objreg.get('status-command', scope='window', window=self.win_id) - completer_obj = completer.Completer(cmd, self.win_id, self._completion) + completer_obj = completer.Completer(cmd, self._completion) self._completion.selection_changed.connect( completer_obj.on_selection_changed) objreg.register('completion', self._completion, scope='window',