Render alias text for use imports

* removes one method breaking the flow
This commit is contained in:
hecatia-elegua 2023-04-06 17:25:30 +02:00
parent b4515d987f
commit 33ee157f3b
6 changed files with 84 additions and 61 deletions

View File

@ -78,14 +78,6 @@ fn add_opt(&mut self, item: Option<CompletionItem>) {
} }
} }
pub(crate) fn add_all<I>(&mut self, items: I)
where
I: IntoIterator,
I::Item: Into<CompletionItem>,
{
items.into_iter().for_each(|item| self.add(item.into()))
}
pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) { pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) {
let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword); let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword);
item.add_to(self); item.add_to(self);

View File

@ -88,7 +88,13 @@ pub(crate) fn complete_expr_path(
let module_scope = module.scope(ctx.db, Some(ctx.module)); let module_scope = module.scope(ctx.db, Some(ctx.module));
for (name, def) in module_scope { for (name, def) in module_scope {
if scope_def_applicable(def) { 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),
);
} }
} }
} }

View File

@ -257,13 +257,8 @@ fn import_on_the_fly(
}; };
let user_input_lowercased = potential_import_name.to_lowercase(); let user_input_lowercased = potential_import_name.to_lowercase();
acc.add_all(
import_assets import_assets
.search_for_imports( .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
)
.into_iter() .into_iter()
.filter(ns_filter) .filter(ns_filter)
.filter(|import| { .filter(|import| {
@ -271,16 +266,13 @@ fn import_on_the_fly(
&& !ctx.is_item_hidden(&import.original_item) && !ctx.is_item_hidden(&import.original_item)
}) })
.sorted_by_key(|located_import| { .sorted_by_key(|located_import| {
compute_fuzzy_completion_order_key( compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased)
&located_import.import_path,
&user_input_lowercased,
)
}) })
.filter_map(|import| { .filter_map(|import| {
render_resolution_with_import(RenderContext::new(ctx), path_ctx, import) render_resolution_with_import(RenderContext::new(ctx), path_ctx, import)
}) })
.map(|builder| builder.build()), .map(|builder| builder.build())
); .for_each(|item| acc.add(item));
Some(()) Some(())
} }
@ -305,13 +297,8 @@ fn import_on_the_fly_pat_(
}; };
let user_input_lowercased = potential_import_name.to_lowercase(); let user_input_lowercased = potential_import_name.to_lowercase();
acc.add_all(
import_assets import_assets
.search_for_imports( .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
)
.into_iter() .into_iter()
.filter(ns_filter) .filter(ns_filter)
.filter(|import| { .filter(|import| {
@ -319,16 +306,13 @@ fn import_on_the_fly_pat_(
&& !ctx.is_item_hidden(&import.original_item) && !ctx.is_item_hidden(&import.original_item)
}) })
.sorted_by_key(|located_import| { .sorted_by_key(|located_import| {
compute_fuzzy_completion_order_key( compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased)
&located_import.import_path,
&user_input_lowercased,
)
}) })
.filter_map(|import| { .filter_map(|import| {
render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import) render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import)
}) })
.map(|builder| builder.build()), .map(|builder| builder.build())
); .for_each(|item| acc.add(item));
Some(()) Some(())
} }

View File

@ -410,7 +410,12 @@ pub(crate) fn from_resolution(
resolution: hir::ScopeDef, resolution: hir::ScopeDef,
) -> Self { ) -> Self {
let doc_aliases = ctx.doc_aliases_in_scope(resolution); 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 { pub(crate) fn build(self) -> CompletionItem {

View File

@ -14,7 +14,6 @@
use ide_db::{ use ide_db::{
helpers::item_name, imports::import_assets::LocatedImport, RootDatabase, SnippetCap, SymbolKind, helpers::item_name, imports::import_assets::LocatedImport, RootDatabase, SnippetCap, SymbolKind,
}; };
use itertools::Itertools;
use syntax::{AstNode, SmolStr, SyntaxKind, TextRange}; use syntax::{AstNode, SmolStr, SyntaxKind, TextRange};
use crate::{ use crate::{
@ -210,7 +209,9 @@ pub(crate) fn render_resolution_with_import(
) -> Option<Builder> { ) -> Option<Builder> {
let resolution = ScopeDef::from(import_edit.original_item); let resolution = ScopeDef::from(import_edit.original_item);
let local_name = scope_def_to_name(resolution, &ctx, &import_edit)?; 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)) Some(render_resolution_path(ctx, path_ctx, local_name, Some(import_edit), resolution))
} }

View File

@ -1189,3 +1189,38 @@ fn here_we_go() {
"#]], "#]],
); );
} }
#[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
"#]],
);
}