From 59630977a5c5376d9210ae61d5baba52239f1bc1 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 19 Apr 2021 19:50:11 +0200 Subject: Fix some find_path bugs around inner items --- crates/hir_def/src/find_path.rs | 63 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 2c4bbe585..dc3f2908f 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs @@ -119,8 +119,7 @@ fn find_path_inner( // - if the item is the crate root, return `crate` let root = def_map.crate_root(db); - if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { - // FIXME: the `block_id()` check should be unnecessary, but affects the result + if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) { return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); } @@ -131,7 +130,7 @@ fn find_path_inner( } // - if the item is the crate root of a dependency crate, return the name from the extern prelude - for (name, def_id) in def_map.extern_prelude() { + for (name, def_id) in root.def_map(db).extern_prelude() { if item == ItemInNs::Types(*def_id) { let name = scope_name.unwrap_or_else(|| name.clone()); return Some(ModPath::from_segments(PathKind::Plain, vec![name])); @@ -298,6 +297,7 @@ fn find_local_import_locations( let data = &def_map[from.local_id]; let mut worklist = data.children.values().map(|child| def_map.module_id(*child)).collect::>(); + // FIXME: do we need to traverse out of block expressions here? for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { worklist.push(ancestor); } @@ -947,10 +947,11 @@ fn main() { $0 } "#, + // FIXME: these could use fewer/better prefixes "module::CompleteMe", - "module::CompleteMe", "crate::module::CompleteMe", - "self::module::CompleteMe", + "crate::module::CompleteMe", + "crate::module::CompleteMe", ) } @@ -977,6 +978,28 @@ mod bar { ) } + #[test] + fn from_inside_module_with_inner_items() { + check_found_path( + r#" +mod baz { + pub struct Foo {} +} + +mod bar { + fn bar() { + fn inner() {} + $0 + } +} + "#, + "crate::baz::Foo", + "crate::baz::Foo", + "crate::baz::Foo", + "crate::baz::Foo", + ) + } + #[test] fn recursive_pub_mod_reexport() { cov_mark::check!(recursive_imports); @@ -1004,4 +1027,34 @@ pub mod name { "self::name::AsName", ); } + + #[test] + fn extern_crate() { + check_found_path( + r#" +//- /main.rs crate:main deps:dep +$0 +//- /dep.rs crate:dep +"#, + "dep", + "dep", + "dep", + "dep", + ); + + check_found_path( + r#" +//- /main.rs crate:main deps:dep +fn f() { + fn inner() {} + $0 +} +//- /dep.rs crate:dep +"#, + "dep", + "dep", + "dep", + "dep", + ); + } } -- cgit v1.2.3 From ec051863780bdd4389f72cb279cf4e84f21a2c2d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 19 Apr 2021 19:53:29 +0200 Subject: Add autoimport test with inner items --- crates/ide_assists/src/handlers/auto_import.rs | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 5ccd7f7a2..49aa70f74 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -934,4 +934,37 @@ fn main() { ", ); } + + #[test] + fn inner_items() { + check_assist( + auto_import, + r#" +mod baz { + pub struct Foo {} +} + +mod bar { + fn bar() { + Foo$0; + println!("Hallo"); + } +} +"#, + r#" +mod baz { + pub struct Foo {} +} + +mod bar { + use crate::baz::Foo; + + fn bar() { + Foo; + println!("Hallo"); + } +} +"#, + ); + } } -- cgit v1.2.3