aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs141
1 files changed, 73 insertions, 68 deletions
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index d48772225..069092528 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -12,58 +12,23 @@ use ra_text_edit::{TextEdit, TextEditBuilder};
12use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase}; 12use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase};
13 13
14pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { 14pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
15 let syntax = db.parse(file_id); 15 let source_file = db.parse(file_id);
16 let mut res = Vec::new();
17
18 syntax_errors(&mut res, &source_file);
19
20 for node in source_file.syntax().descendants() {
21 check_unnecessary_braces_in_use_statement(&mut res, file_id, node);
22 check_struct_shorthand_initialization(&mut res, file_id, node);
23 }
16 24
17 let mut res = syntax_diagnostics(file_id, &syntax);
18 if let Some(m) = source_binder::module_from_file_id(db, file_id) { 25 if let Some(m) = source_binder::module_from_file_id(db, file_id) {
19 for (name_node, problem) in m.problems(db) { 26 check_module(&mut res, db, file_id, m);
20 let source_root = db.file_source_root(file_id);
21 let diag = match problem {
22 Problem::UnresolvedModule { candidate } => {
23 let create_file =
24 FileSystemEdit::CreateFile { source_root, path: candidate.clone() };
25 let fix = SourceChange {
26 label: "create module".to_string(),
27 source_file_edits: Vec::new(),
28 file_system_edits: vec![create_file],
29 cursor_position: None,
30 };
31 Diagnostic {
32 range: name_node.range(),
33 message: "unresolved module".to_string(),
34 severity: Severity::Error,
35 fix: Some(fix),
36 }
37 }
38 Problem::NotDirOwner { move_to, candidate } => {
39 let move_file = FileSystemEdit::MoveFile {
40 src: file_id,
41 dst_source_root: source_root,
42 dst_path: move_to.clone(),
43 };
44 let create_file =
45 FileSystemEdit::CreateFile { source_root, path: move_to.join(candidate) };
46 let fix = SourceChange {
47 label: "move file and create module".to_string(),
48 source_file_edits: Vec::new(),
49 file_system_edits: vec![move_file, create_file],
50 cursor_position: None,
51 };
52 Diagnostic {
53 range: name_node.range(),
54 message: "can't declare module at this location".to_string(),
55 severity: Severity::Error,
56 fix: Some(fix),
57 }
58 }
59 };
60 res.push(diag)
61 }
62 }; 27 };
63 res 28 res
64} 29}
65 30
66fn syntax_diagnostics(file_id: FileId, file: &SourceFile) -> Vec<Diagnostic> { 31fn syntax_errors(acc: &mut Vec<Diagnostic>, source_file: &SourceFile) {
67 fn location_to_range(location: Location) -> TextRange { 32 fn location_to_range(location: Location) -> TextRange {
68 match location { 33 match location {
69 Location::Offset(offset) => TextRange::offset_len(offset, 1.into()), 34 Location::Offset(offset) => TextRange::offset_len(offset, 1.into()),
@@ -71,28 +36,17 @@ fn syntax_diagnostics(file_id: FileId, file: &SourceFile) -> Vec<Diagnostic> {
71 } 36 }
72 } 37 }
73 38
74 let mut errors: Vec<Diagnostic> = file 39 acc.extend(source_file.errors().into_iter().map(|err| Diagnostic {
75 .errors() 40 range: location_to_range(err.location()),
76 .into_iter() 41 message: format!("Syntax Error: {}", err),
77 .map(|err| Diagnostic { 42 severity: Severity::Error,
78 range: location_to_range(err.location()), 43 fix: None,
79 message: format!("Syntax Error: {}", err), 44 }));
80 severity: Severity::Error,
81 fix: None,
82 })
83 .collect();
84
85 for node in file.syntax().descendants() {
86 check_unnecessary_braces_in_use_statement(file_id, &mut errors, node);
87 check_struct_shorthand_initialization(file_id, &mut errors, node);
88 }
89
90 errors
91} 45}
92 46
93fn check_unnecessary_braces_in_use_statement( 47fn check_unnecessary_braces_in_use_statement(
94 file_id: FileId,
95 acc: &mut Vec<Diagnostic>, 48 acc: &mut Vec<Diagnostic>,
49 file_id: FileId,
96 node: &SyntaxNode, 50 node: &SyntaxNode,
97) -> Option<()> { 51) -> Option<()> {
98 let use_tree_list = ast::UseTreeList::cast(node)?; 52 let use_tree_list = ast::UseTreeList::cast(node)?;
@@ -140,8 +94,8 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
140} 94}
141 95
142fn check_struct_shorthand_initialization( 96fn check_struct_shorthand_initialization(
143 file_id: FileId,
144 acc: &mut Vec<Diagnostic>, 97 acc: &mut Vec<Diagnostic>,
98 file_id: FileId,
145 node: &SyntaxNode, 99 node: &SyntaxNode,
146) -> Option<()> { 100) -> Option<()> {
147 let struct_lit = ast::StructLit::cast(node)?; 101 let struct_lit = ast::StructLit::cast(node)?;
@@ -173,19 +127,70 @@ fn check_struct_shorthand_initialization(
173 Some(()) 127 Some(())
174} 128}
175 129
130fn check_module(
131 acc: &mut Vec<Diagnostic>,
132 db: &RootDatabase,
133 file_id: FileId,
134 module: hir::Module,
135) {
136 let source_root = db.file_source_root(file_id);
137 for (name_node, problem) in module.problems(db) {
138 let diag = match problem {
139 Problem::UnresolvedModule { candidate } => {
140 let create_file =
141 FileSystemEdit::CreateFile { source_root, path: candidate.clone() };
142 let fix = SourceChange {
143 label: "create module".to_string(),
144 source_file_edits: Vec::new(),
145 file_system_edits: vec![create_file],
146 cursor_position: None,
147 };
148 Diagnostic {
149 range: name_node.range(),
150 message: "unresolved module".to_string(),
151 severity: Severity::Error,
152 fix: Some(fix),
153 }
154 }
155 Problem::NotDirOwner { move_to, candidate } => {
156 let move_file = FileSystemEdit::MoveFile {
157 src: file_id,
158 dst_source_root: source_root,
159 dst_path: move_to.clone(),
160 };
161 let create_file =
162 FileSystemEdit::CreateFile { source_root, path: move_to.join(candidate) };
163 let fix = SourceChange {
164 label: "move file and create module".to_string(),
165 source_file_edits: Vec::new(),
166 file_system_edits: vec![move_file, create_file],
167 cursor_position: None,
168 };
169 Diagnostic {
170 range: name_node.range(),
171 message: "can't declare module at this location".to_string(),
172 severity: Severity::Error,
173 fix: Some(fix),
174 }
175 }
176 };
177 acc.push(diag)
178 }
179}
180
176#[cfg(test)] 181#[cfg(test)]
177mod tests { 182mod tests {
178 use test_utils::assert_eq_text; 183 use test_utils::assert_eq_text;
179 184
180 use super::*; 185 use super::*;
181 186
182 type DiagnosticChecker = fn(FileId, &mut Vec<Diagnostic>, &SyntaxNode) -> Option<()>; 187 type DiagnosticChecker = fn(&mut Vec<Diagnostic>, FileId, &SyntaxNode) -> Option<()>;
183 188
184 fn check_not_applicable(code: &str, func: DiagnosticChecker) { 189 fn check_not_applicable(code: &str, func: DiagnosticChecker) {
185 let file = SourceFile::parse(code); 190 let file = SourceFile::parse(code);
186 let mut diagnostics = Vec::new(); 191 let mut diagnostics = Vec::new();
187 for node in file.syntax().descendants() { 192 for node in file.syntax().descendants() {
188 func(FileId(0), &mut diagnostics, node); 193 func(&mut diagnostics, FileId(0), node);
189 } 194 }
190 assert!(diagnostics.is_empty()); 195 assert!(diagnostics.is_empty());
191 } 196 }
@@ -194,7 +199,7 @@ mod tests {
194 let file = SourceFile::parse(before); 199 let file = SourceFile::parse(before);
195 let mut diagnostics = Vec::new(); 200 let mut diagnostics = Vec::new();
196 for node in file.syntax().descendants() { 201 for node in file.syntax().descendants() {
197 func(FileId(0), &mut diagnostics, node); 202 func(&mut diagnostics, FileId(0), node);
198 } 203 }
199 let diagnostic = 204 let diagnostic =
200 diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before)); 205 diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before));