Skip to content

Commit 4ed862f

Browse files
committed
wip refactor
1 parent 936f4a9 commit 4ed862f

File tree

4 files changed

+887
-595
lines changed

4 files changed

+887
-595
lines changed

jedi_language_server/notebook_utils.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
NamedTuple,
99
Optional,
1010
Union,
11+
cast,
1112
)
1213

1314
import attrs
@@ -21,6 +22,7 @@
2122
TextDocumentEdit,
2223
TextEdit,
2324
)
25+
from pygls.uris import to_fs_path
2426
from pygls.workspace import TextDocument, Workspace
2527

2628

@@ -35,6 +37,12 @@ def notebook_coordinate_mapper(
3537
)
3638
if notebook_document is None:
3739
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)
3846
cells = [
3947
workspace.text_documents[cell.document]
4048
for cell in notebook_document.cells
@@ -81,6 +89,13 @@ def __init__(
8189

8290
start_line = end_line
8391

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+
8499
@property
85100
def source(self) -> str:
86101
"""Concatenated notebook source."""
@@ -194,9 +209,12 @@ def cell_text_document_edits(
194209

195210

196211
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]]:
199214
"""Convert concatenated notebook locations to cell locations, leaving text document locations as-is."""
215+
if locations is None:
216+
return None
217+
200218
results = []
201219
for location in locations:
202220
mapper = notebook_coordinate_mapper(
@@ -208,4 +226,16 @@ def text_document_or_cell_locations(
208226
location = cell_location
209227

210228
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

Comments
 (0)