aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics/fixes/unresolved_module.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics/fixes/unresolved_module.rs')
-rw-r--r--crates/ide/src/diagnostics/fixes/unresolved_module.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics/fixes/unresolved_module.rs b/crates/ide/src/diagnostics/fixes/unresolved_module.rs
new file mode 100644
index 000000000..b3d0283bb
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/unresolved_module.rs
@@ -0,0 +1,89 @@
1use hir::{db::AstDatabase, diagnostics::UnresolvedModule, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{base_db::AnchoredPathBuf, source_change::FileSystemEdit, RootDatabase};
4use syntax::AstNode;
5
6use crate::diagnostics::{fix, DiagnosticWithFixes};
7
8impl DiagnosticWithFixes for UnresolvedModule {
9 fn fixes(
10 &self,
11 sema: &Semantics<RootDatabase>,
12 _resolve: &AssistResolveStrategy,
13 ) -> Option<Vec<Assist>> {
14 let root = sema.db.parse_or_expand(self.file)?;
15 let unresolved_module = self.decl.to_node(&root);
16 Some(vec![fix(
17 "create_module",
18 "Create module",
19 FileSystemEdit::CreateFile {
20 dst: AnchoredPathBuf {
21 anchor: self.file.original_file(sema.db),
22 path: self.candidate.clone(),
23 },
24 initial_contents: "".to_string(),
25 }
26 .into(),
27 unresolved_module.syntax().text_range(),
28 )])
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use expect_test::expect;
35
36 use crate::diagnostics::tests::check_expect;
37
38 #[test]
39 fn test_unresolved_module_diagnostic() {
40 check_expect(
41 r#"mod foo;"#,
42 expect![[r#"
43 [
44 Diagnostic {
45 message: "unresolved module",
46 range: 0..8,
47 severity: Error,
48 fixes: Some(
49 [
50 Assist {
51 id: AssistId(
52 "create_module",
53 QuickFix,
54 ),
55 label: "Create module",
56 group: None,
57 target: 0..8,
58 source_change: Some(
59 SourceChange {
60 source_file_edits: {},
61 file_system_edits: [
62 CreateFile {
63 dst: AnchoredPathBuf {
64 anchor: FileId(
65 0,
66 ),
67 path: "foo.rs",
68 },
69 initial_contents: "",
70 },
71 ],
72 is_snippet: false,
73 },
74 ),
75 },
76 ],
77 ),
78 unused: false,
79 code: Some(
80 DiagnosticCode(
81 "unresolved-module",
82 ),
83 ),
84 },
85 ]
86 "#]],
87 );
88 }
89}