More command splitting cleanup

This commit is contained in:
Florian Bruhin 2014-06-03 06:46:13 +02:00
parent 1d2d435231
commit 2eb26c96c4
2 changed files with 42 additions and 27 deletions

View File

@ -141,12 +141,42 @@ class CommandManager:
self._cmd = None
self._args = []
def parse(self, text, aliases=True):
def _get_alias(self, text, alias_no_args):
"""Get an alias from the config.
Args:
text: The text to parse.
alias_no_args: Whether to apply an alias if there are no arguments.
Return:
None if no alias was found.
The new command string if an alias was found.
"""
parts = text.strip().split(maxsplit=1)
try:
alias = config.get('aliases', parts[0])
except (config.NoOptionError, config.NoSectionError):
return None
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
if alias_no_args:
new_cmd = alias
else:
new_cmd = parts[0]
if text.endswith(' '):
new_cmd += ' '
return new_cmd
def parse(self, text, aliases=True, fallback=False, alias_no_args=True):
"""Split the commandline text into command and arguments.
Args:
text: Text to parse.
aliases: Whether to handle aliases.
fallback: Whether to do a fallback splitting when the command was
unknown.
alias_no_args: Whether to apply an alias if there are no arguments.
Raise:
NoSuchCommandError if a command wasn't found.
@ -159,24 +189,20 @@ class CommandManager:
raise NoSuchCommandError("No command given")
cmdstr = parts[0]
if aliases:
try:
alias = config.get('aliases', cmdstr)
except (config.NoOptionError, config.NoSectionError):
pass
else:
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
new_cmd = alias
if text.endswith(' '):
new_cmd += ' '
new_cmd = self._get_alias(text, alias_no_args)
if new_cmd is not None:
logger.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False)
try:
cmd = cmdutils.cmd_dict[cmdstr]
except KeyError:
if fallback:
parts = text.split(' ')
if text.endswith(' '):
parts.append('')
return parts
else:
raise NoSuchCommandError('{}: no such command'.format(cmdstr))
if len(parts) == 1:
args = []
elif cmd.split:

View File

@ -25,7 +25,6 @@ import qutebrowser.keyinput.modeman as modeman
import qutebrowser.commands.utils as cmdutils
from qutebrowser.widgets.misc import MinimalLineEdit
from qutebrowser.commands.managers import CommandManager
from qutebrowser.commands.exceptions import NoSuchCommandError
from qutebrowser.keyinput.modeparsers import STARTCHARS
from qutebrowser.utils.log import completion as logger
from qutebrowser.models.cmdhistory import (History, HistoryEmptyError,
@ -99,25 +98,15 @@ class Command(MinimalLineEdit):
@property
def parts(self):
"""Property to get the text split up in parts."""
text = self.text()
text = self.text()[len(self.prefix):]
if not text:
return []
text = text[len(self.prefix):]
logger.debug("Splitting '{}'".format(text))
manager = CommandManager()
original_cmd = text.strip().split(maxsplit=1)
try:
parts = manager.parse(text)
except NoSuchCommandError:
parts = text.split(' ')
if text.endswith(' '):
parts.append('')
parts = manager.parse(text, fallback=True, alias_no_args=False)
logger.debug("Split parts: {}".format(parts))
if len(parts) == 1 and parts[0]:
parts = original_cmd
return parts
@pyqtSlot()
def _update_cursor_part(self):
"""Get the part index of the commandline where the cursor is over."""