5
5
from collections import defaultdict
6
6
from typing import (
7
7
TYPE_CHECKING ,
8
+ Any ,
8
9
Callable ,
9
10
Dict ,
10
11
Iterable ,
11
12
List ,
12
13
NamedTuple ,
13
14
Optional ,
14
- Tuple ,
15
15
TypeVar ,
16
16
Union ,
17
17
cast ,
33
33
TextDocumentPositionParams ,
34
34
TextEdit ,
35
35
)
36
- from pygls .uris import to_fs_path
37
36
from pygls .workspace import TextDocument , Workspace
38
37
39
38
if TYPE_CHECKING :
@@ -51,12 +50,6 @@ def notebook_coordinate_mapper(
51
50
)
52
51
if notebook_document is None :
53
52
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)
60
53
cells = [
61
54
workspace .text_documents [cell .document ]
62
55
for cell in notebook_document .cells
@@ -103,13 +96,6 @@ def __init__(
103
96
104
97
start_line = end_line
105
98
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
-
113
99
@property
114
100
def source (self ) -> str :
115
101
"""Concatenated notebook source."""
@@ -267,50 +253,63 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
267
253
)
268
254
269
255
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" )
282
257
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]
289
258
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 )
296
263
297
- return document , params
264
+ def __getattr__ (self , name : str ) -> Any :
265
+ return getattr (self .server , name )
298
266
299
267
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
301
287
288
+ server = SERVER
302
289
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
312
294
)
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 )
314
313
315
314
if (
316
315
isinstance (result , list )
@@ -335,3 +334,17 @@ def wrapped(server: JediLanguageServer, params: T_params) -> T:
335
334
return result
336
335
337
336
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