1
1
"""Utility functions for handling notebook documents."""
2
2
3
- from __future__ import annotations
4
-
5
3
from collections import defaultdict
6
4
from typing import (
7
- TYPE_CHECKING ,
8
5
Any ,
9
6
Callable ,
10
7
Dict ,
33
30
TextDocumentPositionParams ,
34
31
TextEdit ,
35
32
)
33
+ from pygls .server import LanguageServer
36
34
from pygls .workspace import TextDocument , Workspace
37
35
38
- if TYPE_CHECKING :
39
- from .server import JediLanguageServer
40
-
41
36
42
37
def notebook_coordinate_mapper (
43
38
workspace : Workspace ,
@@ -247,6 +242,7 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
247
242
RenameParams ,
248
243
TextDocumentPositionParams ,
249
244
]
245
+ T_ls = TypeVar ("T_ls" , bound = LanguageServer )
250
246
T_params = TypeVar (
251
247
"T_params" ,
252
248
bound = NotebookSupportedParams ,
@@ -257,7 +253,7 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
257
253
258
254
259
255
class ServerWrapper :
260
- def __init__ (self , server : JediLanguageServer ):
256
+ def __init__ (self , server : LanguageServer ):
261
257
self .server = server
262
258
self .workspace = WorkspaceWrapper (server .workspace )
263
259
@@ -279,50 +275,44 @@ def get_text_document(self, doc_uri: str) -> TextDocument:
279
275
return TextDocument (uri = notebook ._document .uri , source = notebook .source )
280
276
281
277
282
- # TODO: Mismatched input/output function is needed due to how pygls server.feature() works.
283
278
def supports_notebooks (
284
- f : Callable [[JediLanguageServer , T_params ], T ],
285
- ) -> Callable [[T_params ], T ]:
286
- from .server import SERVER
287
-
288
- server = SERVER
289
-
290
- def wrapped (params : T_params ) -> T :
291
- nonlocal server
279
+ f : Callable [[T_ls , T_params ], T ],
280
+ ) -> Callable [[T_ls , T_params ], T ]:
281
+ def wrapped (ls : T_ls , params : T_params ) -> T :
292
282
notebook = notebook_coordinate_mapper (
293
- server .workspace , cell_uri = params .text_document .uri
283
+ ls .workspace , cell_uri = params .text_document .uri
294
284
)
295
285
if notebook is not None :
296
286
position = getattr (params , "position" , None )
297
- if position is not None :
287
+ if isinstance ( position , Position ) :
298
288
notebook_position = notebook .notebook_position (
299
289
params .text_document .uri , position
300
290
)
301
291
params = attrs .evolve (params , position = notebook_position ) # type: ignore[arg-type]
302
292
303
293
range = getattr (params , "range" , None )
304
- if range is not None :
294
+ if isinstance ( range , Range ) :
305
295
notebook_range = notebook .notebook_range (
306
296
params .text_document .uri , range
307
297
)
308
298
params = attrs .evolve (params , range = notebook_range ) # type: ignore[arg-type]
309
299
310
- server = cast ("JediLanguageServer" , ServerWrapper (server ))
300
+ ls = cast (T_ls , ServerWrapper (ls ))
311
301
312
- result = f (server , params )
302
+ result = f (ls , params )
313
303
314
304
if (
315
305
isinstance (result , list )
316
306
and result
317
307
and isinstance (result [0 ], Location )
318
308
):
319
309
return cast (
320
- T , text_document_or_cell_locations (server .workspace , result )
310
+ T , text_document_or_cell_locations (ls .workspace , result )
321
311
)
322
312
323
313
if isinstance (result , Hover ) and result .range is not None :
324
314
notebook_mapper = notebook_coordinate_mapper (
325
- server .workspace , cell_uri = params .text_document .uri
315
+ ls .workspace , cell_uri = params .text_document .uri
326
316
)
327
317
if notebook_mapper is None :
328
318
return cast (T , result )
0 commit comments