Skip to content

Commit 33ee157

Browse files
Render alias text for use imports
* removes one method breaking the flow
1 parent b4515d9 commit 33ee157

File tree

6 files changed

+84
-61
lines changed

6 files changed

+84
-61
lines changed

crates/ide-completion/src/completions.rs

-8
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ impl Completions {
7878
}
7979
}
8080

81-
pub(crate) fn add_all<I>(&mut self, items: I)
82-
where
83-
I: IntoIterator,
84-
I::Item: Into<CompletionItem>,
85-
{
86-
items.into_iter().for_each(|item| self.add(item.into()))
87-
}
88-
8981
pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) {
9082
let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword);
9183
item.add_to(self);

crates/ide-completion/src/completions/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ pub(crate) fn complete_expr_path(
8888
let module_scope = module.scope(ctx.db, Some(ctx.module));
8989
for (name, def) in module_scope {
9090
if scope_def_applicable(def) {
91-
acc.add_path_resolution(ctx, path_ctx, name, def, ctx.doc_aliases_in_scope(def));
91+
acc.add_path_resolution(
92+
ctx,
93+
path_ctx,
94+
name,
95+
def,
96+
ctx.doc_aliases_in_scope(def),
97+
);
9298
}
9399
}
94100
}

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

+32-48
Original file line numberDiff line numberDiff line change
@@ -257,30 +257,22 @@ fn import_on_the_fly(
257257
};
258258
let user_input_lowercased = potential_import_name.to_lowercase();
259259

260-
acc.add_all(
261-
import_assets
262-
.search_for_imports(
263-
&ctx.sema,
264-
ctx.config.insert_use.prefix_kind,
265-
ctx.config.prefer_no_std,
266-
)
267-
.into_iter()
268-
.filter(ns_filter)
269-
.filter(|import| {
270-
!ctx.is_item_hidden(&import.item_to_import)
271-
&& !ctx.is_item_hidden(&import.original_item)
272-
})
273-
.sorted_by_key(|located_import| {
274-
compute_fuzzy_completion_order_key(
275-
&located_import.import_path,
276-
&user_input_lowercased,
277-
)
278-
})
279-
.filter_map(|import| {
280-
render_resolution_with_import(RenderContext::new(ctx), path_ctx, import)
281-
})
282-
.map(|builder| builder.build()),
283-
);
260+
import_assets
261+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
262+
.into_iter()
263+
.filter(ns_filter)
264+
.filter(|import| {
265+
!ctx.is_item_hidden(&import.item_to_import)
266+
&& !ctx.is_item_hidden(&import.original_item)
267+
})
268+
.sorted_by_key(|located_import| {
269+
compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased)
270+
})
271+
.filter_map(|import| {
272+
render_resolution_with_import(RenderContext::new(ctx), path_ctx, import)
273+
})
274+
.map(|builder| builder.build())
275+
.for_each(|item| acc.add(item));
284276
Some(())
285277
}
286278

@@ -305,30 +297,22 @@ fn import_on_the_fly_pat_(
305297
};
306298
let user_input_lowercased = potential_import_name.to_lowercase();
307299

308-
acc.add_all(
309-
import_assets
310-
.search_for_imports(
311-
&ctx.sema,
312-
ctx.config.insert_use.prefix_kind,
313-
ctx.config.prefer_no_std,
314-
)
315-
.into_iter()
316-
.filter(ns_filter)
317-
.filter(|import| {
318-
!ctx.is_item_hidden(&import.item_to_import)
319-
&& !ctx.is_item_hidden(&import.original_item)
320-
})
321-
.sorted_by_key(|located_import| {
322-
compute_fuzzy_completion_order_key(
323-
&located_import.import_path,
324-
&user_input_lowercased,
325-
)
326-
})
327-
.filter_map(|import| {
328-
render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import)
329-
})
330-
.map(|builder| builder.build()),
331-
);
300+
import_assets
301+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
302+
.into_iter()
303+
.filter(ns_filter)
304+
.filter(|import| {
305+
!ctx.is_item_hidden(&import.item_to_import)
306+
&& !ctx.is_item_hidden(&import.original_item)
307+
})
308+
.sorted_by_key(|located_import| {
309+
compute_fuzzy_completion_order_key(&located_import.import_path, &user_input_lowercased)
310+
})
311+
.filter_map(|import| {
312+
render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import)
313+
})
314+
.map(|builder| builder.build())
315+
.for_each(|item| acc.add(item));
332316
Some(())
333317
}
334318

crates/ide-completion/src/item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,12 @@ impl Builder {
410410
resolution: hir::ScopeDef,
411411
) -> Self {
412412
let doc_aliases = ctx.doc_aliases_in_scope(resolution);
413-
render_path_resolution(RenderContext::new(ctx).doc_aliases(doc_aliases), path_ctx, local_name, resolution)
413+
render_path_resolution(
414+
RenderContext::new(ctx).doc_aliases(doc_aliases),
415+
path_ctx,
416+
local_name,
417+
resolution,
418+
)
414419
}
415420

416421
pub(crate) fn build(self) -> CompletionItem {

crates/ide-completion/src/render.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef};
1414
use ide_db::{
1515
helpers::item_name, imports::import_assets::LocatedImport, RootDatabase, SnippetCap, SymbolKind,
1616
};
17-
use itertools::Itertools;
1817
use syntax::{AstNode, SmolStr, SyntaxKind, TextRange};
1918

2019
use crate::{
@@ -210,7 +209,9 @@ pub(crate) fn render_resolution_with_import(
210209
) -> Option<Builder> {
211210
let resolution = ScopeDef::from(import_edit.original_item);
212211
let local_name = scope_def_to_name(resolution, &ctx, &import_edit)?;
213-
212+
//this now just renders the alias text, but we need to find the aliases earlier and call this with the alias instead
213+
let doc_aliases = ctx.completion.doc_aliases_in_scope(resolution);
214+
let ctx = ctx.doc_aliases(doc_aliases);
214215
Some(render_resolution_path(ctx, path_ctx, local_name, Some(import_edit), resolution))
215216
}
216217

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

+36-1
Original file line numberDiff line numberDiff line change
@@ -1188,4 +1188,39 @@ fn here_we_go() {
11881188
st Bar (alias Qux)
11891189
"#]],
11901190
);
1191-
}
1191+
}
1192+
1193+
#[test]
1194+
fn completes_flyimport_with_doc_alias_in_another_mod() {
1195+
check(
1196+
r#"
1197+
mod foo {
1198+
#[doc(alias = "Qux")]
1199+
pub struct Bar();
1200+
}
1201+
1202+
fn here_we_go() {
1203+
let foo = Bar$0
1204+
}
1205+
"#,
1206+
expect![[r#"
1207+
fn here_we_go() fn()
1208+
md foo
1209+
st Bar (alias Qux) (use foo::Bar)
1210+
bt u32
1211+
kw crate::
1212+
kw false
1213+
kw for
1214+
kw if
1215+
kw if let
1216+
kw loop
1217+
kw match
1218+
kw return
1219+
kw self::
1220+
kw true
1221+
kw unsafe
1222+
kw while
1223+
kw while let
1224+
"#]],
1225+
);
1226+
}

0 commit comments

Comments
 (0)