Refactor completion model changing

This commit is contained in:
Florian Bruhin 2014-03-21 20:01:13 +01:00
parent a643bcd617
commit fa6f464e23
4 changed files with 40 additions and 19 deletions

View File

@ -38,6 +38,7 @@ class Command(QObject):
instance: How to get to the "self" argument of the handler. instance: How to get to the "self" argument of the handler.
A dotted string as viewed from app.py, or None. A dotted string as viewed from app.py, or None.
handler: The handler function to call. handler: The handler function to call.
completion: Completions to use for arguments, as a list of strings.
Signals: Signals:
signal: Gets emitted when something should be called via handle_command signal: Gets emitted when something should be called via handle_command
@ -52,7 +53,7 @@ class Command(QObject):
signal = pyqtSignal(tuple) signal = pyqtSignal(tuple)
def __init__(self, name, split_args, hide, nargs, count, desc, instance, def __init__(self, name, split_args, hide, nargs, count, desc, instance,
handler): handler, completion):
super().__init__() super().__init__()
self.name = name self.name = name
self.split_args = split_args self.split_args = split_args
@ -62,6 +63,7 @@ class Command(QObject):
self.desc = desc self.desc = desc
self.instance = instance self.instance = instance
self.handler = handler self.handler = handler
self.completion = completion
def check(self, args): def check(self, args):
"""Check if the argument count is valid. """Check if the argument count is valid.

View File

@ -38,11 +38,13 @@ class register:
nargs: A (minargs, maxargs) tuple of valid argument counts. nargs: A (minargs, maxargs) tuple of valid argument counts.
split_args: Whether to split the arguments or not. split_args: Whether to split the arguments or not.
hide: Whether to hide the command or not. hide: Whether to hide the command or not.
completion: Which completion to use for arguments, as a list of
strings.
""" """
def __init__(self, instance=None, name=None, nargs=None, split_args=True, def __init__(self, instance=None, name=None, nargs=None, split_args=True,
hide=False): hide=False, completion=None):
"""Gets called on parse-time with the decorator arguments. """Gets called on parse-time with the decorator arguments.
Arguments: Arguments:
@ -55,6 +57,7 @@ class register:
self.hide = hide self.hide = hide
self.nargs = nargs self.nargs = nargs
self.instance = instance self.instance = instance
self.completion = completion
def __call__(self, func): def __call__(self, func):
"""Gets called when a function should be decorated. """Gets called when a function should be decorated.
@ -81,7 +84,8 @@ class register:
desc = func.__doc__.splitlines()[0].strip().rstrip('.') desc = func.__doc__.splitlines()[0].strip().rstrip('.')
cmd = Command(name=mainname, split_args=self.split_args, cmd = Command(name=mainname, split_args=self.split_args,
hide=self.hide, nargs=nargs, count=count, desc=desc, hide=self.hide, nargs=nargs, count=count, desc=desc,
instance=self.instance, handler=func) instance=self.instance, handler=func,
completion=self.completion)
for name in names: for name in names:
cmd_dict[name] = cmd cmd_dict[name] = cmd
return func return func

View File

@ -150,7 +150,7 @@ class Config:
lines.append(keyval) lines.append(keyval)
return lines return lines
@cmdutils.register(instance='config') @cmdutils.register(instance='config', completion=['setting'])
def get(self, section, option, fallback=_UNSET): def get(self, section, option, fallback=_UNSET):
"""Get the real (transformed) value from a section/option.""" """Get the real (transformed) value from a section/option."""
try: try:

View File

@ -33,6 +33,7 @@ from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
QTextCursor) QTextCursor)
import qutebrowser.config.config as config import qutebrowser.config.config as config
import qutebrowser.commands.utils as cmdutils
from qutebrowser.config.style import get_stylesheet from qutebrowser.config.style import get_stylesheet
from qutebrowser.models.completionfilter import CompletionFilterModel from qutebrowser.models.completionfilter import CompletionFilterModel
from qutebrowser.models.commandcompletion import CommandCompletionModel from qutebrowser.models.commandcompletion import CommandCompletionModel
@ -176,36 +177,50 @@ class CompletionView(QTreeView):
text: The new text text: The new text
""" """
# FIXME we should also consider the cursor position
if self._ignore_next: if self._ignore_next:
# Text changed by a completion, so we don't have to complete again. # Text changed by a completion, so we don't have to complete again.
self._ignore_next = False self._ignore_next = False
return return
if not text.startswith(':'): if not text.startswith(':'):
# This is a search or gibberish, so we don't need to complete
# anything (yet)
# FIXME complete searchs
self.hide() self.hide()
self._completing = False self._completing = False
return return
text = text.lstrip(':') text = text.lstrip(':')
if ' ' in text: model = None
spl = text.split() parts = text.split(' ') # FIXME what about commands which use shutil?
if len(spl) == 1 and spl[0] in ['open']: # FIXME if len(parts) == 1:
self.set_model('setting') model = 'command'
else:
self.hide()
self._completing = False
return
else: else:
self.set_model('command') # try to delegate to the command
try:
completions = cmdutils.cmd_dict[parts[0]].completion
logging.debug('completions: {}'.format(completions))
if completions is None:
model = None
else:
model = completions[len(parts) - 2]
except KeyError:
logging.debug("No completions for '{}'".format(parts[0]))
model = None
# FIXME: if this fails, what do we do?
if model is None:
self.hide()
self._completing = False
return
self.set_model(model)
self._completing = True self._completing = True
if text.endswith(' '): pattern = parts[-1] if parts else ''
text = '' logging.debug("pattern: {}".format(pattern))
elif text: self.model.pattern = pattern
text = text.split()[-1]
logging.debug("pattern: {}".format(text))
self.model.pattern = text
self.model.srcmodel.mark_all_items(text) self.model.srcmodel.mark_all_items(text)
if self._enabled: if self._enabled:
self.show() self.show()