]> git.lizzy.rs Git - rust.git/commitdiff
Don't complete paths after attributes
authorLukas Wirth <lukastw97@gmail.com>
Wed, 16 Jun 2021 16:50:18 +0000 (18:50 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Wed, 16 Jun 2021 19:51:21 +0000 (21:51 +0200)
crates/ide_completion/src/completions/keyword.rs
crates/ide_completion/src/completions/snippet.rs
crates/ide_completion/src/completions/unqualified_path.rs
crates/ide_completion/src/context.rs
crates/ide_completion/src/patterns.rs
crates/ide_completion/src/tests.rs
crates/ide_completion/src/tests/item_list.rs

index 7970e75c720d1891e984a2669c8b01ac053c0bb7..0a3df79d46aa823f90ad0e358d6a2405eb8d81b8 100644 (file)
@@ -349,49 +349,6 @@ fn quux() -> i32 {
         );
     }
 
-    #[test]
-    fn test_keywords_in_trait_def() {
-        check(
-            r"trait My { $0 }",
-            expect![[r#"
-                kw unsafe
-                kw fn
-                kw const
-                kw type
-            "#]],
-        );
-    }
-
-    #[test]
-    fn test_keywords_in_impl_def() {
-        check(
-            r"impl My { $0 }",
-            expect![[r#"
-                kw pub(crate)
-                kw pub
-                kw unsafe
-                kw fn
-                kw const
-                kw type
-            "#]],
-        );
-    }
-
-    #[test]
-    fn test_keywords_in_impl_def_with_attr() {
-        check(
-            r"impl My { #[foo] $0 }",
-            expect![[r#"
-                kw pub(crate)
-                kw pub
-                kw unsafe
-                kw fn
-                kw const
-                kw type
-            "#]],
-        );
-    }
-
     #[test]
     fn test_keywords_in_loop() {
         check(
index 4e64a00901b72e92133f7a71052031457cffc72c..d142265e08e3d34744cd3862d99c9529613de404 100644 (file)
@@ -36,7 +36,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
 }
 
 pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
-    if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) {
+    if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) || ctx.path_qual().is_some() {
         return;
     }
     if ctx.has_visibility_prev_sibling() {
index 2c623bf7ae2398159e49da6fb0a714d408637537..3910de2c47e8b6f39c206faec243968067d17a60 100644 (file)
@@ -500,18 +500,6 @@ fn f() fn()
         check(
             r#"
 #[rustc_builtin_macro]
-pub macro Clone {}
-
-struct S;
-impl S {
-    $0
-}
-"#,
-            expect![[r#""#]],
-        );
-        check(
-            r#"
-#[rustc_builtin_macro]
 pub macro bench {}
 
 fn f() {$0}
@@ -772,42 +760,6 @@ impl My$0
         )
     }
 
-    #[test]
-    fn completes_in_assoc_item_list() {
-        check(
-            r#"
-macro_rules! foo {}
-mod bar {}
-
-struct MyStruct {}
-impl MyStruct {
-    $0
-}
-"#,
-            expect![[r#"
-                md bar
-                ma foo!(…) macro_rules! foo
-            "#]],
-        )
-    }
-
-    #[test]
-    fn completes_in_item_list() {
-        check(
-            r#"
-struct MyStruct {}
-macro_rules! foo {}
-mod bar {}
-
-$0
-"#,
-            expect![[r#"
-                md bar
-                ma foo!(…) macro_rules! foo
-            "#]],
-        )
-    }
-
     #[test]
     fn completes_types_and_const_in_arg_list() {
         check(
index 907ffdc7ac393b3b47a8357ee68c7f2e02854335..441c080b127c019b7a9edf94629c57204a01b1ef 100644 (file)
@@ -313,7 +313,10 @@ pub(crate) fn after_if(&self) -> bool {
     pub(crate) fn is_path_disallowed(&self) -> bool {
         self.attribute_under_caret.is_some()
             || self.previous_token_is(T![unsafe])
-            || self.has_visibility_prev_sibling()
+            || matches!(
+                self.prev_sibling,
+                Some(ImmediatePrevSibling::Attribute) | Some(ImmediatePrevSibling::Visibility)
+            )
             || matches!(
                 self.completion_location,
                 Some(ImmediateLocation::Attribute(_))
index 345977d48bfc2c399603a2005a6892d50bc12814..02cfe91e182599d38f297aefc5bdd34ba293dad8 100644 (file)
@@ -20,6 +20,7 @@ pub(crate) enum ImmediatePrevSibling {
     TraitDefName,
     ImplDefType,
     Visibility,
+    Attribute,
 }
 
 /// Direct parent "thing" of what we are currently completing.
@@ -113,6 +114,7 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<Immedi
                 } else {
                     return None
             },
+            ast::Attr(_it) => ImmediatePrevSibling::Attribute,
             _ => return None,
         }
     };
@@ -438,4 +440,9 @@ fn test_if_expr_prev_sibling() {
     fn test_vis_prev_sibling() {
         check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility);
     }
+
+    #[test]
+    fn test_attr_prev_sibling() {
+        check_prev_sibling(r"#[attr] w$0", ImmediatePrevSibling::Attribute);
+    }
 }
index 4485a908e5d3761816135bae9c63ebef24da0d4f..2205603fa6620ea75979a2c03cb421c08300d01a 100644 (file)
@@ -44,7 +44,17 @@ fn monospace_width(s: &str) -> usize {
 }
 
 fn check(ra_fixture: &str, expect: Expect) {
-    let actual = completion_list(ra_fixture);
+    let base = r#"#[rustc_builtin_macro]
+pub macro Clone {}
+enum Enum { Variant }
+struct Struct {}
+#[macro_export]
+macro_rules! foo {}
+mod bar {}
+const CONST: () = ();
+trait Trait {}
+"#;
+    let actual = completion_list(&format!("{}{}", base, ra_fixture));
     expect.assert_eq(&actual)
 }
 
index c8aa44d88aed10a37e3377368cfd94f9b761c250..33b23b8b4c8ad65ec8e99e4ec74c9f67407a4717 100644 (file)
@@ -9,7 +9,7 @@ fn in_mod_item_list() {
     $0
 }
 "#,
-        expect![[r#"
+        expect![[r##"
             kw pub(crate)
             kw pub
             kw unsafe
@@ -28,22 +28,15 @@ fn in_mod_item_list() {
             sn tmod (Test module)
             sn tfn (Test function)
             sn macro_rules
-        "#]],
+            ma foo!(…)          #[macro_export] macro_rules! foo
+        "##]],
     )
 }
 
 #[test]
 fn in_source_file_item_list() {
     check(
-        r#"
-enum Enum { Variant }
-struct MyStruct {}
-#[macro_export]
-macro_rules! foo {}
-mod bar {}
-const CONST: () = ();
-
-$0"#,
+        r#"$0"#,
         expect![[r##"
             kw pub(crate)
             kw pub
@@ -71,18 +64,10 @@ mod bar {}
 }
 
 #[test]
-fn in_qualified_path() {
+fn in_item_list_after_attr() {
     check(
-        r#"
-enum Enum { Variant }
-struct MyStruct {}
-#[macro_export]
-macro_rules! foo {}
-mod bar {}
-const CONST: () = ();
-
-crate::$0"#,
-        expect![[r##"
+        r#"#[attr] $0"#,
+        expect![[r#"
             kw pub(crate)
             kw pub
             kw unsafe
@@ -101,8 +86,32 @@ mod bar {}
             sn tmod (Test module)
             sn tfn (Test function)
             sn macro_rules
+        "#]],
+    )
+}
+
+#[test]
+fn in_qualified_path() {
+    check(
+        r#"crate::$0"#,
+        expect![[r##"
+            kw pub(crate)
+            kw pub
+            kw unsafe
+            kw fn
+            kw const
+            kw type
+            kw impl
+            kw extern
+            kw use
+            kw trait
+            kw static
+            kw mod
+            kw enum
+            kw struct
+            kw union
             md bar
-            ma foo!(…)          #[macro_export] macro_rules! foo
+            ma foo!(…)    #[macro_export] macro_rules! foo
         "##]],
     )
 }
@@ -110,15 +119,7 @@ mod bar {}
 #[test]
 fn after_unsafe_token() {
     check(
-        r#"
-enum Enum { Variant }
-struct MyStruct {}
-#[macro_export]
-macro_rules! foo {}
-mod bar {}
-const CONST: () = ();
-
-unsafe $0"#,
+        r#"unsafe $0"#,
         expect![[r#"
             kw fn
             kw trait
@@ -130,15 +131,7 @@ mod bar {}
 #[test]
 fn after_visibility() {
     check(
-        r#"
-enum Enum { Variant }
-struct MyStruct {}
-#[macro_export]
-macro_rules! foo {}
-mod bar {}
-const CONST: () = ();
-
-pub $0"#,
+        r#"pub $0"#,
         expect![[r#"
             kw unsafe
             kw fn
@@ -154,3 +147,69 @@ mod bar {}
         "#]],
     );
 }
+
+#[test]
+fn after_visibility_unsafe() {
+    // FIXME this shouldn't show `impl`
+    check(
+        r#"pub unsafe $0"#,
+        expect![[r#"
+            kw fn
+            kw trait
+            kw impl
+        "#]],
+    );
+}
+
+#[test]
+fn in_impl_assoc_item_list() {
+    check(
+        r#"impl Struct {
+    $0
+}"#,
+        expect![[r##"
+            kw pub(crate)
+            kw pub
+            kw unsafe
+            kw fn
+            kw const
+            kw type
+            md bar
+            ma foo!(…)    #[macro_export] macro_rules! foo
+            ma foo!(…)    #[macro_export] macro_rules! foo
+        "##]],
+    )
+}
+
+#[test]
+fn in_impl_assoc_item_list_after_attr() {
+    check(
+        r#"impl Struct {
+    #[attr] $0
+}"#,
+        expect![[r#"
+            kw pub(crate)
+            kw pub
+            kw unsafe
+            kw fn
+            kw const
+            kw type
+        "#]],
+    )
+}
+
+#[test]
+fn in_trait_assoc_item_list() {
+    check(
+        r"trait Foo { $0 }",
+        expect![[r##"
+            kw unsafe
+            kw fn
+            kw const
+            kw type
+            md bar
+            ma foo!(…) #[macro_export] macro_rules! foo
+            ma foo!(…) #[macro_export] macro_rules! foo
+        "##]],
+    );
+}