99 List ,
1010 NamedTuple ,
1111 Optional ,
12+ Protocol ,
1213 TypeVar ,
1314 Union ,
1415 cast ,
1718import attrs
1819from lsprotocol .types import (
1920 AnnotatedTextEdit ,
21+ CallHierarchyPrepareParams ,
2022 CodeActionParams ,
23+ ColorPresentationParams ,
2124 CompletionParams ,
25+ DefinitionParams ,
26+ DocumentHighlightParams ,
27+ DocumentOnTypeFormattingParams ,
2228 Hover ,
29+ HoverParams ,
30+ InlayHintParams ,
31+ InlineValueParams ,
2332 Location ,
2433 NotebookDocument ,
2534 OptionalVersionedTextDocumentIdentifier ,
2635 Position ,
36+ PrepareRenameParams ,
2737 Range ,
38+ ReferenceParams ,
2839 RenameParams ,
40+ SemanticTokensRangeParams ,
41+ SignatureHelpParams ,
2942 TextDocumentEdit ,
43+ TextDocumentIdentifier ,
3044 TextDocumentPositionParams ,
3145 TextEdit ,
3246)
@@ -236,16 +250,49 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
236250 return index
237251
238252
239- NotebookSupportedParams = Union [
240- CodeActionParams ,
253+ TextDocumentPositionParamsTypes = (
241254 CompletionParams ,
242255 RenameParams ,
243256 TextDocumentPositionParams ,
244- ]
257+ )
245258T_ls = TypeVar ("T_ls" , bound = LanguageServer )
259+
260+
261+ class _TextDocumentPositionParams (Protocol ):
262+ text_document : TextDocumentIdentifier
263+ position : Position
264+
265+
266+ class _TextDocumentRangeParams (Protocol ):
267+ text_document : TextDocumentIdentifier
268+ range : Range
269+
270+
271+ _TextDocumentPositionOrRangeParams = Union [
272+ _TextDocumentPositionParams , _TextDocumentRangeParams
273+ ]
274+
246275T_params = TypeVar (
247276 "T_params" ,
248- bound = NotebookSupportedParams ,
277+ # # Position
278+ # CallHierarchyPrepareParams,
279+ # CompletionParams,
280+ # DefinitionParams,
281+ # DocumentHighlightParams,
282+ # DocumentOnTypeFormattingParams,
283+ # HoverParams,
284+ # PrepareRenameParams,
285+ # ReferenceParams,
286+ # RenameParams,
287+ # SignatureHelpParams,
288+ # TextDocumentPositionParams,
289+ # # Range
290+ # CodeActionParams,
291+ # ColorPresentationParams,
292+ # InlayHintParams,
293+ # InlineValueParams,
294+ # SemanticTokensRangeParams,
295+ bound = _TextDocumentPositionOrRangeParams ,
249296)
250297
251298
@@ -254,74 +301,98 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
254301
255302class ServerWrapper :
256303 def __init__ (self , server : LanguageServer ):
257- self .server = server
304+ self ._wrapped = server
258305 self .workspace = WorkspaceWrapper (server .workspace )
259306
260307 def __getattr__ (self , name : str ) -> Any :
261- return getattr (self .server , name )
308+ return getattr (self ._wrapped , name )
262309
263310
264311class WorkspaceWrapper :
265312 def __init__ (self , workspace : Workspace ):
266- self .workspace = workspace
313+ self ._wrapped = workspace
267314
268315 def __getattr__ (self , name : str ) -> Any :
269- return getattr (self .workspace , name )
316+ return getattr (self ._wrapped , name )
270317
271318 def get_text_document (self , doc_uri : str ) -> TextDocument :
272- notebook = notebook_coordinate_mapper (self .workspace , cell_uri = doc_uri )
319+ notebook = notebook_coordinate_mapper (self ._wrapped , cell_uri = doc_uri )
273320 if notebook is None :
274- return self .workspace .get_text_document (doc_uri )
321+ return self ._wrapped .get_text_document (doc_uri )
275322 return TextDocument (uri = notebook ._document .uri , source = notebook .source )
276323
277324
325+ def _pre (notebook : NotebookCoordinateMapper , params : T_params ) -> T_params :
326+ if isinstance (
327+ params ,
328+ (
329+ CallHierarchyPrepareParams ,
330+ CompletionParams ,
331+ DefinitionParams ,
332+ DocumentHighlightParams ,
333+ DocumentOnTypeFormattingParams ,
334+ HoverParams ,
335+ PrepareRenameParams ,
336+ ReferenceParams ,
337+ RenameParams ,
338+ SignatureHelpParams ,
339+ TextDocumentPositionParams ,
340+ ),
341+ ):
342+ notebook_position = notebook .notebook_position (
343+ params .text_document .uri , params .position
344+ )
345+ return cast (T_params , attrs .evolve (params , position = notebook_position ))
346+
347+ if isinstance (
348+ params ,
349+ (
350+ CodeActionParams ,
351+ ColorPresentationParams ,
352+ InlayHintParams ,
353+ InlineValueParams ,
354+ SemanticTokensRangeParams ,
355+ ),
356+ ):
357+ notebook_range = notebook .notebook_range (
358+ params .text_document .uri , params .range
359+ )
360+ return cast (T_params , attrs .evolve (params , range = notebook_range ))
361+
362+ return params
363+
364+
365+ def _post (
366+ workspace : Workspace ,
367+ notebook : Optional [NotebookCoordinateMapper ],
368+ params : _TextDocumentPositionOrRangeParams ,
369+ result : T ,
370+ ) -> T :
371+ if isinstance (result , list ) and result and isinstance (result [0 ], Location ):
372+ return cast (T , text_document_or_cell_locations (workspace , result ))
373+
374+ if (
375+ notebook is not None
376+ and isinstance (result , Hover )
377+ and result .range is not None
378+ ):
379+ location = notebook .cell_range (result .range )
380+ if location is not None and location .uri == params .text_document .uri :
381+ return cast (T , attrs .evolve (result , range = location .range ))
382+
383+ return result
384+
385+
278386def supports_notebooks (
279387 f : Callable [[T_ls , T_params ], T ],
280388) -> Callable [[T_ls , T_params ], T ]:
281389 def wrapped (ls : T_ls , params : T_params ) -> T :
282390 notebook = notebook_coordinate_mapper (
283391 ls .workspace , cell_uri = params .text_document .uri
284392 )
285- if notebook is not None :
286- position = getattr (params , "position" , None )
287- if isinstance (position , Position ):
288- notebook_position = notebook .notebook_position (
289- params .text_document .uri , position
290- )
291- params = attrs .evolve (params , position = notebook_position ) # type: ignore[arg-type]
292-
293- range = getattr (params , "range" , None )
294- if isinstance (range , Range ):
295- notebook_range = notebook .notebook_range (
296- params .text_document .uri , range
297- )
298- params = attrs .evolve (params , range = notebook_range ) # type: ignore[arg-type]
299-
300- ls = cast (T_ls , ServerWrapper (ls ))
301-
393+ params = _pre (notebook , params ) if notebook else params
302394 result = f (ls , params )
303-
304- if (
305- isinstance (result , list )
306- and result
307- and isinstance (result [0 ], Location )
308- ):
309- return cast (
310- T , text_document_or_cell_locations (ls .workspace , result )
311- )
312-
313- if isinstance (result , Hover ) and result .range is not None :
314- notebook_mapper = notebook_coordinate_mapper (
315- ls .workspace , cell_uri = params .text_document .uri
316- )
317- if notebook_mapper is None :
318- return cast (T , result )
319- location = notebook_mapper .cell_range (result .range )
320- if location is None or location .uri != params .text_document .uri :
321- return cast (T , result )
322- return cast (T , attrs .evolve (result , range = location .range ))
323-
324- return result
395+ return _post (ls .workspace , notebook , params , result )
325396
326397 return wrapped
327398
0 commit comments