10762: Fix: trigger flyimport on enum variants r=lnicola a=XFFXFF

fixes #10749 

Co-authored-by: zhoufan <1247714429@qq.com>
This commit is contained in:
bors[bot] 2021-11-14 09:20:59 +00:00 committed by GitHub
commit 212095a07d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 0 deletions

View File

@ -113,6 +113,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|| ctx.is_path_disallowed() || ctx.is_path_disallowed()
|| ctx.expects_item() || ctx.expects_item()
|| ctx.expects_assoc_item() || ctx.expects_assoc_item()
|| ctx.expects_variant()
{ {
return None; return None;
} }

View File

@ -171,6 +171,10 @@ pub(crate) fn expects_assoc_item(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl)) matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
} }
pub(crate) fn expects_variant(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Variant))
}
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool { pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Impl)) matches!(self.completion_location, Some(ImmediateLocation::Impl))
} }

View File

@ -45,6 +45,7 @@ pub(crate) enum ImmediateLocation {
StmtList, StmtList,
ItemList, ItemList,
TypeBound, TypeBound,
Variant,
/// Fake file ast node /// Fake file ast node
Attribute(ast::Attr), Attribute(ast::Attr),
/// Fake file ast node /// Fake file ast node
@ -213,6 +214,7 @@ pub(crate) fn determine_location(
ast::SourceFile(_it) => ImmediateLocation::ItemList, ast::SourceFile(_it) => ImmediateLocation::ItemList,
ast::ItemList(_it) => ImmediateLocation::ItemList, ast::ItemList(_it) => ImmediateLocation::ItemList,
ast::RefExpr(_it) => ImmediateLocation::RefExpr, ast::RefExpr(_it) => ImmediateLocation::RefExpr,
ast::Variant(_it) => ImmediateLocation::Variant,
ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) { ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) {
return None; return None;
} else { } else {

View File

@ -1012,3 +1012,34 @@ mod module {
expect![[r#""#]], expect![[r#""#]],
); );
} }
#[test]
fn flyimport_enum_variant() {
check(
r#"
mod foo {
pub struct Barbara;
}
enum Foo {
Barba$0()
}
}"#,
expect![[r#""#]],
);
check(
r#"
mod foo {
pub struct Barbara;
}
enum Foo {
Barba(Barba$0)
}
}"#,
expect![[r#"
st Barbara (use foo::Barbara)
"#]],
)
}