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: Fix duplicated value completions #259

Merged
merged 1 commit into from
Sep 20, 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
20 changes: 12 additions & 8 deletions plugins_/settings/known_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__),
Expand Down Expand Up @@ -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
Expand All @@ -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), ...}
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand Down