From 7ab4d215a076a90d2da27f24fdd02af47d03ca75 Mon Sep 17 00:00:00 2001 From: Mahmud Ridwan Date: Tue, 29 Jul 2025 00:42:08 +0600 Subject: [PATCH 1/3] Copy LAST_JEDI_COMPLETIONS to cell document so that completionItem/resolve will work --- pylsp/python_lsp.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index 36265890..e61b61a4 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -722,6 +722,10 @@ def _cell_document__completion(self, cellDocument, position=None, **_kwargs): if item.get("data", {}).get("doc_uri") == temp_uri: item["data"]["doc_uri"] = cellDocument.uri + # Copy LAST_JEDI_COMPLETIONS to cell document so that completionItem/resolve will work + tempDocument = workspace.get_document(temp_uri) + cellDocument.shared_data["LAST_JEDI_COMPLETIONS"] = tempDocument.shared_data.get("LAST_JEDI_COMPLETIONS", None) + return completions def m_text_document__completion(self, textDocument=None, position=None, **_kwargs): From 1281fb0485260dcd8b06857e2ede1ecf76baed30 Mon Sep 17 00:00:00 2001 From: Mahmud Ridwan Date: Fri, 8 Aug 2025 01:40:48 +0600 Subject: [PATCH 2/3] Add a `completionItem/resolve` test --- test/test_notebook_document.py | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/test_notebook_document.py b/test/test_notebook_document.py index ca0d477d..215258e1 100644 --- a/test/test_notebook_document.py +++ b/test/test_notebook_document.py @@ -530,3 +530,71 @@ def test_notebook_completion(client_server_pair) -> None: }, ], } + + +@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows") +def test_notebook_completion_resolve(client_server_pair) -> None: + """ + Tests that completion item resolve works correctly + """ + client, server = client_server_pair + send_initialize_request(client) + + # Open notebook + with patch.object(server._endpoint, "notify") as mock_notify: + send_notebook_did_open( + client, + [ + "def answer():\n\t'''Returns an important number.'''\n\treturn 42", + "ans", + ], + ) + # wait for expected diagnostics messages + wait_for_condition(lambda: mock_notify.call_count >= 2) + assert len(server.workspace.documents) == 3 + for uri in ["cell_1_uri", "cell_2_uri", "notebook_uri"]: + assert uri in server.workspace.documents + + future = client._endpoint.request( + "textDocument/completion", + { + "textDocument": { + "uri": "cell_2_uri", + }, + "position": {"line": 0, "character": 3}, + }, + ) + result = future.result(CALL_TIMEOUT_IN_SECONDS) + assert result == { + "isIncomplete": False, + "items": [ + { + "data": {"doc_uri": "cell_2_uri"}, + "insertText": "answer", + "kind": 3, + "label": "answer()", + "sortText": "aanswer", + }, + ], + } + + future = client._endpoint.request( + "completionItem/resolve", + { + "data": {"doc_uri": "cell_2_uri"}, + "label": "answer()", + }, + ) + result = future.result(CALL_TIMEOUT_IN_SECONDS) + del result["detail"] # The value of this is unpredictable. + assert result == { + "data": {"doc_uri": "cell_2_uri"}, + "insertText": "answer", + "kind": 3, + "label": "answer()", + "sortText": "aanswer", + "documentation": { + "kind": "markdown", + "value": "```python\nanswer()\n```\n\n\nReturns an important number.", + }, + } From 4167ef4098f013c6cd504f5557543a71b8259af9 Mon Sep 17 00:00:00 2001 From: Mahmud Ridwan Date: Fri, 8 Aug 2025 09:48:26 +0600 Subject: [PATCH 3/3] Format python_lsp.py --- pylsp/python_lsp.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index e61b61a4..c239644b 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -724,7 +724,9 @@ def _cell_document__completion(self, cellDocument, position=None, **_kwargs): # Copy LAST_JEDI_COMPLETIONS to cell document so that completionItem/resolve will work tempDocument = workspace.get_document(temp_uri) - cellDocument.shared_data["LAST_JEDI_COMPLETIONS"] = tempDocument.shared_data.get("LAST_JEDI_COMPLETIONS", None) + cellDocument.shared_data["LAST_JEDI_COMPLETIONS"] = ( + tempDocument.shared_data.get("LAST_JEDI_COMPLETIONS", None) + ) return completions