Skip to content

Commit 223979b

Browse files
authored
Merge pull request #18382 from dqkqd/issue-17042
fix: auto-complete import for aliased function and module
2 parents bb3239d + 962d340 commit 223979b

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

crates/ide-completion/src/render.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ 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+
let local_name = get_import_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.
286286
let doc_aliases = ctx.completion.doc_aliases_in_scope(resolution);
287287
let ctx = ctx.doc_aliases(doc_aliases);
288288
Some(render_resolution_path(ctx, path_ctx, local_name, Some(import_edit), resolution))
@@ -294,7 +294,7 @@ pub(crate) fn render_resolution_with_import_pat(
294294
import_edit: LocatedImport,
295295
) -> Option<Builder> {
296296
let resolution = ScopeDef::from(import_edit.original_item);
297-
let local_name = scope_def_to_name(resolution, &ctx, &import_edit)?;
297+
let local_name = get_import_name(resolution, &ctx, &import_edit)?;
298298
Some(render_resolution_pat(ctx, pattern_ctx, local_name, Some(import_edit), resolution))
299299
}
300300

@@ -357,6 +357,24 @@ pub(crate) fn render_expr(
357357
Some(item)
358358
}
359359

360+
fn get_import_name(
361+
resolution: ScopeDef,
362+
ctx: &RenderContext<'_>,
363+
import_edit: &LocatedImport,
364+
) -> Option<hir::Name> {
365+
// FIXME: Temporary workaround for handling aliased import.
366+
// This should be removed after we have proper support for importing alias.
367+
// <https://github.com/rust-lang/rust-analyzer/issues/14079>
368+
369+
// If `item_to_import` matches `original_item`, we are importing the item itself (not its parent module).
370+
// In this case, we can use the last segment of `import_path`, as it accounts for the aliased name.
371+
if import_edit.item_to_import == import_edit.original_item {
372+
import_edit.import_path.segments().last().cloned()
373+
} else {
374+
scope_def_to_name(resolution, ctx, import_edit)
375+
}
376+
}
377+
360378
fn scope_def_to_name(
361379
resolution: ScopeDef,
362380
ctx: &RenderContext<'_>,

crates/ide-completion/src/tests/flyimport.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,54 @@ mod module {
16691669
"#]],
16701670
);
16711671
}
1672+
1673+
#[test]
1674+
fn re_export_aliased() {
1675+
check(
1676+
r#"
1677+
mod outer {
1678+
mod inner {
1679+
pub struct BarStruct;
1680+
pub fn bar_fun() {}
1681+
pub mod bar {}
1682+
}
1683+
pub use inner::bar as foo;
1684+
pub use inner::bar_fun as foo_fun;
1685+
pub use inner::BarStruct as FooStruct;
1686+
}
1687+
fn function() {
1688+
foo$0
1689+
}
1690+
"#,
1691+
expect![[r#"
1692+
st FooStruct (use outer::FooStruct) BarStruct
1693+
md foo (use outer::foo)
1694+
fn foo_fun() (use outer::foo_fun) fn()
1695+
"#]],
1696+
);
1697+
}
1698+
1699+
#[test]
1700+
fn re_export_aliased_pattern() {
1701+
check(
1702+
r#"
1703+
mod outer {
1704+
mod inner {
1705+
pub struct BarStruct;
1706+
pub fn bar_fun() {}
1707+
pub mod bar {}
1708+
}
1709+
pub use inner::bar as foo;
1710+
pub use inner::bar_fun as foo_fun;
1711+
pub use inner::BarStruct as FooStruct;
1712+
}
1713+
fn function() {
1714+
let foo$0
1715+
}
1716+
"#,
1717+
expect![[r#"
1718+
st FooStruct (use outer::FooStruct)
1719+
md foo (use outer::foo)
1720+
"#]],
1721+
);
1722+
}

0 commit comments

Comments
 (0)