From 33ee157f3b05865984957953df21f4711f8e5cd6 Mon Sep 17 00:00:00 2001 From: hecatia-elegua <108802164+hecatia-elegua@users.noreply.github.com> Date: Thu, 6 Apr 2023 17:25:30 +0200 Subject: [PATCH] Render alias text for use imports * removes one method breaking the flow --- crates/ide-completion/src/completions.rs | 8 -- crates/ide-completion/src/completions/expr.rs | 8 +- .../src/completions/flyimport.rs | 80 ++++++++----------- crates/ide-completion/src/item.rs | 7 +- crates/ide-completion/src/render.rs | 5 +- crates/ide-completion/src/tests/special.rs | 37 ++++++++- 6 files changed, 84 insertions(+), 61 deletions(-) diff --git a/crates/ide-completion/src/completions.rs b/crates/ide-completion/src/completions.rs index 333a3338753..b94bbc40652 100644 --- a/crates/ide-completion/src/completions.rs +++ b/crates/ide-completion/src/completions.rs @@ -78,14 +78,6 @@ fn add_opt(&mut self, item: Option) { } } - pub(crate) fn add_all(&mut self, items: I) - where - I: IntoIterator, - I::Item: Into, - { - items.into_iter().for_each(|item| self.add(item.into())) - } - pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) { let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword); item.add_to(self); diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index 19f4d653207..9daa6984c3e 100644 --- a/crates/ide-completion/src/completions/expr.rs +++ b/crates/ide-completion/src/completions/expr.rs @@ -88,7 +88,13 @@ pub(crate) fn complete_expr_path( let module_scope = module.scope(ctx.db, Some(ctx.module)); for (name, def) in module_scope { if scope_def_applicable(def) { - acc.add_path_resolution(ctx, path_ctx, name, def, ctx.doc_aliases_in_scope(def)); + acc.add_path_resolution( + ctx, + path_ctx, + name, + def, + ctx.doc_aliases_in_scope(def), + ); } } } diff --git a/crates/ide-completion/src/completions/flyimport.rs b/crates/ide-completion/src/completions/flyimport.rs index 0979f6a6dfc..6d023e12bca 100644 --- a/crates/ide-completion/src/completions/flyimport.rs +++ b/crates/ide-completion/src/completions/flyimport.rs @@ -257,30 +257,22 @@ fn import_on_the_fly( }; let user_input_lowercased = potential_import_name.to_lowercase(); - acc.add_all( - import_assets - .search_for_imports( - &ctx.sema, - ctx.config.insert_use.prefix_kind, - ctx.config.prefer_no_std, - ) - .into_iter() - .filter(ns_filter) - .filter(|import| { - !ctx.is_item_hidden(&import.item_to_import) - && !ctx.is_item_hidden(&import.original_item) - }) - .sorted_by_key(|located_import| { - compute_fuzzy_completion_order_key( - &located_import.import_path, - &user_input_lowercased, - ) - }) - .filter_map(|import| { - render_resolution_with_import(RenderContext::new(ctx), path_ctx, import) - }) - .map(|builder| builder.build()), - ); + import_assets + .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std) + .into_iter() + .filter(ns_filter) + .filter(|import| { + !ctx.is_item_hidden(&import.item_to_import) + && !ctx.is_item_hidden(&import.original_item) + }) + .sorted_by_key(|located_import| { + compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased) + }) + .filter_map(|import| { + render_resolution_with_import(RenderContext::new(ctx), path_ctx, import) + }) + .map(|builder| builder.build()) + .for_each(|item| acc.add(item)); Some(()) } @@ -305,30 +297,22 @@ fn import_on_the_fly_pat_( }; let user_input_lowercased = potential_import_name.to_lowercase(); - acc.add_all( - import_assets - .search_for_imports( - &ctx.sema, - ctx.config.insert_use.prefix_kind, - ctx.config.prefer_no_std, - ) - .into_iter() - .filter(ns_filter) - .filter(|import| { - !ctx.is_item_hidden(&import.item_to_import) - && !ctx.is_item_hidden(&import.original_item) - }) - .sorted_by_key(|located_import| { - compute_fuzzy_completion_order_key( - &located_import.import_path, - &user_input_lowercased, - ) - }) - .filter_map(|import| { - render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import) - }) - .map(|builder| builder.build()), - ); + import_assets + .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std) + .into_iter() + .filter(ns_filter) + .filter(|import| { + !ctx.is_item_hidden(&import.item_to_import) + && !ctx.is_item_hidden(&import.original_item) + }) + .sorted_by_key(|located_import| { + compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased) + }) + .filter_map(|import| { + render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import) + }) + .map(|builder| builder.build()) + .for_each(|item| acc.add(item)); Some(()) } diff --git a/crates/ide-completion/src/item.rs b/crates/ide-completion/src/item.rs index a993ea8e90b..e95cae9d67f 100644 --- a/crates/ide-completion/src/item.rs +++ b/crates/ide-completion/src/item.rs @@ -410,7 +410,12 @@ pub(crate) fn from_resolution( resolution: hir::ScopeDef, ) -> Self { let doc_aliases = ctx.doc_aliases_in_scope(resolution); - render_path_resolution(RenderContext::new(ctx).doc_aliases(doc_aliases), path_ctx, local_name, resolution) + render_path_resolution( + RenderContext::new(ctx).doc_aliases(doc_aliases), + path_ctx, + local_name, + resolution, + ) } pub(crate) fn build(self) -> CompletionItem { diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 62a357e085a..9673252ff52 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -14,7 +14,6 @@ use ide_db::{ helpers::item_name, imports::import_assets::LocatedImport, RootDatabase, SnippetCap, SymbolKind, }; -use itertools::Itertools; use syntax::{AstNode, SmolStr, SyntaxKind, TextRange}; use crate::{ @@ -210,7 +209,9 @@ pub(crate) fn render_resolution_with_import( ) -> Option { let resolution = ScopeDef::from(import_edit.original_item); let local_name = scope_def_to_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); Some(render_resolution_path(ctx, path_ctx, local_name, Some(import_edit), resolution)) } diff --git a/crates/ide-completion/src/tests/special.rs b/crates/ide-completion/src/tests/special.rs index e11a2c4797b..e23f3ae72eb 100644 --- a/crates/ide-completion/src/tests/special.rs +++ b/crates/ide-completion/src/tests/special.rs @@ -1188,4 +1188,39 @@ fn here_we_go() { st Bar (alias Qux) "#]], ); -} \ No newline at end of file +} + +#[test] +fn completes_flyimport_with_doc_alias_in_another_mod() { + check( + r#" +mod foo { + #[doc(alias = "Qux")] + pub struct Bar(); +} + +fn here_we_go() { + let foo = Bar$0 +} +"#, + expect![[r#" + fn here_we_go() fn() + md foo + st Bar (alias Qux) (use foo::Bar) + bt u32 + kw crate:: + kw false + kw for + kw if + kw if let + kw loop + kw match + kw return + kw self:: + kw true + kw unsafe + kw while + kw while let + "#]], + ); +}