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

View File

@ -38,11 +38,13 @@ class register:
nargs: A (minargs, maxargs) tuple of valid argument counts.
split_args: Whether to split the arguments 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,
hide=False):
hide=False, completion=None):
"""Gets called on parse-time with the decorator arguments.
Arguments:
@ -55,6 +57,7 @@ class register:
self.hide = hide
self.nargs = nargs
self.instance = instance
self.completion = completion
def __call__(self, func):
"""Gets called when a function should be decorated.
@ -81,7 +84,8 @@ class register:
desc = func.__doc__.splitlines()[0].strip().rstrip('.')
cmd = Command(name=mainname, split_args=self.split_args,
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:
cmd_dict[name] = cmd
return func

View File

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

View File

@ -33,6 +33,7 @@ from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
QTextCursor)
import qutebrowser.config.config as config
import qutebrowser.commands.utils as cmdutils
from qutebrowser.config.style import get_stylesheet
from qutebrowser.models.completionfilter import CompletionFilterModel
from qutebrowser.models.commandcompletion import CommandCompletionModel
@ -176,36 +177,50 @@ class CompletionView(QTreeView):
text: The new text
"""
# FIXME we should also consider the cursor position
if self._ignore_next:
# Text changed by a completion, so we don't have to complete again.
self._ignore_next = False
return
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._completing = False
return
text = text.lstrip(':')
if ' ' in text:
spl = text.split()
if len(spl) == 1 and spl[0] in ['open']: # FIXME
self.set_model('setting')
else:
self.hide()
self._completing = False
return
model = None
parts = text.split(' ') # FIXME what about commands which use shutil?
if len(parts) == 1:
model = 'command'
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
if text.endswith(' '):
text = ''
elif text:
text = text.split()[-1]
logging.debug("pattern: {}".format(text))
self.model.pattern = text
pattern = parts[-1] if parts else ''
logging.debug("pattern: {}".format(pattern))
self.model.pattern = pattern
self.model.srcmodel.mark_all_items(text)
if self._enabled:
self.show()