1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
from typing import (
Literal,
TypedDict,
Union,
TypeVar,
Dict,
Optional,
List,
cast,
NotRequired,
TYPE_CHECKING,
)
from collections.abc import Callable, Iterable, Mapping
from debputy.lsp.diagnostics import DiagnosticData
from debputy.util import _warn
if TYPE_CHECKING:
import lsprotocol.types as types
from debputy.linting.lint_util import LintState
else:
import debputy.lsprotocol.types as types
try:
from debputy.lsp.vendoring._deb822_repro.locatable import (
Position as TEPosition,
Range as TERange,
)
from pygls.server import LanguageServer
from pygls.workspace import TextDocument
from debputy.lsp.debputy_ls import DebputyLanguageServer
except ImportError:
pass
CodeActionName = Literal[
"correct-text",
"remove-line",
"remove-range",
"insert-text-on-line-after-diagnostic",
]
class CorrectTextCodeAction(TypedDict):
code_action: Literal["correct-text"]
correct_value: str
proposed_title: NotRequired[str]
class InsertTextOnLineAfterDiagnosticCodeAction(TypedDict):
code_action: Literal["insert-text-on-line-after-diagnostic"]
text_to_insert: str
class RemoveLineCodeAction(TypedDict):
code_action: Literal["remove-line"]
class RemoveRangeCodeAction(TypedDict):
code_action: Literal["remove-range"]
proposed_title: NotRequired[str]
def propose_correct_text_quick_fix(
correct_value: str,
*,
proposed_title: str | None = None,
) -> CorrectTextCodeAction:
r: CorrectTextCodeAction = {
"code_action": "correct-text",
"correct_value": correct_value,
}
if proposed_title:
r["proposed_title"] = proposed_title
return r
def propose_insert_text_on_line_after_diagnostic_quick_fix(
text_to_insert: str,
) -> InsertTextOnLineAfterDiagnosticCodeAction:
return {
"code_action": "insert-text-on-line-after-diagnostic",
"text_to_insert": text_to_insert,
}
def propose_remove_line_quick_fix() -> RemoveLineCodeAction:
return {
"code_action": "remove-line",
}
def propose_remove_range_quick_fix(
*,
proposed_title: str | None = None,
) -> RemoveRangeCodeAction:
r: RemoveRangeCodeAction = {
"code_action": "remove-range",
}
if proposed_title:
r["proposed_title"] = proposed_title
return r
CODE_ACTION_HANDLERS: dict[
CodeActionName,
Callable[
["LintState", Mapping[str, str], types.CodeActionParams, types.Diagnostic],
Iterable[types.CodeAction | types.Command],
],
] = {}
M = TypeVar("M", bound=Mapping[str, str])
Handler = Callable[
["LintState", M, types.CodeActionParams, types.Diagnostic],
Iterable[Union[types.CodeAction, types.Command]],
]
def _code_handler_for(action_name: CodeActionName) -> Callable[[Handler], Handler]:
def _wrapper(func: Handler) -> Handler:
assert action_name not in CODE_ACTION_HANDLERS
CODE_ACTION_HANDLERS[action_name] = func
return func
return _wrapper
@_code_handler_for("correct-text")
def _correct_value_code_action(
lint_state: "LintState",
code_action_data: CorrectTextCodeAction,
code_action_params: types.CodeActionParams,
diagnostic: types.Diagnostic,
) -> Iterable[types.CodeAction | types.Command]:
corrected_value = code_action_data["correct_value"]
title = code_action_data.get("proposed_title")
if not title:
title = f'Replace with "{corrected_value}"'
yield _simple_quick_fix(
lint_state,
code_action_params,
title,
[diagnostic],
diagnostic.range,
corrected_value,
)
@_code_handler_for("insert-text-on-line-after-diagnostic")
def _insert_text_on_line_after_diagnostic_code_action(
lint_state: "LintState",
code_action_data: InsertTextOnLineAfterDiagnosticCodeAction,
code_action_params: types.CodeActionParams,
diagnostic: types.Diagnostic,
) -> Iterable[types.CodeAction | types.Command]:
corrected_value = code_action_data["text_to_insert"]
line_no = diagnostic.range.end.line
if diagnostic.range.end.character > 0:
line_no += 1
insert_range = types.Range(
types.Position(
line_no,
0,
),
types.Position(
line_no,
0,
),
)
yield _simple_quick_fix(
lint_state,
code_action_params,
f'Insert "{corrected_value}"',
[diagnostic],
insert_range,
corrected_value,
)
def _simple_quick_fix(
lint_state: "LintState",
_code_action_params: types.CodeActionParams,
title: str,
diagnostics: list[types.Diagnostic],
affected_range: types.Range,
replacement_text: str,
) -> types.CodeAction:
doc_uri = lint_state.doc_uri
edit = types.TextEdit(
affected_range,
replacement_text,
)
if lint_state.workspace_text_edit_support.supports_document_changes:
ws_edit = types.WorkspaceEdit(
document_changes=[
types.TextDocumentEdit(
text_document=types.OptionalVersionedTextDocumentIdentifier(
doc_uri,
lint_state.doc_version,
),
edits=[edit],
)
]
)
else:
ws_edit = types.WorkspaceEdit(
changes={doc_uri: [edit]},
)
return types.CodeAction(
title=title,
kind=types.CodeActionKind.QuickFix,
diagnostics=diagnostics,
edit=ws_edit,
)
def range_compatible_with_remove_line_fix(range_: types.Range | TERange) -> bool:
if isinstance(range_, TERange):
start = range_.start_pos
end = range_.end_pos
if start.line_position != end.line_position and (
start.line_position + 1 != end.line_position or end.cursor_position > 0
):
return False
else:
start = range_.start
end = range_.end
if start.line != end.line and (start.line + 1 != end.line or end.character > 0):
return False
return True
@_code_handler_for("remove-line")
def _remove_line_code_action(
lint_state: "LintState",
_code_action_data: RemoveLineCodeAction,
code_action_params: types.CodeActionParams,
diagnostic: types.Diagnostic,
) -> Iterable[types.CodeAction | types.Command]:
start = code_action_params.range.start
if range_compatible_with_remove_line_fix(code_action_params.range):
_warn(
"Bug: the quick was used for a diagnostic that spanned multiple lines and would corrupt the file."
)
return
delete_range = types.Range(
start=types.Position(
line=start.line,
character=0,
),
end=types.Position(
line=start.line + 1,
character=0,
),
)
yield _simple_quick_fix(
lint_state,
code_action_params,
"Remove the line",
[diagnostic],
delete_range,
"",
)
@_code_handler_for("remove-range")
def _remove_range_code_action(
lint_state: "LintState",
code_action_data: RemoveRangeCodeAction,
code_action_params: types.CodeActionParams,
diagnostic: types.Diagnostic,
) -> Iterable[types.CodeAction | types.Command]:
title = code_action_data.get("proposed_title", "Delete")
yield _simple_quick_fix(
lint_state,
code_action_params,
title,
[diagnostic],
diagnostic.range,
"",
)
def accepts_quickfixes(
code_action_params: types.CodeActionParams,
) -> bool:
only = code_action_params.context.only
if not only:
return True
return types.CodeActionKind.QuickFix in only
def provide_standard_quickfixes_from_diagnostics_ls(
ls: "DebputyLanguageServer",
code_action_params: types.CodeActionParams,
) -> list[types.Command | types.CodeAction] | None:
if not accepts_quickfixes(code_action_params):
return None
doc_uri = code_action_params.text_document.uri
matched_diagnostics = ls.diagnostics_in_range(
doc_uri,
code_action_params.range,
)
if not matched_diagnostics:
return None
doc = ls.workspace.get_text_document(doc_uri)
return _provide_standard_quickfixes_from_diagnostics(
ls.lint_state(doc),
code_action_params,
matched_diagnostics,
)
def provide_standard_quickfixes_from_diagnostics_lint(
lint_state: "LintState",
code_action_params: types.CodeActionParams,
) -> list[types.Command | types.CodeAction] | None:
return _provide_standard_quickfixes_from_diagnostics(
lint_state,
code_action_params,
code_action_params.context.diagnostics,
)
def _provide_standard_quickfixes_from_diagnostics(
lint_state: "LintState",
code_action_params: types.CodeActionParams,
matched_diagnostics: list[types.Diagnostic],
) -> list[types.Command | types.CodeAction] | None:
actions: list[types.Command | types.CodeAction] = []
for diagnostic in matched_diagnostics:
if not isinstance(diagnostic.data, dict):
continue
data: DiagnosticData = cast("DiagnosticData", diagnostic.data)
quickfixes = data.get("quickfixes")
if quickfixes is None:
continue
for action_suggestion in quickfixes:
if (
action_suggestion
and isinstance(action_suggestion, Mapping)
and "code_action" in action_suggestion
):
action_name: CodeActionName = action_suggestion["code_action"]
handler = CODE_ACTION_HANDLERS.get(action_name)
if handler is not None:
actions.extend(
handler(
lint_state,
cast("Mapping[str, str]", action_suggestion),
code_action_params,
diagnostic,
)
)
else:
_warn(f"No codeAction handler for {action_name} !?")
if not actions:
return None
return actions
|