Skip to content

Commit 3e0b496

Browse files
committed
working
1 parent 9244d5a commit 3e0b496

File tree

4 files changed

+551
-882
lines changed

4 files changed

+551
-882
lines changed

jedi_language_server/notebook_utils.py

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from collections import defaultdict
66
from typing import (
77
TYPE_CHECKING,
8+
Any,
89
Callable,
910
Dict,
1011
Iterable,
1112
List,
1213
NamedTuple,
1314
Optional,
14-
Tuple,
1515
TypeVar,
1616
Union,
1717
cast,
@@ -33,7 +33,6 @@
3333
TextDocumentPositionParams,
3434
TextEdit,
3535
)
36-
from pygls.uris import to_fs_path
3736
from pygls.workspace import TextDocument, Workspace
3837

3938
if TYPE_CHECKING:
@@ -51,12 +50,6 @@ def notebook_coordinate_mapper(
5150
)
5251
if notebook_document is None:
5352
return None
54-
# msg = "Notebook document not found for"
55-
# if notebook_uri is not None:
56-
# msg += f" notebook_uri={notebook_uri}"
57-
# if cell_uri is not None:
58-
# msg += f" cell_uri={cell_uri}"
59-
# raise ValueError(msg)
6053
cells = [
6154
workspace.text_documents[cell.document]
6255
for cell in notebook_document.cells
@@ -103,13 +96,6 @@ def __init__(
10396

10497
start_line = end_line
10598

106-
@property
107-
def path(self) -> str:
108-
path = to_fs_path(self._document.uri)
109-
if path is None:
110-
raise ValueError("Could not convert notebook URI to path")
111-
return cast(str, path)
112-
11399
@property
114100
def source(self) -> str:
115101
"""Concatenated notebook source."""
@@ -267,50 +253,63 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
267253
)
268254

269255

270-
def notebook_text_document_and_params(
271-
workspace: Workspace,
272-
params: T_params,
273-
) -> Tuple[TextDocument, T_params]:
274-
notebook = notebook_coordinate_mapper(
275-
workspace, cell_uri=params.text_document.uri
276-
)
277-
if notebook is None:
278-
raise ValueError(
279-
f"Notebook not found with cell URI: {params.text_document.uri}"
280-
)
281-
document = TextDocument(uri=notebook._document.uri, source=notebook.source)
256+
T = TypeVar("T")
282257

283-
position = getattr(params, "position", None)
284-
if position is not None:
285-
notebook_position = notebook.notebook_position(
286-
params.text_document.uri, position
287-
)
288-
params = attrs.evolve(params, position=notebook_position) # type: ignore[arg-type]
289258

290-
range = getattr(params, "range", None)
291-
if range is not None:
292-
notebook_range = notebook.notebook_range(
293-
params.text_document.uri, range
294-
)
295-
params = attrs.evolve(params, range=notebook_range) # type: ignore[arg-type]
259+
class ServerWrapper:
260+
def __init__(self, server: JediLanguageServer):
261+
self.server = server
262+
self.workspace = WorkspaceWrapper(server.workspace)
296263

297-
return document, params
264+
def __getattr__(self, name: str) -> Any:
265+
return getattr(self.server, name)
298266

299267

300-
T = TypeVar("T")
268+
class WorkspaceWrapper:
269+
def __init__(self, workspace: Workspace):
270+
self.workspace = workspace
271+
272+
def __getattr__(self, name: str) -> Any:
273+
return getattr(self.workspace, name)
274+
275+
def get_text_document(self, doc_uri: str) -> TextDocument:
276+
notebook = notebook_coordinate_mapper(self.workspace, cell_uri=doc_uri)
277+
if notebook is None:
278+
return self.workspace.get_text_document(doc_uri)
279+
return TextDocument(uri=notebook._document.uri, source=notebook.source)
280+
281+
282+
# TODO: Mismatched input/output function is needed due to how pygls server.feature() works.
283+
def supports_notebooks(
284+
f: Callable[[JediLanguageServer, T_params], T],
285+
) -> Callable[[T_params], T]:
286+
from .server import SERVER
301287

288+
server = SERVER
302289

303-
def with_notebook_support(
304-
f: Callable[
305-
[JediLanguageServer, T_params, Optional[TextDocument]],
306-
T,
307-
],
308-
) -> Callable[[JediLanguageServer, T_params], T]:
309-
def wrapped(server: JediLanguageServer, params: T_params) -> T:
310-
document, params = notebook_text_document_and_params(
311-
server.workspace, params
290+
def wrapped(params: T_params) -> T:
291+
nonlocal server
292+
notebook = notebook_coordinate_mapper(
293+
server.workspace, cell_uri=params.text_document.uri
312294
)
313-
result = f(server, params, document)
295+
if notebook is not None:
296+
position = getattr(params, "position", None)
297+
if position is not None:
298+
notebook_position = notebook.notebook_position(
299+
params.text_document.uri, position
300+
)
301+
params = attrs.evolve(params, position=notebook_position) # type: ignore[arg-type]
302+
303+
range = getattr(params, "range", None)
304+
if range is not None:
305+
notebook_range = notebook.notebook_range(
306+
params.text_document.uri, range
307+
)
308+
params = attrs.evolve(params, range=notebook_range) # type: ignore[arg-type]
309+
310+
server = cast("JediLanguageServer", ServerWrapper(server))
311+
312+
result = f(server, params)
314313

315314
if (
316315
isinstance(result, list)
@@ -335,3 +334,17 @@ def wrapped(server: JediLanguageServer, params: T_params) -> T:
335334
return result
336335

337336
return wrapped
337+
338+
339+
def cell_filename(
340+
workspace: Workspace,
341+
cell_uri: str,
342+
) -> str:
343+
notebook = notebook_coordinate_mapper(workspace, cell_uri=cell_uri)
344+
if notebook is None:
345+
raise ValueError(
346+
f"Notebook document not found for cell URI: {cell_uri}"
347+
)
348+
index = notebook.cell_index(cell_uri)
349+
assert index is not None
350+
return f"cell {index + 1}"

0 commit comments

Comments
 (0)