Skip to content

Commit 180c2e9

Browse files
committed
fix rope autoimport completion
1 parent a362006 commit 180c2e9

3 files changed

Lines changed: 53 additions & 14 deletions

File tree

pylsp/plugins/jedi_completion.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,27 @@ def pylsp_completion_item_resolve(
147147
document,
148148
):
149149
"""Resolve formatted completion for given non-resolved completion"""
150+
151+
if (
152+
"LAST_JEDI_COMPLETIONS" not in document.shared_data
153+
or completion_item["label"] not in document.shared_data["LAST_JEDI_COMPLETIONS"]
154+
):
155+
return None
156+
150157
shared_data = document.shared_data["LAST_JEDI_COMPLETIONS"].get(
151158
completion_item["label"]
152159
)
153160

154-
completion_capabilities = config.capabilities.get("textDocument", {}).get(
155-
"completion", {}
156-
)
157-
item_capabilities = completion_capabilities.get("completionItem", {})
158-
supported_markup_kinds = item_capabilities.get("documentationFormat", ["markdown"])
159-
preferred_markup_kind = _utils.choose_markup_kind(supported_markup_kinds)
160-
161161
if shared_data:
162+
completion_capabilities = config.capabilities.get("textDocument", {}).get(
163+
"completion", {}
164+
)
165+
item_capabilities = completion_capabilities.get("completionItem", {})
166+
supported_markup_kinds = item_capabilities.get(
167+
"documentationFormat", ["markdown"]
168+
)
169+
preferred_markup_kind = _utils.choose_markup_kind(supported_markup_kinds)
170+
162171
completion, data = shared_data
163172
return _resolve_completion(
164173
completion,

pylsp/plugins/rope_autoimport.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,31 @@ def pylsp_completions(
265265
)
266266
if len(results) > MAX_RESULTS_COMPLETIONS:
267267
results = results[:MAX_RESULTS_COMPLETIONS]
268+
269+
# most recently retrieved completion items, used for resolution
270+
document.shared_data["LAST_ROPE_AUTOIMPORT_COMPLETIONS"] = {
271+
# label is the only required property; here it is assumed to be unique
272+
result["label"]: ...
273+
for result in results
274+
}
275+
268276
return results
269277

270278

279+
@hookimpl
280+
def pylsp_completion_item_resolve(config, completion_item, document):
281+
"""Resolve formatted completion for given non-resolved completion"""
282+
283+
if (
284+
"LAST_ROPE_AUTOIMPORT_COMPLETIONS" not in document.shared_data
285+
or completion_item["label"]
286+
not in document.shared_data["LAST_ROPE_AUTOIMPORT_COMPLETIONS"]
287+
):
288+
return None
289+
290+
return completion_item
291+
292+
271293
def _document(import_statement: str) -> str:
272294
return """# Auto-Import\n""" + import_statement
273295

pylsp/plugins/rope_completion.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,26 @@ def pylsp_completions(config, workspace, document, position):
9292
@hookimpl
9393
def pylsp_completion_item_resolve(config, completion_item, document):
9494
"""Resolve formatted completion for given non-resolved completion"""
95+
96+
if (
97+
"LAST_ROPE_COMPLETIONS" not in document.shared_data
98+
or completion_item["label"] not in document.shared_data["LAST_ROPE_COMPLETIONS"]
99+
):
100+
return None
101+
95102
shared_data = document.shared_data["LAST_ROPE_COMPLETIONS"].get(
96103
completion_item["label"]
97104
)
98105

99-
completion_capabilities = config.capabilities.get("textDocument", {}).get(
100-
"completion", {}
101-
)
102-
item_capabilities = completion_capabilities.get("completionItem", {})
103-
supported_markup_kinds = item_capabilities.get("documentationFormat", ["markdown"])
104-
preferred_markup_kind = _utils.choose_markup_kind(supported_markup_kinds)
105-
106106
if shared_data:
107+
completion_capabilities = config.capabilities.get("textDocument", {}).get(
108+
"completion", {}
109+
)
110+
item_capabilities = completion_capabilities.get("completionItem", {})
111+
supported_markup_kinds = item_capabilities.get(
112+
"documentationFormat", ["markdown"]
113+
)
114+
preferred_markup_kind = _utils.choose_markup_kind(supported_markup_kinds)
107115
completion, data = shared_data
108116
return _resolve_completion(completion, data, preferred_markup_kind)
109117
return completion_item

0 commit comments

Comments
 (0)