rust-analyzer/crates/ra_ide_api/src/diagnostics.rs

70 lines
2.7 KiB
Rust
Raw Normal View History

2019-02-08 11:24:39 +00:00
use hir::{Problem, source_binder};
use ra_ide_api_light::Severity;
2019-02-08 08:52:18 +00:00
use ra_db::SourceDatabase;
2019-01-08 19:33:36 +00:00
2019-02-08 11:30:21 +00:00
use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, db::RootDatabase};
2019-01-08 19:33:36 +00:00
2019-02-08 11:30:21 +00:00
pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
let syntax = db.parse(file_id);
2019-01-08 19:33:36 +00:00
2019-02-08 11:30:21 +00:00
let mut res = ra_ide_api_light::diagnostics(&syntax)
.into_iter()
.map(|d| Diagnostic {
range: d.range,
message: d.msg,
severity: d.severity,
fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
})
.collect::<Vec<_>>();
if let Some(m) = source_binder::module_from_file_id(db, file_id) {
for (name_node, problem) in m.problems(db) {
let source_root = db.file_source_root(file_id);
let diag = match problem {
Problem::UnresolvedModule { candidate } => {
let create_file = FileSystemEdit::CreateFile {
source_root,
path: candidate.clone(),
};
let fix = SourceChange {
label: "create module".to_string(),
source_file_edits: Vec::new(),
file_system_edits: vec![create_file],
cursor_position: None,
};
Diagnostic {
range: name_node.range(),
message: "unresolved module".to_string(),
severity: Severity::Error,
fix: Some(fix),
2019-01-08 19:33:36 +00:00
}
2019-02-08 11:30:21 +00:00
}
Problem::NotDirOwner { move_to, candidate } => {
let move_file = FileSystemEdit::MoveFile {
src: file_id,
dst_source_root: source_root,
dst_path: move_to.clone(),
};
let create_file = FileSystemEdit::CreateFile {
source_root,
path: move_to.join(candidate),
};
let fix = SourceChange {
label: "move file and create module".to_string(),
source_file_edits: Vec::new(),
file_system_edits: vec![move_file, create_file],
cursor_position: None,
};
Diagnostic {
range: name_node.range(),
message: "can't declare module at this location".to_string(),
severity: Severity::Error,
fix: Some(fix),
2019-01-08 19:33:36 +00:00
}
2019-02-08 11:30:21 +00:00
}
};
res.push(diag)
}
};
res
2019-01-08 19:33:36 +00:00
}