aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics/unlinked_file.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-15 01:23:29 +0000
committerGitHub <[email protected]>2021-03-15 01:23:29 +0000
commitde360275416ca095102f2b17d6ca1de3bd091fdb (patch)
tree319752b9ec07b075941aeab8795242f3a92ec2c0 /crates/ide/src/diagnostics/unlinked_file.rs
parent5ba7852cf153688d5b5035a9a2a2145aa7334d79 (diff)
parent32e1ca54eac6b5d401c2f33ec1281794ef9a0a52 (diff)
Merge #7966
7966: Diagnose files that aren't in the module tree r=jonas-schievink a=jonas-schievink Fixes https://github.com/rust-analyzer/rust-analyzer/issues/6377 I'm not sure if this is the best way to do this. It will cause false positives for all `include!`d files (though I'm not sure how much IDE functionality we have for these). Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/ide/src/diagnostics/unlinked_file.rs')
-rw-r--r--crates/ide/src/diagnostics/unlinked_file.rs156
1 files changed, 156 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics/unlinked_file.rs b/crates/ide/src/diagnostics/unlinked_file.rs
new file mode 100644
index 000000000..c5741bf6b
--- /dev/null
+++ b/crates/ide/src/diagnostics/unlinked_file.rs
@@ -0,0 +1,156 @@
1//! Diagnostic emitted for files that aren't part of any crate.
2
3use hir::{
4 db::DefDatabase,
5 diagnostics::{Diagnostic, DiagnosticCode},
6 InFile,
7};
8use ide_db::{
9 base_db::{FileId, FileLoader, SourceDatabase, SourceDatabaseExt},
10 source_change::SourceChange,
11 RootDatabase,
12};
13use syntax::{
14 ast::{self, ModuleItemOwner, NameOwner},
15 AstNode, SyntaxNodePtr,
16};
17use text_edit::TextEdit;
18
19use crate::Fix;
20
21use super::fixes::DiagnosticWithFix;
22
23#[derive(Debug)]
24pub(crate) struct UnlinkedFile {
25 pub(crate) file_id: FileId,
26 pub(crate) node: SyntaxNodePtr,
27}
28
29impl Diagnostic for UnlinkedFile {
30 fn code(&self) -> DiagnosticCode {
31 DiagnosticCode("unlinked-file")
32 }
33
34 fn message(&self) -> String {
35 "file not included in module tree".to_string()
36 }
37
38 fn display_source(&self) -> InFile<SyntaxNodePtr> {
39 InFile::new(self.file_id.into(), self.node.clone())
40 }
41
42 fn as_any(&self) -> &(dyn std::any::Any + Send + 'static) {
43 self
44 }
45}
46
47impl DiagnosticWithFix for UnlinkedFile {
48 fn fix(&self, sema: &hir::Semantics<RootDatabase>) -> Option<Fix> {
49 // If there's an existing module that could add a `mod` item to include the unlinked file,
50 // suggest that as a fix.
51
52 let source_root = sema.db.source_root(sema.db.file_source_root(self.file_id));
53 let our_path = source_root.path_for_file(&self.file_id)?;
54 let module_name = our_path.name_and_extension()?.0;
55
56 // Candidates to look for:
57 // - `mod.rs` in the same folder
58 // - we also check `main.rs` and `lib.rs`
59 // - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
60 let parent = our_path.parent()?;
61 let mut paths =
62 vec![parent.join("mod.rs")?, parent.join("main.rs")?, parent.join("lib.rs")?];
63
64 // `submod/bla.rs` -> `submod.rs`
65 if let Some(newmod) = (|| {
66 let name = parent.name_and_extension()?.0;
67 parent.parent()?.join(&format!("{}.rs", name))
68 })() {
69 paths.push(newmod);
70 }
71
72 for path in &paths {
73 if let Some(parent_id) = source_root.file_for_path(path) {
74 for krate in sema.db.relevant_crates(*parent_id).iter() {
75 let crate_def_map = sema.db.crate_def_map(*krate);
76 for (_, module) in crate_def_map.modules() {
77 if module.origin.is_inline() {
78 // We don't handle inline `mod parent {}`s, they use different paths.
79 continue;
80 }
81
82 if module.origin.file_id() == Some(*parent_id) {
83 return make_fix(sema.db, *parent_id, module_name, self.file_id);
84 }
85 }
86 }
87 }
88 }
89
90 None
91 }
92}
93
94fn make_fix(
95 db: &RootDatabase,
96 parent_file_id: FileId,
97 new_mod_name: &str,
98 added_file_id: FileId,
99) -> Option<Fix> {
100 fn is_outline_mod(item: &ast::Item) -> bool {
101 matches!(item, ast::Item::Module(m) if m.item_list().is_none())
102 }
103
104 let mod_decl = format!("mod {};", new_mod_name);
105 let ast: ast::SourceFile = db.parse(parent_file_id).tree();
106
107 let mut builder = TextEdit::builder();
108
109 // If there's an existing `mod m;` statement matching the new one, don't emit a fix (it's
110 // probably `#[cfg]`d out).
111 for item in ast.items() {
112 if let ast::Item::Module(m) = item {
113 if let Some(name) = m.name() {
114 if m.item_list().is_none() && name.to_string() == new_mod_name {
115 cov_mark::hit!(unlinked_file_skip_fix_when_mod_already_exists);
116 return None;
117 }
118 }
119 }
120 }
121
122 // If there are existing `mod m;` items, append after them (after the first group of them, rather).
123 match ast
124 .items()
125 .skip_while(|item| !is_outline_mod(item))
126 .take_while(|item| is_outline_mod(item))
127 .last()
128 {
129 Some(last) => {
130 cov_mark::hit!(unlinked_file_append_to_existing_mods);
131 builder.insert(last.syntax().text_range().end(), format!("\n{}", mod_decl));
132 }
133 None => {
134 // Prepend before the first item in the file.
135 match ast.items().next() {
136 Some(item) => {
137 cov_mark::hit!(unlinked_file_prepend_before_first_item);
138 builder.insert(item.syntax().text_range().start(), format!("{}\n\n", mod_decl));
139 }
140 None => {
141 // No items in the file, so just append at the end.
142 cov_mark::hit!(unlinked_file_empty_file);
143 builder.insert(ast.syntax().text_range().end(), format!("{}\n", mod_decl));
144 }
145 }
146 }
147 }
148
149 let edit = builder.finish();
150 let trigger_range = db.parse(added_file_id).tree().syntax().text_range();
151 Some(Fix::new(
152 &format!("Insert `{}`", mod_decl),
153 SourceChange::from_text_edit(parent_file_id, edit),
154 trigger_range,
155 ))
156}