11"""Utility functions for handling notebook documents."""
22
3- from __future__ import annotations
4-
53from collections import defaultdict
64from typing import (
7- TYPE_CHECKING ,
85 Any ,
96 Callable ,
107 Dict ,
3330 TextDocumentPositionParams ,
3431 TextEdit ,
3532)
33+ from pygls .server import LanguageServer
3634from pygls .workspace import TextDocument , Workspace
3735
38- if TYPE_CHECKING :
39- from .server import JediLanguageServer
40-
4136
4237def notebook_coordinate_mapper (
4338 workspace : Workspace ,
@@ -247,6 +242,7 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
247242 RenameParams ,
248243 TextDocumentPositionParams ,
249244]
245+ T_ls = TypeVar ("T_ls" , bound = LanguageServer )
250246T_params = TypeVar (
251247 "T_params" ,
252248 bound = NotebookSupportedParams ,
@@ -257,7 +253,7 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
257253
258254
259255class ServerWrapper :
260- def __init__ (self , server : JediLanguageServer ):
256+ def __init__ (self , server : LanguageServer ):
261257 self .server = server
262258 self .workspace = WorkspaceWrapper (server .workspace )
263259
@@ -279,50 +275,44 @@ def get_text_document(self, doc_uri: str) -> TextDocument:
279275 return TextDocument (uri = notebook ._document .uri , source = notebook .source )
280276
281277
282- # TODO: Mismatched input/output function is needed due to how pygls server.feature() works.
283278def 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 :
292282 notebook = notebook_coordinate_mapper (
293- server .workspace , cell_uri = params .text_document .uri
283+ ls .workspace , cell_uri = params .text_document .uri
294284 )
295285 if notebook is not None :
296286 position = getattr (params , "position" , None )
297- if position is not None :
287+ if isinstance ( position , Position ) :
298288 notebook_position = notebook .notebook_position (
299289 params .text_document .uri , position
300290 )
301291 params = attrs .evolve (params , position = notebook_position ) # type: ignore[arg-type]
302292
303293 range = getattr (params , "range" , None )
304- if range is not None :
294+ if isinstance ( range , Range ) :
305295 notebook_range = notebook .notebook_range (
306296 params .text_document .uri , range
307297 )
308298 params = attrs .evolve (params , range = notebook_range ) # type: ignore[arg-type]
309299
310- server = cast ("JediLanguageServer" , ServerWrapper (server ))
300+ ls = cast (T_ls , ServerWrapper (ls ))
311301
312- result = f (server , params )
302+ result = f (ls , params )
313303
314304 if (
315305 isinstance (result , list )
316306 and result
317307 and isinstance (result [0 ], Location )
318308 ):
319309 return cast (
320- T , text_document_or_cell_locations (server .workspace , result )
310+ T , text_document_or_cell_locations (ls .workspace , result )
321311 )
322312
323313 if isinstance (result , Hover ) and result .range is not None :
324314 notebook_mapper = notebook_coordinate_mapper (
325- server .workspace , cell_uri = params .text_document .uri
315+ ls .workspace , cell_uri = params .text_document .uri
326316 )
327317 if notebook_mapper is None :
328318 return cast (T , result )
0 commit comments