]> git.lizzy.rs Git - rust.git/commitdiff
fix: flyimport triggers on enum variant declarations
authorzhoufan <1247714429@qq.com>
Sun, 14 Nov 2021 04:16:21 +0000 (12:16 +0800)
committerzhoufan <1247714429@qq.com>
Sun, 14 Nov 2021 04:16:21 +0000 (12:16 +0800)
crates/ide_completion/src/completions/flyimport.rs
crates/ide_completion/src/context.rs
crates/ide_completion/src/patterns.rs
crates/ide_completion/src/tests/flyimport.rs

index 47036f2e132cb7dd6eba790129b1a4b258ae192b..784a1a0637c017e21ee0e4d15c924db911ca7a42 100644 (file)
@@ -113,6 +113,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
         || ctx.is_path_disallowed()
         || ctx.expects_item()
         || ctx.expects_assoc_item()
+        || ctx.expects_variant()
     {
         return None;
     }
index 05ae95769b002251330f864327d7d8f30e25ecf4..8759e2d108c7ad2f7061991b4c0d5d063fe76430 100644 (file)
@@ -171,6 +171,10 @@ pub(crate) fn expects_assoc_item(&self) -> bool {
         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 {
         matches!(self.completion_location, Some(ImmediateLocation::Impl))
     }
index 4c2e704a7fbd6ce6bd6ba1b715cf25f8989c40e6..13c739325c806c7d1a9968cec6d8e2fa580b9465 100644 (file)
@@ -45,6 +45,7 @@ pub(crate) enum ImmediateLocation {
     StmtList,
     ItemList,
     TypeBound,
+    Variant,
     /// Fake file ast node
     Attribute(ast::Attr),
     /// Fake file ast node
@@ -213,6 +214,7 @@ pub(crate) fn determine_location(
             ast::SourceFile(_it) => ImmediateLocation::ItemList,
             ast::ItemList(_it) => ImmediateLocation::ItemList,
             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)) {
                 return None;
             } else {
index 201443e10c2f0c56ac00bd5781a7fc65fb8a1e71..18880a67aa6ffcbb348db13ef9d51f71ed7458ef 100644 (file)
@@ -1012,3 +1012,34 @@ mod module {
         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)
+        "#]],
+    )
+}