aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/diagnostics.rs')
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
new file mode 100644
index 000000000..a499ac7c6
--- /dev/null
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -0,0 +1,69 @@
1use hir::{Problem, source_binder};
2use ra_ide_api_light::Severity;
3use ra_db::SourceDatabase;
4
5use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, db::RootDatabase};
6
7pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
8 let syntax = db.parse(file_id);
9
10 let mut res = ra_ide_api_light::diagnostics(&syntax)
11 .into_iter()
12 .map(|d| Diagnostic {
13 range: d.range,
14 message: d.msg,
15 severity: d.severity,
16 fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
17 })
18 .collect::<Vec<_>>();
19 if let Some(m) = source_binder::module_from_file_id(db, file_id) {
20 for (name_node, problem) in m.problems(db) {
21 let source_root = db.file_source_root(file_id);
22 let diag = match problem {
23 Problem::UnresolvedModule { candidate } => {
24 let create_file = FileSystemEdit::CreateFile {
25 source_root,
26 path: candidate.clone(),
27 };
28 let fix = SourceChange {
29 label: "create module".to_string(),
30 source_file_edits: Vec::new(),
31 file_system_edits: vec![create_file],
32 cursor_position: None,
33 };
34 Diagnostic {
35 range: name_node.range(),
36 message: "unresolved module".to_string(),
37 severity: Severity::Error,
38 fix: Some(fix),
39 }
40 }
41 Problem::NotDirOwner { move_to, candidate } => {
42 let move_file = FileSystemEdit::MoveFile {
43 src: file_id,
44 dst_source_root: source_root,
45 dst_path: move_to.clone(),
46 };
47 let create_file = FileSystemEdit::CreateFile {
48 source_root,
49 path: move_to.join(candidate),
50 };
51 let fix = SourceChange {
52 label: "move file and create module".to_string(),
53 source_file_edits: Vec::new(),
54 file_system_edits: vec![move_file, create_file],
55 cursor_position: None,
56 };
57 Diagnostic {
58 range: name_node.range(),
59 message: "can't declare module at this location".to_string(),
60 severity: Severity::Error,
61 fix: Some(fix),
62 }
63 }
64 };
65 res.push(diag)
66 }
67 };
68 res
69}