Skip to content

Commit 79fc79f

Browse files
committed
Use document-specific cache proposed in the review
The document identifier is stored in the completion item data.
1 parent 7c1ebf1 commit 79fc79f

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

Diff for: pylsp/hookspecs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def pylsp_completions(config, workspace, document, position):
3030

3131

3232
@hookspec(firstresult=True)
33-
def pylsp_completion_item_resolve(config, workspace, completion_item):
33+
def pylsp_completion_item_resolve(config, workspace, document, completion_item):
3434
pass
3535

3636

Diff for: pylsp/plugins/jedi_completion.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,17 @@
3333
# Types of parso node for errors
3434
_ERRORS = ('error_node', )
3535

36-
# most recently retrieved completion items, used for resolution
37-
_LAST_COMPLETIONS = {}
38-
3936

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

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

51-
code_position["fuzzy"] = settings.get("fuzzy", False)
46+
code_position['fuzzy'] = settings.get('fuzzy', False)
5247
completions = document.jedi_script(use_document_path=True).complete(**code_position)
5348

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

80-
_LAST_COMPLETIONS = {
75+
for completion_dict in ready_completions:
76+
completion_dict['data'] = {
77+
'doc_uri': document.uri
78+
}
79+
80+
# most recently retrieved completion items, used for resolution
81+
document.shared_data['LAST_JEDI_COMPLETIONS'] = {
8182
# label is the only required property; here it is assumed to be unique
8283
completion['label']: (completion, data)
8384
for completion, data in zip(ready_completions, completions)
@@ -87,9 +88,9 @@ def pylsp_completions(config, document, position):
8788

8889

8990
@hookimpl
90-
def pylsp_completion_item_resolve(completion_item):
91+
def pylsp_completion_item_resolve(completion_item, document):
9192
"""Resolve formatted completion for given non-resolved completion"""
92-
completion, data = _LAST_COMPLETIONS.get(completion_item['label'])
93+
completion, data = document.shared_data['LAST_JEDI_COMPLETIONS'].get(completion_item['label'])
9394
return _resolve_completion(completion, data)
9495

9596

Diff for: pylsp/plugins/rope_completion.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
log = logging.getLogger(__name__)
1111

12-
# most recently retrieved completion items, used for resolution
13-
_LAST_COMPLETIONS = {}
14-
1512

1613
@hookimpl
1714
def pylsp_settings():
@@ -32,8 +29,6 @@ def _resolve_completion(completion, data):
3229
@hookimpl
3330
def pylsp_completions(config, workspace, document, position):
3431
# pylint: disable=too-many-locals
35-
# pylint: disable=global-statement
36-
global _LAST_COMPLETIONS
3732

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

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

8379

8480
@hookimpl
85-
def pylsp_completion_item_resolve(completion_item):
81+
def pylsp_completion_item_resolve(completion_item, document):
8682
"""Resolve formatted completion for given non-resolved completion"""
87-
completion, data = _LAST_COMPLETIONS.get(completion_item['label'])
83+
completion, data = document.shared_data['LAST_ROPE_COMPLETIONS'].get(completion_item['label'])
8884
return _resolve_completion(completion, data)
8985

9086

Diff for: pylsp/python_lsp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ def completions(self, doc_uri, position):
251251
}
252252

253253
def completion_item_resolve(self, completion_item):
254-
return self._hook('pylsp_completion_item_resolve', completion_item=completion_item)
254+
doc_uri = completion_item.get('data', {}).get('doc_uri', None)
255+
return self._hook('pylsp_completion_item_resolve', doc_uri, completion_item=completion_item)
255256

256257
def definitions(self, doc_uri, position):
257258
return flatten(self._hook('pylsp_definitions', doc_uri, position=position))

Diff for: pylsp/workspace.py

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def __init__(self, uri, workspace, source=None, version=None, local=True, extra_
138138
self.path = uris.to_fs_path(uri)
139139
self.dot_path = _utils.path_to_dot_name(self.path)
140140
self.filename = os.path.basename(self.path)
141+
self.shared_data = {}
141142

142143
self._config = workspace._config
143144
self._workspace = workspace

Diff for: test/plugins/test_completion.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ def test_jedi_completion_item_resolve(config, workspace):
158158
assert 'detail' not in documented_hello_item
159159

160160
resolved_documented_hello = pylsp_jedi_completion_item_resolve(
161-
completion_item=documented_hello_item
161+
completion_item=documented_hello_item,
162+
document=doc
162163
)
163164
assert 'Sends a polite greeting' in resolved_documented_hello['documentation']
164165

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

438-
resolved = pylsp_jedi_completion_item_resolve(completions[0])
439+
resolved = pylsp_jedi_completion_item_resolve(completions[0], doc)
439440
assert 'changelog generator' in resolved['documentation'].lower()
440441

441442

0 commit comments

Comments
 (0)