diff options
-rw-r--r-- | crates/ra_editor/src/lib.rs | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 36cabed25..48676f2e8 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -31,10 +31,17 @@ pub struct HighlightedRange { | |||
31 | pub tag: &'static str, | 31 | pub tag: &'static str, |
32 | } | 32 | } |
33 | 33 | ||
34 | #[derive(Debug, Copy, Clone)] | ||
35 | pub enum Severity { | ||
36 | Error, | ||
37 | Warning | ||
38 | } | ||
39 | |||
34 | #[derive(Debug)] | 40 | #[derive(Debug)] |
35 | pub struct Diagnostic { | 41 | pub struct Diagnostic { |
36 | pub range: TextRange, | 42 | pub range: TextRange, |
37 | pub msg: String, | 43 | pub msg: String, |
44 | pub severity: Severity, | ||
38 | } | 45 | } |
39 | 46 | ||
40 | #[derive(Debug)] | 47 | #[derive(Debug)] |
@@ -97,13 +104,36 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> { | |||
97 | } | 104 | } |
98 | } | 105 | } |
99 | 106 | ||
100 | file.errors() | 107 | let mut errors: Vec<Diagnostic> = file.errors() |
101 | .into_iter() | 108 | .into_iter() |
102 | .map(|err| Diagnostic { | 109 | .map(|err| Diagnostic { |
103 | range: location_to_range(err.location()), | 110 | range: location_to_range(err.location()), |
104 | msg: format!("Syntax Error: {}", err), | 111 | msg: format!("Syntax Error: {}", err), |
112 | severity: Severity::Error, | ||
105 | }) | 113 | }) |
106 | .collect() | 114 | .collect(); |
115 | |||
116 | let warnings = check_unnecessary_braces_in_use_statement(file); | ||
117 | |||
118 | errors.extend(warnings); | ||
119 | errors | ||
120 | } | ||
121 | |||
122 | fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagnostic> { | ||
123 | let mut diagnostics = Vec::new(); | ||
124 | for node in file.syntax().descendants() { | ||
125 | if let Some(use_tree_list) = ast::UseTreeList::cast(node) { | ||
126 | if use_tree_list.use_trees().count() <= 1 { | ||
127 | diagnostics.push(Diagnostic { | ||
128 | range: use_tree_list.syntax().range(), | ||
129 | msg: format!("Unnecessary braces in use statement"), | ||
130 | severity: Severity::Warning, | ||
131 | }) | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | |||
136 | diagnostics | ||
107 | } | 137 | } |
108 | 138 | ||
109 | pub fn syntax_tree(file: &SourceFileNode) -> String { | 139 | pub fn syntax_tree(file: &SourceFileNode) -> String { |
@@ -204,4 +234,25 @@ fn test_foo() {} | |||
204 | 234 | ||
205 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); | 235 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); |
206 | } | 236 | } |
237 | |||
238 | #[test] | ||
239 | fn test_check_unnecessary_braces_in_use_statement() { | ||
240 | let file = SourceFileNode::parse( | ||
241 | r#" | ||
242 | use a; | ||
243 | use {b}; | ||
244 | use a::{c}; | ||
245 | use a::{c, d::e}; | ||
246 | use a::{c, d::{e}}; | ||
247 | fn main() {} | ||
248 | "#, | ||
249 | ); | ||
250 | let diagnostics = check_unnecessary_braces_in_use_statement(&file); | ||
251 | assert_eq_dbg( | ||
252 | r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: Warning }, | ||
253 | Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: Warning }, | ||
254 | Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: Warning }]"#, | ||
255 | &diagnostics, | ||
256 | ) | ||
257 | } | ||
207 | } | 258 | } |