From bd8c95a5803300c746e5843d19e642dbcd685f04 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 24 Sep 2021 14:20:43 +0200 Subject: [PATCH] Fix incorrect mod.rs handling in unlinked_file fixes --- .../src/handlers/unlinked_file.rs | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/crates/ide_diagnostics/src/handlers/unlinked_file.rs b/crates/ide_diagnostics/src/handlers/unlinked_file.rs index 06c1f2cf820..9eafd42d86e 100644 --- a/crates/ide_diagnostics/src/handlers/unlinked_file.rs +++ b/crates/ide_diagnostics/src/handlers/unlinked_file.rs @@ -38,21 +38,35 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option> { let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id)); let our_path = source_root.path_for_file(&file_id)?; - let (module_name, _) = our_path.name_and_extension()?; + let (mut module_name, _) = our_path.name_and_extension()?; // Candidates to look for: - // - `mod.rs` in the same folder - // - we also check `main.rs` and `lib.rs` + // - `mod.rs`, `main.rs` and `lib.rs` in the same folder // - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id` let parent = our_path.parent()?; - let mut paths = vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?]; + let paths = { + let temp; + let parent = if module_name == "mod" { + // for mod.rs we need to actually look up one higher + // and take the parent as our to be module name + let (name, _) = parent.name_and_extension()?; + module_name = name; + temp = parent.parent()?; + &temp + } else { + &parent + }; + let mut paths = + vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?]; - // `submod/bla.rs` -> `submod.rs` - let parent_mod = (|| { - let (name, _) = parent.name_and_extension()?; - parent.parent()?.join(&format!("{}.rs", name)) - })(); - paths.extend(parent_mod); + // `submod/bla.rs` -> `submod.rs` + let parent_mod = (|| { + let (name, _) = parent.name_and_extension()?; + parent.parent()?.join(&format!("{}.rs", name)) + })(); + paths.extend(parent_mod); + paths + }; for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) { for &krate in ctx.sema.db.relevant_crates(parent_id).iter() { @@ -156,6 +170,7 @@ fn make_fixes( #[cfg(test)] mod tests { + use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix}; #[test] @@ -232,6 +247,34 @@ mod foo; ); } + #[test] + fn unlinked_file_insert_in_empty_file_mod_file() { + check_fix( + r#" +//- /main.rs +//- /foo/mod.rs +$0 +"#, + r#" +mod foo; +"#, + ); + check_fix( + r#" +//- /main.rs +mod bar; +//- /bar.rs +// bar module +//- /bar/foo/mod.rs +$0 +"#, + r#" +// bar module +mod foo; +"#, + ); + } + #[test] fn unlinked_file_old_style_modrs() { check_fix(