Skip to content

Commit e6b64fd

Browse files
bors[bot]Veykril
andauthored
Merge #10328
10328: fix: Fix incorrect mod.rs handling in unlinked_file fixes r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents d598d0b + bd8c95a commit e6b64fd

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

crates/ide_diagnostics/src/handlers/unlinked_file.rs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,35 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
3838

3939
let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id));
4040
let our_path = source_root.path_for_file(&file_id)?;
41-
let (module_name, _) = our_path.name_and_extension()?;
41+
let (mut module_name, _) = our_path.name_and_extension()?;
4242

4343
// Candidates to look for:
44-
// - `mod.rs` in the same folder
45-
// - we also check `main.rs` and `lib.rs`
44+
// - `mod.rs`, `main.rs` and `lib.rs` in the same folder
4645
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
4746
let parent = our_path.parent()?;
48-
let mut paths = vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
49-
50-
// `submod/bla.rs` -> `submod.rs`
51-
let parent_mod = (|| {
52-
let (name, _) = parent.name_and_extension()?;
53-
parent.parent()?.join(&format!("{}.rs", name))
54-
})();
55-
paths.extend(parent_mod);
47+
let paths = {
48+
let temp;
49+
let parent = if module_name == "mod" {
50+
// for mod.rs we need to actually look up one higher
51+
// and take the parent as our to be module name
52+
let (name, _) = parent.name_and_extension()?;
53+
module_name = name;
54+
temp = parent.parent()?;
55+
&temp
56+
} else {
57+
&parent
58+
};
59+
let mut paths =
60+
vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
61+
62+
// `submod/bla.rs` -> `submod.rs`
63+
let parent_mod = (|| {
64+
let (name, _) = parent.name_and_extension()?;
65+
parent.parent()?.join(&format!("{}.rs", name))
66+
})();
67+
paths.extend(parent_mod);
68+
paths
69+
};
5670

5771
for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) {
5872
for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
@@ -156,6 +170,7 @@ fn make_fixes(
156170

157171
#[cfg(test)]
158172
mod tests {
173+
159174
use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
160175

161176
#[test]
@@ -232,6 +247,34 @@ mod foo;
232247
);
233248
}
234249

250+
#[test]
251+
fn unlinked_file_insert_in_empty_file_mod_file() {
252+
check_fix(
253+
r#"
254+
//- /main.rs
255+
//- /foo/mod.rs
256+
$0
257+
"#,
258+
r#"
259+
mod foo;
260+
"#,
261+
);
262+
check_fix(
263+
r#"
264+
//- /main.rs
265+
mod bar;
266+
//- /bar.rs
267+
// bar module
268+
//- /bar/foo/mod.rs
269+
$0
270+
"#,
271+
r#"
272+
// bar module
273+
mod foo;
274+
"#,
275+
);
276+
}
277+
235278
#[test]
236279
fn unlinked_file_old_style_modrs() {
237280
check_fix(

0 commit comments

Comments
 (0)