aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-16 16:56:04 +0100
committerLukas Wirth <[email protected]>2021-06-16 20:51:20 +0100
commit9ea6ee6b2785da02ff1963fbbc2eea340450905c (patch)
tree6ee0a69dc2e44eb04c1f7e3841a423da4b2bf4ca /crates/ide_completion
parent1a8f76a224aff472cf29bab828f313c19e31eb02 (diff)
Don't show incorrect completions after unsafe or visiblity node
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/keyword.rs22
-rw-r--r--crates/ide_completion/src/completions/snippet.rs7
-rw-r--r--crates/ide_completion/src/context.rs17
-rw-r--r--crates/ide_completion/src/tests/item_list.rs34
4 files changed, 38 insertions, 42 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 0bfdf9603..7970e75c7 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -90,11 +90,13 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
90 } 90 }
91 91
92 if expects_item || has_block_expr_parent { 92 if expects_item || has_block_expr_parent {
93 if !ctx.has_visibility_prev_sibling() {
94 add_keyword("impl", "impl $1 {\n $0\n}");
95 add_keyword("extern", "extern $0");
96 }
93 add_keyword("use", "use $0"); 97 add_keyword("use", "use $0");
94 add_keyword("impl", "impl $1 {\n $0\n}");
95 add_keyword("trait", "trait $1 {\n $0\n}"); 98 add_keyword("trait", "trait $1 {\n $0\n}");
96 add_keyword("static", "static $0"); 99 add_keyword("static", "static $0");
97 add_keyword("extern", "extern $0");
98 add_keyword("mod", "mod $0"); 100 add_keyword("mod", "mod $0");
99 } 101 }
100 102
@@ -241,11 +243,11 @@ mod tests {
241 kw fn 243 kw fn
242 kw const 244 kw const
243 kw type 245 kw type
244 kw use
245 kw impl 246 kw impl
247 kw extern
248 kw use
246 kw trait 249 kw trait
247 kw static 250 kw static
248 kw extern
249 kw mod 251 kw mod
250 kw match 252 kw match
251 kw while 253 kw while
@@ -269,11 +271,11 @@ mod tests {
269 kw fn 271 kw fn
270 kw const 272 kw const
271 kw type 273 kw type
272 kw use
273 kw impl 274 kw impl
275 kw extern
276 kw use
274 kw trait 277 kw trait
275 kw static 278 kw static
276 kw extern
277 kw mod 279 kw mod
278 kw match 280 kw match
279 kw while 281 kw while
@@ -297,11 +299,11 @@ mod tests {
297 kw fn 299 kw fn
298 kw const 300 kw const
299 kw type 301 kw type
300 kw use
301 kw impl 302 kw impl
303 kw extern
304 kw use
302 kw trait 305 kw trait
303 kw static 306 kw static
304 kw extern
305 kw mod 307 kw mod
306 kw match 308 kw match
307 kw while 309 kw while
@@ -399,11 +401,11 @@ fn quux() -> i32 {
399 kw fn 401 kw fn
400 kw const 402 kw const
401 kw type 403 kw type
402 kw use
403 kw impl 404 kw impl
405 kw extern
406 kw use
404 kw trait 407 kw trait
405 kw static 408 kw static
406 kw extern
407 kw mod 409 kw mod
408 kw match 410 kw match
409 kw while 411 kw while
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index 5560f1acf..4e64a0090 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -1,6 +1,7 @@
1//! This file provides snippet completions, like `pd` => `eprintln!(...)`. 1//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
2 2
3use ide_db::helpers::SnippetCap; 3use ide_db::helpers::SnippetCap;
4use syntax::T;
4 5
5use crate::{ 6use crate::{
6 context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem, 7 context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem,
@@ -35,9 +36,13 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
35} 36}
36 37
37pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { 38pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
38 if !ctx.expects_item() { 39 if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) {
39 return; 40 return;
40 } 41 }
42 if ctx.has_visibility_prev_sibling() {
43 return; // technically we could do some of these snippet completions if we were to put the
44 // attributes before the vis node.
45 }
41 let cap = match ctx.config.snippet_cap { 46 let cap = match ctx.config.snippet_cap {
42 Some(it) => it, 47 Some(it) => it,
43 None => return, 48 None => return,
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 3885db702..907ffdc7a 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -311,13 +311,16 @@ impl<'a> CompletionContext<'a> {
311 } 311 }
312 312
313 pub(crate) fn is_path_disallowed(&self) -> bool { 313 pub(crate) fn is_path_disallowed(&self) -> bool {
314 matches!( 314 self.attribute_under_caret.is_some()
315 self.completion_location, 315 || self.previous_token_is(T![unsafe])
316 Some(ImmediateLocation::Attribute(_)) 316 || self.has_visibility_prev_sibling()
317 | Some(ImmediateLocation::ModDeclaration(_)) 317 || matches!(
318 | Some(ImmediateLocation::RecordPat(_)) 318 self.completion_location,
319 | Some(ImmediateLocation::RecordExpr(_)) 319 Some(ImmediateLocation::Attribute(_))
320 ) || self.attribute_under_caret.is_some() 320 | Some(ImmediateLocation::ModDeclaration(_))
321 | Some(ImmediateLocation::RecordPat(_))
322 | Some(ImmediateLocation::RecordExpr(_))
323 )
321 } 324 }
322 325
323 pub(crate) fn expects_expression(&self) -> bool { 326 pub(crate) fn expects_expression(&self) -> bool {
diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs
index e7b77d7e7..c8aa44d88 100644
--- a/crates/ide_completion/src/tests/item_list.rs
+++ b/crates/ide_completion/src/tests/item_list.rs
@@ -16,11 +16,11 @@ fn in_mod_item_list() {
16 kw fn 16 kw fn
17 kw const 17 kw const
18 kw type 18 kw type
19 kw use
20 kw impl 19 kw impl
20 kw extern
21 kw use
21 kw trait 22 kw trait
22 kw static 23 kw static
23 kw extern
24 kw mod 24 kw mod
25 kw enum 25 kw enum
26 kw struct 26 kw struct
@@ -51,11 +51,11 @@ $0"#,
51 kw fn 51 kw fn
52 kw const 52 kw const
53 kw type 53 kw type
54 kw use
55 kw impl 54 kw impl
55 kw extern
56 kw use
56 kw trait 57 kw trait
57 kw static 58 kw static
58 kw extern
59 kw mod 59 kw mod
60 kw enum 60 kw enum
61 kw struct 61 kw struct
@@ -89,11 +89,11 @@ crate::$0"#,
89 kw fn 89 kw fn
90 kw const 90 kw const
91 kw type 91 kw type
92 kw use
93 kw impl 92 kw impl
93 kw extern
94 kw use
94 kw trait 95 kw trait
95 kw static 96 kw static
96 kw extern
97 kw mod 97 kw mod
98 kw enum 98 kw enum
99 kw struct 99 kw struct
@@ -119,17 +119,11 @@ mod bar {}
119const CONST: () = (); 119const CONST: () = ();
120 120
121unsafe $0"#, 121unsafe $0"#,
122 expect![[r##" 122 expect![[r#"
123 kw fn 123 kw fn
124 kw trait 124 kw trait
125 kw impl 125 kw impl
126 sn tmod (Test module) 126 "#]],
127 sn tfn (Test function)
128 sn macro_rules
129 md bar
130 ma foo!(…) #[macro_export] macro_rules! foo
131 ma foo!(…) #[macro_export] macro_rules! foo
132 "##]],
133 ); 127 );
134} 128}
135 129
@@ -145,26 +139,18 @@ mod bar {}
145const CONST: () = (); 139const CONST: () = ();
146 140
147pub $0"#, 141pub $0"#,
148 expect![[r##" 142 expect![[r#"
149 kw unsafe 143 kw unsafe
150 kw fn 144 kw fn
151 kw const 145 kw const
152 kw type 146 kw type
153 kw use 147 kw use
154 kw impl
155 kw trait 148 kw trait
156 kw static 149 kw static
157 kw extern
158 kw mod 150 kw mod
159 kw enum 151 kw enum
160 kw struct 152 kw struct
161 kw union 153 kw union
162 sn tmod (Test module) 154 "#]],
163 sn tfn (Test function)
164 sn macro_rules
165 md bar
166 ma foo!(…) #[macro_export] macro_rules! foo
167 ma foo!(…) #[macro_export] macro_rules! foo
168 "##]],
169 ); 155 );
170} 156}