aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics/fixes/unresolved_module.rs
blob: 81244b29386e5ed7b6ffdb425c8596d91d434d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use hir::{db::AstDatabase, diagnostics::UnresolvedModule, Semantics};
use ide_assists::{Assist, AssistResolveStrategy};
use ide_db::{base_db::AnchoredPathBuf, source_change::FileSystemEdit, RootDatabase};
use syntax::AstNode;

use crate::diagnostics::{fix, DiagnosticWithFix};

impl DiagnosticWithFix for UnresolvedModule {
    fn fix(
        &self,
        sema: &Semantics<RootDatabase>,
        _resolve: &AssistResolveStrategy,
    ) -> Option<Assist> {
        let root = sema.db.parse_or_expand(self.file)?;
        let unresolved_module = self.decl.to_node(&root);
        Some(fix(
            "create_module",
            "Create module",
            FileSystemEdit::CreateFile {
                dst: AnchoredPathBuf {
                    anchor: self.file.original_file(sema.db),
                    path: self.candidate.clone(),
                },
                initial_contents: "".to_string(),
            }
            .into(),
            unresolved_module.syntax().text_range(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use expect_test::expect;

    use crate::diagnostics::tests::check_expect;

    #[test]
    fn test_unresolved_module_diagnostic() {
        check_expect(
            r#"mod foo;"#,
            expect![[r#"
                [
                    Diagnostic {
                        message: "unresolved module",
                        range: 0..8,
                        severity: Error,
                        fix: Some(
                            Assist {
                                id: AssistId(
                                    "create_module",
                                    QuickFix,
                                ),
                                label: "Create module",
                                group: None,
                                target: 0..8,
                                source_change: Some(
                                    SourceChange {
                                        source_file_edits: {},
                                        file_system_edits: [
                                            CreateFile {
                                                dst: AnchoredPathBuf {
                                                    anchor: FileId(
                                                        0,
                                                    ),
                                                    path: "foo.rs",
                                                },
                                                initial_contents: "",
                                            },
                                        ],
                                        is_snippet: false,
                                    },
                                ),
                            },
                        ),
                        unused: false,
                        code: Some(
                            DiagnosticCode(
                                "unresolved-module",
                            ),
                        ),
                    },
                ]
            "#]],
        );
    }
}