]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/unqualified_path.rs
simplify
[rust.git] / crates / ide_completion / src / completions / unqualified_path.rs
index e9d0ff66589abe49f44acea8ab1eadf361aa9306..046a393aeceb325530cf3b73bb9b1a3361ef575e 100644 (file)
@@ -2,41 +2,34 @@
 
 use hir::ScopeDef;
 use syntax::AstNode;
-use test_utils::mark;
 
 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()
-        || ctx.record_pat_syntax.is_some()
-        || ctx.attribute_under_caret.is_some()
-        || ctx.mod_declaration_under_caret.is_some()
-    {
+    if ctx.is_path_disallowed() {
         return;
     }
 
-    if let Some(ty) = &ctx.expected_type {
-        super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
+    if let Some(hir::Adt::Enum(e)) =
+        ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
+    {
+        super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| {
             acc.add_qualified_enum_variant(ctx, variant, path)
         });
     }
 
-    if ctx.is_pat_binding_or_const {
-        return;
-    }
-
     ctx.scope.process_all_names(&mut |name, res| {
         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
-            mark::hit!(skip_lifetime_completion);
+            cov_mark::hit!(skip_lifetime_completion);
             return;
         }
         if ctx.use_item_syntax.is_some() {
             if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
                 if name_ref.syntax().text() == name.to_string().as_str() {
-                    mark::hit!(self_fulfilling_completion);
+                    cov_mark::hit!(self_fulfilling_completion);
                     return;
                 }
             }
@@ -48,7 +41,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
 #[cfg(test)]
 mod tests {
     use expect_test::{expect, Expect};
-    use test_utils::mark;
 
     use crate::{
         test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
@@ -66,7 +58,7 @@ fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect)
 
     #[test]
     fn self_fulfilling_completion() {
-        mark::check!(self_fulfilling_completion);
+        cov_mark::check!(self_fulfilling_completion);
         check(
             r#"
 use foo$0
@@ -90,7 +82,7 @@ fn quux(x: Option<Enum>) {
     }
 }
 "#,
-            expect![[""]],
+            expect![[r#""#]],
         );
     }
 
@@ -106,7 +98,7 @@ fn quux(x: Option<Enum>) {
     }
 }
 "#,
-            expect![[""]],
+            expect![[r#""#]],
         );
     }
 
@@ -141,7 +133,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
             "#]],
         );
     }
@@ -163,7 +155,7 @@ fn quux() {
             expect![[r#"
                 lc b      i32
                 lc a
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -178,14 +170,14 @@ fn quux() {
 "#,
             expect![[r#"
                 lc x
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
 
     #[test]
     fn completes_if_prefix_is_keyword() {
-        mark::check!(completes_if_prefix_is_keyword);
+        cov_mark::check!(completes_if_prefix_is_keyword);
         check_edit(
             "wherewolf",
             r#"
@@ -209,25 +201,25 @@ fn completes_generic_params() {
             r#"fn quux<T>() { $0 }"#,
             expect![[r#"
                 tp T
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
         check(
             r#"fn quux<const C: usize>() { $0 }"#,
             expect![[r#"
                 cp C
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
 
     #[test]
     fn does_not_complete_lifetimes() {
-        mark::check!(skip_lifetime_completion);
+        cov_mark::check!(skip_lifetime_completion);
         check(
             r#"fn quux<'a>() { $0 }"#,
             expect![[r#"
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -265,7 +257,7 @@ fn quux() { $0 }
 "#,
             expect![[r#"
                 st S
-                fn quux() -> ()
+                fn quux() fn()
                 en E
             "#]],
         );
@@ -318,7 +310,7 @@ fn quux() { $0 }
 }
 "#,
             expect![[r#"
-                fn quux() -> ()
+                fn quux() fn()
                 st Bar
             "#]],
         );
@@ -333,7 +325,7 @@ fn x() -> $0
 "#,
             expect![[r#"
                 st Foo
-                fn x() -> ()
+                fn x() fn()
             "#]],
         );
     }
@@ -354,7 +346,7 @@ fn foo() {
             expect![[r#"
                 lc bar   i32
                 lc bar   i32
-                fn foo() -> ()
+                fn foo() fn()
             "#]],
         );
     }
@@ -384,7 +376,7 @@ fn foo() { let x: $0 }
 mod prelude { struct Option; }
 "#,
             expect![[r#"
-                fn foo()  -> ()
+                fn foo()  fn()
                 md std
                 st Option
             "#]],
@@ -414,7 +406,7 @@ macro_rules! concat { }
 }
 "#,
             expect![[r##"
-                fn f()        -> ()
+                fn f()        fn()
                 ma concat!(…) #[macro_export] macro_rules! concat
                 md std
             "##]],
@@ -441,7 +433,7 @@ fn foo() { let x: $0 }
 mod prelude { struct String; }
 "#,
             expect![[r#"
-                fn foo()  -> ()
+                fn foo()  fn()
                 md std
                 md core
                 st String
@@ -472,7 +464,7 @@ fn main() { let v = $0 }
             expect![[r##"
                 md m1
                 ma baz!(…) #[macro_export] macro_rules! baz
-                fn main()  -> ()
+                fn main()  fn()
                 md m2
                 ma bar!(…) macro_rules! bar
                 ma foo!(…) macro_rules! foo
@@ -488,7 +480,7 @@ macro_rules! foo { () => {} }
 fn foo() { $0 }
 "#,
             expect![[r#"
-                fn foo()   -> ()
+                fn foo()   fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -502,7 +494,7 @@ macro_rules! foo { () => {} }
 fn main() { let x: $0 }
 "#,
             expect![[r#"
-                fn main()  -> ()
+                fn main()  fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -516,7 +508,7 @@ macro_rules! foo { () => {} }
 fn main() { $0 }
 "#,
             expect![[r#"
-                fn main()  -> ()
+                fn main()  fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -532,8 +524,8 @@ fn frobnicate() {}
 }
 "#,
             expect![[r#"
-                fn frobnicate() -> ()
-                fn main()       -> ()
+                fn frobnicate() fn()
+                fn main()       fn()
             "#]],
         );
     }
@@ -551,7 +543,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -570,7 +562,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -589,7 +581,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -604,72 +596,12 @@ fn completes_unresolved_uses() {
 fn main() { $0 }
 "#,
             expect![[r#"
-                fn main() -> ()
+                fn main() fn()
                 ?? Quux
             "#]],
         );
     }
 
-    #[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(
@@ -682,7 +614,7 @@ fn main() { let foo: Foo = Q$0 }
                 ev Foo::Baz  ()
                 ev Foo::Quux ()
                 en Foo
-                fn main()    -> ()
+                fn main()    fn()
             "#]],
         )
     }
@@ -697,29 +629,7 @@ fn f() -> m::E { V$0 }
             expect![[r#"
                 ev m::E::V ()
                 md m
-                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
+                fn f()     fn() -> E
             "#]],
         )
     }