8000 support notebookDocument__did_close · tkrabel-db/python-lsp-server@2dd7165 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2dd7165

Browse files
committed
support notebookDocument__did_close
1 parent ab3bcee commit 2dd7165

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

pylsp/python_lsp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ def folding(self, doc_uri):
466466
def m_completion_item__resolve(self, **completionItem):
467467
return self.completion_item_resolve(completionItem)
468468

469-
# TODO: add m_notebook_document__did_close
470469
def m_notebook_document__did_open(self, notebookDocument=None, cellTextDocuments=None, **_kwargs):
471470
workspace = self._match_uri_to_workspace(notebookDocument['uri'])
472471
workspace.put_notebook_document(notebookDocument['uri'], notebookDocument['notebookType'],
@@ -476,6 +475,13 @@ def m_notebook_document__did_open(self, notebookDocument=None, cellTextDocuments
476475
workspace.put_cell_document(cell['uri'], cell['languageId'], cell['text'], version=cell.get('version'))
477476
self.lint(notebookDocument['uri'], is_saved=True)
478477

478+
def m_notebook_document__did_close(self, notebookDocument=None, cellTextDocuments=None, **_kwargs):
479+
workspace = self._match_uri_to_workspace(notebookDocument['uri'])
480+
for cell in (cellTextDocuments or []):
481+
workspace.publish_diagnostics(cell['uri'], [])
482+
workspace.rm_document(cell['uri'])
483+
workspace.rm_document(notebookDocument['uri'])
484+
479485
def m_notebook_document__did_change(self, notebookDocument=None, change=None, **_kwargs):
480486
"""
481487
Changes to the notebook document.

pylsp/workspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def wrapper(self, *args, **kwargs):
3333
return wrapper
3434

3535

36-
# pylint: disable=too-many-public-methods
3736
class Workspace:
3837

38+
# pylint: disable=too-many-public-methods
3939

4040
M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
4141
M_PROGRESS = '$/progress'

test/test_notebook_document.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,74 @@ def test_notebook_document__did_change(client_server_pair): # pylint: disable=re
404404
),
405405
]
406406
mock_notify.assert_has_calls(expected_call_args)
407+
408+
409+
def test_notebook__did_close(client_server_pair): # pylint: disable=redefined-outer-name
410+
client, server = client_server_pair
411+
client._endpoint.request(
412+
"initialize",
413+
{
414+
"processId": 1234,
415+
"rootPath": os.path.dirname(__file__),
416+
"initializationOptions": {},
417+
},
418+
).result(timeout=CALL_TIMEOUT)
419+
420+
# Open notebook
421+
with patch.object(server._endpoint, "notify") as mock_notify:
422+
client._endpoint.notify(
423+
"notebookDocument/didOpen",
424+
{
425+
"notebookDocument": {
426+
"uri": "notebook_uri",
427+
"notebookType": "jupyter-notebook",
428+
"cells": [
429+
{
430+
"kind": NotebookCellKind.Code,
431+
"document": "cell_1_uri",
432+
},
433+
{
434+
"kind": NotebookCellKind.Code,
435+
"document": "cell_2_uri",
436+
},
437+
],
438+
},
439+
"cellTextDocuments": [
440+
{
441+
"uri": "cell_1_uri",
442+
"languageId": "python",
443+
"text": "import sys",
444+
},
445+
{
446+
"uri": "cell_2_uri",
447+
"languageId": "python",
448+
"text": "",
449+
},
450+
],
451+
},
452+
)
453+
wait_for_condition(lambda: mock_notify.call_count >= 2)
454+
assert len(server.workspace.documents) == 3
455+
for uri in ["cell_1_uri", "cell_2_uri", "notebook_uri"]:
456+
assert uri in server.workspace.documents
457+
458+
# Close notebook
459+
with patch.object(server._endpoint, "notify") as mock_notify:
460+
client._endpoint.notify(
461+
"notebookDocument/didClose",
462+
{
463+
"notebookDocument": {
464+
"uri": "notebook_uri",
465+
},
466+
"cellTextDocuments": [
467+
{
468+
"uri": "cell_1_uri",
469+
},
470+
{
471+
"uri": "cell_2_uri",
472+
},
473+
],
474+
},
475+
)
476+
wait_for_condition(lambda: mock_notify.call_count >= 2)
477+
assert len(server.workspace.documents) == 0

0 commit comments

Comments
 (0)
0