Skip to content

Commit cf5f1e8

Browse files
committed
refactor: separate function for getting import name
1 parent c4b0977 commit cf5f1e8

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

crates/ide-completion/src/render.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,7 @@ pub(crate) fn render_resolution_with_import(
281281
import_edit: LocatedImport,
282282
) -> Option<Builder> {
283283
let resolution = ScopeDef::from(import_edit.original_item);
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-
};
284+
let local_name = get_import_name(resolution, &ctx, &import_edit)?;
291285
// This now just renders the alias text, but we need to find the aliases earlier and call this with the alias instead.
292286
let doc_aliases = ctx.completion.doc_aliases_in_scope(resolution);
293287
let ctx = ctx.doc_aliases(doc_aliases);
@@ -363,6 +357,24 @@ pub(crate) fn render_expr(
363357
Some(item)
364358
}
365359

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+
366378
fn scope_def_to_name(
367379
resolution: ScopeDef,
368380
ctx: &RenderContext<'_>,

0 commit comments

Comments
 (0)