9
9
List ,
10
10
NamedTuple ,
11
11
Optional ,
12
+ Protocol ,
12
13
TypeVar ,
13
14
Union ,
14
15
cast ,
17
18
import attrs
18
19
from lsprotocol .types import (
19
20
AnnotatedTextEdit ,
21
+ CallHierarchyPrepareParams ,
20
22
CodeActionParams ,
23
+ ColorPresentationParams ,
21
24
CompletionParams ,
25
+ DefinitionParams ,
26
+ DocumentHighlightParams ,
27
+ DocumentOnTypeFormattingParams ,
22
28
Hover ,
29
+ HoverParams ,
30
+ InlayHintParams ,
31
+ InlineValueParams ,
23
32
Location ,
24
33
NotebookDocument ,
25
34
OptionalVersionedTextDocumentIdentifier ,
26
35
Position ,
36
+ PrepareRenameParams ,
27
37
Range ,
38
+ ReferenceParams ,
28
39
RenameParams ,
40
+ SemanticTokensRangeParams ,
41
+ SignatureHelpParams ,
29
42
TextDocumentEdit ,
43
+ TextDocumentIdentifier ,
30
44
TextDocumentPositionParams ,
31
45
TextEdit ,
32
46
)
@@ -236,16 +250,49 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
236
250
return index
237
251
238
252
239
- NotebookSupportedParams = Union [
240
- CodeActionParams ,
253
+ TextDocumentPositionParamsTypes = (
241
254
CompletionParams ,
242
255
RenameParams ,
243
256
TextDocumentPositionParams ,
244
- ]
257
+ )
245
258
T_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
+
246
275
T_params = TypeVar (
247
276
"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 ,
249
296
)
250
297
251
298
@@ -254,74 +301,98 @@ def cell_index(workspace: Workspace, cell_uri: str) -> int:
254
301
255
302
class ServerWrapper :
256
303
def __init__ (self , server : LanguageServer ):
257
- self .server = server
304
+ self ._wrapped = server
258
305
self .workspace = WorkspaceWrapper (server .workspace )
259
306
260
307
def __getattr__ (self , name : str ) -> Any :
261
- return getattr (self .server , name )
308
+ return getattr (self ._wrapped , name )
262
309
263
310
264
311
class WorkspaceWrapper :
265
312
def __init__ (self , workspace : Workspace ):
266
- self .workspace = workspace
313
+ self ._wrapped = workspace
267
314
268
315
def __getattr__ (self , name : str ) -> Any :
269
- return getattr (self .workspace , name )
316
+ return getattr (self ._wrapped , name )
270
317
271
318
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 )
273
320
if notebook is None :
274
- return self .workspace .get_text_document (doc_uri )
321
+ return self ._wrapped .get_text_document (doc_uri )
275
322
return TextDocument (uri = notebook ._document .uri , source = notebook .source )
276
323
277
324
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
+
278
386
def supports_notebooks (
279
387
f : Callable [[T_ls , T_params ], T ],
280
388
) -> Callable [[T_ls , T_params ], T ]:
281
389
def wrapped (ls : T_ls , params : T_params ) -> T :
282
390
notebook = notebook_coordinate_mapper (
283
391
ls .workspace , cell_uri = params .text_document .uri
284
392
)
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
302
394
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 )
325
396
326
397
return wrapped
327
398
0 commit comments