]> git.lizzy.rs Git - rust.git/commitdiff
unqualfied_path completions aren't responsible for pattern completions
authorLukas Wirth <lukastw97@gmail.com>
Mon, 15 Mar 2021 16:23:08 +0000 (17:23 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Mon, 15 Mar 2021 16:23:08 +0000 (17:23 +0100)
crates/ide_completion/src/completions/pattern.rs
crates/ide_completion/src/completions/unqualified_path.rs
crates/ide_completion/src/render/pattern.rs

index 9282c38279323c1394caf9eef3649cbfe3103d8e..46cef58f011a01688c16425298c765aefcc66e86 100644 (file)
@@ -11,10 +11,13 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
         return;
     }
 
-    if let Some(ty) = &ctx.expected_type {
-        super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
-            acc.add_qualified_variant_pat(ctx, variant, path)
-        });
+    if !ctx.is_irrefutable_pat_binding {
+        if let Some(ty) = ctx.expected_type.as_ref() {
+            super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
+                acc.add_qualified_variant_pat(ctx, variant, path.clone());
+                acc.add_qualified_enum_variant(ctx, variant, path);
+            });
+        }
     }
 
     // FIXME: ideally, we should look at the type we are matching against and
@@ -85,7 +88,7 @@ mod m {}
 struct Bar { f: u32 }
 
 fn foo() {
-   match E::X { $0 }
+   match E::X { a$0 }
 }
 "#,
             expect![[r#"
@@ -106,10 +109,11 @@ macro_rules! m { ($e:expr) => { $e } }
 enum E { X }
 
 fn foo() {
-   m!(match E::X { $0 })
+   m!(match E::X { a$0 })
 }
 "#,
             expect![[r#"
+                ev E::X  ()
                 en E
                 ma m!(…) macro_rules! m
             "#]],
@@ -129,7 +133,7 @@ mod m {}
 struct Bar { f: u32 }
 
 fn foo() {
-   let $0
+   let a$0
 }
 "#,
             expect![[r#"
@@ -147,7 +151,7 @@ enum E { X }
 static FOO: E = E::X;
 struct Bar { f: u32 }
 
-fn foo($0) {
+fn foo(a$0) {
 }
 "#,
             expect![[r#"
@@ -163,7 +167,7 @@ fn completes_pat_in_let() {
 struct Bar { f: u32 }
 
 fn foo() {
-   let $0
+   let a$0
 }
 "#,
             expect![[r#"
@@ -179,7 +183,7 @@ fn completes_param_pattern() {
 struct Foo { bar: String, baz: String }
 struct Bar(String, String);
 struct Baz;
-fn outer($0) {}
+fn outer(a$0) {}
 "#,
             expect![[r#"
                 bn Foo Foo { bar$1, baz$2 }: Foo$0
@@ -196,7 +200,7 @@ struct Foo { bar: String, baz: String }
 struct Bar(String, String);
 struct Baz;
 fn outer() {
-    let $0
+    let a$0
 }
 "#,
             expect![[r#"
@@ -215,7 +219,7 @@ struct Foo { bar: i32, baz: i32 }
 struct Baz;
 fn outer() {
     match () {
-        $0
+        a$0
     }
 }
 "#,
@@ -239,7 +243,7 @@ pub struct Foo { pub bar: i32, baz: i32 }
 
 fn outer() {
     match () {
-        $0
+        a$0
     }
 }
 "#,
@@ -258,7 +262,7 @@ fn only_shows_ident_completion() {
 struct Foo(i32);
 fn main() {
     match Foo(92) {
-        $0(92) => (),
+        a$0(92) => (),
     }
 }
 "#,
@@ -281,7 +285,7 @@ fn completes_self_pats() {
 impl Foo {
     fn foo() {
         match () {
-            $0
+            a$0
         }
     }
 }
@@ -314,4 +318,86 @@ fn foo() {
             "#]],
         )
     }
+
+    #[test]
+    fn completes_enum_variant_matcharm() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    match foo { Qu$0 }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_matcharm_ref() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    match &foo { Qu$0 }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_iflet() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    if let Qu$0 = foo { }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_impl() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+impl Foo {
+    fn foo() { match Foo::Bar { Q$0 } }
+}
+"#,
+            expect![[r#"
+                ev Self::Bar  ()
+                ev Self::Baz  ()
+                ev Self::Quux ()
+                ev Foo::Bar   ()
+                ev Foo::Baz   ()
+                ev Foo::Quux  ()
+                sp Self
+                en Foo
+            "#]],
+        )
+    }
 }
index 044dfd16027b09ee9772efd8983772c053bc9ccb..5ef80f6a72b0d105122ab526b691b2c1e9525e26 100644 (file)
@@ -6,7 +6,7 @@
 use crate::{CompletionContext, Completions};
 
 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
-    if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
+    if !ctx.is_trivial_path {
         return;
     }
     if ctx.record_lit_syntax.is_some()
@@ -23,10 +23,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
         });
     }
 
-    if ctx.is_pat_binding_or_const {
-        return;
-    }
-
     ctx.scope.process_all_names(&mut |name, res| {
         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
             cov_mark::hit!(skip_lifetime_completion);
@@ -608,66 +604,6 @@ fn main() -> ()
         );
     }
 
-    #[test]
-    fn completes_enum_variant_matcharm() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    match foo { Qu$0 }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
-    #[test]
-    fn completes_enum_variant_matcharm_ref() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    match &foo { Qu$0 }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
-    #[test]
-    fn completes_enum_variant_iflet() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    if let Qu$0 = foo { }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
     #[test]
     fn completes_enum_variant_basic_expr() {
         check(
@@ -700,28 +636,6 @@ fn f()     -> E
         )
     }
 
-    #[test]
-    fn completes_enum_variant_impl() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-impl Foo {
-    fn foo() { match Foo::Bar { Q$0 } }
-}
-"#,
-            expect![[r#"
-                ev Self::Bar  ()
-                ev Self::Baz  ()
-                ev Self::Quux ()
-                ev Foo::Bar   ()
-                ev Foo::Baz   ()
-                ev Foo::Quux  ()
-                sp Self
-                en Foo
-            "#]],
-        )
-    }
-
     #[test]
     fn dont_complete_attr() {
         check(
index ca2926125446fa2928db66c3098a200ae1c69d71..b4e80f424a2019641c1f908c31bd002ee235e8dc 100644 (file)
@@ -6,24 +6,6 @@
 
 use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
 
-fn visible_fields(
-    ctx: &RenderContext<'_>,
-    fields: &[hir::Field],
-    item: impl HasAttrs,
-) -> Option<(Vec<hir::Field>, bool)> {
-    let module = ctx.completion.scope.module()?;
-    let n_fields = fields.len();
-    let fields = fields
-        .into_iter()
-        .filter(|field| field.is_visible_from(ctx.db(), module))
-        .copied()
-        .collect::<Vec<_>>();
-
-    let fields_omitted =
-        n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
-    Some((fields, fields_omitted))
-}
-
 pub(crate) fn render_struct_pat(
     ctx: RenderContext<'_>,
     strukt: hir::Struct,
@@ -148,3 +130,21 @@ fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool)
         name = name
     )
 }
+
+fn visible_fields(
+    ctx: &RenderContext<'_>,
+    fields: &[hir::Field],
+    item: impl HasAttrs,
+) -> Option<(Vec<hir::Field>, bool)> {
+    let module = ctx.completion.scope.module()?;
+    let n_fields = fields.len();
+    let fields = fields
+        .into_iter()
+        .filter(|field| field.is_visible_from(ctx.db(), module))
+        .copied()
+        .collect::<Vec<_>>();
+
+    let fields_omitted =
+        n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
+    Some((fields, fields_omitted))
+}