Split CommandRunner into runner/parser

This commit is contained in:
Florian Bruhin 2017-06-19 16:34:15 +02:00
parent d4cbd4ace4
commit a6c629899e
3 changed files with 23 additions and 13 deletions

View File

@ -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:

View File

@ -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

View File

@ -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',