9
9
List ,
10
10
NamedTuple ,
11
11
Optional ,
12
- Protocol ,
13
12
TypeVar ,
14
13
Union ,
15
14
cast ,
40
39
SemanticTokensRangeParams ,
41
40
SignatureHelpParams ,
42
41
TextDocumentEdit ,
43
- TextDocumentIdentifier ,
44
42
TextDocumentPositionParams ,
45
43
TextEdit ,
46
44
)
@@ -239,62 +237,61 @@ def text_document_or_cell_locations(
239
237
return results if results else None
240
238
241
239
242
- def cell_index (workspace : Workspace , cell_uri : str ) -> int :
243
- notebook = notebook_coordinate_mapper (workspace , cell_uri = cell_uri )
244
- if notebook is None :
240
+ def cell_filename (
241
+ workspace : Workspace ,
242
+ cell_uri : str ,
243
+ ) -> str :
244
+ mapper = notebook_coordinate_mapper (workspace , cell_uri = cell_uri )
245
+ if mapper is None :
245
246
raise ValueError (
246
247
f"Notebook document not found for cell URI: { cell_uri } "
247
248
)
248
- index = notebook .cell_index (cell_uri )
249
+ index = mapper .cell_index (cell_uri )
249
250
assert index is not None
250
- return index
251
+ return f"cell { index + 1 } "
252
+
251
253
254
+ T_ls = TypeVar ("T_ls" , bound = LanguageServer )
252
255
253
- TextDocumentPositionParamsTypes = (
256
+ T_params = TypeVar (
257
+ "T_params" ,
258
+ CallHierarchyPrepareParams ,
259
+ CodeActionParams ,
260
+ ColorPresentationParams ,
254
261
CompletionParams ,
262
+ DefinitionParams ,
263
+ DocumentHighlightParams ,
264
+ DocumentOnTypeFormattingParams ,
265
+ HoverParams ,
266
+ InlayHintParams ,
267
+ InlineValueParams ,
268
+ PrepareRenameParams ,
269
+ ReferenceParams ,
255
270
RenameParams ,
271
+ SemanticTokensRangeParams ,
272
+ SignatureHelpParams ,
256
273
TextDocumentPositionParams ,
257
274
)
258
- T_ls = TypeVar ("T_ls" , bound = LanguageServer )
259
-
260
275
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
276
+ _TextDocumentCoordinatesParams = Union [
277
+ CallHierarchyPrepareParams ,
278
+ CodeActionParams ,
279
+ ColorPresentationParams ,
280
+ CompletionParams ,
281
+ DefinitionParams ,
282
+ DocumentHighlightParams ,
283
+ DocumentOnTypeFormattingParams ,
284
+ HoverParams ,
285
+ InlayHintParams ,
286
+ InlineValueParams ,
287
+ PrepareRenameParams ,
288
+ ReferenceParams ,
289
+ RenameParams ,
290
+ SemanticTokensRangeParams ,
291
+ SignatureHelpParams ,
292
+ TextDocumentPositionParams ,
273
293
]
274
294
275
- T_params = TypeVar (
276
- "T_params" ,
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 ,
296
- )
297
-
298
295
299
296
T = TypeVar ("T" )
300
297
@@ -322,61 +319,41 @@ def get_text_document(self, doc_uri: str) -> TextDocument:
322
319
return TextDocument (uri = notebook ._document .uri , source = notebook .source )
323
320
324
321
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
- ):
322
+ def _map_params_to_notebook (
323
+ notebook : NotebookCoordinateMapper , params : T_params
324
+ ) -> T_params :
325
+ if hasattr (params , "position" ):
342
326
notebook_position = notebook .notebook_position (
343
327
params .text_document .uri , params .position
344
328
)
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
- ):
329
+ # Ignore mypy error since it doesn't seem to narrow params via the hasattr.
330
+ params = attrs .evolve (params , position = notebook_position ) # type: ignore[call-arg]
331
+
332
+ if hasattr (params , "range" ):
357
333
notebook_range = notebook .notebook_range (
358
334
params .text_document .uri , params .range
359
335
)
360
- return cast (T_params , attrs .evolve (params , range = notebook_range ))
336
+ # Ignore mypy error since it doesn't seem to narrow params via the hasattr.
337
+ params = attrs .evolve (params , range = notebook_range ) # type: ignore[call-arg]
361
338
362
339
return params
363
340
364
341
365
- def _post (
342
+ def _map_result_to_cells (
366
343
workspace : Workspace ,
367
- notebook : Optional [NotebookCoordinateMapper ],
368
- params : _TextDocumentPositionOrRangeParams ,
344
+ mapper : Optional [NotebookCoordinateMapper ],
345
+ params : _TextDocumentCoordinatesParams ,
369
346
result : T ,
370
347
) -> T :
371
348
if isinstance (result , list ) and result and isinstance (result [0 ], Location ):
372
349
return cast (T , text_document_or_cell_locations (workspace , result ))
373
350
374
351
if (
375
- notebook is not None
352
+ mapper is not None
376
353
and isinstance (result , Hover )
377
354
and result .range is not None
378
355
):
379
- location = notebook .cell_range (result .range )
356
+ location = mapper .cell_range (result .range )
380
357
if location is not None and location .uri == params .text_document .uri :
381
358
return cast (T , attrs .evolve (result , range = location .range ))
382
359
@@ -387,25 +364,11 @@ def supports_notebooks(
387
364
f : Callable [[T_ls , T_params ], T ],
388
365
) -> Callable [[T_ls , T_params ], T ]:
389
366
def wrapped (ls : T_ls , params : T_params ) -> T :
390
- notebook = notebook_coordinate_mapper (
367
+ mapper = notebook_coordinate_mapper (
391
368
ls .workspace , cell_uri = params .text_document .uri
392
369
)
393
- params = _pre ( notebook , params ) if notebook else params
370
+ params = _map_params_to_notebook ( mapper , params ) if mapper else params
394
371
result = f (ls , params )
395
- return _post (ls .workspace , notebook , params , result )
372
+ return _map_result_to_cells (ls .workspace , mapper , params , result )
396
373
397
374
return wrapped
398
-
399
-
400
- def cell_filename (
401
- workspace : Workspace ,
402
- cell_uri : str ,
403
- ) -> str :
404
- notebook = notebook_coordinate_mapper (workspace , cell_uri = cell_uri )
405
- if notebook is None :
406
- raise ValueError (
407
- f"Notebook document not found for cell URI: { cell_uri } "
408
- )
409
- index = notebook .cell_index (cell_uri )
410
- assert index is not None
411
- return f"cell { index + 1 } "
0 commit comments