Skip to content

Commit dce7dc4

Browse files
Merge #2978
2978: Auto import functions r=flodiebold a=SomeoneToIgnore A follow up for #2887 (comment) I've used the logic for conversion from the https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir_def/src/item_scope.rs#L169 method. I'm not fond of how the conversion is implemented and for my needs, I can simply replace the `hir_def::item_scope::ItemInNs::Types(item.into())` with `hir_def::item_scope::ItemInNs::Values(item.into())` and it will work, so I can use this approach instead, if you find it a better one. Co-authored-by: Kirill Bulatov <[email protected]>
2 parents 96bd4f5 + d318876 commit dce7dc4

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,28 @@ mod tests {
211211
}",
212212
);
213213
}
214+
215+
#[test]
216+
fn function_import() {
217+
check_assist_with_imports_locator(
218+
auto_import,
219+
TestImportsLocator::new,
220+
r"
221+
test_function<|>
222+
223+
pub mod PubMod {
224+
pub fn test_function() {};
225+
}
226+
",
227+
r"
228+
use PubMod::test_function;
229+
230+
test_function<|>
231+
232+
pub mod PubMod {
233+
pub fn test_function() {};
234+
}
235+
",
236+
);
237+
}
214238
}

crates/ra_hir/src/code_model.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl_froms!(
119119
BuiltinType
120120
);
121121

122-
pub use hir_def::{attr::Attrs, visibility::Visibility, AssocItemId};
122+
pub use hir_def::{attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId};
123123
use rustc_hash::FxHashSet;
124124

125125
impl Module {
@@ -238,11 +238,16 @@ impl Module {
238238
item: ModuleDef,
239239
) -> Option<hir_def::path::ModPath> {
240240
// FIXME expose namespace choice
241-
hir_def::find_path::find_path(
242-
db,
243-
hir_def::item_scope::ItemInNs::Types(item.into()),
244-
self.into(),
245-
)
241+
hir_def::find_path::find_path(db, determine_item_namespace(item), self.into())
242+
}
243+
}
244+
245+
fn determine_item_namespace(module_def: ModuleDef) -> ItemInNs {
246+
match module_def {
247+
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
248+
ItemInNs::Values(module_def.into())
249+
}
250+
_ => ItemInNs::Types(module_def.into()),
246251
}
247252
}
248253

0 commit comments

Comments
 (0)