diff options
author | Lukas Wirth <[email protected]> | 2021-06-16 17:50:18 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-06-16 20:51:21 +0100 |
commit | 11115ebad8d0cb367478a4f154abe08c0c25aa95 (patch) | |
tree | d4260c26782bc61c904c0d78707ea74830bbbfbe | |
parent | 9ea6ee6b2785da02ff1963fbbc2eea340450905c (diff) |
Don't complete paths after attributes
-rw-r--r-- | crates/ide_completion/src/completions/keyword.rs | 43 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/snippet.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/unqualified_path.rs | 48 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 5 | ||||
-rw-r--r-- | crates/ide_completion/src/patterns.rs | 7 | ||||
-rw-r--r-- | crates/ide_completion/src/tests.rs | 12 | ||||
-rw-r--r-- | crates/ide_completion/src/tests/item_list.rs | 141 |
7 files changed, 123 insertions, 135 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index 7970e75c7..0a3df79d4 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs | |||
@@ -350,49 +350,6 @@ fn quux() -> i32 { | |||
350 | } | 350 | } |
351 | 351 | ||
352 | #[test] | 352 | #[test] |
353 | fn test_keywords_in_trait_def() { | ||
354 | check( | ||
355 | r"trait My { $0 }", | ||
356 | expect![[r#" | ||
357 | kw unsafe | ||
358 | kw fn | ||
359 | kw const | ||
360 | kw type | ||
361 | "#]], | ||
362 | ); | ||
363 | } | ||
364 | |||
365 | #[test] | ||
366 | fn test_keywords_in_impl_def() { | ||
367 | check( | ||
368 | r"impl My { $0 }", | ||
369 | expect![[r#" | ||
370 | kw pub(crate) | ||
371 | kw pub | ||
372 | kw unsafe | ||
373 | kw fn | ||
374 | kw const | ||
375 | kw type | ||
376 | "#]], | ||
377 | ); | ||
378 | } | ||
379 | |||
380 | #[test] | ||
381 | fn test_keywords_in_impl_def_with_attr() { | ||
382 | check( | ||
383 | r"impl My { #[foo] $0 }", | ||
384 | expect![[r#" | ||
385 | kw pub(crate) | ||
386 | kw pub | ||
387 | kw unsafe | ||
388 | kw fn | ||
389 | kw const | ||
390 | kw type | ||
391 | "#]], | ||
392 | ); | ||
393 | } | ||
394 | |||
395 | #[test] | ||
396 | fn test_keywords_in_loop() { | 353 | fn test_keywords_in_loop() { |
397 | check( | 354 | check( |
398 | r"fn my() { loop { $0 } }", | 355 | r"fn my() { loop { $0 } }", |
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs index 4e64a0090..d142265e0 100644 --- a/crates/ide_completion/src/completions/snippet.rs +++ b/crates/ide_completion/src/completions/snippet.rs | |||
@@ -36,7 +36,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte | |||
36 | } | 36 | } |
37 | 37 | ||
38 | pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { | 38 | pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { |
39 | if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) { | 39 | if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) || ctx.path_qual().is_some() { |
40 | return; | 40 | return; |
41 | } | 41 | } |
42 | if ctx.has_visibility_prev_sibling() { | 42 | if ctx.has_visibility_prev_sibling() { |
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 2c623bf7a..3910de2c4 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs | |||
@@ -500,18 +500,6 @@ fn f() {$0} | |||
500 | check( | 500 | check( |
501 | r#" | 501 | r#" |
502 | #[rustc_builtin_macro] | 502 | #[rustc_builtin_macro] |
503 | pub macro Clone {} | ||
504 | |||
505 | struct S; | ||
506 | impl S { | ||
507 | $0 | ||
508 | } | ||
509 | "#, | ||
510 | expect![[r#""#]], | ||
511 | ); | ||
512 | check( | ||
513 | r#" | ||
514 | #[rustc_builtin_macro] | ||
515 | pub macro bench {} | 503 | pub macro bench {} |
516 | 504 | ||
517 | fn f() {$0} | 505 | fn f() {$0} |
@@ -773,42 +761,6 @@ impl My$0 | |||
773 | } | 761 | } |
774 | 762 | ||
775 | #[test] | 763 | #[test] |
776 | fn completes_in_assoc_item_list() { | ||
777 | check( | ||
778 | r#" | ||
779 | macro_rules! foo {} | ||
780 | mod bar {} | ||
781 | |||
782 | struct MyStruct {} | ||
783 | impl MyStruct { | ||
784 | $0 | ||
785 | } | ||
786 | "#, | ||
787 | expect![[r#" | ||
788 | md bar | ||
789 | ma foo!(…) macro_rules! foo | ||
790 | "#]], | ||
791 | ) | ||
792 | } | ||
793 | |||
794 | #[test] | ||
795 | fn completes_in_item_list() { | ||
796 | check( | ||
797 | r#" | ||
798 | struct MyStruct {} | ||
799 | macro_rules! foo {} | ||
800 | mod bar {} | ||
801 | |||
802 | $0 | ||
803 | "#, | ||
804 | expect![[r#" | ||
805 | md bar | ||
806 | ma foo!(…) macro_rules! foo | ||
807 | "#]], | ||
808 | ) | ||
809 | } | ||
810 | |||
811 | #[test] | ||
812 | fn completes_types_and_const_in_arg_list() { | 764 | fn completes_types_and_const_in_arg_list() { |
813 | check( | 765 | check( |
814 | r#" | 766 | r#" |
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 907ffdc7a..441c080b1 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -313,7 +313,10 @@ impl<'a> CompletionContext<'a> { | |||
313 | pub(crate) fn is_path_disallowed(&self) -> bool { | 313 | pub(crate) fn is_path_disallowed(&self) -> bool { |
314 | self.attribute_under_caret.is_some() | 314 | self.attribute_under_caret.is_some() |
315 | || self.previous_token_is(T![unsafe]) | 315 | || self.previous_token_is(T![unsafe]) |
316 | || self.has_visibility_prev_sibling() | 316 | || matches!( |
317 | self.prev_sibling, | ||
318 | Some(ImmediatePrevSibling::Attribute) | Some(ImmediatePrevSibling::Visibility) | ||
319 | ) | ||
317 | || matches!( | 320 | || matches!( |
318 | self.completion_location, | 321 | self.completion_location, |
319 | Some(ImmediateLocation::Attribute(_)) | 322 | Some(ImmediateLocation::Attribute(_)) |
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs index 345977d48..02cfe91e1 100644 --- a/crates/ide_completion/src/patterns.rs +++ b/crates/ide_completion/src/patterns.rs | |||
@@ -20,6 +20,7 @@ pub(crate) enum ImmediatePrevSibling { | |||
20 | TraitDefName, | 20 | TraitDefName, |
21 | ImplDefType, | 21 | ImplDefType, |
22 | Visibility, | 22 | Visibility, |
23 | Attribute, | ||
23 | } | 24 | } |
24 | 25 | ||
25 | /// Direct parent "thing" of what we are currently completing. | 26 | /// 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 | |||
113 | } else { | 114 | } else { |
114 | return None | 115 | return None |
115 | }, | 116 | }, |
117 | ast::Attr(_it) => ImmediatePrevSibling::Attribute, | ||
116 | _ => return None, | 118 | _ => return None, |
117 | } | 119 | } |
118 | }; | 120 | }; |
@@ -438,4 +440,9 @@ mod tests { | |||
438 | fn test_vis_prev_sibling() { | 440 | fn test_vis_prev_sibling() { |
439 | check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility); | 441 | check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility); |
440 | } | 442 | } |
443 | |||
444 | #[test] | ||
445 | fn test_attr_prev_sibling() { | ||
446 | check_prev_sibling(r"#[attr] w$0", ImmediatePrevSibling::Attribute); | ||
447 | } | ||
441 | } | 448 | } |
diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs index 4485a908e..2205603fa 100644 --- a/crates/ide_completion/src/tests.rs +++ b/crates/ide_completion/src/tests.rs | |||
@@ -44,7 +44,17 @@ fn completion_list_with_config(config: CompletionConfig, code: &str) -> String { | |||
44 | } | 44 | } |
45 | 45 | ||
46 | fn check(ra_fixture: &str, expect: Expect) { | 46 | fn check(ra_fixture: &str, expect: Expect) { |
47 | let actual = completion_list(ra_fixture); | 47 | let base = r#"#[rustc_builtin_macro] |
48 | pub macro Clone {} | ||
49 | enum Enum { Variant } | ||
50 | struct Struct {} | ||
51 | #[macro_export] | ||
52 | macro_rules! foo {} | ||
53 | mod bar {} | ||
54 | const CONST: () = (); | ||
55 | trait Trait {} | ||
56 | "#; | ||
57 | let actual = completion_list(&format!("{}{}", base, ra_fixture)); | ||
48 | expect.assert_eq(&actual) | 58 | expect.assert_eq(&actual) |
49 | } | 59 | } |
50 | 60 | ||
diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs index c8aa44d88..33b23b8b4 100644 --- a/crates/ide_completion/src/tests/item_list.rs +++ b/crates/ide_completion/src/tests/item_list.rs | |||
@@ -9,7 +9,7 @@ fn in_mod_item_list() { | |||
9 | $0 | 9 | $0 |
10 | } | 10 | } |
11 | "#, | 11 | "#, |
12 | expect![[r#" | 12 | expect![[r##" |
13 | kw pub(crate) | 13 | kw pub(crate) |
14 | kw pub | 14 | kw pub |
15 | kw unsafe | 15 | kw unsafe |
@@ -28,22 +28,15 @@ fn in_mod_item_list() { | |||
28 | sn tmod (Test module) | 28 | sn tmod (Test module) |
29 | sn tfn (Test function) | 29 | sn tfn (Test function) |
30 | sn macro_rules | 30 | sn macro_rules |
31 | "#]], | 31 | ma foo!(…) #[macro_export] macro_rules! foo |
32 | "##]], | ||
32 | ) | 33 | ) |
33 | } | 34 | } |
34 | 35 | ||
35 | #[test] | 36 | #[test] |
36 | fn in_source_file_item_list() { | 37 | fn in_source_file_item_list() { |
37 | check( | 38 | check( |
38 | r#" | 39 | r#"$0"#, |
39 | enum Enum { Variant } | ||
40 | struct MyStruct {} | ||
41 | #[macro_export] | ||
42 | macro_rules! foo {} | ||
43 | mod bar {} | ||
44 | const CONST: () = (); | ||
45 | |||
46 | $0"#, | ||
47 | expect![[r##" | 40 | expect![[r##" |
48 | kw pub(crate) | 41 | kw pub(crate) |
49 | kw pub | 42 | kw pub |
@@ -71,18 +64,10 @@ $0"#, | |||
71 | } | 64 | } |
72 | 65 | ||
73 | #[test] | 66 | #[test] |
74 | fn in_qualified_path() { | 67 | fn in_item_list_after_attr() { |
75 | check( | 68 | check( |
76 | r#" | 69 | r#"#[attr] $0"#, |
77 | enum Enum { Variant } | 70 | expect![[r#" |
78 | struct MyStruct {} | ||
79 | #[macro_export] | ||
80 | macro_rules! foo {} | ||
81 | mod bar {} | ||
82 | const CONST: () = (); | ||
83 | |||
84 | crate::$0"#, | ||
85 | expect![[r##" | ||
86 | kw pub(crate) | 71 | kw pub(crate) |
87 | kw pub | 72 | kw pub |
88 | kw unsafe | 73 | kw unsafe |
@@ -101,8 +86,32 @@ crate::$0"#, | |||
101 | sn tmod (Test module) | 86 | sn tmod (Test module) |
102 | sn tfn (Test function) | 87 | sn tfn (Test function) |
103 | sn macro_rules | 88 | sn macro_rules |
89 | "#]], | ||
90 | ) | ||
91 | } | ||
92 | |||
93 | #[test] | ||
94 | fn in_qualified_path() { | ||
95 | check( | ||
96 | r#"crate::$0"#, | ||
97 | expect![[r##" | ||
98 | kw pub(crate) | ||
99 | kw pub | ||
100 | kw unsafe | ||
101 | kw fn | ||
102 | kw const | ||
103 | kw type | ||
104 | kw impl | ||
105 | kw extern | ||
106 | kw use | ||
107 | kw trait | ||
108 | kw static | ||
109 | kw mod | ||
110 | kw enum | ||
111 | kw struct | ||
112 | kw union | ||
104 | md bar | 113 | md bar |
105 | ma foo!(…) #[macro_export] macro_rules! foo | 114 | ma foo!(…) #[macro_export] macro_rules! foo |
106 | "##]], | 115 | "##]], |
107 | ) | 116 | ) |
108 | } | 117 | } |
@@ -110,15 +119,7 @@ crate::$0"#, | |||
110 | #[test] | 119 | #[test] |
111 | fn after_unsafe_token() { | 120 | fn after_unsafe_token() { |
112 | check( | 121 | check( |
113 | r#" | 122 | r#"unsafe $0"#, |
114 | enum Enum { Variant } | ||
115 | struct MyStruct {} | ||
116 | #[macro_export] | ||
117 | macro_rules! foo {} | ||
118 | mod bar {} | ||
119 | const CONST: () = (); | ||
120 | |||
121 | unsafe $0"#, | ||
122 | expect![[r#" | 123 | expect![[r#" |
123 | kw fn | 124 | kw fn |
124 | kw trait | 125 | kw trait |
@@ -130,15 +131,7 @@ unsafe $0"#, | |||
130 | #[test] | 131 | #[test] |
131 | fn after_visibility() { | 132 | fn after_visibility() { |
132 | check( | 133 | check( |
133 | r#" | 134 | r#"pub $0"#, |
134 | enum Enum { Variant } | ||
135 | struct MyStruct {} | ||
136 | #[macro_export] | ||
137 | macro_rules! foo {} | ||
138 | mod bar {} | ||
139 | const CONST: () = (); | ||
140 | |||
141 | pub $0"#, | ||
142 | expect![[r#" | 135 | expect![[r#" |
143 | kw unsafe | 136 | kw unsafe |
144 | kw fn | 137 | kw fn |
@@ -154,3 +147,69 @@ pub $0"#, | |||
154 | "#]], | 147 | "#]], |
155 | ); | 148 | ); |
156 | } | 149 | } |
150 | |||
151 | #[test] | ||
152 | fn after_visibility_unsafe() { | ||
153 | // FIXME this shouldn't show `impl` | ||
154 | check( | ||
155 | r#"pub unsafe $0"#, | ||
156 | expect![[r#" | ||
157 | kw fn | ||
158 | kw trait | ||
159 | kw impl | ||
160 | "#]], | ||
161 | ); | ||
162 | } | ||
163 | |||
164 | #[test] | ||
165 | fn in_impl_assoc_item_list() { | ||
166 | check( | ||
167 | r#"impl Struct { | ||
168 | $0 | ||
169 | }"#, | ||
170 | expect![[r##" | ||
171 | kw pub(crate) | ||
172 | kw pub | ||
173 | kw unsafe | ||
174 | kw fn | ||
175 | kw const | ||
176 | kw type | ||
177 | md bar | ||
178 | ma foo!(…) #[macro_export] macro_rules! foo | ||
179 | ma foo!(…) #[macro_export] macro_rules! foo | ||
180 | "##]], | ||
181 | ) | ||
182 | } | ||
183 | |||
184 | #[test] | ||
185 | fn in_impl_assoc_item_list_after_attr() { | ||
186 | check( | ||
187 | r#"impl Struct { | ||
188 | #[attr] $0 | ||
189 | }"#, | ||
190 | expect![[r#" | ||
191 | kw pub(crate) | ||
192 | kw pub | ||
193 | kw unsafe | ||
194 | kw fn | ||
195 | kw const | ||
196 | kw type | ||
197 | "#]], | ||
198 | ) | ||
199 | } | ||
200 | |||
201 | #[test] | ||
202 | fn in_trait_assoc_item_list() { | ||
203 | check( | ||
204 | r"trait Foo { $0 }", | ||
205 | expect![[r##" | ||
206 | kw unsafe | ||
207 | kw fn | ||
208 | kw const | ||
209 | kw type | ||
210 | md bar | ||
211 | ma foo!(…) #[macro_export] macro_rules! foo | ||
212 | ma foo!(…) #[macro_export] macro_rules! foo | ||
213 | "##]], | ||
214 | ); | ||
215 | } | ||