about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKhanh Duong Quoc <dqkqdlot@gmail.com>2024-10-24 19:56:24 +0900
committerKhanh Duong Quoc <dqkqdlot@gmail.com>2024-10-24 19:57:53 +0900
commit75a659e4d271aee9ebb1371dab13ff2aabea3d65 (patch)
tree6221381aa7929fd7f3cc3190b6c4e3ee520be390
parent3bb20dd2bd10186b78149282596a39218e689875 (diff)
downloadrust-75a659e4d271aee9ebb1371dab13ff2aabea3d65.tar.gz
rust-75a659e4d271aee9ebb1371dab13ff2aabea3d65.zip
refactor: separate function for getting import name
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/render.rs26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/render.rs b/src/tools/rust-analyzer/crates/ide-completion/src/render.rs
index edaa82d88fb..08a3450fda8 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/render.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/render.rs
@@ -281,13 +281,7 @@ pub(crate) fn render_resolution_with_import(
     import_edit: LocatedImport,
 ) -> Option<Builder> {
     let resolution = ScopeDef::from(import_edit.original_item);
-    // Use the last segment when `item_to_import` matches `original_item`,
-    // as it will take the aliased name into account.
-    let local_name = if import_edit.item_to_import == import_edit.original_item {
-        import_edit.import_path.segments().last()?.clone()
-    } else {
-        scope_def_to_name(resolution, &ctx, &import_edit)?
-    };
+    let local_name = get_import_name(resolution, &ctx, &import_edit)?;
     // This now just renders the alias text, but we need to find the aliases earlier and call this with the alias instead.
     let doc_aliases = ctx.completion.doc_aliases_in_scope(resolution);
     let ctx = ctx.doc_aliases(doc_aliases);
@@ -363,6 +357,24 @@ pub(crate) fn render_expr(
     Some(item)
 }
 
+fn get_import_name(
+    resolution: ScopeDef,
+    ctx: &RenderContext<'_>,
+    import_edit: &LocatedImport,
+) -> Option<hir::Name> {
+    // FIXME: Temporary workaround for handling aliased import.
+    // This should be removed after we have proper support for importing alias.
+    // <https://github.com/rust-lang/rust-analyzer/issues/14079>
+
+    // If `item_to_import` matches `original_item`, we are importing the item itself (not its parent module).
+    // In this case, we can use the last segment of `import_path`, as it accounts for the aliased name.
+    if import_edit.item_to_import == import_edit.original_item {
+        import_edit.import_path.segments().last().cloned()
+    } else {
+        scope_def_to_name(resolution, ctx, import_edit)
+    }
+}
+
 fn scope_def_to_name(
     resolution: ScopeDef,
     ctx: &RenderContext<'_>,