2023-09-29 10:37:57 +00:00
|
|
|
use hir::{db::ExpandDatabase, HirFileIdExt};
|
2021-06-14 10:27:11 +00:00
|
|
|
use ide_db::{assists::Assist, base_db::AnchoredPathBuf, source_change::FileSystemEdit};
|
2022-03-11 15:49:41 +00:00
|
|
|
use itertools::Itertools;
|
2021-05-17 09:04:17 +00:00
|
|
|
use syntax::AstNode;
|
|
|
|
|
2023-06-14 22:17:22 +00:00
|
|
|
use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsContext};
|
2021-05-17 09:04:17 +00:00
|
|
|
|
2021-06-13 11:41:19 +00:00
|
|
|
// Diagnostic: unresolved-module
|
|
|
|
//
|
|
|
|
// This diagnostic is triggered if rust-analyzer is unable to discover referred module.
|
2021-06-14 16:32:39 +00:00
|
|
|
pub(crate) fn unresolved_module(
|
2021-06-13 12:27:15 +00:00
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
d: &hir::UnresolvedModule,
|
|
|
|
) -> Diagnostic {
|
2023-06-14 22:17:22 +00:00
|
|
|
Diagnostic::new_with_syntax_node_ptr(
|
|
|
|
ctx,
|
|
|
|
DiagnosticCode::RustcHardError("E0583"),
|
2022-03-11 15:49:41 +00:00
|
|
|
match &*d.candidates {
|
2024-02-09 15:35:55 +00:00
|
|
|
[] => "unresolved module".to_owned(),
|
2022-12-23 18:42:58 +00:00
|
|
|
[candidate] => format!("unresolved module, can't find module file: {candidate}"),
|
2022-03-11 15:49:41 +00:00
|
|
|
[candidates @ .., last] => {
|
|
|
|
format!(
|
|
|
|
"unresolved module, can't find module file: {}, or {}",
|
|
|
|
candidates.iter().format(", "),
|
|
|
|
last
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2024-01-18 12:59:49 +00:00
|
|
|
d.decl.map(|it| it.into()),
|
2021-06-13 11:41:19 +00:00
|
|
|
)
|
|
|
|
.with_fixes(fixes(ctx, d))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Option<Vec<Assist>> {
|
2023-04-16 17:20:48 +00:00
|
|
|
let root = ctx.sema.db.parse_or_expand(d.decl.file_id);
|
2021-06-13 11:41:19 +00:00
|
|
|
let unresolved_module = d.decl.value.to_node(&root);
|
2022-03-11 15:49:41 +00:00
|
|
|
Some(
|
|
|
|
d.candidates
|
|
|
|
.iter()
|
|
|
|
.map(|candidate| {
|
|
|
|
fix(
|
|
|
|
"create_module",
|
2022-04-04 13:54:29 +00:00
|
|
|
&format!("Create module at `{candidate}`"),
|
2022-03-11 15:49:41 +00:00
|
|
|
FileSystemEdit::CreateFile {
|
|
|
|
dst: AnchoredPathBuf {
|
2024-07-17 15:35:40 +00:00
|
|
|
anchor: d.decl.file_id.original_file(ctx.sema.db).file_id(),
|
2022-03-11 15:49:41 +00:00
|
|
|
path: candidate.clone(),
|
|
|
|
},
|
2024-02-09 15:35:55 +00:00
|
|
|
initial_contents: "".to_owned(),
|
2022-03-11 15:49:41 +00:00
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
unresolved_module.syntax().text_range(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
2021-05-17 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-01-04 17:20:38 +00:00
|
|
|
use crate::tests::check_diagnostics;
|
2021-06-13 11:41:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unresolved_module() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
mod bar;
|
2022-03-11 15:49:41 +00:00
|
|
|
//^^^^^^^^ 💡 error: unresolved module, can't find module file: bar.rs, or bar/mod.rs
|
2021-06-13 11:41:19 +00:00
|
|
|
mod baz {}
|
|
|
|
//- /foo.rs
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2021-05-17 09:04:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic() {
|
2024-01-04 17:20:38 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
mod foo;
|
|
|
|
//^^^^^^^^ 💡 error: unresolved module, can't find module file: foo.rs, or foo/mod.rs
|
|
|
|
"#,
|
2021-05-17 09:04:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|