fix: Enable completions for <_>::$0

This commit is contained in:
Lukas Wirth 2022-01-01 22:20:03 +01:00
parent 55f3297e78
commit 809461cc42

View File

@ -2,7 +2,7 @@
use std::iter; use std::iter;
use hir::ScopeDef; use hir::{ScopeDef, Trait};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ast, AstNode}; use syntax::{ast, AstNode};
@ -27,6 +27,25 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
_ => return, _ => return,
}; };
// special case `<_>::$0` as this doesn't resolve to anything.
if path.qualifier().is_none() {
if matches!(
path.segment().and_then(|it| it.kind()),
Some(ast::PathSegmentKind::Type {
type_ref: Some(ast::Type::InferType(_)),
trait_ref: None,
})
) {
cov_mark::hit!(completion_type_anchor_empty);
ctx.scope
.visible_traits()
.into_iter()
.flat_map(|it| Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, ctx, item));
return;
}
}
let resolution = match ctx.sema.resolve_path(path) { let resolution = match ctx.sema.resolve_path(path) {
Some(res) => res, Some(res) => res,
None => return, None => return,
@ -707,4 +726,28 @@ pub mod m {}
expect![[r#""#]], expect![[r#""#]],
) )
} }
#[test]
fn type_anchor_empty() {
cov_mark::check!(completion_type_anchor_empty);
check(
r#"
trait Foo {
fn foo() -> Self;
}
struct Bar;
impl Foo for Bar {
fn foo() -> {
Bar
}
}
fn bar() -> Bar {
<_>::$0
}
"#,
expect![[r#"
fn foo() (as Foo) fn() -> Self
"#]],
)
}
} }