diff options
author | Lukas Wirth <[email protected]> | 2021-06-17 14:43:21 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-06-17 14:43:21 +0100 |
commit | 2a48b532208de413e4e5d39e81c33a4644ecaa22 (patch) | |
tree | 8c8fb1436a64a6bfd56558f41b734a966aa5725e /crates | |
parent | a9a77671f2405e0cb65160c17268beec5114e259 (diff) |
Correct completions in items tests
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_completion/src/completions/keyword.rs | 3 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/snippet.rs | 6 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/unqualified_path.rs | 5 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 4 | ||||
-rw-r--r-- | crates/ide_completion/src/tests/items.rs | 63 |
5 files changed, 57 insertions, 24 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index c5cd3c2f7..c99fdef05 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs | |||
@@ -69,6 +69,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
69 | 69 | ||
70 | if ctx.has_impl_or_trait_prev_sibling() { | 70 | if ctx.has_impl_or_trait_prev_sibling() { |
71 | add_keyword("where", "where "); | 71 | add_keyword("where", "where "); |
72 | if ctx.has_impl_prev_sibling() { | ||
73 | add_keyword("for", "for "); | ||
74 | } | ||
72 | return; | 75 | return; |
73 | } | 76 | } |
74 | if ctx.previous_token_is(T![unsafe]) { | 77 | if ctx.previous_token_is(T![unsafe]) { |
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs index 81ddfa34f..cbc20cc2c 100644 --- a/crates/ide_completion/src/completions/snippet.rs +++ b/crates/ide_completion/src/completions/snippet.rs | |||
@@ -36,7 +36,11 @@ 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]) || ctx.path_qual().is_some() { | 39 | if !ctx.expects_item() |
40 | || ctx.previous_token_is(T![unsafe]) | ||
41 | || ctx.path_qual().is_some() | ||
42 | || ctx.has_impl_or_trait_prev_sibling() | ||
43 | { | ||
40 | return; | 44 | return; |
41 | } | 45 | } |
42 | if ctx.has_visibility_prev_sibling() { | 46 | 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 1864bfbcc..8ea5a2d5b 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs | |||
@@ -6,7 +6,7 @@ use syntax::{ast, AstNode}; | |||
6 | use crate::{patterns::ImmediateLocation, CompletionContext, Completions}; | 6 | use crate::{patterns::ImmediateLocation, CompletionContext, Completions}; |
7 | 7 | ||
8 | pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { | 8 | pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { |
9 | if ctx.is_path_disallowed() || !ctx.is_trivial_path() { | 9 | if ctx.is_path_disallowed() || !ctx.is_trivial_path() || ctx.has_impl_or_trait_prev_sibling() { |
10 | return; | 10 | return; |
11 | } | 11 | } |
12 | 12 | ||
@@ -68,6 +68,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC | |||
68 | return; | 68 | return; |
69 | } | 69 | } |
70 | let add_resolution = match res { | 70 | let add_resolution = match res { |
71 | ScopeDef::ImplSelfType(_) => { | ||
72 | !ctx.previous_token_is(syntax::T![impl]) && !ctx.previous_token_is(syntax::T![for]) | ||
73 | } | ||
71 | // Don't suggest attribute macros and derives. | 74 | // Don't suggest attribute macros and derives. |
72 | ScopeDef::MacroDef(mac) => mac.is_fn_like(), | 75 | ScopeDef::MacroDef(mac) => mac.is_fn_like(), |
73 | // no values in type places | 76 | // no values in type places |
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 240cac1de..d7a7e9cca 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -305,6 +305,10 @@ impl<'a> CompletionContext<'a> { | |||
305 | ) | 305 | ) |
306 | } | 306 | } |
307 | 307 | ||
308 | pub(crate) fn has_impl_prev_sibling(&self) -> bool { | ||
309 | matches!(self.prev_sibling, Some(ImmediatePrevSibling::ImplDefType)) | ||
310 | } | ||
311 | |||
308 | pub(crate) fn has_visibility_prev_sibling(&self) -> bool { | 312 | pub(crate) fn has_visibility_prev_sibling(&self) -> bool { |
309 | matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility)) | 313 | matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility)) |
310 | } | 314 | } |
diff --git a/crates/ide_completion/src/tests/items.rs b/crates/ide_completion/src/tests/items.rs index dd4ba3864..8dfb8221b 100644 --- a/crates/ide_completion/src/tests/items.rs +++ b/crates/ide_completion/src/tests/items.rs | |||
@@ -23,13 +23,11 @@ trait Trait {} | |||
23 | 23 | ||
24 | #[test] | 24 | #[test] |
25 | fn target_type_or_trait_in_impl_block() { | 25 | fn target_type_or_trait_in_impl_block() { |
26 | // FIXME: should not complete `Self` | ||
27 | check( | 26 | check( |
28 | r#" | 27 | r#" |
29 | impl My$0 | 28 | impl Tra$0 |
30 | "#, | 29 | "#, |
31 | expect![[r##" | 30 | expect![[r##" |
32 | sp Self | ||
33 | tt Trait | 31 | tt Trait |
34 | en Enum | 32 | en Enum |
35 | st Struct | 33 | st Struct |
@@ -58,36 +56,57 @@ impl My$0 | |||
58 | } | 56 | } |
59 | 57 | ||
60 | #[test] | 58 | #[test] |
61 | fn after_trait_name_in_trait_def() { | 59 | fn target_type_in_trait_impl_block() { |
62 | // FIXME: should only complete `where` | ||
63 | check( | 60 | check( |
64 | r"trait A $0", | 61 | r#" |
62 | impl Trait for Str$0 | ||
63 | "#, | ||
65 | expect![[r##" | 64 | expect![[r##" |
66 | kw where | 65 | tt Trait |
67 | sn tmod (Test module) | 66 | en Enum |
68 | sn tfn (Test function) | 67 | st Struct |
69 | sn macro_rules | ||
70 | md bar | 68 | md bar |
71 | ma foo!(…) #[macro_export] macro_rules! foo | 69 | ma foo!(…) #[macro_export] macro_rules! foo |
72 | ma foo!(…) #[macro_export] macro_rules! foo | 70 | ma foo!(…) #[macro_export] macro_rules! foo |
71 | bt u32 | ||
72 | bt bool | ||
73 | bt u8 | ||
74 | bt isize | ||
75 | bt u16 | ||
76 | bt u64 | ||
77 | bt u128 | ||
78 | bt f32 | ||
79 | bt i128 | ||
80 | bt i16 | ||
81 | bt str | ||
82 | bt i64 | ||
83 | bt char | ||
84 | bt f64 | ||
85 | bt i32 | ||
86 | bt i8 | ||
87 | bt usize | ||
73 | "##]], | 88 | "##]], |
89 | ) | ||
90 | } | ||
91 | |||
92 | #[test] | ||
93 | fn after_trait_name_in_trait_def() { | ||
94 | check( | ||
95 | r"trait A $0", | ||
96 | expect![[r#" | ||
97 | kw where | ||
98 | "#]], | ||
74 | ); | 99 | ); |
75 | } | 100 | } |
76 | 101 | ||
77 | #[test] | 102 | #[test] |
78 | fn after_trait_or_target_name_in_impl() { | 103 | fn after_trait_or_target_name_in_impl() { |
79 | // FIXME: should only complete `for` and `where` | ||
80 | check( | 104 | check( |
81 | r"impl A $0", | 105 | r"impl Trait $0", |
82 | expect![[r##" | 106 | expect![[r#" |
83 | kw where | 107 | kw where |
84 | sn tmod (Test module) | 108 | kw for |
85 | sn tfn (Test function) | 109 | "#]], |
86 | sn macro_rules | ||
87 | md bar | ||
88 | ma foo!(…) #[macro_export] macro_rules! foo | ||
89 | ma foo!(…) #[macro_export] macro_rules! foo | ||
90 | "##]], | ||
91 | ); | 110 | ); |
92 | } | 111 | } |
93 | 112 | ||