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.rs87
1 files changed, 87 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..81244b293
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/unresolved_module.rs
@@ -0,0 +1,87 @@
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, DiagnosticWithFix};
7
8impl DiagnosticWithFix for UnresolvedModule {
9 fn fix(
10 &self,
11 sema: &Semantics<RootDatabase>,
12 _resolve: &AssistResolveStrategy,
13 ) -> Option<Assist> {
14 let root = sema.db.parse_or_expand(self.file)?;
15 let unresolved_module = self.decl.to_node(&root);
16 Some(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 fix: Some(
49 Assist {
50 id: AssistId(
51 "create_module",
52 QuickFix,
53 ),
54 label: "Create module",
55 group: None,
56 target: 0..8,
57 source_change: Some(
58 SourceChange {
59 source_file_edits: {},
60 file_system_edits: [
61 CreateFile {
62 dst: AnchoredPathBuf {
63 anchor: FileId(
64 0,
65 ),
66 path: "foo.rs",
67 },
68 initial_contents: "",
69 },
70 ],
71 is_snippet: false,
72 },
73 ),
74 },
75 ),
76 unused: false,
77 code: Some(
78 DiagnosticCode(
79 "unresolved-module",
80 ),
81 ),
82 },
83 ]
84 "#]],
85 );
86 }
87}