8
8
NamedTuple ,
9
9
Optional ,
10
10
Union ,
11
+ cast ,
11
12
)
12
13
13
14
import attrs
21
22
TextDocumentEdit ,
22
23
TextEdit ,
23
24
)
25
+ from pygls .uris import to_fs_path
24
26
from pygls .workspace import TextDocument , Workspace
25
27
26
28
@@ -35,6 +37,12 @@ def notebook_coordinate_mapper(
35
37
)
36
38
if notebook_document is None :
37
39
return None
40
+ # msg = "Notebook document not found for"
41
+ # if notebook_uri is not None:
42
+ # msg += f" notebook_uri={notebook_uri}"
43
+ # if cell_uri is not None:
44
+ # msg += f" cell_uri={cell_uri}"
45
+ # raise ValueError(msg)
38
46
cells = [
39
47
workspace .text_documents [cell .document ]
40
48
for cell in notebook_document .cells
@@ -81,6 +89,13 @@ def __init__(
81
89
82
90
start_line = end_line
83
91
92
+ @property
93
+ def path (self ) -> str :
94
+ path = to_fs_path (self ._document .uri )
95
+ if path is None :
96
+ raise ValueError ("Could not convert notebook URI to path" )
97
+ return cast (str , path )
98
+
84
99
@property
85
100
def source (self ) -> str :
86
101
"""Concatenated notebook source."""
@@ -194,9 +209,12 @@ def cell_text_document_edits(
194
209
195
210
196
211
def text_document_or_cell_locations (
197
- workspace : Workspace , locations : List [Location ]
198
- ) -> List [Location ]:
212
+ workspace : Workspace , locations : Optional [ List [Location ] ]
213
+ ) -> Optional [ List [Location ] ]:
199
214
"""Convert concatenated notebook locations to cell locations, leaving text document locations as-is."""
215
+ if locations is None :
216
+ return None
217
+
200
218
results = []
201
219
for location in locations :
202
220
mapper = notebook_coordinate_mapper (
@@ -208,4 +226,16 @@ def text_document_or_cell_locations(
208
226
location = cell_location
209
227
210
228
results .append (location )
211
- return results
229
+
230
+ return results if results else None
231
+
232
+
233
+ def cell_index (workspace : Workspace , cell_uri : str ) -> int :
234
+ notebook = notebook_coordinate_mapper (workspace , cell_uri = cell_uri )
235
+ if notebook is None :
236
+ raise ValueError (
237
+ f"Notebook document not found for cell URI: { cell_uri } "
238
+ )
239
+ index = notebook .cell_index (cell_uri )
240
+ assert index is not None
241
+ return index
0 commit comments