Skip to content
← Back

src/analysis/quick_fix_synthesizer.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use Collections.Iterable
5
use Collections.LIST
6
use Collections.MAP
7
use Collections.SET
8
9
use Ghul.Pipes
10
11
use Trees = Syntax.Trees
12
13
use Source.LOCATION
14
15
// Attaches quick fixes to collected diagnostics before they go out on
16
// the wire. The compiler is the sole author of fixes: clients render
17
// whatever arrives here without any per-code knowledge, so adding or
18
// changing a fix is a compiler-only change.
19
//
20
// Two kinds of fix are synthesized:
21
//
22
// - Removal of a redundant operator, for the redundant-presence-test /
23
// redundant-unwrap / redundant-coalesce warnings. These diagnostics
24
// are reported on the operator expression with the range ending at
25
// the operator, so the '?' and '!' edits fall out of the diagnostic
26
// range; the '?.' edit needs the member-access node to find where
27
// the receiver ends.
28
//
29
// - A `@suppress("<code>")` insertion for every coded diagnostic, one
30
// per enclosing scope the pragma can attach to (the pragma wraps the
31
// next definition or statement, so anchors are the AST nodes of that
32
// shape whose location contains the diagnostic).
33
//
34
// Edit coordinates use the wire convention of DIAGNOSTIC ranges:
35
// 1-based, end column exclusive (AST LOCATION end columns are
36
// inclusive, hence the +1s below). Every removal edit carries the text
37
// it expects to delete so the client can drop a fix whose buffer has
38
// drifted since the compile that produced it.
39
class QUICK_FIX_SYNTHESIZER is
40
attach_fixes(
41
diagnostics: Collections.List[Protocol.DIAGNOSTIC],
42
source_files: Iterable[Compiler.SOURCE_FILE]
43
) static is
44
let targets_by_path = MAP[string, LIST[FIX_TARGET]]()
45
46
for d in diagnostics do
47
let code = d.code
48
49
if !code? \/ code.length == 0 then
50
continue
51
fi
52
53
let targets: LIST[FIX_TARGET] mut
54
55
if !targets_by_path.try_get_value(d.path, targets ref) then
56
targets = LIST[FIX_TARGET]()
57
targets_by_path.add(d.path, targets)
58
fi
59
60
targets.add(FIX_TARGET(d, code))
61
od
62
63
if targets_by_path.count == 0 then
64
return
65
fi
66
67
for source_file in source_files do
68
let targets: LIST[FIX_TARGET] mut
69
70
if
71
targets_by_path.try_get_value(source_file.file_name, targets ref)
72
then
73
source_file.definition.walk(FIX_SITE_VISITOR(targets))
74
75
for target in targets do
76
target.attach_fixes()
77
od
78
fi
79
od
80
si
81
si
82
83
enum AnchorKind is
84
BLOCK, // a statement
85
MEMBER, // a variable definition (field)
86
METHOD, // a function, property or indexer definition
87
TYPE // a class, trait, struct, union or enum definition
88
si
89
90
class ANCHOR is
91
location: LOCATION
92
kind: AnchorKind
93
94
init(location: LOCATION, kind: AnchorKind) is
95
self.location = location
96
self.kind = kind
97
si
98
si
99
100
// Per-diagnostic accumulator: the fix-site walk records the enclosing
101
// suppress anchors and (for redundant-coalesce) the receiver location
102
// of the matching member access, then attach_fixes assembles the wire
103
// DTOs.
104
class FIX_TARGET is
105
_diagnostic: Protocol.DIAGNOSTIC
106
_code: string
107
_anchors: LIST[ANCHOR]
108
_coalesce_receiver: LOCATION?
109
110
init(diagnostic: Protocol.DIAGNOSTIC, code: string) is
111
_diagnostic = diagnostic
112
_code = code
113
_anchors = LIST[ANCHOR]()
114
si
115
116
// Node location contains the diagnostic's wire range. Wire end
117
// columns are exclusive, LOCATION end columns inclusive.
118
contains_diagnostic(location: LOCATION) -> bool =>
119
(
120
location.start_line < _diagnostic.start_line \/
121
(location.start_line == _diagnostic.start_line /\ location.start_column <= _diagnostic.start_column)
122
) /\
123
(
124
location.end_line > _diagnostic.end_line \/
125
(location.end_line == _diagnostic.end_line /\ location.end_column >= _diagnostic.end_column - 1)
126
)
127
128
// Node location is exactly the diagnostic's wire range.
129
matches_diagnostic(location: LOCATION) -> bool =>
130
location.start_line == _diagnostic.start_line /\
131
location.start_column == _diagnostic.start_column /\
132
location.end_line == _diagnostic.end_line /\
133
location.end_column == _diagnostic.end_column - 1
134
135
record_anchor(location: LOCATION, kind: AnchorKind) is
136
if contains_diagnostic(location) then
137
_anchors.add(ANCHOR(location, kind))
138
fi
139
si
140
141
record_coalesce_receiver(member_location: LOCATION, receiver_location: LOCATION) is
142
if _code =~ "redundant-coalesce" /\ matches_diagnostic(member_location) then
143
_coalesce_receiver = receiver_location
144
fi
145
si
146
147
attach_fixes() is
148
let fixes = LIST[Protocol.QUICK_FIX]()
149
150
let removal = _build_removal_fix()
151
152
if removal? then
153
fixes.add(removal)
154
fi
155
156
_add_suppress_fixes(fixes)
157
158
if fixes.count > 0 then
159
_diagnostic.fixes = fixes
160
fi
161
si
162
163
_build_removal_fix() -> Protocol.QUICK_FIX? is
164
if _code =~ "redundant-presence-test" then
165
return _remove_trailing_operator("?")
166
elif _code =~ "redundant-unwrap" then
167
return _remove_trailing_operator("!")
168
elif _code =~ "redundant-coalesce" /\ _coalesce_receiver? then
169
// Deleting the '?' of '?.' turns the conditional access
170
// into a plain one. The '?' is the character after the
171
// receiver expression ends.
172
let receiver = _coalesce_receiver
173
174
return _single_edit_fix(
175
"Replace '?.' with '.'",
176
true,
177
Protocol.FIX_EDIT(
178
receiver.end_line, receiver.end_column + 1,
179
receiver.end_line, receiver.end_column + 2,
180
"?", ""
181
)
182
)
183
fi
184
185
return null
186
si
187
188
// The diagnostic range ends at the operator, so the operator is
189
// its last character.
190
_remove_trailing_operator(operator: string) -> Protocol.QUICK_FIX =>
191
_single_edit_fix(
192
"Remove redundant '{operator}'",
193
true,
194
Protocol.FIX_EDIT(
195
_diagnostic.end_line, _diagnostic.end_column - 1,
196
_diagnostic.end_line, _diagnostic.end_column,
197
operator, ""
198
)
199
)
200
201
_add_suppress_fixes(fixes: LIST[Protocol.QUICK_FIX]) is
202
// Locals sit inside a let statement, which is already the
203
// innermost useful anchor; a variable anchor only stands on
204
// its own for a field, where no statement contains it.
205
let anchors =
206
_anchors
207
|> filter(a => a.kind != AnchorKind.MEMBER \/ !_is_inside_block_anchor(a))
208
|> sort((a, b) => _compare_innermost_first(a.location, b.location))
209
|> collect()
210
211
let emitted_lines = SET[int]()
212
let emitted mut = 0
213
214
for anchor in anchors do
215
if emitted >= 3 then
216
break
217
fi
218
219
if emitted_lines.contains(anchor.location.start_line) then
220
continue
221
fi
222
223
emitted_lines.add(anchor.location.start_line)
224
225
let label =
226
if emitted == 0 then
227
"Suppress here"
228
elif anchor.kind == AnchorKind.METHOD then
229
"Suppress for enclosing method"
230
elif anchor.kind == AnchorKind.TYPE then
231
"Suppress for enclosing type"
232
else
233
"Suppress for enclosing block"
234
fi
235
236
fixes.add(_build_suppress_fix(label, anchor.location))
237
238
emitted = emitted + 1
239
240
// Wider scopes than the enclosing method or type add
241
// noise, not reach.
242
if anchor.kind == AnchorKind.METHOD \/ anchor.kind == AnchorKind.TYPE then
243
break
244
fi
245
od
246
si
247
248
_is_inside_block_anchor(anchor: ANCHOR) -> bool =>
249
_anchors
250
|> any(
251
other =>
252
other.kind == AnchorKind.BLOCK /\
253
other.location.contains(anchor.location)
254
)
255
256
_build_suppress_fix(label: string, anchor: LOCATION) -> Protocol.QUICK_FIX is
257
let indent = "".pad_left(anchor.start_column - 1)
258
259
return _single_edit_fix(
260
"{label}: @suppress(\"{_code}\")",
261
false,
262
Protocol.FIX_EDIT(
263
anchor.start_line, 1,
264
anchor.start_line, 1,
265
null, "{indent}@suppress(\"{_code}\")\n"
266
)
267
)
268
si
269
270
_single_edit_fix(title: string, is_preferred: bool, edit: Protocol.FIX_EDIT) -> Protocol.QUICK_FIX static is
271
let edits = LIST[Protocol.FIX_EDIT]()
272
273
edits.add(edit)
274
275
return Protocol.QUICK_FIX(title, is_preferred, edits)
276
si
277
278
_compare_innermost_first(a: LOCATION, b: LOCATION) -> int static is
279
// Nested anchors all contain the diagnostic, so the later a
280
// span starts (and the earlier it ends) the more deeply
281
// nested it is.
282
if a.start_line != b.start_line then
283
return b.start_line - a.start_line
284
fi
285
286
if a.start_column != b.start_column then
287
return b.start_column - a.start_column
288
fi
289
290
if a.end_line != b.end_line then
291
return a.end_line - b.end_line
292
fi
293
294
return a.end_column - b.end_column
295
si
296
si
297
298
// Collects, for every target diagnostic in a file, the suppress
299
// anchors that contain it and the coalescing member access it was
300
// reported on. Statements arrive through the enter_node hook the
301
// statement-list walk already provides; definition anchors need their
302
// specific visit overloads because the definitions-list walk has no
303
// per-child hook.
304
class FIX_SITE_VISITOR: Syntax.Visitor is
305
_targets: LIST[FIX_TARGET]
306
307
init(targets: LIST[FIX_TARGET]) is
308
super.init()
309
310
_targets = targets
311
si
312
313
_record_anchor(location: LOCATION, kind: AnchorKind) is
314
for target in _targets do
315
target.record_anchor(location, kind)
316
od
317
si
318
319
enter_node(node: Trees.Node) is
320
if isa Trees.Statements.Statement(node) then
321
_record_anchor(node.location, AnchorKind.BLOCK)
322
fi
323
si
324
325
visit(variable: Trees.Variables.VARIABLE) is
326
_record_anchor(variable.location, AnchorKind.MEMBER)
327
si
328
329
visit(function: Trees.Definitions.FUNCTION) is
330
_record_anchor(function.location, AnchorKind.METHOD)
331
si
332
333
visit(property: Trees.Definitions.PROPERTY) is
334
_record_anchor(property.location, AnchorKind.METHOD)
335
si
336
337
visit(indexer: Trees.Definitions.INDEXER) is
338
_record_anchor(indexer.location, AnchorKind.METHOD)
339
si
340
341
visit(`class: Trees.Definitions.CLASS) is
342
_record_anchor(`class.location, AnchorKind.TYPE)
343
si
344
345
visit(`trait: Trees.Definitions.TRAIT) is
346
_record_anchor(`trait.location, AnchorKind.TYPE)
347
si
348
349
visit(`struct: Trees.Definitions.STRUCT) is
350
_record_anchor(`struct.location, AnchorKind.TYPE)
351
si
352
353
visit(`union: Trees.Definitions.UNION) is
354
_record_anchor(`union.location, AnchorKind.TYPE)
355
si
356
357
visit(`enum: Trees.Definitions.ENUM) is
358
_record_anchor(`enum.location, AnchorKind.TYPE)
359
si
360
361
visit(member: Trees.Expressions.MEMBER) is
362
if member.is_coalesce then
363
for target in _targets do
364
target.record_coalesce_receiver(member.location, member.left.location)
365
od
366
fi
367
si
368
si
369
si