Fix incorrect mod.rs handling in unlinked_file fixes
This commit is contained in:
parent
d598d0b4f1
commit
bd8c95a580
@ -38,21 +38,35 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
|
|||||||
|
|
||||||
let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id));
|
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 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:
|
// Candidates to look for:
|
||||||
// - `mod.rs` in the same folder
|
// - `mod.rs`, `main.rs` and `lib.rs` in the same folder
|
||||||
// - we also check `main.rs` and `lib.rs`
|
|
||||||
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
|
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
|
||||||
let parent = our_path.parent()?;
|
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`
|
// `submod/bla.rs` -> `submod.rs`
|
||||||
let parent_mod = (|| {
|
let parent_mod = (|| {
|
||||||
let (name, _) = parent.name_and_extension()?;
|
let (name, _) = parent.name_and_extension()?;
|
||||||
parent.parent()?.join(&format!("{}.rs", name))
|
parent.parent()?.join(&format!("{}.rs", name))
|
||||||
})();
|
})();
|
||||||
paths.extend(parent_mod);
|
paths.extend(parent_mod);
|
||||||
|
paths
|
||||||
|
};
|
||||||
|
|
||||||
for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) {
|
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() {
|
for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
|
||||||
@ -156,6 +170,7 @@ fn make_fixes(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
|
use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
|
||||||
|
|
||||||
#[test]
|
#[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]
|
#[test]
|
||||||
fn unlinked_file_old_style_modrs() {
|
fn unlinked_file_old_style_modrs() {
|
||||||
check_fix(
|
check_fix(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user