Skip to content

Commit 0c9ee29

Browse files
bors[bot]Veykril
andauthored
Merge #6585
6585: Link rustc error page and clippy lint page via CodeDescription r=kjeremy a=Veykril Fixes #6371 This makes the error code in here clickable, same for clippy lints ![image](https://user-images.githubusercontent.com/3757771/99459468-6d110b00-292e-11eb-9cde-d43ec9cebc09.png) For clippy I just chose the master build of the site as I believe that to be pretty much always the best fitting. Co-authored-by: Lukas Wirth <[email protected]>
2 parents 99975d0 + 91a1a83 commit 0c9ee29

File tree

8 files changed

+149
-9
lines changed

8 files changed

+149
-9
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"trivially_copy_pass_by_ref",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"rust-lang.github.io",
37+
),
38+
),
39+
port: None,
40+
path: "/rust-clippy/master/index.html",
41+
query: None,
42+
fragment: Some(
43+
"trivially_copy_pass_by_ref",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"clippy",
3350
),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"E0277",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"doc.rust-lang.org",
37+
),
38+
),
39+
port: None,
40+
path: "/error-index.html",
41+
query: None,
42+
fragment: Some(
43+
"E0277",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"rustc",
3350
),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"E0053",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"doc.rust-lang.org",
37+
),
38+
),
39+
port: None,
40+
path: "/error-index.html",
41+
query: None,
42+
fragment: Some(
43+
"E0053",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"rustc",
3350
),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"E0308",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"doc.rust-lang.org",
37+
),
38+
),
39+
port: None,
40+
path: "/error-index.html",
41+
query: None,
42+
fragment: Some(
43+
"E0308",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"rustc",
3350
),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"E0061",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"doc.rust-lang.org",
37+
),
38+
),
39+
port: None,
40+
path: "/error-index.html",
41+
query: None,
42+
fragment: Some(
43+
"E0061",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"rustc",
3350
),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@
2727
"let_and_return",
2828
),
2929
),
30-
code_description: None,
30+
code_description: Some(
31+
CodeDescription {
32+
href: Url {
33+
scheme: "https",
34+
host: Some(
35+
Domain(
36+
"rust-lang.github.io",
37+
),
38+
),
39+
port: None,
40+
path: "/rust-clippy/master/index.html",
41+
query: None,
42+
fragment: Some(
43+
"let_and_return",
44+
),
45+
},
46+
},
47+
),
3148
source: Some(
3249
"clippy",
3350
),

crates/rust-analyzer/src/diagnostics/to_proto.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
211211
}
212212
}
213213

214+
let code_description = match source.as_str() {
215+
"rustc" => rustc_code_description(code.as_deref()),
216+
"clippy" => clippy_code_description(code.as_deref()),
217+
_ => None,
218+
};
219+
214220
primary_spans
215221
.iter()
216222
.map(|primary_span| {
@@ -248,7 +254,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
248254
range: in_macro_location.range,
249255
severity,
250256
code: code.clone().map(lsp_types::NumberOrString::String),
251-
code_description: None,
257+
code_description: code_description.clone(),
252258
source: Some(source.clone()),
253259
message: message.clone(),
254260
related_information: Some(information_for_additional_diagnostic),
@@ -269,7 +275,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
269275
range: location.range,
270276
severity,
271277
code: code.clone().map(lsp_types::NumberOrString::String),
272-
code_description: None,
278+
code_description: code_description.clone(),
273279
source: Some(source.clone()),
274280
message,
275281
related_information: if related_information.is_empty() {
@@ -292,6 +298,31 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
292298
.collect()
293299
}
294300

301+
fn rustc_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
302+
code.filter(|code| {
303+
let mut chars = code.chars();
304+
chars.next().map_or(false, |c| c == 'E')
305+
&& chars.by_ref().take(4).all(|c| c.is_ascii_digit())
306+
&& chars.next().is_none()
307+
})
308+
.and_then(|code| {
309+
lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", code))
310+
.ok()
311+
.map(|href| lsp_types::CodeDescription { href })
312+
})
313+
}
314+
315+
fn clippy_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
316+
code.and_then(|code| {
317+
lsp_types::Url::parse(&format!(
318+
"https://rust-lang.github.io/rust-clippy/master/index.html#{}",
319+
code
320+
))
321+
.ok()
322+
.map(|href| lsp_types::CodeDescription { href })
323+
})
324+
}
325+
295326
#[cfg(test)]
296327
#[cfg(not(windows))]
297328
mod tests {

crates/rust-analyzer/src/handlers.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,14 @@ pub(crate) fn publish_diagnostics(
11291129
range: to_proto::range(&line_index, d.range),
11301130
severity: Some(to_proto::diagnostic_severity(d.severity)),
11311131
code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String),
1132-
code_description: None,
1132+
code_description: d.code.and_then(|code| {
1133+
lsp_types::Url::parse(&format!(
1134+
"https://rust-analyzer.github.io/manual.html#{}",
1135+
code.as_str()
1136+
))
1137+
.ok()
1138+
.map(|href| lsp_types::CodeDescription { href })
1139+
}),
11331140
source: Some("rust-analyzer".to_string()),
11341141
message: d.message,
11351142
related_information: None,

0 commit comments

Comments
 (0)