diff options
Diffstat (limited to 'crates/ra_cargo_watch/src/conv.rs')
-rw-r--r-- | crates/ra_cargo_watch/src/conv.rs | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/crates/ra_cargo_watch/src/conv.rs b/crates/ra_cargo_watch/src/conv.rs new file mode 100644 index 000000000..3bd4bf7a5 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv.rs | |||
@@ -0,0 +1,280 @@ | |||
1 | //! This module provides the functionality needed to convert diagnostics from | ||
2 | //! `cargo check` json format to the LSP diagnostic format. | ||
3 | use cargo_metadata::diagnostic::{ | ||
4 | Applicability, Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan, | ||
5 | DiagnosticSpanMacroExpansion, | ||
6 | }; | ||
7 | use lsp_types::{ | ||
8 | Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Location, | ||
9 | NumberOrString, Position, Range, Url, | ||
10 | }; | ||
11 | use std::{fmt::Write, path::PathBuf}; | ||
12 | |||
13 | #[cfg(test)] | ||
14 | mod test; | ||
15 | |||
16 | /// Converts a Rust level string to a LSP severity | ||
17 | fn map_level_to_severity(val: DiagnosticLevel) -> Option<DiagnosticSeverity> { | ||
18 | match val { | ||
19 | DiagnosticLevel::Ice => Some(DiagnosticSeverity::Error), | ||
20 | DiagnosticLevel::Error => Some(DiagnosticSeverity::Error), | ||
21 | DiagnosticLevel::Warning => Some(DiagnosticSeverity::Warning), | ||
22 | DiagnosticLevel::Note => Some(DiagnosticSeverity::Information), | ||
23 | DiagnosticLevel::Help => Some(DiagnosticSeverity::Hint), | ||
24 | DiagnosticLevel::Unknown => None, | ||
25 | } | ||
26 | } | ||
27 | |||
28 | /// Check whether a file name is from macro invocation | ||
29 | fn is_from_macro(file_name: &str) -> bool { | ||
30 | file_name.starts_with('<') && file_name.ends_with('>') | ||
31 | } | ||
32 | |||
33 | /// Converts a Rust macro span to a LSP location recursively | ||
34 | fn map_macro_span_to_location( | ||
35 | span_macro: &DiagnosticSpanMacroExpansion, | ||
36 | workspace_root: &PathBuf, | ||
37 | ) -> Option<Location> { | ||
38 | if !is_from_macro(&span_macro.span.file_name) { | ||
39 | return Some(map_span_to_location(&span_macro.span, workspace_root)); | ||
40 | } | ||
41 | |||
42 | if let Some(expansion) = &span_macro.span.expansion { | ||
43 | return map_macro_span_to_location(&expansion, workspace_root); | ||
44 | } | ||
45 | |||
46 | None | ||
47 | } | ||
48 | |||
49 | /// Converts a Rust span to a LSP location | ||
50 | fn map_span_to_location(span: &DiagnosticSpan, workspace_root: &PathBuf) -> Location { | ||
51 | if is_from_macro(&span.file_name) && span.expansion.is_some() { | ||
52 | let expansion = span.expansion.as_ref().unwrap(); | ||
53 | if let Some(macro_range) = map_macro_span_to_location(&expansion, workspace_root) { | ||
54 | return macro_range; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | let mut file_name = workspace_root.clone(); | ||
59 | file_name.push(&span.file_name); | ||
60 | let uri = Url::from_file_path(file_name).unwrap(); | ||
61 | |||
62 | let range = Range::new( | ||
63 | Position::new(span.line_start as u64 - 1, span.column_start as u64 - 1), | ||
64 | Position::new(span.line_end as u64 - 1, span.column_end as u64 - 1), | ||
65 | ); | ||
66 | |||
67 | Location { uri, range } | ||
68 | } | ||
69 | |||
70 | /// Converts a secondary Rust span to a LSP related information | ||
71 | /// | ||
72 | /// If the span is unlabelled this will return `None`. | ||
73 | fn map_secondary_span_to_related( | ||
74 | span: &DiagnosticSpan, | ||
75 | workspace_root: &PathBuf, | ||
76 | ) -> Option<DiagnosticRelatedInformation> { | ||
77 | if let Some(label) = &span.label { | ||
78 | let location = map_span_to_location(span, workspace_root); | ||
79 | Some(DiagnosticRelatedInformation { location, message: label.clone() }) | ||
80 | } else { | ||
81 | // Nothing to label this with | ||
82 | None | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /// Determines if diagnostic is related to unused code | ||
87 | fn is_unused_or_unnecessary(rd: &RustDiagnostic) -> bool { | ||
88 | if let Some(code) = &rd.code { | ||
89 | match code.code.as_str() { | ||
90 | "dead_code" | "unknown_lints" | "unreachable_code" | "unused_attributes" | ||
91 | | "unused_imports" | "unused_macros" | "unused_variables" => true, | ||
92 | _ => false, | ||
93 | } | ||
94 | } else { | ||
95 | false | ||
96 | } | ||
97 | } | ||
98 | |||
99 | /// Determines if diagnostic is related to deprecated code | ||
100 | fn is_deprecated(rd: &RustDiagnostic) -> bool { | ||
101 | if let Some(code) = &rd.code { | ||
102 | match code.code.as_str() { | ||
103 | "deprecated" => true, | ||
104 | _ => false, | ||
105 | } | ||
106 | } else { | ||
107 | false | ||
108 | } | ||
109 | } | ||
110 | |||
111 | #[derive(Debug)] | ||
112 | pub struct SuggestedFix { | ||
113 | pub title: String, | ||
114 | pub location: Location, | ||
115 | pub replacement: String, | ||
116 | pub applicability: Applicability, | ||
117 | pub diagnostics: Vec<Diagnostic>, | ||
118 | } | ||
119 | |||
120 | impl std::cmp::PartialEq<SuggestedFix> for SuggestedFix { | ||
121 | fn eq(&self, other: &SuggestedFix) -> bool { | ||
122 | if self.title == other.title | ||
123 | && self.location == other.location | ||
124 | && self.replacement == other.replacement | ||
125 | { | ||
126 | // Applicability doesn't impl PartialEq... | ||
127 | match (&self.applicability, &other.applicability) { | ||
128 | (Applicability::MachineApplicable, Applicability::MachineApplicable) => true, | ||
129 | (Applicability::HasPlaceholders, Applicability::HasPlaceholders) => true, | ||
130 | (Applicability::MaybeIncorrect, Applicability::MaybeIncorrect) => true, | ||
131 | (Applicability::Unspecified, Applicability::Unspecified) => true, | ||
132 | _ => false, | ||
133 | } | ||
134 | } else { | ||
135 | false | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | enum MappedRustChildDiagnostic { | ||
141 | Related(DiagnosticRelatedInformation), | ||
142 | SuggestedFix(SuggestedFix), | ||
143 | MessageLine(String), | ||
144 | } | ||
145 | |||
146 | fn map_rust_child_diagnostic( | ||
147 | rd: &RustDiagnostic, | ||
148 | workspace_root: &PathBuf, | ||
149 | ) -> MappedRustChildDiagnostic { | ||
150 | let span: &DiagnosticSpan = match rd.spans.iter().find(|s| s.is_primary) { | ||
151 | Some(span) => span, | ||
152 | None => { | ||
153 | // `rustc` uses these spanless children as a way to print multi-line | ||
154 | // messages | ||
155 | return MappedRustChildDiagnostic::MessageLine(rd.message.clone()); | ||
156 | } | ||
157 | }; | ||
158 | |||
159 | // If we have a primary span use its location, otherwise use the parent | ||
160 | let location = map_span_to_location(&span, workspace_root); | ||
161 | |||
162 | if let Some(suggested_replacement) = &span.suggested_replacement { | ||
163 | // Include our replacement in the title unless it's empty | ||
164 | let title = if !suggested_replacement.is_empty() { | ||
165 | format!("{}: '{}'", rd.message, suggested_replacement) | ||
166 | } else { | ||
167 | rd.message.clone() | ||
168 | }; | ||
169 | |||
170 | MappedRustChildDiagnostic::SuggestedFix(SuggestedFix { | ||
171 | title, | ||
172 | location, | ||
173 | replacement: suggested_replacement.clone(), | ||
174 | applicability: span.suggestion_applicability.clone().unwrap_or(Applicability::Unknown), | ||
175 | diagnostics: vec![], | ||
176 | }) | ||
177 | } else { | ||
178 | MappedRustChildDiagnostic::Related(DiagnosticRelatedInformation { | ||
179 | location, | ||
180 | message: rd.message.clone(), | ||
181 | }) | ||
182 | } | ||
183 | } | ||
184 | |||
185 | #[derive(Debug)] | ||
186 | pub(crate) struct MappedRustDiagnostic { | ||
187 | pub location: Location, | ||
188 | pub diagnostic: Diagnostic, | ||
189 | pub suggested_fixes: Vec<SuggestedFix>, | ||
190 | } | ||
191 | |||
192 | /// Converts a Rust root diagnostic to LSP form | ||
193 | /// | ||
194 | /// This flattens the Rust diagnostic by: | ||
195 | /// | ||
196 | /// 1. Creating a LSP diagnostic with the root message and primary span. | ||
197 | /// 2. Adding any labelled secondary spans to `relatedInformation` | ||
198 | /// 3. Categorising child diagnostics as either `SuggestedFix`es, | ||
199 | /// `relatedInformation` or additional message lines. | ||
200 | /// | ||
201 | /// If the diagnostic has no primary span this will return `None` | ||
202 | pub(crate) fn map_rust_diagnostic_to_lsp( | ||
203 | rd: &RustDiagnostic, | ||
204 | workspace_root: &PathBuf, | ||
205 | ) -> Option<MappedRustDiagnostic> { | ||
206 | let primary_span = rd.spans.iter().find(|s| s.is_primary)?; | ||
207 | |||
208 | let location = map_span_to_location(&primary_span, workspace_root); | ||
209 | |||
210 | let severity = map_level_to_severity(rd.level); | ||
211 | let mut primary_span_label = primary_span.label.as_ref(); | ||
212 | |||
213 | let mut source = String::from("rustc"); | ||
214 | let mut code = rd.code.as_ref().map(|c| c.code.clone()); | ||
215 | if let Some(code_val) = &code { | ||
216 | // See if this is an RFC #2103 scoped lint (e.g. from Clippy) | ||
217 | let scoped_code: Vec<&str> = code_val.split("::").collect(); | ||
218 | if scoped_code.len() == 2 { | ||
219 | source = String::from(scoped_code[0]); | ||
220 | code = Some(String::from(scoped_code[1])); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | let mut related_information = vec![]; | ||
225 | let mut tags = vec![]; | ||
226 | |||
227 | for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) { | ||
228 | let related = map_secondary_span_to_related(secondary_span, workspace_root); | ||
229 | if let Some(related) = related { | ||
230 | related_information.push(related); | ||
231 | } | ||
232 | } | ||
233 | |||
234 | let mut suggested_fixes = vec![]; | ||
235 | let mut message = rd.message.clone(); | ||
236 | for child in &rd.children { | ||
237 | let child = map_rust_child_diagnostic(&child, workspace_root); | ||
238 | match child { | ||
239 | MappedRustChildDiagnostic::Related(related) => related_information.push(related), | ||
240 | MappedRustChildDiagnostic::SuggestedFix(suggested_fix) => { | ||
241 | suggested_fixes.push(suggested_fix) | ||
242 | } | ||
243 | MappedRustChildDiagnostic::MessageLine(message_line) => { | ||
244 | write!(&mut message, "\n{}", message_line).unwrap(); | ||
245 | |||
246 | // These secondary messages usually duplicate the content of the | ||
247 | // primary span label. | ||
248 | primary_span_label = None; | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | |||
253 | if let Some(primary_span_label) = primary_span_label { | ||
254 | write!(&mut message, "\n{}", primary_span_label).unwrap(); | ||
255 | } | ||
256 | |||
257 | if is_unused_or_unnecessary(rd) { | ||
258 | tags.push(DiagnosticTag::Unnecessary); | ||
259 | } | ||
260 | |||
261 | if is_deprecated(rd) { | ||
262 | tags.push(DiagnosticTag::Deprecated); | ||
263 | } | ||
264 | |||
265 | let diagnostic = Diagnostic { | ||
266 | range: location.range, | ||
267 | severity, | ||
268 | code: code.map(NumberOrString::String), | ||
269 | source: Some(source), | ||
270 | message, | ||
271 | related_information: if !related_information.is_empty() { | ||
272 | Some(related_information) | ||
273 | } else { | ||
274 | None | ||
275 | }, | ||
276 | tags: if !tags.is_empty() { Some(tags) } else { None }, | ||
277 | }; | ||
278 | |||
279 | Some(MappedRustDiagnostic { location, diagnostic, suggested_fixes }) | ||
280 | } | ||