From 940d1677e16dd6634d724b1b4e2f84a86dd63101 Mon Sep 17 00:00:00 2001 From: DeathAxe Date: Fri, 20 Sep 2019 17:53:01 +0200 Subject: [PATCH] Settings: Fix duplicated value completions This commit fixes some regressions introduced by 30983c799d9b048bb1ab5795fe117c70c1969f8e which cause 1. duplicated completions (e.g.: "caret_style") and 2. missing `(default)` marks for list values (e.g.: "indent_guide_options"). The method `_completions_from_comment()` needs to do default marking in order to prevent `_completions_from_default()` to add the same value. The `format_completion_item()` function needs to handle `list` type default values when setting `is_default`. Force `_completions_from_default()` to mark values added from the `default` list by passing `is_default=True` to the `format_completion_item()`. It's just an optimization to avoid the `value in default` check. --- plugins_/settings/known_settings.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/plugins_/settings/known_settings.py b/plugins_/settings/known_settings.py index 7a401945..0cc637d0 100644 --- a/plugins_/settings/known_settings.py +++ b/plugins_/settings/known_settings.py @@ -50,7 +50,8 @@ def format_completion_item(value, default=None, is_default=False, label=None, de """ if isinstance(value, dict): raise ValueError("Cannot format dictionary value", value) - is_default = is_default or default == value + if not is_default: + is_default = value in default if isinstance(default, list) else value == default return (("{0} \t(default) {1}" if is_default else "{0} \t{1}") .format(sublime.encode_value(label or value).strip('"'), description or type(value).__name__), @@ -587,11 +588,11 @@ def _value_completions_for(self, key): elif key == 'theme': completions = self._theme_completions(default) else: - completions = self._completions_from_comment(key) + completions = self._completions_from_comment(key, default) completions |= self._completions_from_default(key, default) return completions - def _completions_from_comment(self, key): + def _completions_from_comment(self, key, default): """Parse settings comments and return all possible values. Many settings are commented with a list of quoted words representing @@ -601,6 +602,9 @@ def _completions_from_comment(self, key): Arguments: key (string): the settings key name to read comments from + default (any): + the default value of the setting used to mark completion items + as "default". Returns: {(trigger, contents), ...} @@ -623,13 +627,13 @@ def _completions_from_comment(self, key): # Suggest list items as completions instead of a string # representation of the list. # Unless it's a dict. - completions.update(format_completion_item(v) for v in value - if not isinstance(v, dict)) + completions.update(format_completion_item(v, default) + for v in value if not isinstance(v, dict)) elif isinstance(value, dict): # TODO what should we do with dicts? pass else: - completions.add(format_completion_item(value)) + completions.add(format_completion_item(value, default)) for match in re.finditer(r'"([\.\w]+)"', comment): # quotation marks either wrap a string, a numeric or a boolean @@ -639,7 +643,7 @@ def _completions_from_comment(self, key): value = decode_value(value) except ValueError: pass - completions.add(format_completion_item(value)) + completions.add(format_completion_item(value, default)) return completions @@ -660,7 +664,7 @@ def _completions_from_default(key, default): elif isinstance(default, bool): return set(format_completion_item(value, default=default) for value in [True, False]) elif isinstance(default, list): - return {format_completion_item(value, default=default) for value in default} + return {format_completion_item(value, is_default=True) for value in default} elif isinstance(default, dict): return set() # TODO can't complete these yet else: