diff options
author | Aleksey Kladov <[email protected]> | 2019-01-08 19:17:36 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-08 19:17:36 +0000 |
commit | 1967884d6836219ee78a754ca5c66ac781351559 (patch) | |
tree | 7594f37cd0a5200eb097b9d472c61f0223d01d05 /crates/ra_ide_api_light/src/diagnostics.rs | |
parent | 4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (diff) |
rename ra_editor -> ra_ide_api_light
Diffstat (limited to 'crates/ra_ide_api_light/src/diagnostics.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/diagnostics.rs | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/crates/ra_ide_api_light/src/diagnostics.rs b/crates/ra_ide_api_light/src/diagnostics.rs new file mode 100644 index 000000000..2b695dfdf --- /dev/null +++ b/crates/ra_ide_api_light/src/diagnostics.rs | |||
@@ -0,0 +1,266 @@ | |||
1 | use itertools::Itertools; | ||
2 | |||
3 | use ra_syntax::{ | ||
4 | Location, SourceFile, SyntaxKind, TextRange, SyntaxNode, | ||
5 | ast::{self, AstNode}, | ||
6 | |||
7 | }; | ||
8 | use ra_text_edit::{TextEdit, TextEditBuilder}; | ||
9 | |||
10 | use crate::{Diagnostic, LocalEdit, Severity}; | ||
11 | |||
12 | pub fn diagnostics(file: &SourceFile) -> Vec<Diagnostic> { | ||
13 | fn location_to_range(location: Location) -> TextRange { | ||
14 | match location { | ||
15 | Location::Offset(offset) => TextRange::offset_len(offset, 1.into()), | ||
16 | Location::Range(range) => range, | ||
17 | } | ||
18 | } | ||
19 | |||
20 | let mut errors: Vec<Diagnostic> = file | ||
21 | .errors() | ||
22 | .into_iter() | ||
23 | .map(|err| Diagnostic { | ||
24 | range: location_to_range(err.location()), | ||
25 | msg: format!("Syntax Error: {}", err), | ||
26 | severity: Severity::Error, | ||
27 | fix: None, | ||
28 | }) | ||
29 | .collect(); | ||
30 | |||
31 | for node in file.syntax().descendants() { | ||
32 | check_unnecessary_braces_in_use_statement(&mut errors, node); | ||
33 | check_struct_shorthand_initialization(&mut errors, node); | ||
34 | } | ||
35 | |||
36 | errors | ||
37 | } | ||
38 | |||
39 | fn check_unnecessary_braces_in_use_statement( | ||
40 | acc: &mut Vec<Diagnostic>, | ||
41 | node: &SyntaxNode, | ||
42 | ) -> Option<()> { | ||
43 | let use_tree_list = ast::UseTreeList::cast(node)?; | ||
44 | if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { | ||
45 | let range = use_tree_list.syntax().range(); | ||
46 | let edit = | ||
47 | text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(single_use_tree) | ||
48 | .unwrap_or_else(|| { | ||
49 | let to_replace = single_use_tree.syntax().text().to_string(); | ||
50 | let mut edit_builder = TextEditBuilder::default(); | ||
51 | edit_builder.delete(range); | ||
52 | edit_builder.insert(range.start(), to_replace); | ||
53 | edit_builder.finish() | ||
54 | }); | ||
55 | |||
56 | acc.push(Diagnostic { | ||
57 | range, | ||
58 | msg: format!("Unnecessary braces in use statement"), | ||
59 | severity: Severity::WeakWarning, | ||
60 | fix: Some(LocalEdit { | ||
61 | label: "Remove unnecessary braces".to_string(), | ||
62 | edit, | ||
63 | cursor_position: None, | ||
64 | }), | ||
65 | }); | ||
66 | } | ||
67 | |||
68 | Some(()) | ||
69 | } | ||
70 | |||
71 | fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( | ||
72 | single_use_tree: &ast::UseTree, | ||
73 | ) -> Option<TextEdit> { | ||
74 | let use_tree_list_node = single_use_tree.syntax().parent()?; | ||
75 | if single_use_tree | ||
76 | .path()? | ||
77 | .segment()? | ||
78 | .syntax() | ||
79 | .first_child()? | ||
80 | .kind() | ||
81 | == SyntaxKind::SELF_KW | ||
82 | { | ||
83 | let start = use_tree_list_node.prev_sibling()?.range().start(); | ||
84 | let end = use_tree_list_node.range().end(); | ||
85 | let range = TextRange::from_to(start, end); | ||
86 | let mut edit_builder = TextEditBuilder::default(); | ||
87 | edit_builder.delete(range); | ||
88 | return Some(edit_builder.finish()); | ||
89 | } | ||
90 | None | ||
91 | } | ||
92 | |||
93 | fn check_struct_shorthand_initialization( | ||
94 | acc: &mut Vec<Diagnostic>, | ||
95 | node: &SyntaxNode, | ||
96 | ) -> Option<()> { | ||
97 | let struct_lit = ast::StructLit::cast(node)?; | ||
98 | let named_field_list = struct_lit.named_field_list()?; | ||
99 | for named_field in named_field_list.fields() { | ||
100 | if let (Some(name_ref), Some(expr)) = (named_field.name_ref(), named_field.expr()) { | ||
101 | let field_name = name_ref.syntax().text().to_string(); | ||
102 | let field_expr = expr.syntax().text().to_string(); | ||
103 | if field_name == field_expr { | ||
104 | let mut edit_builder = TextEditBuilder::default(); | ||
105 | edit_builder.delete(named_field.syntax().range()); | ||
106 | edit_builder.insert(named_field.syntax().range().start(), field_name); | ||
107 | let edit = edit_builder.finish(); | ||
108 | |||
109 | acc.push(Diagnostic { | ||
110 | range: named_field.syntax().range(), | ||
111 | msg: format!("Shorthand struct initialization"), | ||
112 | severity: Severity::WeakWarning, | ||
113 | fix: Some(LocalEdit { | ||
114 | label: "use struct shorthand initialization".to_string(), | ||
115 | edit, | ||
116 | cursor_position: None, | ||
117 | }), | ||
118 | }); | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | Some(()) | ||
123 | } | ||
124 | |||
125 | #[cfg(test)] | ||
126 | mod tests { | ||
127 | use crate::test_utils::assert_eq_text; | ||
128 | |||
129 | use super::*; | ||
130 | |||
131 | type DiagnosticChecker = fn(&mut Vec<Diagnostic>, &SyntaxNode) -> Option<()>; | ||
132 | |||
133 | fn check_not_applicable(code: &str, func: DiagnosticChecker) { | ||
134 | let file = SourceFile::parse(code); | ||
135 | let mut diagnostics = Vec::new(); | ||
136 | for node in file.syntax().descendants() { | ||
137 | func(&mut diagnostics, node); | ||
138 | } | ||
139 | assert!(diagnostics.is_empty()); | ||
140 | } | ||
141 | |||
142 | fn check_apply(before: &str, after: &str, func: DiagnosticChecker) { | ||
143 | let file = SourceFile::parse(before); | ||
144 | let mut diagnostics = Vec::new(); | ||
145 | for node in file.syntax().descendants() { | ||
146 | func(&mut diagnostics, node); | ||
147 | } | ||
148 | let diagnostic = diagnostics | ||
149 | .pop() | ||
150 | .unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before)); | ||
151 | let fix = diagnostic.fix.unwrap(); | ||
152 | let actual = fix.edit.apply(&before); | ||
153 | assert_eq_text!(after, &actual); | ||
154 | } | ||
155 | |||
156 | #[test] | ||
157 | fn test_check_unnecessary_braces_in_use_statement() { | ||
158 | check_not_applicable( | ||
159 | " | ||
160 | use a; | ||
161 | use a::{c, d::e}; | ||
162 | ", | ||
163 | check_unnecessary_braces_in_use_statement, | ||
164 | ); | ||
165 | check_apply( | ||
166 | "use {b};", | ||
167 | "use b;", | ||
168 | check_unnecessary_braces_in_use_statement, | ||
169 | ); | ||
170 | check_apply( | ||
171 | "use a::{c};", | ||
172 | "use a::c;", | ||
173 | check_unnecessary_braces_in_use_statement, | ||
174 | ); | ||
175 | check_apply( | ||
176 | "use a::{self};", | ||
177 | "use a;", | ||
178 | check_unnecessary_braces_in_use_statement, | ||
179 | ); | ||
180 | check_apply( | ||
181 | "use a::{c, d::{e}};", | ||
182 | "use a::{c, d::e};", | ||
183 | check_unnecessary_braces_in_use_statement, | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | #[test] | ||
188 | fn test_check_struct_shorthand_initialization() { | ||
189 | check_not_applicable( | ||
190 | r#" | ||
191 | struct A { | ||
192 | a: &'static str | ||
193 | } | ||
194 | |||
195 | fn main() { | ||
196 | A { | ||
197 | a: "hello" | ||
198 | } | ||
199 | } | ||
200 | "#, | ||
201 | check_struct_shorthand_initialization, | ||
202 | ); | ||
203 | |||
204 | check_apply( | ||
205 | r#" | ||
206 | struct A { | ||
207 | a: &'static str | ||
208 | } | ||
209 | |||
210 | fn main() { | ||
211 | let a = "haha"; | ||
212 | A { | ||
213 | a: a | ||
214 | } | ||
215 | } | ||
216 | "#, | ||
217 | r#" | ||
218 | struct A { | ||
219 | a: &'static str | ||
220 | } | ||
221 | |||
222 | fn main() { | ||
223 | let a = "haha"; | ||
224 | A { | ||
225 | a | ||
226 | } | ||
227 | } | ||
228 | "#, | ||
229 | check_struct_shorthand_initialization, | ||
230 | ); | ||
231 | |||
232 | check_apply( | ||
233 | r#" | ||
234 | struct A { | ||
235 | a: &'static str, | ||
236 | b: &'static str | ||
237 | } | ||
238 | |||
239 | fn main() { | ||
240 | let a = "haha"; | ||
241 | let b = "bb"; | ||
242 | A { | ||
243 | a: a, | ||
244 | b | ||
245 | } | ||
246 | } | ||
247 | "#, | ||
248 | r#" | ||
249 | struct A { | ||
250 | a: &'static str, | ||
251 | b: &'static str | ||
252 | } | ||
253 | |||
254 | fn main() { | ||
255 | let a = "haha"; | ||
256 | let b = "bb"; | ||
257 | A { | ||
258 | a, | ||
259 | b | ||
260 | } | ||
261 | } | ||
262 | "#, | ||
263 | check_struct_shorthand_initialization, | ||
264 | ); | ||
265 | } | ||
266 | } | ||