diff options
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 163 |
1 files changed, 160 insertions, 3 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index fe32f39b6..22697a537 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -6,6 +6,7 @@ | |||
6 | 6 | ||
7 | mod fixes; | 7 | mod fixes; |
8 | mod field_shorthand; | 8 | mod field_shorthand; |
9 | mod unlinked_file; | ||
9 | 10 | ||
10 | use std::cell::RefCell; | 11 | use std::cell::RefCell; |
11 | 12 | ||
@@ -22,6 +23,7 @@ use syntax::{ | |||
22 | SyntaxNode, SyntaxNodePtr, TextRange, | 23 | SyntaxNode, SyntaxNodePtr, TextRange, |
23 | }; | 24 | }; |
24 | use text_edit::TextEdit; | 25 | use text_edit::TextEdit; |
26 | use unlinked_file::UnlinkedFile; | ||
25 | 27 | ||
26 | use crate::{FileId, Label, SourceChange}; | 28 | use 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 |
@@ -197,9 +211,13 @@ pub(crate) fn diagnostics( | |||
197 | ); | 211 | ); |
198 | }); | 212 | }); |
199 | 213 | ||
200 | if let Some(m) = sema.to_module_def(file_id) { | 214 | match sema.to_module_def(file_id) { |
201 | m.diagnostics(db, &mut sink); | 215 | Some(m) => m.diagnostics(db, &mut sink), |
202 | }; | 216 | None => { |
217 | sink.push(UnlinkedFile { file_id, node: SyntaxNodePtr::new(&parse.tree().syntax()) }); | ||
218 | } | ||
219 | } | ||
220 | |||
203 | drop(sink); | 221 | drop(sink); |
204 | res.into_inner() | 222 | res.into_inner() |
205 | } | 223 | } |
@@ -307,6 +325,17 @@ mod tests { | |||
307 | ); | 325 | ); |
308 | } | 326 | } |
309 | 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 | |||
310 | /// 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 |
311 | /// apply to the file containing the cursor. | 340 | /// apply to the file containing the cursor. |
312 | pub(crate) fn check_no_diagnostics(ra_fixture: &str) { | 341 | pub(crate) fn check_no_diagnostics(ra_fixture: &str) { |
@@ -975,4 +1004,132 @@ impl TestStruct { | |||
975 | 1004 | ||
976 | check_fix(input, expected); | 1005 | check_fix(input, expected); |
977 | } | 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 | ||
1014 | fn f() {} | ||
1015 | //- /foo.rs | ||
1016 | $0 | ||
1017 | "#, | ||
1018 | r#" | ||
1019 | mod foo; | ||
1020 | |||
1021 | fn 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 | |||
1034 | mod preexisting; | ||
1035 | |||
1036 | mod preexisting2; | ||
1037 | |||
1038 | struct S; | ||
1039 | |||
1040 | mod preexisting_bottom;) | ||
1041 | //- /foo.rs | ||
1042 | $0 | ||
1043 | "#, | ||
1044 | r#" | ||
1045 | //! Comment on top | ||
1046 | |||
1047 | mod preexisting; | ||
1048 | |||
1049 | mod preexisting2; | ||
1050 | mod foo; | ||
1051 | |||
1052 | struct S; | ||
1053 | |||
1054 | mod 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#" | ||
1069 | mod foo; | ||
1070 | "#, | ||
1071 | ); | ||
1072 | } | ||
1073 | |||
1074 | #[test] | ||
1075 | fn unlinked_file_old_style_modrs() { | ||
1076 | check_fix( | ||
1077 | r#" | ||
1078 | //- /main.rs | ||
1079 | mod submod; | ||
1080 | //- /submod/mod.rs | ||
1081 | // in mod.rs | ||
1082 | //- /submod/foo.rs | ||
1083 | $0 | ||
1084 | "#, | ||
1085 | r#" | ||
1086 | // in mod.rs | ||
1087 | mod foo; | ||
1088 | "#, | ||
1089 | ); | ||
1090 | } | ||
1091 | |||
1092 | #[test] | ||
1093 | fn unlinked_file_new_style_mod() { | ||
1094 | check_fix( | ||
1095 | r#" | ||
1096 | //- /main.rs | ||
1097 | mod submod; | ||
1098 | //- /submod.rs | ||
1099 | //- /submod/foo.rs | ||
1100 | $0 | ||
1101 | "#, | ||
1102 | r#" | ||
1103 | mod 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)] | ||
1115 | mod 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))] | ||
1129 | mod foo; | ||
1130 | |||
1131 | //- /foo.rs | ||
1132 | "#, | ||
1133 | ); | ||
1134 | } | ||
978 | } | 1135 | } |