Skip to content

Commit

Permalink
Use document-specific cache proposed in the review
Browse files Browse the repository at this point in the history
The document identifier is stored in the completion item data.
  • Loading branch information
krassowski committed Jul 10, 2021
1 parent 7c1ebf1 commit 79fc79f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pylsp/hookspecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def pylsp_completions(config, workspace, document, position):


@hookspec(firstresult=True)
def pylsp_completion_item_resolve(config, workspace, completion_item):
def pylsp_completion_item_resolve(config, workspace, document, completion_item):
pass


Expand Down
19 changes: 10 additions & 9 deletions pylsp/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@
# Types of parso node for errors
_ERRORS = ('error_node', )

# most recently retrieved completion items, used for resolution
_LAST_COMPLETIONS = {}


@hookimpl
def pylsp_completions(config, document, position):
"""Get formatted completions for current code position"""
# pylint: disable=too-many-locals
# pylint: disable=global-statement
global _LAST_COMPLETIONS

settings = config.plugin_settings('jedi_completion', document_path=document.path)
resolve_eagerly = settings.get('eager', False)
code_position = _utils.position_to_jedi_linecolumn(document, position)

code_position["fuzzy"] = settings.get("fuzzy", False)
code_position['fuzzy'] = settings.get('fuzzy', False)
completions = document.jedi_script(use_document_path=True).complete(**code_position)

if not completions:
Expand Down Expand Up @@ -77,7 +72,13 @@ def pylsp_completions(config, document, position):
completion_dict['label'] += ' object'
ready_completions.append(completion_dict)

_LAST_COMPLETIONS = {
for completion_dict in ready_completions:
completion_dict['data'] = {
'doc_uri': document.uri
}

# most recently retrieved completion items, used for resolution
document.shared_data['LAST_JEDI_COMPLETIONS'] = {
# label is the only required property; here it is assumed to be unique
completion['label']: (completion, data)
for completion, data in zip(ready_completions, completions)
Expand All @@ -87,9 +88,9 @@ def pylsp_completions(config, document, position):


@hookimpl
def pylsp_completion_item_resolve(completion_item):
def pylsp_completion_item_resolve(completion_item, document):
"""Resolve formatted completion for given non-resolved completion"""
completion, data = _LAST_COMPLETIONS.get(completion_item['label'])
completion, data = document.shared_data['LAST_JEDI_COMPLETIONS'].get(completion_item['label'])
return _resolve_completion(completion, data)


Expand Down
12 changes: 4 additions & 8 deletions pylsp/plugins/rope_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

log = logging.getLogger(__name__)

# most recently retrieved completion items, used for resolution
_LAST_COMPLETIONS = {}


@hookimpl
def pylsp_settings():
Expand All @@ -32,8 +29,6 @@ def _resolve_completion(completion, data):
@hookimpl
def pylsp_completions(config, workspace, document, position):
# pylint: disable=too-many-locals
# pylint: disable=global-statement
global _LAST_COMPLETIONS

settings = config.plugin_settings('rope_completion', document_path=document.path)
resolve_eagerly = settings.get('eager', False)
Expand Down Expand Up @@ -70,7 +65,8 @@ def pylsp_completions(config, workspace, document, position):
item = _resolve_completion(item, d)
new_definitions.append(item)

_LAST_COMPLETIONS = {
# most recently retrieved completion items, used for resolution
document.shared_data['LAST_ROPE_COMPLETIONS'] = {
# label is the only required property; here it is assumed to be unique
completion['label']: (completion, data)
for completion, data in zip(new_definitions, definitions)
Expand All @@ -82,9 +78,9 @@ def pylsp_completions(config, workspace, document, position):


@hookimpl
def pylsp_completion_item_resolve(completion_item):
def pylsp_completion_item_resolve(completion_item, document):
"""Resolve formatted completion for given non-resolved completion"""
completion, data = _LAST_COMPLETIONS.get(completion_item['label'])
completion, data = document.shared_data['LAST_ROPE_COMPLETIONS'].get(completion_item['label'])
return _resolve_completion(completion, data)


Expand Down
3 changes: 2 additions & 1 deletion pylsp/python_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def completions(self, doc_uri, position):
}

def completion_item_resolve(self, completion_item):
return self._hook('pylsp_completion_item_resolve', completion_item=completion_item)
doc_uri = completion_item.get('data', {}).get('doc_uri', None)
return self._hook('pylsp_completion_item_resolve', doc_uri, completion_item=completion_item)

def definitions(self, doc_uri, position):
return flatten(self._hook('pylsp_definitions', doc_uri, position=position))
Expand Down
1 change: 1 addition & 0 deletions pylsp/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def __init__(self, uri, workspace, source=None, version=None, local=True, extra_
self.path = uris.to_fs_path(uri)
self.dot_path = _utils.path_to_dot_name(self.path)
self.filename = os.path.basename(self.path)
self.shared_data = {}

self._config = workspace._config
self._workspace = workspace
Expand Down
5 changes: 3 additions & 2 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def test_jedi_completion_item_resolve(config, workspace):
assert 'detail' not in documented_hello_item

resolved_documented_hello = pylsp_jedi_completion_item_resolve(
completion_item=documented_hello_item
completion_item=documented_hello_item,
document=doc
)
assert 'Sends a polite greeting' in resolved_documented_hello['documentation']

Expand Down Expand Up @@ -435,7 +436,7 @@ def test_jedi_completion_environment(workspace):
completions = pylsp_jedi_completions(doc._config, doc, com_position)
assert completions[0]['label'] == 'loghub'

resolved = pylsp_jedi_completion_item_resolve(completions[0])
resolved = pylsp_jedi_completion_item_resolve(completions[0], doc)
assert 'changelog generator' in resolved['documentation'].lower()


Expand Down

0 comments on commit 79fc79f

Please sign in to comment.