diff options
author | gfreezy <[email protected]> | 2019-01-01 14:45:53 +0000 |
---|---|---|
committer | gfreezy <[email protected]> | 2019-01-01 14:45:53 +0000 |
commit | 22ea00d5ff2a993735702ec8e28fecfcbf4ffc4f (patch) | |
tree | 673ab5897a62d6c8772cff50651f6cabd5c5be3e /crates/ra_analysis/src/completion | |
parent | f408b1caa3acdaf7b05498963e70da7f662bf2e3 (diff) |
complete "self" and "super"
Diffstat (limited to 'crates/ra_analysis/src/completion')
-rw-r--r-- | crates/ra_analysis/src/completion/complete_keyword.rs | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index a6fbd1e81..dd81fc008 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs | |||
@@ -15,28 +15,38 @@ fn keyword(kw: &str, snippet: &str) -> CompletionItem { | |||
15 | } | 15 | } |
16 | 16 | ||
17 | pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { | 17 | pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { |
18 | if !ctx.is_trivial_path { | ||
19 | return; | ||
20 | } | ||
21 | |||
22 | // complete keyword "crate" in use stmt | 18 | // complete keyword "crate" in use stmt |
23 | if let (Some(use_item), None) = (&ctx.use_item_syntax, &ctx.path_prefix) { | 19 | match (ctx.use_item_syntax.as_ref(), ctx.path_prefix.as_ref()) { |
24 | let mut complete_crate = false; | 20 | (Some(_), None) => { |
25 | let use_tree = use_item.use_tree(); | ||
26 | if let Some(use_tree) = use_tree { | ||
27 | let tree_str = use_tree.syntax().text().to_string(); | ||
28 | if tree_str != "crate" && "crate".starts_with(&tree_str) { | ||
29 | complete_crate = true; | ||
30 | } | ||
31 | } else { | ||
32 | complete_crate = true; | ||
33 | } | ||
34 | if complete_crate { | ||
35 | CompletionItem::new(CompletionKind::Keyword, "crate") | 21 | CompletionItem::new(CompletionKind::Keyword, "crate") |
36 | .kind(CompletionItemKind::Keyword) | 22 | .kind(CompletionItemKind::Keyword) |
37 | .lookup_by("crate") | 23 | .lookup_by("crate") |
24 | .snippet("crate::") | ||
25 | .add_to(acc); | ||
26 | CompletionItem::new(CompletionKind::Keyword, "self") | ||
27 | .kind(CompletionItemKind::Keyword) | ||
28 | .lookup_by("self") | ||
29 | .add_to(acc); | ||
30 | CompletionItem::new(CompletionKind::Keyword, "super") | ||
31 | .kind(CompletionItemKind::Keyword) | ||
32 | .lookup_by("super") | ||
38 | .add_to(acc); | 33 | .add_to(acc); |
39 | } | 34 | } |
35 | (Some(_), Some(_)) => { | ||
36 | CompletionItem::new(CompletionKind::Keyword, "self") | ||
37 | .kind(CompletionItemKind::Keyword) | ||
38 | .lookup_by("self") | ||
39 | .add_to(acc); | ||
40 | CompletionItem::new(CompletionKind::Keyword, "super") | ||
41 | .kind(CompletionItemKind::Keyword) | ||
42 | .lookup_by("super") | ||
43 | .add_to(acc); | ||
44 | } | ||
45 | _ => {} | ||
46 | } | ||
47 | |||
48 | if !ctx.is_trivial_path { | ||
49 | return; | ||
40 | } | 50 | } |
41 | 51 | ||
42 | let fn_def = match ctx.function_syntax { | 52 | let fn_def = match ctx.function_syntax { |
@@ -292,21 +302,35 @@ mod tests { | |||
292 | } | 302 | } |
293 | 303 | ||
294 | #[test] | 304 | #[test] |
295 | fn completes_crate_in_use_stmt() { | 305 | fn completes_keywords_in_use_stmt() { |
296 | check_keyword_completion( | 306 | check_keyword_completion( |
297 | r" | 307 | r" |
298 | use <|> | 308 | use <|> |
299 | ", | 309 | ", |
300 | r#" | 310 | r#" |
301 | crate "crate" | 311 | crate "crate" "crate::" |
312 | self "self" | ||
313 | super "super" | ||
302 | "#, | 314 | "#, |
303 | ); | 315 | ); |
304 | // No completion: lambda isolates control flow | 316 | |
317 | check_keyword_completion( | ||
318 | r" | ||
319 | use a::<|> | ||
320 | ", | ||
321 | r#" | ||
322 | self "self" | ||
323 | super "super" | ||
324 | "#, | ||
325 | ); | ||
326 | |||
305 | check_keyword_completion( | 327 | check_keyword_completion( |
306 | r" | 328 | r" |
307 | use a<|> | 329 | use a::{b, <|>} |
308 | ", | 330 | ", |
309 | r#" | 331 | r#" |
332 | self "self" | ||
333 | super "super" | ||
310 | "#, | 334 | "#, |
311 | ); | 335 | ); |
312 | } | 336 | } |