From 132063ad9653c21d5393bfd710978f408a0386be Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 17 Nov 2020 21:26:37 +0100 Subject: Link rustc error codes in diagnostics --- crates/rust-analyzer/src/diagnostics/to_proto.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index 93bef5c8b..d3720de33 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -2,7 +2,7 @@ //! `cargo check` json format to the LSP diagnostic format. use std::{collections::HashMap, path::Path}; -use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan}; +use flycheck::{Applicability, DiagnosticCode, DiagnosticLevel, DiagnosticSpan}; use stdx::format_to; use crate::{lsp_ext, to_proto::url_from_abs_path}; @@ -211,6 +211,8 @@ pub(crate) fn map_rust_diagnostic_to_lsp( } } + let code_description = rustc_code_description(rd.code.as_ref()); + primary_spans .iter() .map(|primary_span| { @@ -248,7 +250,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( range: in_macro_location.range, severity, code: code.clone().map(lsp_types::NumberOrString::String), - code_description: None, + code_description: code_description.clone(), source: Some(source.clone()), message: message.clone(), related_information: Some(information_for_additional_diagnostic), @@ -269,7 +271,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( range: location.range, severity, code: code.clone().map(lsp_types::NumberOrString::String), - code_description: None, + code_description: code_description.clone(), source: Some(source.clone()), message, related_information: if related_information.is_empty() { @@ -292,6 +294,20 @@ pub(crate) fn map_rust_diagnostic_to_lsp( .collect() } +fn rustc_code_description(code: Option<&DiagnosticCode>) -> Option { + code.filter(|c| { + let mut chars = c.code.chars(); + chars.next().map_or(false, |c| c == 'E') + && chars.by_ref().take(4).all(|c| c.is_ascii_digit()) + && chars.next().is_none() + }) + .and_then(|c| { + lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", c.code)) + .ok() + .map(|href| lsp_types::CodeDescription { href }) + }) +} + #[cfg(test)] #[cfg(not(windows))] mod tests { -- cgit v1.2.3 From 863fdcfa249fa4e14f1edc28082bae5f2a7872c7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 17 Nov 2020 21:56:37 +0100 Subject: Link clippy lint codes in diagnostics --- crates/rust-analyzer/src/diagnostics/to_proto.rs | 29 ++++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index d3720de33..324019614 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -2,7 +2,7 @@ //! `cargo check` json format to the LSP diagnostic format. use std::{collections::HashMap, path::Path}; -use flycheck::{Applicability, DiagnosticCode, DiagnosticLevel, DiagnosticSpan}; +use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan}; use stdx::format_to; use crate::{lsp_ext, to_proto::url_from_abs_path}; @@ -211,7 +211,11 @@ pub(crate) fn map_rust_diagnostic_to_lsp( } } - let code_description = rustc_code_description(rd.code.as_ref()); + let code_description = match source.as_str() { + "rustc" => rustc_code_description(code.as_deref()), + "clippy" => clippy_code_description(code.as_deref()), + _ => None, + }; primary_spans .iter() @@ -294,20 +298,31 @@ pub(crate) fn map_rust_diagnostic_to_lsp( .collect() } -fn rustc_code_description(code: Option<&DiagnosticCode>) -> Option { - code.filter(|c| { - let mut chars = c.code.chars(); +fn rustc_code_description(code: Option<&str>) -> Option { + code.filter(|code| { + let mut chars = code.chars(); chars.next().map_or(false, |c| c == 'E') && chars.by_ref().take(4).all(|c| c.is_ascii_digit()) && chars.next().is_none() }) - .and_then(|c| { - lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", c.code)) + .and_then(|code| { + lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", code)) .ok() .map(|href| lsp_types::CodeDescription { href }) }) } +fn clippy_code_description(code: Option<&str>) -> Option { + code.and_then(|code| { + lsp_types::Url::parse(&format!( + "https://rust-lang.github.io/rust-clippy/master/index.html#{}", + code + )) + .ok() + .map(|href| lsp_types::CodeDescription { href }) + }) +} + #[cfg(test)] #[cfg(not(windows))] mod tests { -- cgit v1.2.3 From 56a0021e61e67838b2f258697471ec07b2f3b723 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 17 Nov 2020 22:49:38 +0100 Subject: update diagnostics test_data with code_description changes --- .../src/diagnostics/test_data/clippy_pass_by_ref.txt | 19 ++++++++++++++++++- .../diagnostics/test_data/handles_macro_location.txt | 19 ++++++++++++++++++- .../test_data/rustc_incompatible_type_for_trait.txt | 19 ++++++++++++++++++- .../diagnostics/test_data/rustc_mismatched_type.txt | 19 ++++++++++++++++++- .../test_data/rustc_wrong_number_of_parameters.txt | 19 ++++++++++++++++++- .../src/diagnostics/test_data/snap_multi_line_fix.txt | 19 ++++++++++++++++++- 6 files changed, 108 insertions(+), 6 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt index 5b2e5187a..72f6c5725 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt @@ -27,7 +27,24 @@ "trivially_copy_pass_by_ref", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "rust-lang.github.io", + ), + ), + port: None, + path: "/rust-clippy/master/index.html", + query: None, + fragment: Some( + "trivially_copy_pass_by_ref", + ), + }, + }, + ), source: Some( "clippy", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt index 116f0ff73..eb4a6b597 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt @@ -27,7 +27,24 @@ "E0277", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0277", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt index 2cbf657e5..19f72196d 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt @@ -27,7 +27,24 @@ "E0053", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0053", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt index 1142dc2ac..15ac95d72 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt @@ -27,7 +27,24 @@ "E0308", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0308", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt index 782c72dbd..b9650f3e4 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt @@ -27,7 +27,24 @@ "E0061", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0061", + ), + }, + }, + ), source: Some( "rustc", ), 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 d3f27ab6a..c45f68a91 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 @@ -27,7 +27,24 @@ "let_and_return", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "rust-lang.github.io", + ), + ), + port: None, + path: "/rust-clippy/master/index.html", + query: None, + fragment: Some( + "let_and_return", + ), + }, + }, + ), source: Some( "clippy", ), -- cgit v1.2.3 From 91a1a836015f61accb73f7b855b852fc22e810fc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 18 Nov 2020 08:54:52 +0100 Subject: Fill code_description for rust_analyzer diagnostics --- crates/rust-analyzer/src/handlers.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 118e7276f..1cf4139d2 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1129,7 +1129,14 @@ pub(crate) fn publish_diagnostics( range: to_proto::range(&line_index, d.range), severity: Some(to_proto::diagnostic_severity(d.severity)), code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String), - code_description: None, + code_description: d.code.and_then(|code| { + lsp_types::Url::parse(&format!( + "https://rust-analyzer.github.io/manual.html#{}", + code.as_str() + )) + .ok() + .map(|href| lsp_types::CodeDescription { href }) + }), source: Some("rust-analyzer".to_string()), message: d.message, related_information: None, -- cgit v1.2.3