-
Notifications
You must be signed in to change notification settings - Fork 213
Create a cache for snippets #83
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
Merged
+178
−127
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bd4869
Move LabelResolver to its own file
ccordoba12 4b487f1
Abstract resolver class in order to generalize it
ccordoba12 8483efd
Add snippet cache
ccordoba12 d129676
Rename resolve options because they apply now to labels and snippets
ccordoba12 d92e90a
Testing: Check that we resolve at most a certain number of snippets
ccordoba12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
from collections import defaultdict | ||
import logging | ||
from time import time | ||
|
||
from jedi.api.classes import Completion | ||
|
||
from pylsp import lsp | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
# ---- Base class | ||
# ----------------------------------------------------------------------------- | ||
class Resolver: | ||
|
||
def __init__(self, callback, resolve_on_error, time_to_live=60 * 30): | ||
self.callback = callback | ||
self.resolve_on_error = resolve_on_error | ||
self._cache = {} | ||
self._time_to_live = time_to_live | ||
self._cache_ttl = defaultdict(set) | ||
self._clear_every = 2 | ||
# see https://github.com/davidhalter/jedi/blob/master/jedi/inference/helpers.py#L194-L202 | ||
self._cached_modules = {'pandas', 'numpy', 'tensorflow', 'matplotlib'} | ||
|
||
@property | ||
def cached_modules(self): | ||
return self._cached_modules | ||
|
||
@cached_modules.setter | ||
def cached_modules(self, new_value): | ||
self._cached_modules = set(new_value) | ||
|
||
def clear_outdated(self): | ||
now = self.time_key() | ||
to_clear = [ | ||
timestamp | ||
for timestamp in self._cache_ttl | ||
if timestamp < now | ||
] | ||
for time_key in to_clear: | ||
for key in self._cache_ttl[time_key]: | ||
del self._cache[key] | ||
del self._cache_ttl[time_key] | ||
|
||
def time_key(self): | ||
return int(time() / self._time_to_live) | ||
|
||
def get_or_create(self, completion: Completion): | ||
if not completion.full_name: | ||
use_cache = False | ||
else: | ||
module_parts = completion.full_name.split('.') | ||
use_cache = module_parts and module_parts[0] in self._cached_modules | ||
|
||
if use_cache: | ||
key = self._create_completion_id(completion) | ||
if key not in self._cache: | ||
if self.time_key() % self._clear_every == 0: | ||
self.clear_outdated() | ||
|
||
self._cache[key] = self.resolve(completion) | ||
self._cache_ttl[self.time_key()].add(key) | ||
return self._cache[key] | ||
|
||
return self.resolve(completion) | ||
|
||
def _create_completion_id(self, completion: Completion): | ||
return ( | ||
completion.full_name, completion.module_path, | ||
completion.line, completion.column, | ||
self.time_key() | ||
) | ||
|
||
def resolve(self, completion): | ||
try: | ||
sig = completion.get_signatures() | ||
return self.callback(completion, sig) | ||
except Exception as e: # pylint: disable=broad-except | ||
log.warning( | ||
'Something went wrong when resolving label for {completion}: {e}', | ||
completion=completion, e=e | ||
) | ||
return self.resolve_on_error | ||
|
||
|
||
# ---- Label resolver | ||
# ----------------------------------------------------------------------------- | ||
def format_label(completion, sig): | ||
if sig and completion.type in ('function', 'method'): | ||
params = ', '.join(param.name for param in sig[0].params) | ||
label = '{}({})'.format(completion.name, params) | ||
return label | ||
return completion.name | ||
|
||
|
||
LABEL_RESOLVER = Resolver(callback=format_label, resolve_on_error='') | ||
|
||
|
||
# ---- Snippets resolver | ||
# ----------------------------------------------------------------------------- | ||
def format_snippet(completion, sig): | ||
if not sig: | ||
return {} | ||
|
||
snippet_completion = {} | ||
|
||
positional_args = [param for param in sig[0].params | ||
if '=' not in param.description and | ||
param.name not in {'/', '*'}] | ||
|
||
if len(positional_args) > 1: | ||
# For completions with params, we can generate a snippet instead | ||
snippet_completion['insertTextFormat'] = lsp.InsertTextFormat.Snippet | ||
snippet = completion.name + '(' | ||
for i, param in enumerate(positional_args): | ||
snippet += '${%s:%s}' % (i + 1, param.name) | ||
if i < len(positional_args) - 1: | ||
snippet += ', ' | ||
snippet += ')$0' | ||
snippet_completion['insertText'] = snippet | ||
elif len(positional_args) == 1: | ||
snippet_completion['insertTextFormat'] = lsp.InsertTextFormat.Snippet | ||
snippet_completion['insertText'] = completion.name + '($0)' | ||
else: | ||
snippet_completion['insertText'] = completion.name + '()' | ||
|
||
return snippet_completion | ||
|
||
|
||
SNIPPET_RESOLVER = Resolver(callback=format_snippet, resolve_on_error={}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this a BC change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to document this backwards compatibility difference somewhere to let clients that these flags have changed names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok, I totally agree with that. Will do it in our release notes when 1.3.0 is out.