Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings: Improve completions of known values #258

Merged
merged 3 commits into from
Sep 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 68 additions & 30 deletions plugins_/settings/known_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@ def html_encode(string):
.replace("\n", "<br>") 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):
Expand Down Expand Up @@ -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()
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:
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
Expand All @@ -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

Expand Down Expand Up @@ -676,41 +690,64 @@ 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():
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("*.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("/")
completions.add((
"{0} \t{1}".format(file_name, package), scheme_path))
_, package, *_, name = scheme_path.split("/")
completions.add(format_completion_item(
value=scheme_path, is_default=scheme_path == default,
label=name, description=package))
Copy link
Member

@FichteFoll FichteFoll Sep 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see this, tmTheme files need to be specified by path, not by name. I'll fix this. Nvm, the value is still correct.

return completions

@staticmethod
def _theme_completions():
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(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.

Expand All @@ -725,5 +762,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