From 9245ef8e23f3f9c863ac4fd13db8fd3f9f06867d Mon Sep 17 00:00:00 2001 From: DeathAxe Date: Thu, 19 Sep 2019 18:09:47 +0200 Subject: [PATCH 1/3] Settings: Fix syntax specific encoding completions This commit modifies 8b904c60797ae56d598d8516265b701d5dc8648b by handling encoding completions exactly the same way as `color_scheme` and `theme` keys. Therefore the decision to return completions for `default_encoding` and `fallback_encoding` is made directly in the `_value_completions_for` method, which is meant to do exactly this. The if-branches are sorted by the key name rather than logical meaning. Same for the definition of the called methods. There is no need to merge comments and default values into the result if a complete list of values is already available. The `_known_completions()` method automatically marks the default encoding value. --- plugins_/settings/known_settings.py | 66 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/plugins_/settings/known_settings.py b/plugins_/settings/known_settings.py index f2bcbb12..51f31215 100644 --- a/plugins_/settings/known_settings.py +++ b/plugins_/settings/known_settings.py @@ -30,15 +30,27 @@ def html_encode(string): .replace("\n", "
") if string else "" -def format_completion_item(value, default=False): - """Create a completion item with its type as description.""" +def format_completion_item(value, is_default=False, label=None, description=None): + """Create a completion item with its type as description. + + Arguments: + value (any): + The value which is added when completions are committed. + If `label` is none, the `value` is used as label, too. + is_default (bool): + If `True` the completion item is marked '(default)'. + label (str): + An alternative label to use to present the `value` + in the completions panel. + description (str): + An optional description to display after the label. + If `None` is provided, the data type of `value` is displayed. + """ if isinstance(value, dict): raise ValueError("Cannot format dictionary value", value) - default_str = "(default) " if default else "" - return ("{0} \t{2}{1}".format(sublime.encode_value(value).strip('"'), - type(value).__name__, - default_str), - value) + 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__), value) def decode_value(string): @@ -561,16 +573,18 @@ def _value_completions_for(self, key): {(trigger, contents), ...} A set of all completions. """ + l.debug("building completions for key %r", key) + default = self.defaults.get(key) + l.debug("default value: %r", default) + if key == 'color_scheme': completions = self._color_scheme_completions() + elif key in ('default_encoding', 'fallback_encoding'): + completions = self._encoding_completions(default) elif key == 'theme': completions = self._theme_completions() else: - l.debug("building completions for key %r", key) - default = self.defaults.get(key) - l.debug("default value: %r", default) completions = self._completions_from_comment(key) - completions |= self._known_completions(key) completions |= self._completions_from_default(key, default) completions = self._marked_default_completions(completions, default) return completions @@ -597,7 +611,7 @@ def _marked_default_completions(completions, default): for item in completions: value = item[1] if is_list and value in default or value == default: - item = format_completion_item(value, default=True) + item = format_completion_item(value, is_default=True) default_completions.add(item) return default_completions @@ -676,15 +690,6 @@ def _completions_from_default(key, default): else: return {format_completion_item(default)} - def _known_completions(self, key): - """Provide known completions for select settings.""" - if ( - self.filename == "Preferences.sublime-settings" - and key in ('fallback_encoding', 'default_encoding') - ): - return set(map(format_completion_item, encodings.SUBLIME_TO_STANDARD.keys())) - return set() - @staticmethod def _color_scheme_completions(): """Create completions of all visible color schemes. @@ -707,6 +712,25 @@ def _color_scheme_completions(): "{0} \t{1}".format(file_name, package), scheme_path)) return completions + @staticmethod + def _encoding_completions(default): + """Create completions of all available encoding values. + + default (string): + The default `encoding` value. + + Returns: + {(trigger, contents), ...} + A set of all completions. + - trigger (string): the encoding in sublime format + - contents (string): the encoding in sublime format + """ + return set(map( + lambda x: format_completion_item( + x, is_default=x == default, description="encoding"), + encodings.SUBLIME_TO_STANDARD.keys() + )) + @staticmethod def _theme_completions(): """Create completions of all visible themes. From 40af42099a992d716e19a592b3d0ecc260b823e2 Mon Sep 17 00:00:00 2001 From: DeathAxe Date: Thu, 19 Sep 2019 18:52:51 +0200 Subject: [PATCH 2/3] Settings: Fix default color_scheme and theme marking This commit applies the `(default) ` mark to the default `color_scheme` and `theme` values. --- plugins_/settings/known_settings.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/plugins_/settings/known_settings.py b/plugins_/settings/known_settings.py index 51f31215..a45a36a6 100644 --- a/plugins_/settings/known_settings.py +++ b/plugins_/settings/known_settings.py @@ -578,11 +578,11 @@ def _value_completions_for(self, key): l.debug("default value: %r", default) if key == 'color_scheme': - completions = self._color_scheme_completions() + completions = self._color_scheme_completions(default) elif key in ('default_encoding', 'fallback_encoding'): completions = self._encoding_completions(default) elif key == 'theme': - completions = self._theme_completions() + completions = self._theme_completions(default) else: completions = self._completions_from_comment(key) completions |= self._completions_from_default(key, default) @@ -691,25 +691,29 @@ def _completions_from_default(key, default): return {format_completion_item(default)} @staticmethod - def _color_scheme_completions(): + def _color_scheme_completions(default): """Create completions of all visible color schemes. The set will not include color schemes matching at least one entry of `"settings.exclude_color_scheme_patterns": []`. + default (string): + The default `color_scheme` value. + Returns: {(trigger, contents], ...} A set of all completions. - trigger (string): base file name of the color scheme - - contents (string): the path to commit to the settings + - contents (string): the value to commit to the settings """ hidden = get_setting('settings.exclude_color_scheme_patterns') or [] completions = set() for scheme_path in sublime.find_resources("*.tmTheme"): if not any(hide in scheme_path for hide in hidden): _, package, *_, file_name = scheme_path.split("/") - completions.add(( - "{0} \t{1}".format(file_name, package), scheme_path)) + completions.add(format_completion_item( + value=scheme_path, is_default=scheme_path == default, + label=file_name, description=package)) return completions @staticmethod @@ -732,9 +736,12 @@ def _encoding_completions(default): )) @staticmethod - def _theme_completions(): + def _theme_completions(default): """Create completions of all visible themes. + default (string): + The default `theme` value. + The set will not include color schemes matching at least one entry of `"settings.exclude_theme_patterns": []` setting. @@ -749,5 +756,6 @@ def _theme_completions(): for theme in sublime.find_resources("*.sublime-theme"): theme = os.path.basename(theme) if not any(hide in theme for hide in hidden): - completions.add(("{0} \ttheme".format(theme), theme)) + completions.add(format_completion_item( + value=theme, is_default=theme == default, description="theme")) return completions From 3f61a04752e1786de8118da09b34cb26e3508a31 Mon Sep 17 00:00:00 2001 From: DeathAxe Date: Thu, 19 Sep 2019 19:37:55 +0200 Subject: [PATCH 3/3] Settings: Add support for sublime-color-scheme completions Up to this commit color schemes with the "new" sublime-color-scheme format were not added to the completions for the `color_scheme` settings. --- plugins_/settings/known_settings.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins_/settings/known_settings.py b/plugins_/settings/known_settings.py index a45a36a6..a68fd402 100644 --- a/plugins_/settings/known_settings.py +++ b/plugins_/settings/known_settings.py @@ -708,12 +708,18 @@ def _color_scheme_completions(default): """ hidden = get_setting('settings.exclude_color_scheme_patterns') or [] completions = set() + for scheme_path in sublime.find_resources("*.sublime-color-scheme"): + if not any(hide in scheme_path for hide in hidden): + _, package, *_, name = scheme_path.split("/") + completions.add(format_completion_item( + value=name, is_default=name == default, description=package)) + for scheme_path in sublime.find_resources("*.tmTheme"): if not any(hide in scheme_path for hide in hidden): - _, package, *_, file_name = scheme_path.split("/") + _, package, *_, name = scheme_path.split("/") completions.add(format_completion_item( value=scheme_path, is_default=scheme_path == default, - label=file_name, description=package)) + label=name, description=package)) return completions @staticmethod