|
1 |
| -from typing import List, Optional, Tuple, TypeVar, Union |
| 1 | +from typing import List, Optional |
2 | 2 |
|
3 |
| -import attrs |
4 | 3 | from lsprotocol.types import (
|
5 | 4 | TEXT_DOCUMENT_CODE_ACTION,
|
6 | 5 | TEXT_DOCUMENT_COMPLETION,
|
|
26 | 25 | WorkspaceEdit,
|
27 | 26 | )
|
28 | 27 | from pygls.workspace import Workspace
|
29 |
| -from pygls.workspace.text_document import TextDocument |
30 | 28 |
|
31 | 29 | from . import notebook_utils, server_utils
|
32 | 30 | from .server import SERVER, JediLanguageServer
|
33 | 31 |
|
34 |
| -T_params = TypeVar( |
35 |
| - "T_params", |
36 |
| - bound=Union[ |
37 |
| - CodeActionParams, |
38 |
| - CompletionParams, |
39 |
| - RenameParams, |
40 |
| - TextDocumentPositionParams, |
41 |
| - ], |
42 |
| -) |
43 |
| - |
44 | 32 |
|
45 | 33 | @SERVER.notebook_feature(TEXT_DOCUMENT_COMPLETION)
|
46 | 34 | def completion(
|
47 | 35 | server: JediLanguageServer, params: CompletionParams
|
48 | 36 | ) -> Optional[CompletionList]:
|
49 |
| - document, params = _notebook_text_document_and_params( |
50 |
| - server.workspace, params |
| 37 | + return notebook_utils.with_notebook_support(server_utils.completion)( |
| 38 | + server, params |
51 | 39 | )
|
52 |
| - return server_utils.completion(server, params, document) |
53 | 40 |
|
54 | 41 |
|
55 | 42 | @SERVER.notebook_feature(TEXT_DOCUMENT_SIGNATURE_HELP)
|
56 | 43 | def signature_help(
|
57 | 44 | server: JediLanguageServer, params: TextDocumentPositionParams
|
58 | 45 | ) -> Optional[SignatureHelp]:
|
59 |
| - document, params = _notebook_text_document_and_params( |
60 |
| - server.workspace, params |
| 46 | + return notebook_utils.with_notebook_support(server_utils.signature_help)( |
| 47 | + server, params |
61 | 48 | )
|
62 |
| - return server_utils.signature_help(server, params, document) |
63 | 49 |
|
64 | 50 |
|
65 | 51 | @SERVER.notebook_feature(TEXT_DOCUMENT_DECLARATION)
|
66 | 52 | def declaration(
|
67 | 53 | server: JediLanguageServer, params: TextDocumentPositionParams
|
68 | 54 | ) -> Optional[List[Location]]:
|
69 |
| - document, params = _notebook_text_document_and_params( |
70 |
| - server.workspace, params |
71 |
| - ) |
72 |
| - locations = server_utils.declaration(server, params, document) |
73 |
| - return notebook_utils.text_document_or_cell_locations( |
74 |
| - server.workspace, locations |
| 55 | + return notebook_utils.with_notebook_support(server_utils.declaration)( |
| 56 | + server, params |
75 | 57 | )
|
76 | 58 |
|
77 | 59 |
|
78 | 60 | @SERVER.notebook_feature(TEXT_DOCUMENT_DEFINITION)
|
79 | 61 | def definition(
|
80 | 62 | server: JediLanguageServer, params: TextDocumentPositionParams
|
81 | 63 | ) -> Optional[List[Location]]:
|
82 |
| - document, params = _notebook_text_document_and_params( |
83 |
| - server.workspace, params |
84 |
| - ) |
85 |
| - locations = server_utils.definition(server, params, document) |
86 |
| - return notebook_utils.text_document_or_cell_locations( |
87 |
| - server.workspace, locations |
| 64 | + return notebook_utils.with_notebook_support(server_utils.definition)( |
| 65 | + server, params |
88 | 66 | )
|
89 | 67 |
|
90 | 68 |
|
91 | 69 | @SERVER.notebook_feature(TEXT_DOCUMENT_TYPE_DEFINITION)
|
92 | 70 | def type_definition(
|
93 | 71 | server: JediLanguageServer, params: TextDocumentPositionParams
|
94 | 72 | ) -> Optional[List[Location]]:
|
95 |
| - document, params = _notebook_text_document_and_params( |
96 |
| - server.workspace, params |
97 |
| - ) |
98 |
| - locations = server_utils.type_definition(server, params, document) |
99 |
| - return notebook_utils.text_document_or_cell_locations( |
100 |
| - server.workspace, locations |
| 73 | + return notebook_utils.with_notebook_support(server_utils.type_definition)( |
| 74 | + server, params |
101 | 75 | )
|
102 | 76 |
|
103 | 77 |
|
104 | 78 | def hover(
|
105 | 79 | server: JediLanguageServer, params: TextDocumentPositionParams
|
106 | 80 | ) -> Optional[Hover]:
|
107 |
| - document, params = _notebook_text_document_and_params( |
108 |
| - server.workspace, params |
| 81 | + return notebook_utils.with_notebook_support(server_utils.hover)( |
| 82 | + server, params |
109 | 83 | )
|
110 |
| - # TODO: Can we clean this up? |
111 |
| - # Maybe have a single notebook_to_cell and vice versa function that |
112 |
| - # can handle most needed lsprotocol types? |
113 |
| - hover = server_utils.hover(server, params, document) |
114 |
| - if hover is None or hover.range is None: |
115 |
| - return hover |
116 |
| - notebook_mapper = notebook_utils.notebook_coordinate_mapper( |
117 |
| - server.workspace, cell_uri=params.text_document.uri |
118 |
| - ) |
119 |
| - if notebook_mapper is None: |
120 |
| - return hover |
121 |
| - location = notebook_mapper.cell_range(hover.range) |
122 |
| - if location is None or location.uri != params.text_document.uri: |
123 |
| - return hover |
124 |
| - return attrs.evolve(hover, range=location.range) |
125 | 84 |
|
126 | 85 |
|
127 | 86 | @SERVER.notebook_feature(TEXT_DOCUMENT_REFERENCES)
|
128 | 87 | def references(
|
129 | 88 | server: JediLanguageServer, params: TextDocumentPositionParams
|
130 | 89 | ) -> Optional[List[Location]]:
|
131 |
| - document, params = _notebook_text_document_and_params( |
132 |
| - server.workspace, params |
133 |
| - ) |
134 |
| - locations = server_utils.references(server, params, document) |
135 |
| - return notebook_utils.text_document_or_cell_locations( |
136 |
| - server.workspace, locations |
| 90 | + return notebook_utils.with_notebook_support(server_utils.references)( |
| 91 | + server, params |
137 | 92 | )
|
138 | 93 |
|
139 | 94 |
|
140 | 95 | @SERVER.notebook_feature(TEXT_DOCUMENT_RENAME)
|
141 | 96 | def rename(
|
142 | 97 | server: JediLanguageServer, params: RenameParams
|
143 | 98 | ) -> Optional[WorkspaceEdit]:
|
144 |
| - document, params = _notebook_text_document_and_params( |
145 |
| - server.workspace, params |
| 99 | + return notebook_utils.with_notebook_support(server_utils.rename)( |
| 100 | + server, params |
146 | 101 | )
|
147 |
| - return server_utils.rename(server, params, document) |
148 | 102 |
|
149 | 103 |
|
150 | 104 | @SERVER.notebook_feature(TEXT_DOCUMENT_CODE_ACTION)
|
151 | 105 | def code_action(
|
152 | 106 | server: JediLanguageServer,
|
153 | 107 | params: CodeActionParams,
|
154 | 108 | ) -> Optional[List[CodeAction]]:
|
155 |
| - document, params = _notebook_text_document_and_params( |
156 |
| - server.workspace, params |
| 109 | + return notebook_utils.with_notebook_support(server_utils.code_action)( |
| 110 | + server, params |
157 | 111 | )
|
158 |
| - return server_utils.code_action(server, params, document) |
159 | 112 |
|
160 | 113 |
|
161 | 114 | # NOTEBOOK_DOCUMENT_DID_SAVE
|
@@ -244,36 +197,6 @@ def did_close_notebook_document_default(
|
244 | 197 | """Actions run on notebookDocument/didClose: default."""
|
245 | 198 |
|
246 | 199 |
|
247 |
| -def _notebook_text_document_and_params( |
248 |
| - workspace: Workspace, |
249 |
| - params: T_params, |
250 |
| -) -> Tuple[TextDocument, T_params]: |
251 |
| - notebook = notebook_utils.notebook_coordinate_mapper( |
252 |
| - workspace, cell_uri=params.text_document.uri |
253 |
| - ) |
254 |
| - if notebook is None: |
255 |
| - raise ValueError( |
256 |
| - f"Notebook not found with cell URI: {params.text_document.uri}" |
257 |
| - ) |
258 |
| - document = TextDocument(uri=notebook._document.uri, source=notebook.source) |
259 |
| - |
260 |
| - position = getattr(params, "position", None) |
261 |
| - if position is not None: |
262 |
| - notebook_position = notebook.notebook_position( |
263 |
| - params.text_document.uri, position |
264 |
| - ) |
265 |
| - params = attrs.evolve(params, position=notebook_position) # type: ignore[arg-type] |
266 |
| - |
267 |
| - range = getattr(params, "range", None) |
268 |
| - if range is not None: |
269 |
| - notebook_range = notebook.notebook_range( |
270 |
| - params.text_document.uri, range |
271 |
| - ) |
272 |
| - params = attrs.evolve(params, range=notebook_range) # type: ignore[arg-type] |
273 |
| - |
274 |
| - return document, params |
275 |
| - |
276 |
| - |
277 | 200 | def _cell_filename(
|
278 | 201 | workspace: Workspace,
|
279 | 202 | cell_uri: str,
|
|
0 commit comments