Skip to content

Commit 8a084e6

Browse files
bors[bot]Brandon
andauthored
Merge #10902
10902: Handle multiple cargo check quick fix spans r=Veykril a=brandondong Resolves #10705. **Cause:** - For a cargo check diagnostic with multiple spans, only a single quick fix action would be created at the location of `spans[0]`. Additionally, the hover window details would only show the location of `spans[0]` next to the message. **Fix:** - Allow cargo check quick fix actions to be triggerable from multiple selection ranges. Specifically, if the selection intersects with any of the replacement spans, the quick fix action is shown. - No change in behavior for the hover window details. It's pretty minor and I think showing multiple locations next to the message may be more confusing anyways. Co-authored-by: Brandon <[email protected]>
2 parents 6434ada + fa28185 commit 8a084e6

14 files changed

+372
-283
lines changed

crates/rust-analyzer/src/diagnostics.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ pub(crate) struct DiagnosticCollection {
2929

3030
#[derive(Debug, Clone)]
3131
pub(crate) struct Fix {
32-
pub(crate) range: lsp_types::Range,
32+
// Fixes may be triggerable from multiple ranges.
33+
pub(crate) ranges: Vec<lsp_types::Range>,
3334
pub(crate) action: lsp_ext::CodeAction,
3435
}
3536

@@ -43,7 +44,7 @@ impl DiagnosticCollection {
4344
&mut self,
4445
file_id: FileId,
4546
diagnostic: lsp_types::Diagnostic,
46-
fixes: Vec<lsp_ext::CodeAction>,
47+
fix: Option<Fix>,
4748
) {
4849
let diagnostics = self.check.entry(file_id).or_default();
4950
for existing_diagnostic in diagnostics.iter() {
@@ -53,10 +54,7 @@ impl DiagnosticCollection {
5354
}
5455

5556
let check_fixes = Arc::make_mut(&mut self.check_fixes);
56-
check_fixes
57-
.entry(file_id)
58-
.or_default()
59-
.extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
57+
check_fixes.entry(file_id).or_default().extend(fix);
6058
diagnostics.push(diagnostic);
6159
self.changes.insert(file_id);
6260
}

crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
tags: None,
115115
data: None,
116116
},
117-
fixes: [],
117+
fix: None,
118118
},
119119
MappedRustDiagnostic {
120120
url: Url {
@@ -205,7 +205,7 @@
205205
tags: None,
206206
data: None,
207207
},
208-
fixes: [],
208+
fix: None,
209209
},
210210
MappedRustDiagnostic {
211211
url: Url {
@@ -296,55 +296,69 @@
296296
tags: None,
297297
data: None,
298298
},
299-
fixes: [
300-
CodeAction {
301-
title: "consider passing by value instead: `self`",
302-
group: None,
303-
kind: Some(
304-
CodeActionKind(
305-
"quickfix",
299+
fix: Some(
300+
Fix {
301+
ranges: [
302+
Range {
303+
start: Position {
304+
line: 41,
305+
character: 23,
306+
},
307+
end: Position {
308+
line: 41,
309+
character: 28,
310+
},
311+
},
312+
],
313+
action: CodeAction {
314+
title: "consider passing by value instead: `self`",
315+
group: None,
316+
kind: Some(
317+
CodeActionKind(
318+
"quickfix",
319+
),
306320
),
307-
),
308-
edit: Some(
309-
SnippetWorkspaceEdit {
310-
changes: Some(
311-
{
312-
Url {
313-
scheme: "file",
314-
cannot_be_a_base: false,
315-
username: "",
316-
password: None,
317-
host: None,
318-
port: None,
319-
path: "/test/compiler/mir/tagset.rs",
320-
query: None,
321-
fragment: None,
322-
}: [
323-
TextEdit {
324-
range: Range {
325-
start: Position {
326-
line: 41,
327-
character: 23,
328-
},
329-
end: Position {
330-
line: 41,
331-
character: 28,
321+
edit: Some(
322+
SnippetWorkspaceEdit {
323+
changes: Some(
324+
{
325+
Url {
326+
scheme: "file",
327+
cannot_be_a_base: false,
328+
username: "",
329+
password: None,
330+
host: None,
331+
port: None,
332+
path: "/test/compiler/mir/tagset.rs",
333+
query: None,
334+
fragment: None,
335+
}: [
336+
TextEdit {
337+
range: Range {
338+
start: Position {
339+
line: 41,
340+
character: 23,
341+
},
342+
end: Position {
343+
line: 41,
344+
character: 28,
345+
},
332346
},
347+
new_text: "self",
333348
},
334-
new_text: "self",
335-
},
336-
],
337-
},
338-
),
339-
document_changes: None,
340-
change_annotations: None,
341-
},
342-
),
343-
is_preferred: Some(
344-
true,
345-
),
346-
data: None,
349+
],
350+
},
351+
),
352+
document_changes: None,
353+
change_annotations: None,
354+
},
355+
),
356+
is_preferred: Some(
357+
true,
358+
),
359+
data: None,
360+
},
347361
},
348-
],
362+
),
349363
},
350364
]

crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@
5959
tags: None,
6060
data: None,
6161
},
62-
fixes: [],
62+
fix: None,
6363
},
6464
]

crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
tags: None,
6565
data: None,
6666
},
67-
fixes: [],
67+
fix: None,
6868
},
6969
MappedRustDiagnostic {
7070
url: Url {
@@ -131,7 +131,7 @@
131131
tags: None,
132132
data: None,
133133
},
134-
fixes: [],
134+
fix: None,
135135
},
136136
MappedRustDiagnostic {
137137
url: Url {
@@ -224,6 +224,6 @@
224224
tags: None,
225225
data: None,
226226
},
227-
fixes: [],
227+
fix: None,
228228
},
229229
]

crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@
5959
tags: None,
6060
data: None,
6161
},
62-
fixes: [],
62+
fix: None,
6363
},
6464
]

crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@
5959
tags: None,
6060
data: None,
6161
},
62-
fixes: [],
62+
fix: None,
6363
},
6464
]

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
),
7373
data: None,
7474
},
75-
fixes: [],
75+
fix: None,
7676
},
7777
MappedRustDiagnostic {
7878
url: Url {
@@ -143,55 +143,69 @@
143143
tags: None,
144144
data: None,
145145
},
146-
fixes: [
147-
CodeAction {
148-
title: "consider prefixing with an underscore: `_foo`",
149-
group: None,
150-
kind: Some(
151-
CodeActionKind(
152-
"quickfix",
146+
fix: Some(
147+
Fix {
148+
ranges: [
149+
Range {
150+
start: Position {
151+
line: 290,
152+
character: 8,
153+
},
154+
end: Position {
155+
line: 290,
156+
character: 11,
157+
},
158+
},
159+
],
160+
action: CodeAction {
161+
title: "consider prefixing with an underscore: `_foo`",
162+
group: None,
163+
kind: Some(
164+
CodeActionKind(
165+
"quickfix",
166+
),
153167
),
154-
),
155-
edit: Some(
156-
SnippetWorkspaceEdit {
157-
changes: Some(
158-
{
159-
Url {
160-
scheme: "file",
161-
cannot_be_a_base: false,
162-
username: "",
163-
password: None,
164-
host: None,
165-
port: None,
166-
path: "/test/driver/subcommand/repl.rs",
167-
query: None,
168-
fragment: None,
169-
}: [
170-
TextEdit {
171-
range: Range {
172-
start: Position {
173-
line: 290,
174-
character: 8,
175-
},
176-
end: Position {
177-
line: 290,
178-
character: 11,
168+
edit: Some(
169+
SnippetWorkspaceEdit {
170+
changes: Some(
171+
{
172+
Url {
173+
scheme: "file",
174+
cannot_be_a_base: false,
175+
username: "",
176+
password: None,
177+
host: None,
178+
port: None,
179+
path: "/test/driver/subcommand/repl.rs",
180+
query: None,
181+
fragment: None,
182+
}: [
183+
TextEdit {
184+
range: Range {
185+
start: Position {
186+
line: 290,
187+
character: 8,
188+
},
189+
end: Position {
190+
line: 290,
191+
character: 11,
192+
},
179193
},
194+
new_text: "_foo",
180195
},
181-
new_text: "_foo",
182-
},
183-
],
184-
},
185-
),
186-
document_changes: None,
187-
change_annotations: None,
188-
},
189-
),
190-
is_preferred: Some(
191-
true,
192-
),
193-
data: None,
196+
],
197+
},
198+
),
199+
document_changes: None,
200+
change_annotations: None,
201+
},
202+
),
203+
is_preferred: Some(
204+
true,
205+
),
206+
data: None,
207+
},
194208
},
195-
],
209+
),
196210
},
197211
]

0 commit comments

Comments
 (0)