Skip to content

Commit 3bb20dd

Browse files
committed
fix: auto-complete import for aliased function and module
1 parent 1fbaccd commit 3bb20dd

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/render.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,14 @@ pub(crate) fn render_resolution_with_import(
281281
import_edit: LocatedImport,
282282
) -> Option<Builder> {
283283
let resolution = ScopeDef::from(import_edit.original_item);
284-
let local_name = scope_def_to_name(resolution, &ctx, &import_edit)?;
285-
//this now just renders the alias text, but we need to find the aliases earlier and call this with the alias instead
284+
// Use the last segment when `item_to_import` matches `original_item`,
285+
// as it will take the aliased name into account.
286+
let local_name = if import_edit.item_to_import == import_edit.original_item {
287+
import_edit.import_path.segments().last()?.clone()
288+
} else {
289+
scope_def_to_name(resolution, &ctx, &import_edit)?
290+
};
291+
// This now just renders the alias text, but we need to find the aliases earlier and call this with the alias instead.
286292
let doc_aliases = ctx.completion.doc_aliases_in_scope(resolution);
287293
let ctx = ctx.doc_aliases(doc_aliases);
288294
Some(render_resolution_path(ctx, path_ctx, local_name, Some(import_edit), resolution))

src/tools/rust-analyzer/crates/ide-completion/src/tests/flyimport.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,45 @@ mod module {
16691669
"#]],
16701670
);
16711671
}
1672+
1673+
#[test]
1674+
fn re_export_aliased_function() {
1675+
check(
1676+
r#"
1677+
//- /lib.rs crate:bar
1678+
pub fn func(_: i32) -> i32 {}
1679+
1680+
//- /lib.rs crate:foo deps:bar
1681+
pub use bar::func as my_func;
1682+
1683+
//- /main.rs crate:main deps:foo
1684+
fn main() {
1685+
m$0
1686+
}
1687+
"#,
1688+
expect![[r#"
1689+
fn my_func(…) (use foo::my_func) fn(i32) -> i32
1690+
"#]],
1691+
);
1692+
}
1693+
1694+
#[test]
1695+
fn re_export_aliased_module() {
1696+
check(
1697+
r#"
1698+
//- /lib.rs crate:bar
1699+
pub mod baz {}
1700+
1701+
//- /lib.rs crate:foo deps:bar
1702+
pub use bar::baz as my_baz;
1703+
1704+
//- /main.rs crate:main deps:foo
1705+
fn main() {
1706+
m$0
1707+
}
1708+
"#,
1709+
expect![[r#"
1710+
md my_baz (use foo::my_baz)
1711+
"#]],
1712+
);
1713+
}

0 commit comments

Comments
 (0)