Merge branch 'alias-multi-commands' of https://github.com/mgoral/qutebrowser into mgoral-alias-multi-commands

This commit is contained in:
Florian Bruhin 2016-06-30 12:34:18 +02:00
commit 900ad1ba6d
4 changed files with 28 additions and 20 deletions

View File

@ -26,7 +26,7 @@ from PyQt5.QtCore import pyqtSlot, QUrl, QObject
from qutebrowser.config import config, configexc from qutebrowser.config import config, configexc
from qutebrowser.commands import cmdexc, cmdutils from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.utils import message, log, objreg, qtutils from qutebrowser.utils import message, objreg, qtutils
from qutebrowser.misc import split from qutebrowser.misc import split
@ -80,21 +80,23 @@ class CommandRunner(QObject):
self._partial_match = partial_match self._partial_match = partial_match
self._win_id = win_id self._win_id = win_id
def _get_alias(self, text): def _get_alias(self, text, default=None):
"""Get an alias from the config. """Get an alias from the config.
Args: Args:
text: The text to parse. text: The text to parse.
default : Default value to return when alias was not found. By
default it is set to None.
Return: Return:
None if no alias was found. The new command string if an alias was found. Default value
The new command string if an alias was found. otherwise.
""" """
parts = text.strip().split(maxsplit=1) parts = text.strip().split(maxsplit=1)
try: try:
alias = config.get('aliases', parts[0]) alias = config.get('aliases', parts[0])
except (configexc.NoOptionError, configexc.NoSectionError): except (configexc.NoOptionError, configexc.NoSectionError):
return None return default
try: try:
new_cmd = '{} {}'.format(alias, parts[1]) new_cmd = '{} {}'.format(alias, parts[1])
except IndexError: except IndexError:
@ -103,7 +105,7 @@ class CommandRunner(QObject):
new_cmd += ' ' new_cmd += ' '
return new_cmd return new_cmd
def parse_all(self, text, *args, **kwargs): def parse_all(self, text, aliases=True, *args, **kwargs):
"""Split a command on ;; and parse all parts. """Split a command on ;; and parse all parts.
If the first command in the commandline is a non-split one, it only If the first command in the commandline is a non-split one, it only
@ -111,11 +113,15 @@ class CommandRunner(QObject):
Args: Args:
text: Text to parse. text: Text to parse.
aliases: Whether to handle aliases.
*args/**kwargs: Passed to parse(). *args/**kwargs: Passed to parse().
Yields: Yields:
ParseResult tuples. ParseResult tuples.
""" """
if aliases:
text = self._get_alias(text, text)
if ';;' in text: if ';;' in text:
# Get the first command and check if it doesn't want to have ;; # Get the first command and check if it doesn't want to have ;;
# split. # split.
@ -159,12 +165,11 @@ class CommandRunner(QObject):
cmdline = text.split() cmdline = text.split()
return ParseResult(cmd=None, args=None, cmdline=cmdline, count=count) return ParseResult(cmd=None, args=None, cmdline=cmdline, count=count)
def parse(self, text, *, aliases=True, fallback=False, keep=False): def parse(self, text, *, fallback=False, keep=False):
"""Split the commandline text into command and arguments. """Split the commandline text into command and arguments.
Args: Args:
text: Text to parse. text: Text to parse.
aliases: Whether to handle aliases.
fallback: Whether to do a fallback splitting when the command was fallback: Whether to do a fallback splitting when the command was
unknown. unknown.
keep: Whether to keep special chars and whitespace keep: Whether to keep special chars and whitespace
@ -178,13 +183,6 @@ class CommandRunner(QObject):
if not cmdstr and not fallback: if not cmdstr and not fallback:
raise cmdexc.NoSuchCommandError("No command given") raise cmdexc.NoSuchCommandError("No command given")
if aliases:
new_cmd = self._get_alias(text)
if new_cmd is not None:
log.commands.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False, fallback=fallback,
keep=keep)
if self._partial_match: if self._partial_match:
cmdstr = self._completion_match(cmdstr) cmdstr = self._completion_match(cmdstr)

View File

@ -354,7 +354,7 @@ class Completer(QObject):
if completion.enabled: if completion.enabled:
completion.show() completion.show()
def split(self, keep=False, aliases=False): def split(self, keep=False):
"""Get the text split up in parts. """Get the text split up in parts.
Args: Args:
@ -371,7 +371,7 @@ class Completer(QObject):
# the whitespace. # the whitespace.
return [text] return [text]
runner = runners.CommandRunner(self._win_id) runner = runners.CommandRunner(self._win_id)
result = runner.parse(text, fallback=True, aliases=aliases, keep=keep) result = runner.parse(text, fallback=True, keep=keep)
parts = result.cmdline parts = result.cmdline
if self._empty_item_idx is not None: if self._empty_item_idx is not None:
log.completion.debug("Empty element queued at {}, " log.completion.debug("Empty element queued at {}, "

View File

@ -43,10 +43,20 @@ class TestCommandRunner:
with pytest.raises(cmdexc.NoSuchCommandError): with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all(cmdline_test.cmd, aliases=False)) list(cr.parse_all(cmdline_test.cmd, aliases=False))
def test_parse_all_with_alias(self, cmdline_test, config_stub):
config_stub.data = {'aliases': {'alias_name': cmdline_test.cmd}}
cr = runners.CommandRunner(0)
if cmdline_test.valid:
assert len(list(cr.parse_all("alias_name"))) > 0
else:
with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all("alias_name"))
def test_parse_with_count(self): def test_parse_with_count(self):
"""Test parsing of commands with a count.""" """Test parsing of commands with a count."""
cr = runners.CommandRunner(0) cr = runners.CommandRunner(0)
result = cr.parse('20:scroll down', aliases=False) result = cr.parse('20:scroll down')
assert result.cmd.name == 'scroll' assert result.cmd.name == 'scroll'
assert result.count == 20 assert result.count == 20
assert result.args == ['down'] assert result.args == ['down']
@ -58,5 +68,5 @@ class TestCommandRunner:
The same with it being disabled is tested by test_parse_all. The same with it being disabled is tested by test_parse_all.
""" """
cr = runners.CommandRunner(0, partial_match=True) cr = runners.CommandRunner(0, partial_match=True)
result = cr.parse('message-i', aliases=False) result = cr.parse('message-i')
assert result.cmd.name == 'message-info' assert result.cmd.name == 'message-info'

View File

@ -306,7 +306,7 @@ class TestDefaultConfig:
runner = runners.CommandRunner(win_id=0) runner = runners.CommandRunner(win_id=0)
for sectname in configdata.KEY_DATA: for sectname in configdata.KEY_DATA:
for cmd in conf.get_bindings_for(sectname).values(): for cmd in conf.get_bindings_for(sectname).values():
runner.parse(cmd, aliases=False) runner.parse(cmd)
def test_upgrade_version(self): def test_upgrade_version(self):
"""Fail when the qutebrowser version changed. """Fail when the qutebrowser version changed.