Split NormalKeyParser from CommandKeyParser

This commit is contained in:
Florian Bruhin 2014-04-24 22:56:55 +02:00
parent 0def82fe8c
commit 8cca182734
3 changed files with 43 additions and 34 deletions

View File

@ -56,7 +56,7 @@ import qutebrowser.keyinput.modes as modes
import qutebrowser.utils.message as message import qutebrowser.utils.message as message
from qutebrowser.widgets.mainwindow import MainWindow from qutebrowser.widgets.mainwindow import MainWindow
from qutebrowser.widgets.crash import CrashDialog from qutebrowser.widgets.crash import CrashDialog
from qutebrowser.keyinput.normalmode import CommandKeyParser from qutebrowser.keyinput.normalmode import NormalKeyParser
from qutebrowser.keyinput.insertmode import InsertKeyParser from qutebrowser.keyinput.insertmode import InsertKeyParser
from qutebrowser.keyinput.hintmode import HintKeyParser from qutebrowser.keyinput.hintmode import HintKeyParser
from qutebrowser.commands.parsers import CommandParser, SearchParser from qutebrowser.commands.parsers import CommandParser, SearchParser
@ -125,7 +125,7 @@ class QuteBrowser(QApplication):
self.commandparser = CommandParser() self.commandparser = CommandParser()
self.searchparser = SearchParser() self.searchparser = SearchParser()
self._keyparsers = { self._keyparsers = {
'normal': CommandKeyParser(self), 'normal': NormalKeyParser(self),
'hint': HintKeyParser(self), 'hint': HintKeyParser(self),
'insert': InsertKeyParser(self), 'insert': InsertKeyParser(self),
} }

View File

@ -25,6 +25,9 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer
from PyQt5.QtGui import QKeySequence from PyQt5.QtGui import QKeySequence
import qutebrowser.config.config as config import qutebrowser.config.config as config
import qutebrowser.utils.message as message
from qutebrowser.commands.parsers import (CommandParser, ArgumentCountError,
NoSuchCommandError)
class KeyParser(QObject): class KeyParser(QObject):
@ -333,3 +336,39 @@ class KeyParser(QObject):
"defined!") "defined!")
if section == self._confsectname: if section == self._confsectname:
self.read_config() self.read_config()
class CommandKeyParser(KeyParser):
"""KeyChainParser for command bindings.
Attributes:
commandparser: Commandparser instance.
"""
def __init__(self, parent=None, supports_count=None,
supports_chains=False):
super().__init__(parent, supports_count=supports_count,
supports_chains=supports_chains)
self.commandparser = CommandParser()
def _run_or_fill(self, cmdstr, count=None, ignore_exc=True):
"""Run the command in cmdstr or fill the statusbar if args missing.
Args:
cmdstr: The command string.
count: Optional command count.
ignore_exc: Ignore exceptions.
"""
try:
self.commandparser.run(cmdstr, count=count, ignore_exc=ignore_exc)
except NoSuchCommandError:
pass
except ArgumentCountError:
logging.debug('Filling statusbar with partial command {}'.format(
cmdstr))
message.set_cmd_text(':{} '.format(cmdstr))
def execute(self, cmdstr, count=None):
"""Handle a completed keychain."""
self._run_or_fill(cmdstr, count, ignore_exc=False)

View File

@ -24,43 +24,17 @@ Module attributes:
import logging import logging
import qutebrowser.utils.message as message import qutebrowser.utils.message as message
from qutebrowser.keyinput.keyparser import KeyParser from qutebrowser.keyinput.keyparser import CommandKeyParser
from qutebrowser.commands.parsers import (CommandParser, ArgumentCountError,
NoSuchCommandError)
STARTCHARS = ":/?" STARTCHARS = ":/?"
class CommandKeyParser(KeyParser): class NormalKeyParser(CommandKeyParser):
"""KeyChainParser for command bindings.
Attributes:
commandparser: Commandparser instance.
"""
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent, supports_count=True, supports_chains=True) super().__init__(parent, supports_count=True, supports_chains=True)
self.commandparser = CommandParser()
self.read_config('keybind') self.read_config('keybind')
def _run_or_fill(self, cmdstr, count=None, ignore_exc=True):
"""Run the command in cmdstr or fill the statusbar if args missing.
Args:
cmdstr: The command string.
count: Optional command count.
ignore_exc: Ignore exceptions.
"""
try:
self.commandparser.run(cmdstr, count=count, ignore_exc=ignore_exc)
except NoSuchCommandError:
pass
except ArgumentCountError:
logging.debug('Filling statusbar with partial command {}'.format(
cmdstr))
message.set_cmd_text(':{} '.format(cmdstr))
def _handle_single_key(self, e): def _handle_single_key(self, e):
"""Override _handle_single_key to abort if the key is a startchar. """Override _handle_single_key to abort if the key is a startchar.
@ -75,7 +49,3 @@ class CommandKeyParser(KeyParser):
message.set_cmd_text(txt) message.set_cmd_text(txt)
return True return True
return super()._handle_single_key(e) return super()._handle_single_key(e)
def execute(self, cmdstr, count=None):
"""Handle a completed keychain."""
self._run_or_fill(cmdstr, count, ignore_exc=False)