From 1638823fb30689ef8bbd4054d93fd83d0ecf4a93 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 22 Apr 2014 15:19:01 +0200 Subject: [PATCH] Add a complete() method to config types --- qutebrowser/config/config.py | 2 +- qutebrowser/config/conftypes.py | 29 +++++++++++++++++++++---- qutebrowser/models/settingcompletion.py | 8 ++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 268505a0b..8116c593e 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -174,7 +174,7 @@ class Config(QObject): for descline in desc.splitlines(): lines += wrapper.wrap(descline) valid_values = option.typ.valid_values - if valid_values is not None and valid_values.show: + if valid_values is not None: if valid_values.descriptions: for val in valid_values: desc = valid_values.descriptions[val] diff --git a/qutebrowser/config/conftypes.py b/qutebrowser/config/conftypes.py index 9fa3e3769..338a0f5ba 100644 --- a/qutebrowser/config/conftypes.py +++ b/qutebrowser/config/conftypes.py @@ -44,13 +44,11 @@ class ValidValues: Attributes: values: A list with the allowed untransformed values. descriptions: A dict with value/desc mappings. - show: Whether to show the values in the config or not. """ - def __init__(self, *vals, show=True): + def __init__(self, *vals): self.descriptions = {} self.values = [] - self.show = show for v in vals: if isinstance(v, str): # Value without description @@ -119,6 +117,27 @@ class BaseType: raise NotImplementedError("{} does not implement validate.".format( self.__class__.__name__)) + def complete(self): + """Return a list of possible values for completion. + + The default implementation just returns valid_values, but it might be + useful to override this for special cases. + + Return: + A list of (value, description) tuples or None. + """ + if self.valid_values is None: + return None + else: + out = [] + for val in self.valid_values: + try: + desc = self.valid_values.descriptions[val] + except KeyError: + desc = "" + out.append((val, desc)) + return out + class String(BaseType): @@ -155,7 +174,6 @@ class Bool(BaseType): _BOOLEAN_STATES: A dictionary of strings mapped to their bool meanings. """ - valid_values = ValidValues('true', 'false', show=False) typestr = 'bool' # Taken from configparser @@ -169,6 +187,9 @@ class Bool(BaseType): if value.lower() not in Bool._BOOLEAN_STATES: raise ValidationError(value, "must be a boolean!") + def complete(self): + return [('true', ''), ('false', '')] + class Int(BaseType): diff --git a/qutebrowser/models/settingcompletion.py b/qutebrowser/models/settingcompletion.py index 2e041e6b0..c90da7b25 100644 --- a/qutebrowser/models/settingcompletion.py +++ b/qutebrowser/models/settingcompletion.py @@ -62,12 +62,8 @@ class SettingValueCompletionModel(CompletionModel): def __init__(self, section, option, parent=None): super().__init__(parent) cat = self.new_category("Setting values for {}".format(option)) - vals = configdata.DATA[section][option].typ.valid_values + vals = configdata.DATA[section][option].typ.complete() if vals is None: raise NoCompletionsError - for val in vals: - try: - desc = vals.descriptions[val] - except KeyError: - desc = "" + for (val, desc) in vals: self.new_item(cat, val, desc)