From 36500fe49630f0ac29e57ff1af737b2bd7bff5a8 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 15 Jan 2021 18:11:54 +0100 Subject: Don't duplicate rustc diagnostics fixes --- .../test_data/rustc_unused_variable.txt | 47 +------- .../test_data/rustc_unused_variable_as_hint.txt | 47 +------- .../test_data/rustc_unused_variable_as_info.txt | 47 +------- .../diagnostics/test_data/snap_multi_line_fix.txt | 120 +-------------------- crates/rust-analyzer/src/diagnostics/to_proto.rs | 53 ++++----- 5 files changed, 32 insertions(+), 282 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt index 23d42b4d0..5c282fe67 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt @@ -66,52 +66,7 @@ ), data: None, }, - fixes: [ - CodeAction { - title: "consider prefixing with an underscore", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - host: None, - port: None, - path: "/test/driver/subcommand/repl.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 290, - character: 8, - }, - end: Position { - line: 290, - character: 11, - }, - }, - new_text: "_foo", - }, - ], - }, - ), - document_changes: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - ], + fixes: [], }, MappedRustDiagnostic { url: Url { diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt index 4e428bedc..d36d7693d 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt @@ -66,52 +66,7 @@ ), data: None, }, - fixes: [ - CodeAction { - title: "consider prefixing with an underscore", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - host: None, - port: None, - path: "/test/driver/subcommand/repl.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 290, - character: 8, - }, - end: Position { - line: 290, - character: 11, - }, - }, - new_text: "_foo", - }, - ], - }, - ), - document_changes: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - ], + fixes: [], }, MappedRustDiagnostic { url: Url { diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt index 4ddd7efae..17845b711 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt @@ -66,52 +66,7 @@ ), data: None, }, - fixes: [ - CodeAction { - title: "consider prefixing with an underscore", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - host: None, - port: None, - path: "/test/driver/subcommand/repl.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 290, - character: 8, - }, - end: Position { - line: 290, - character: 11, - }, - }, - new_text: "_foo", - }, - ], - }, - ), - document_changes: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - ], + fixes: [], }, MappedRustDiagnostic { url: Url { diff --git a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt index 4cbdb3b92..a19962167 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt @@ -102,65 +102,7 @@ tags: None, data: None, }, - fixes: [ - CodeAction { - title: "return the expression directly", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - host: None, - port: None, - path: "/test/src/main.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 2, - character: 4, - }, - end: Position { - line: 2, - character: 30, - }, - }, - new_text: "", - }, - TextEdit { - range: Range { - start: Position { - line: 3, - character: 4, - }, - end: Position { - line: 3, - character: 5, - }, - }, - new_text: "(0..10).collect()", - }, - ], - }, - ), - document_changes: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - ], + fixes: [], }, MappedRustDiagnostic { url: Url { @@ -242,65 +184,7 @@ tags: None, data: None, }, - fixes: [ - CodeAction { - title: "return the expression directly", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - host: None, - port: None, - path: "/test/src/main.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 2, - character: 4, - }, - end: Position { - line: 2, - character: 30, - }, - }, - new_text: "", - }, - TextEdit { - range: Range { - start: Position { - line: 3, - character: 4, - }, - end: Position { - line: 3, - character: 5, - }, - }, - new_text: "(0..10).collect()", - }, - ], - }, - ), - document_changes: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - ], + fixes: [], }, MappedRustDiagnostic { url: Url { diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index 540759198..757899484 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -74,11 +74,13 @@ fn diagnostic_related_information( Some(lsp_types::DiagnosticRelatedInformation { location, message }) } +struct SubDiagnostic { + related: lsp_types::DiagnosticRelatedInformation, + suggested_fix: Option, +} + enum MappedRustChildDiagnostic { - Related { - related: lsp_types::DiagnosticRelatedInformation, - suggested_fix: Option, - }, + SubDiagnostic(SubDiagnostic), MessageLine(String), } @@ -105,15 +107,15 @@ fn map_rust_child_diagnostic( } if edit_map.is_empty() { - MappedRustChildDiagnostic::Related { + MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic { related: lsp_types::DiagnosticRelatedInformation { location: location(workspace_root, spans[0]), message: rd.message.clone(), }, suggested_fix: None, - } + }) } else { - MappedRustChildDiagnostic::Related { + MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic { related: lsp_types::DiagnosticRelatedInformation { location: location(workspace_root, spans[0]), message: rd.message.clone(), @@ -130,7 +132,7 @@ fn map_rust_child_diagnostic( is_preferred: Some(true), data: None, }), - } + }) } } @@ -175,26 +177,22 @@ pub(crate) fn map_rust_diagnostic_to_lsp( } let mut needs_primary_span_label = true; - let mut related_information = Vec::new(); + let mut subdiagnostics = Vec::new(); let mut tags = Vec::new(); for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) { let related = diagnostic_related_information(workspace_root, secondary_span); if let Some(related) = related { - related_information.push(related); + subdiagnostics.push(SubDiagnostic { related, suggested_fix: None }); } } - let mut fixes = Vec::new(); let mut message = rd.message.clone(); for child in &rd.children { let child = map_rust_child_diagnostic(workspace_root, &child); match child { - MappedRustChildDiagnostic::Related { related, suggested_fix } => { - related_information.push(related); - if let Some(code_action) = suggested_fix { - fixes.push(code_action); - } + MappedRustChildDiagnostic::SubDiagnostic(sub) => { + subdiagnostics.push(sub); } MappedRustChildDiagnostic::MessageLine(message_line) => { format_to!(message, "\n{}", message_line); @@ -284,7 +282,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( diagnostics.push(MappedRustDiagnostic { url: in_macro_location.uri, diagnostic, - fixes: fixes.clone(), + fixes: Vec::new(), }); } @@ -298,17 +296,20 @@ pub(crate) fn map_rust_diagnostic_to_lsp( code_description: code_description.clone(), source: Some(source.clone()), message, - related_information: if related_information.is_empty() { + related_information: if subdiagnostics.is_empty() { None } else { - let mut related = related_information.clone(); + let mut related = subdiagnostics + .iter() + .map(|sub| sub.related.clone()) + .collect::>(); related.extend(related_macro_info); Some(related) }, tags: if tags.is_empty() { None } else { Some(tags.clone()) }, data: None, }, - fixes: fixes.clone(), + fixes: Vec::new(), }); // Emit hint-level diagnostics for all `related_information` entries such as "help"s. @@ -318,21 +319,21 @@ pub(crate) fn map_rust_diagnostic_to_lsp( location, message: "original diagnostic".to_string(), }; - for info in &related_information { + for sub in &subdiagnostics { // Filter out empty/non-existent messages, as they greatly confuse VS Code. - if info.message.is_empty() { + if sub.related.message.is_empty() { continue; } diagnostics.push(MappedRustDiagnostic { - url: info.location.uri.clone(), - fixes: fixes.clone(), // share fixes to make them easier to apply + url: sub.related.location.uri.clone(), + fixes: sub.suggested_fix.iter().cloned().collect(), diagnostic: lsp_types::Diagnostic { - range: info.location.range, + range: sub.related.location.range, severity: Some(lsp_types::DiagnosticSeverity::Hint), code: code.clone().map(lsp_types::NumberOrString::String), code_description: code_description.clone(), source: Some(source.clone()), - message: info.message.clone(), + message: sub.related.message.clone(), related_information: Some(vec![back_ref.clone()]), tags: None, // don't apply modifiers again data: None, -- cgit v1.2.3