diff options
Diffstat (limited to 'crates/ide_completion/src')
-rw-r--r-- | crates/ide_completion/src/completions/keyword.rs | 22 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 23 | ||||
-rw-r--r-- | crates/ide_completion/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/test_utils.rs | 2 |
4 files changed, 34 insertions, 15 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index b635e0ca3..61b667104 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs | |||
@@ -9,22 +9,21 @@ use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKin | |||
9 | pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) { | 9 | pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) { |
10 | // complete keyword "crate" in use stmt | 10 | // complete keyword "crate" in use stmt |
11 | let source_range = ctx.source_range(); | 11 | let source_range = ctx.source_range(); |
12 | let kw_completion = move |text: &str| { | ||
13 | let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, text); | ||
14 | item.kind(CompletionItemKind::Keyword).insert_text(text); | ||
15 | item | ||
16 | }; | ||
12 | 17 | ||
13 | if ctx.use_item_syntax.is_some() { | 18 | if ctx.use_item_syntax.is_some() { |
14 | if ctx.path_qual.is_none() { | 19 | if ctx.path_qual.is_none() { |
15 | let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "crate::"); | 20 | kw_completion("crate::").add_to(acc); |
16 | item.kind(CompletionItemKind::Keyword).insert_text("crate::"); | ||
17 | item.add_to(acc); | ||
18 | } | 21 | } |
19 | let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "self"); | 22 | kw_completion("self").add_to(acc); |
20 | item.kind(CompletionItemKind::Keyword); | ||
21 | item.add_to(acc); | ||
22 | if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) | 23 | if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) |
23 | .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) | 24 | .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) |
24 | { | 25 | { |
25 | let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "super::"); | 26 | kw_completion("super::").add_to(acc); |
26 | item.kind(CompletionItemKind::Keyword).insert_text("super::"); | ||
27 | item.add_to(acc); | ||
28 | } | 27 | } |
29 | } | 28 | } |
30 | 29 | ||
@@ -32,9 +31,8 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC | |||
32 | if let Some(receiver) = &ctx.dot_receiver { | 31 | if let Some(receiver) = &ctx.dot_receiver { |
33 | if let Some(ty) = ctx.sema.type_of_expr(receiver) { | 32 | if let Some(ty) = ctx.sema.type_of_expr(receiver) { |
34 | if ty.impls_future(ctx.db) { | 33 | if ty.impls_future(ctx.db) { |
35 | let mut item = | 34 | let mut item = kw_completion("await"); |
36 | CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await"); | 35 | item.detail("expr.await"); |
37 | item.kind(CompletionItemKind::Keyword).detail("expr.await").insert_text("await"); | ||
38 | item.add_to(acc); | 36 | item.add_to(acc); |
39 | } | 37 | } |
40 | }; | 38 | }; |
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 62ef40818..787eb2fd3 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -313,7 +313,8 @@ impl<'a> CompletionContext<'a> { | |||
313 | cov_mark::hit!(expected_type_let_with_leading_char); | 313 | cov_mark::hit!(expected_type_let_with_leading_char); |
314 | cov_mark::hit!(expected_type_let_without_leading_char); | 314 | cov_mark::hit!(expected_type_let_without_leading_char); |
315 | let ty = it.pat() | 315 | let ty = it.pat() |
316 | .and_then(|pat| self.sema.type_of_pat(&pat)); | 316 | .and_then(|pat| self.sema.type_of_pat(&pat)) |
317 | .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it))); | ||
317 | let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { | 318 | let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { |
318 | ident.name().map(NameOrNameRef::Name) | 319 | ident.name().map(NameOrNameRef::Name) |
319 | } else { | 320 | } else { |
@@ -720,6 +721,26 @@ fn foo() { | |||
720 | } | 721 | } |
721 | 722 | ||
722 | #[test] | 723 | #[test] |
724 | fn expected_type_let_pat() { | ||
725 | check_expected_type_and_name( | ||
726 | r#" | ||
727 | fn foo() { | ||
728 | let x$0 = 0u32; | ||
729 | } | ||
730 | "#, | ||
731 | expect![[r#"ty: u32, name: ?"#]], | ||
732 | ); | ||
733 | check_expected_type_and_name( | ||
734 | r#" | ||
735 | fn foo() { | ||
736 | let $0 = 0u32; | ||
737 | } | ||
738 | "#, | ||
739 | expect![[r#"ty: u32, name: ?"#]], | ||
740 | ); | ||
741 | } | ||
742 | |||
743 | #[test] | ||
723 | fn expected_type_fn_param_without_leading_char() { | 744 | fn expected_type_fn_param_without_leading_char() { |
724 | cov_mark::check!(expected_type_fn_param_without_leading_char); | 745 | cov_mark::check!(expected_type_fn_param_without_leading_char); |
725 | check_expected_type_and_name( | 746 | check_expected_type_and_name( |
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index e32633565..645349215 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs | |||
@@ -80,7 +80,7 @@ pub use crate::{ | |||
80 | // | 80 | // |
81 | // And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities. | 81 | // And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities. |
82 | // Those are the additional completion options with automatic `use` import and options from all project importable items, | 82 | // Those are the additional completion options with automatic `use` import and options from all project importable items, |
83 | // fuzzy matched agains the completion imput. | 83 | // fuzzy matched against the completion input. |
84 | // | 84 | // |
85 | // image::https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif[] | 85 | // image::https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif[] |
86 | 86 | ||
diff --git a/crates/ide_completion/src/test_utils.rs b/crates/ide_completion/src/test_utils.rs index c9857ec5f..939fb2d47 100644 --- a/crates/ide_completion/src/test_utils.rs +++ b/crates/ide_completion/src/test_utils.rs | |||
@@ -20,7 +20,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { | |||
20 | add_call_argument_snippets: true, | 20 | add_call_argument_snippets: true, |
21 | snippet_cap: SnippetCap::new(true), | 21 | snippet_cap: SnippetCap::new(true), |
22 | insert_use: InsertUseConfig { | 22 | insert_use: InsertUseConfig { |
23 | merge: Some(MergeBehavior::Full), | 23 | merge: Some(MergeBehavior::Crate), |
24 | prefix_kind: PrefixKind::Plain, | 24 | prefix_kind: PrefixKind::Plain, |
25 | group: true, | 25 | group: true, |
26 | }, | 26 | }, |