1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
############################################################################
# Original work Copyright 2017 Palantir Technologies, Inc. #
# Original work licensed under the MIT License. #
# See ThirdPartyNotices.txt in the project root for license information. #
# All modifications Copyright (c) Open Law Library. All rights reserved. #
# #
# Licensed under the Apache License, Version 2.0 (the "License") #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http: // www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
import os
import pytest
from lsprotocol import types
from pygls import uris
from pygls.workspace import Workspace
DOC_URI = uris.from_fs_path(__file__)
DOC_TEXT = """test"""
DOC = types.TextDocumentItem(
uri=DOC_URI, language_id="plaintext", version=0, text=DOC_TEXT
)
NOTEBOOK = types.NotebookDocument(
uri="file:///path/to/notebook.ipynb",
notebook_type="jupyter-notebook",
version=0,
cells=[
types.NotebookCell(
kind=types.NotebookCellKind.Code,
document="nb-cell-scheme://path/to/notebook.ipynb#cv32321",
),
types.NotebookCell(
kind=types.NotebookCellKind.Code,
document="nb-cell-scheme://path/to/notebook.ipynb#cp897h32",
),
],
)
NB_CELL_1 = types.TextDocumentItem(
uri="nb-cell-scheme://path/to/notebook.ipynb#cv32321",
language_id="python",
version=0,
text="# cell 1",
)
NB_CELL_2 = types.TextDocumentItem(
uri="nb-cell-scheme://path/to/notebook.ipynb#cp897h32",
language_id="python",
version=0,
text="# cell 2",
)
NB_CELL_3 = types.TextDocumentItem(
uri="nb-cell-scheme://path/to/notebook.ipynb#cq343eeds",
language_id="python",
version=0,
text="# cell 3",
)
def test_add_folder(workspace):
dir_uri = os.path.dirname(DOC_URI)
dir_name = "test"
workspace.add_folder(types.WorkspaceFolder(uri=dir_uri, name=dir_name))
assert workspace.folders[dir_uri].name == dir_name
def test_get_notebook_document_by_uri(workspace):
"""Ensure that we can get a notebook given its uri."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook == NOTEBOOK
@pytest.mark.parametrize(
"cell,expected",
[
(NB_CELL_1, NOTEBOOK),
(NB_CELL_2, NOTEBOOK),
(NB_CELL_3, None),
(DOC, None),
],
)
def test_get_notebook_document_by_cell_uri(workspace, cell, expected):
"""Ensure that we can get a notebook given a uri of one of its cells"""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(cell_uri=cell.uri)
assert notebook == expected
def test_get_text_document(workspace):
workspace.put_text_document(DOC)
assert workspace.get_text_document(DOC_URI).source == DOC_TEXT
def test_get_missing_document(tmpdir, workspace):
doc_path = tmpdir.join("test_document.py")
doc_path.write(DOC_TEXT)
doc_uri = uris.from_fs_path(str(doc_path))
assert workspace.get_text_document(doc_uri).source == DOC_TEXT
def test_put_notebook_document(workspace):
"""Ensure that we can add notebook documents to the workspace correctly."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
assert NOTEBOOK.uri in workspace._notebook_documents
assert NB_CELL_1.uri in workspace._text_documents
assert NB_CELL_2.uri in workspace._text_documents
def test_put_text_document(workspace):
workspace.put_text_document(DOC)
assert DOC_URI in workspace._text_documents
def test_remove_folder(workspace):
dir_uri = os.path.dirname(DOC_URI)
dir_name = "test"
workspace.add_folder(types.WorkspaceFolder(uri=dir_uri, name=dir_name))
workspace.remove_folder(dir_uri)
assert dir_uri not in workspace.folders
def test_remove_notebook_document(workspace):
"""Ensure that we can correctly remove a document from the workspace."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
assert NOTEBOOK.uri in workspace._notebook_documents
assert NB_CELL_1.uri in workspace._text_documents
assert NB_CELL_2.uri in workspace._text_documents
params = types.DidCloseNotebookDocumentParams(
notebook_document=types.NotebookDocumentIdentifier(uri=NOTEBOOK.uri),
cell_text_documents=[
types.TextDocumentIdentifier(uri=NB_CELL_1.uri),
types.TextDocumentIdentifier(uri=NB_CELL_2.uri),
],
)
workspace.remove_notebook_document(params)
assert NOTEBOOK.uri not in workspace._notebook_documents
assert NB_CELL_1.uri not in workspace._text_documents
assert NB_CELL_2.uri not in workspace._text_documents
def test_remove_text_document(workspace):
workspace.put_text_document(DOC)
assert workspace.get_text_document(DOC_URI).source == DOC_TEXT
workspace.remove_text_document(DOC_URI)
assert workspace.get_text_document(DOC_URI)._source is None
def test_update_notebook_metadata(workspace):
"""Ensure we can update a notebook's metadata correctly."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 0
assert notebook.metadata is None
params = types.DidChangeNotebookDocumentParams(
notebook_document=types.VersionedNotebookDocumentIdentifier(
uri=NOTEBOOK.uri, version=31
),
change=types.NotebookDocumentChangeEvent(
metadata={"custom": "metadata"},
),
)
workspace.update_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 31
assert notebook.metadata == {"custom": "metadata"}
def test_update_notebook_cell_data(workspace):
"""Ensure we can update a notebook correctly when cell data changes."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 0
cell_1 = notebook.cells[0]
assert cell_1.metadata is None
assert cell_1.execution_summary is None
cell_2 = notebook.cells[1]
assert cell_2.metadata is None
assert cell_2.execution_summary is None
params = types.DidChangeNotebookDocumentParams(
notebook_document=types.VersionedNotebookDocumentIdentifier(
uri=NOTEBOOK.uri, version=31
),
change=types.NotebookDocumentChangeEvent(
cells=types.NotebookDocumentChangeEventCellsType(
data=[
types.NotebookCell(
kind=types.NotebookCellKind.Code,
document=NB_CELL_1.uri,
metadata={"slideshow": {"slide_type": "skip"}},
execution_summary=types.ExecutionSummary(
execution_order=2, success=True
),
),
types.NotebookCell(
kind=types.NotebookCellKind.Code,
document=NB_CELL_2.uri,
metadata={"slideshow": {"slide_type": "note"}},
execution_summary=types.ExecutionSummary(
execution_order=3, success=False
),
),
]
)
),
)
workspace.update_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 31
cell_1 = notebook.cells[0]
assert cell_1.metadata == {"slideshow": {"slide_type": "skip"}}
assert cell_1.execution_summary == types.ExecutionSummary(
execution_order=2, success=True
)
cell_2 = notebook.cells[1]
assert cell_2.metadata == {"slideshow": {"slide_type": "note"}}
assert cell_2.execution_summary == types.ExecutionSummary(
execution_order=3, success=False
)
def test_update_notebook_cell_content(workspace):
"""Ensure we can update a notebook correctly when the cell contents change."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 0
cell_1 = workspace.get_text_document(NB_CELL_1.uri)
assert cell_1.version == 0
assert cell_1.source == "# cell 1"
cell_2 = workspace.get_text_document(NB_CELL_2.uri)
assert cell_2.version == 0
assert cell_2.source == "# cell 2"
params = types.DidChangeNotebookDocumentParams(
notebook_document=types.VersionedNotebookDocumentIdentifier(
uri=NOTEBOOK.uri, version=31
),
change=types.NotebookDocumentChangeEvent(
cells=types.NotebookDocumentChangeEventCellsType(
text_content=[
types.NotebookDocumentChangeEventCellsTypeTextContentType(
document=types.VersionedTextDocumentIdentifier(
uri=NB_CELL_1.uri, version=13
),
changes=[
types.TextDocumentContentChangeEvent_Type1(
text="new text",
range=types.Range(
start=types.Position(line=0, character=0),
end=types.Position(line=0, character=8),
),
)
],
),
types.NotebookDocumentChangeEventCellsTypeTextContentType(
document=types.VersionedTextDocumentIdentifier(
uri=NB_CELL_2.uri, version=21
),
changes=[
types.TextDocumentContentChangeEvent_Type1(
text="",
range=types.Range(
start=types.Position(line=0, character=0),
end=types.Position(line=0, character=8),
),
),
types.TextDocumentContentChangeEvent_Type1(
text="other text",
range=types.Range(
start=types.Position(line=0, character=0),
end=types.Position(line=0, character=0),
),
),
],
),
]
)
),
)
workspace.update_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 31
cell_1 = workspace.get_text_document(NB_CELL_1.uri)
assert cell_1.version == 13
assert cell_1.source == "new text"
cell_2 = workspace.get_text_document(NB_CELL_2.uri)
assert cell_2.version == 21
assert cell_2.source == "other text"
def test_update_notebook_new_cells(workspace):
"""Ensure that we can correctly add new cells to an existing notebook."""
params = types.DidOpenNotebookDocumentParams(
notebook_document=NOTEBOOK,
cell_text_documents=[
NB_CELL_1,
NB_CELL_2,
],
)
workspace.put_notebook_document(params)
notebook = workspace.get_notebook_document(notebook_uri=NOTEBOOK.uri)
assert notebook.version == 0
cell_uris = [c.document for c in notebook.cells]
assert cell_uris == [NB_CELL_1.uri, NB_CELL_2.uri]
cell_1 = workspace.get_text_document(NB_CELL_1.uri)
assert cell_1.version == 0
assert cell_1.source == "# cell 1"
cell_2 = workspace.get_text_document(NB_CELL_2.uri)
assert cell_2.version == 0
assert cell_2.source == "# cell 2"
params = types.DidChangeNotebookDocumentParams(
notebook_document=types.VersionedNotebookDocumentIdentifier(
uri=NOTEBOOK.uri, version=31
),
change=types.NotebookDocumentChangeEvent(
cells=types.NotebookDocumentChangeEventCellsType(
structure=types.NotebookDocumentChangeEventCellsTypeStructureType(
array=types.NotebookCellArrayChange(
start=1,
delete_count=0,
cells=[
types.NotebookCell(
kind=types.NotebookCellKind.Code, document=NB_CELL_3.uri
)
],
),
did_open=[NB_CELL_3],
)
)
),
)
workspace.update_notebook_document(params)
notebook = workspace.get_notebook_document(cell_uri=NB_CELL_3.uri)
assert notebook.uri == NOTEBOOK.uri
assert NB_CELL_3.uri in workspace._text_documents
cell_uris = [c.document for c in notebook.cells]
assert cell_uris == [NB_CELL_1.uri, NB_CELL_3.uri, NB_CELL_2.uri]
def test_workspace_folders():
wf1 = types.WorkspaceFolder(uri="/ws/f1", name="ws1")
wf2 = types.WorkspaceFolder(uri="/ws/f2", name="ws2")
workspace = Workspace("/ws", workspace_folders=[wf1, wf2])
assert workspace.folders["/ws/f1"] is wf1
assert workspace.folders["/ws/f2"] is wf2
def test_null_workspace():
workspace = Workspace(None)
assert workspace.root_uri is None
assert workspace.root_path is None
|