Skip to content

Commit ff0d22b

Browse files
committed
finalize typing
1 parent 281b720 commit ff0d22b

File tree

1 file changed

+60
-97
lines changed

1 file changed

+60
-97
lines changed

jedi_language_server/notebook_utils.py

Lines changed: 60 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
List,
1010
NamedTuple,
1111
Optional,
12-
Protocol,
1312
TypeVar,
1413
Union,
1514
cast,
@@ -40,7 +39,6 @@
4039
SemanticTokensRangeParams,
4140
SignatureHelpParams,
4241
TextDocumentEdit,
43-
TextDocumentIdentifier,
4442
TextDocumentPositionParams,
4543
TextEdit,
4644
)
@@ -239,62 +237,61 @@ def text_document_or_cell_locations(
239237
return results if results else None
240238

241239

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:
245246
raise ValueError(
246247
f"Notebook document not found for cell URI: {cell_uri}"
247248
)
248-
index = notebook.cell_index(cell_uri)
249+
index = mapper.cell_index(cell_uri)
249250
assert index is not None
250-
return index
251+
return f"cell {index + 1}"
252+
251253

254+
T_ls = TypeVar("T_ls", bound=LanguageServer)
252255

253-
TextDocumentPositionParamsTypes = (
256+
T_params = TypeVar(
257+
"T_params",
258+
CallHierarchyPrepareParams,
259+
CodeActionParams,
260+
ColorPresentationParams,
254261
CompletionParams,
262+
DefinitionParams,
263+
DocumentHighlightParams,
264+
DocumentOnTypeFormattingParams,
265+
HoverParams,
266+
InlayHintParams,
267+
InlineValueParams,
268+
PrepareRenameParams,
269+
ReferenceParams,
255270
RenameParams,
271+
SemanticTokensRangeParams,
272+
SignatureHelpParams,
256273
TextDocumentPositionParams,
257274
)
258-
T_ls = TypeVar("T_ls", bound=LanguageServer)
259-
260275

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,
273293
]
274294

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-
298295

299296
T = TypeVar("T")
300297

@@ -322,61 +319,41 @@ def get_text_document(self, doc_uri: str) -> TextDocument:
322319
return TextDocument(uri=notebook._document.uri, source=notebook.source)
323320

324321

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"):
342326
notebook_position = notebook.notebook_position(
343327
params.text_document.uri, params.position
344328
)
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"):
357333
notebook_range = notebook.notebook_range(
358334
params.text_document.uri, params.range
359335
)
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]
361338

362339
return params
363340

364341

365-
def _post(
342+
def _map_result_to_cells(
366343
workspace: Workspace,
367-
notebook: Optional[NotebookCoordinateMapper],
368-
params: _TextDocumentPositionOrRangeParams,
344+
mapper: Optional[NotebookCoordinateMapper],
345+
params: _TextDocumentCoordinatesParams,
369346
result: T,
370347
) -> T:
371348
if isinstance(result, list) and result and isinstance(result[0], Location):
372349
return cast(T, text_document_or_cell_locations(workspace, result))
373350

374351
if (
375-
notebook is not None
352+
mapper is not None
376353
and isinstance(result, Hover)
377354
and result.range is not None
378355
):
379-
location = notebook.cell_range(result.range)
356+
location = mapper.cell_range(result.range)
380357
if location is not None and location.uri == params.text_document.uri:
381358
return cast(T, attrs.evolve(result, range=location.range))
382359

@@ -387,25 +364,11 @@ def supports_notebooks(
387364
f: Callable[[T_ls, T_params], T],
388365
) -> Callable[[T_ls, T_params], T]:
389366
def wrapped(ls: T_ls, params: T_params) -> T:
390-
notebook = notebook_coordinate_mapper(
367+
mapper = notebook_coordinate_mapper(
391368
ls.workspace, cell_uri=params.text_document.uri
392369
)
393-
params = _pre(notebook, params) if notebook else params
370+
params = _map_params_to_notebook(mapper, params) if mapper else params
394371
result = f(ls, params)
395-
return _post(ls.workspace, notebook, params, result)
372+
return _map_result_to_cells(ls.workspace, mapper, params, result)
396373

397374
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

Comments
 (0)