aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-03-15 00:39:23 +0000
committerJonas Schievink <[email protected]>2021-03-15 00:39:41 +0000
commit8b4cbbb87c6dc946711570096d857603c30a5d8d (patch)
tree90f05142189ae325232cf28d7ee05509fda0266f /crates
parent7b1a0d5fb724d3f802fe23f9b4455ffd6d7e114f (diff)
Redo it properly and add a quickfix
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/Cargo.toml1
-rw-r--r--crates/ide/src/diagnostics.rs161
-rw-r--r--crates/ide/src/diagnostics/unlinked_file.rs154
3 files changed, 309 insertions, 7 deletions
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml
index 107bd8432..6ae7c9e6c 100644
--- a/crates/ide/Cargo.toml
+++ b/crates/ide/Cargo.toml
@@ -38,3 +38,4 @@ hir = { path = "../hir", version = "0.0.0" }
38[dev-dependencies] 38[dev-dependencies]
39test_utils = { path = "../test_utils" } 39test_utils = { path = "../test_utils" }
40expect-test = "1.1" 40expect-test = "1.1"
41cov-mark = "1.1.0"
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 760c84780..22697a537 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -6,6 +6,7 @@
6 6
7mod fixes; 7mod fixes;
8mod field_shorthand; 8mod field_shorthand;
9mod unlinked_file;
9 10
10use std::cell::RefCell; 11use std::cell::RefCell;
11 12
@@ -22,6 +23,7 @@ use syntax::{
22 SyntaxNode, SyntaxNodePtr, TextRange, 23 SyntaxNode, SyntaxNodePtr, TextRange,
23}; 24};
24use text_edit::TextEdit; 25use text_edit::TextEdit;
26use unlinked_file::UnlinkedFile;
25 27
26use crate::{FileId, Label, SourceChange}; 28use crate::{FileId, Label, SourceChange};
27 29
@@ -156,6 +158,18 @@ pub(crate) fn diagnostics(
156 .with_code(Some(d.code())), 158 .with_code(Some(d.code())),
157 ); 159 );
158 }) 160 })
161 .on::<UnlinkedFile, _>(|d| {
162 // Override severity and mark as unused.
163 res.borrow_mut().push(
164 Diagnostic::hint(
165 sema.diagnostics_display_range(d.display_source()).range,
166 d.message(),
167 )
168 .with_unused(true)
169 .with_fix(d.fix(&sema))
170 .with_code(Some(d.code())),
171 );
172 })
159 .on::<hir::diagnostics::UnresolvedProcMacro, _>(|d| { 173 .on::<hir::diagnostics::UnresolvedProcMacro, _>(|d| {
160 // Use more accurate position if available. 174 // Use more accurate position if available.
161 let display_range = d 175 let display_range = d
@@ -200,13 +214,7 @@ pub(crate) fn diagnostics(
200 match sema.to_module_def(file_id) { 214 match sema.to_module_def(file_id) {
201 Some(m) => m.diagnostics(db, &mut sink), 215 Some(m) => m.diagnostics(db, &mut sink),
202 None => { 216 None => {
203 res.borrow_mut().push( 217 sink.push(UnlinkedFile { file_id, node: SyntaxNodePtr::new(&parse.tree().syntax()) });
204 Diagnostic::hint(
205 parse.tree().syntax().text_range(),
206 "file not included in module tree".to_string(),
207 )
208 .with_unused(true),
209 );
210 } 218 }
211 } 219 }
212 220
@@ -317,6 +325,17 @@ mod tests {
317 ); 325 );
318 } 326 }
319 327
328 /// Checks that there's a diagnostic *without* fix at `$0`.
329 fn check_no_fix(ra_fixture: &str) {
330 let (analysis, file_position) = fixture::position(ra_fixture);
331 let diagnostic = analysis
332 .diagnostics(&DiagnosticsConfig::default(), file_position.file_id)
333 .unwrap()
334 .pop()
335 .unwrap();
336 assert!(diagnostic.fix.is_none(), "got a fix when none was expected: {:?}", diagnostic);
337 }
338
320 /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics 339 /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
321 /// apply to the file containing the cursor. 340 /// apply to the file containing the cursor.
322 pub(crate) fn check_no_diagnostics(ra_fixture: &str) { 341 pub(crate) fn check_no_diagnostics(ra_fixture: &str) {
@@ -985,4 +1004,132 @@ impl TestStruct {
985 1004
986 check_fix(input, expected); 1005 check_fix(input, expected);
987 } 1006 }
1007
1008 #[test]
1009 fn unlinked_file_prepend_first_item() {
1010 cov_mark::check!(unlinked_file_prepend_before_first_item);
1011 check_fix(
1012 r#"
1013//- /main.rs
1014fn f() {}
1015//- /foo.rs
1016$0
1017"#,
1018 r#"
1019mod foo;
1020
1021fn f() {}
1022"#,
1023 );
1024 }
1025
1026 #[test]
1027 fn unlinked_file_append_mod() {
1028 cov_mark::check!(unlinked_file_append_to_existing_mods);
1029 check_fix(
1030 r#"
1031//- /main.rs
1032//! Comment on top
1033
1034mod preexisting;
1035
1036mod preexisting2;
1037
1038struct S;
1039
1040mod preexisting_bottom;)
1041//- /foo.rs
1042$0
1043"#,
1044 r#"
1045//! Comment on top
1046
1047mod preexisting;
1048
1049mod preexisting2;
1050mod foo;
1051
1052struct S;
1053
1054mod preexisting_bottom;)
1055"#,
1056 );
1057 }
1058
1059 #[test]
1060 fn unlinked_file_insert_in_empty_file() {
1061 cov_mark::check!(unlinked_file_empty_file);
1062 check_fix(
1063 r#"
1064//- /main.rs
1065//- /foo.rs
1066$0
1067"#,
1068 r#"
1069mod foo;
1070"#,
1071 );
1072 }
1073
1074 #[test]
1075 fn unlinked_file_old_style_modrs() {
1076 check_fix(
1077 r#"
1078//- /main.rs
1079mod submod;
1080//- /submod/mod.rs
1081// in mod.rs
1082//- /submod/foo.rs
1083$0
1084"#,
1085 r#"
1086// in mod.rs
1087mod foo;
1088"#,
1089 );
1090 }
1091
1092 #[test]
1093 fn unlinked_file_new_style_mod() {
1094 check_fix(
1095 r#"
1096//- /main.rs
1097mod submod;
1098//- /submod.rs
1099//- /submod/foo.rs
1100$0
1101"#,
1102 r#"
1103mod foo;
1104"#,
1105 );
1106 }
1107
1108 #[test]
1109 fn unlinked_file_with_cfg_off() {
1110 cov_mark::check!(unlinked_file_skip_fix_when_mod_already_exists);
1111 check_no_fix(
1112 r#"
1113//- /main.rs
1114#[cfg(never)]
1115mod foo;
1116
1117//- /foo.rs
1118$0
1119"#,
1120 );
1121 }
1122
1123 #[test]
1124 fn unlinked_file_with_cfg_on() {
1125 check_no_diagnostics(
1126 r#"
1127//- /main.rs
1128#[cfg(not(never))]
1129mod foo;
1130
1131//- /foo.rs
1132"#,
1133 );
1134 }
988} 1135}
diff --git a/crates/ide/src/diagnostics/unlinked_file.rs b/crates/ide/src/diagnostics/unlinked_file.rs
new file mode 100644
index 000000000..f6dc671b9
--- /dev/null
+++ b/crates/ide/src/diagnostics/unlinked_file.rs
@@ -0,0 +1,154 @@
1use hir::{
2 db::DefDatabase,
3 diagnostics::{Diagnostic, DiagnosticCode},
4 InFile,
5};
6use ide_db::{
7 base_db::{FileId, FileLoader, SourceDatabase, SourceDatabaseExt},
8 source_change::SourceChange,
9 RootDatabase,
10};
11use syntax::{
12 ast::{self, ModuleItemOwner, NameOwner},
13 AstNode, SyntaxNodePtr,
14};
15use text_edit::TextEdit;
16
17use crate::Fix;
18
19use super::fixes::DiagnosticWithFix;
20
21#[derive(Debug)]
22pub struct UnlinkedFile {
23 pub file_id: FileId,
24 pub node: SyntaxNodePtr,
25}
26
27impl Diagnostic for UnlinkedFile {
28 fn code(&self) -> DiagnosticCode {
29 DiagnosticCode("unlinked-file")
30 }
31
32 fn message(&self) -> String {
33 "file not included in module tree".to_string()
34 }
35
36 fn display_source(&self) -> InFile<SyntaxNodePtr> {
37 InFile::new(self.file_id.into(), self.node.clone())
38 }
39
40 fn as_any(&self) -> &(dyn std::any::Any + Send + 'static) {
41 self
42 }
43}
44
45impl DiagnosticWithFix for UnlinkedFile {
46 fn fix(&self, sema: &hir::Semantics<RootDatabase>) -> Option<Fix> {
47 // If there's an existing module that could add a `mod` item to include the unlinked file,
48 // suggest that as a fix.
49
50 let source_root = sema.db.source_root(sema.db.file_source_root(self.file_id));
51 let our_path = source_root.path_for_file(&self.file_id)?;
52 let module_name = our_path.name_and_extension()?.0;
53
54 // Candidates to look for:
55 // - `mod.rs` in the same folder
56 // - we also check `main.rs` and `lib.rs`
57 // - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
58 let parent = our_path.parent()?;
59 let mut paths =
60 vec![parent.join("mod.rs")?, parent.join("main.rs")?, parent.join("lib.rs")?];
61
62 // `submod/bla.rs` -> `submod.rs`
63 if let Some(newmod) = (|| {
64 let name = parent.name_and_extension()?.0;
65 parent.parent()?.join(&format!("{}.rs", name))
66 })() {
67 paths.push(newmod);
68 }
69
70 for path in &paths {
71 if let Some(parent_id) = source_root.file_for_path(path) {
72 for krate in sema.db.relevant_crates(*parent_id).iter() {
73 let crate_def_map = sema.db.crate_def_map(*krate);
74 for (_, module) in crate_def_map.modules() {
75 if module.origin.is_inline() {
76 // We don't handle inline `mod parent {}`s, they use different paths.
77 continue;
78 }
79
80 if module.origin.file_id() == Some(*parent_id) {
81 return make_fix(sema.db, *parent_id, module_name, self.file_id);
82 }
83 }
84 }
85 }
86 }
87
88 None
89 }
90}
91
92fn make_fix(
93 db: &RootDatabase,
94 parent_file_id: FileId,
95 new_mod_name: &str,
96 added_file_id: FileId,
97) -> Option<Fix> {
98 fn is_outline_mod(item: &ast::Item) -> bool {
99 matches!(item, ast::Item::Module(m) if m.item_list().is_none())
100 }
101
102 let mod_decl = format!("mod {};", new_mod_name);
103 let ast: ast::SourceFile = db.parse(parent_file_id).tree();
104
105 let mut builder = TextEdit::builder();
106
107 // If there's an existing `mod m;` statement matching the new one, don't emit a fix (it's
108 // probably `#[cfg]`d out).
109 for item in ast.items() {
110 if let ast::Item::Module(m) = item {
111 if let Some(name) = m.name() {
112 if m.item_list().is_none() && name.to_string() == new_mod_name {
113 cov_mark::hit!(unlinked_file_skip_fix_when_mod_already_exists);
114 return None;
115 }
116 }
117 }
118 }
119
120 // If there are existing `mod m;` items, append after them (after the first group of them, rather).
121 match ast
122 .items()
123 .skip_while(|item| !is_outline_mod(item))
124 .take_while(|item| is_outline_mod(item))
125 .last()
126 {
127 Some(last) => {
128 cov_mark::hit!(unlinked_file_append_to_existing_mods);
129 builder.insert(last.syntax().text_range().end(), format!("\n{}", mod_decl));
130 }
131 None => {
132 // Prepend before the first item in the file.
133 match ast.items().next() {
134 Some(item) => {
135 cov_mark::hit!(unlinked_file_prepend_before_first_item);
136 builder.insert(item.syntax().text_range().start(), format!("{}\n\n", mod_decl));
137 }
138 None => {
139 // No items in the file, so just append at the end.
140 cov_mark::hit!(unlinked_file_empty_file);
141 builder.insert(ast.syntax().text_range().end(), format!("{}\n", mod_decl));
142 }
143 }
144 }
145 }
146
147 let edit = builder.finish();
148 let trigger_range = db.parse(added_file_id).tree().syntax().text_range();
149 Some(Fix::new(
150 &format!("Insert `{}`", mod_decl),
151 SourceChange::from_text_edit(parent_file_id, edit),
152 trigger_range,
153 ))
154}