diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-21 13:06:00 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-21 13:06:00 +0100 |
commit | 1137fc47bbbc8d648db3bc669e41fd059a09dd1d (patch) | |
tree | 8d0e74c6c5a734fc64edf2504aabfd11c09354c4 /crates/ra_ide_api/src/completion/completion_context.rs | |
parent | 7bde8012cb28c44de7ffc779003781d385323808 (diff) | |
parent | 5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 (diff) |
Merge #1572
1572: Provide completion in struct patterns r=matklad a=viorina
Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/completion_context.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_context.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 2f78d5409..6fee7b5be 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs | |||
@@ -21,6 +21,7 @@ pub(crate) struct CompletionContext<'a> { | |||
21 | pub(super) function_syntax: Option<ast::FnDef>, | 21 | pub(super) function_syntax: Option<ast::FnDef>, |
22 | pub(super) use_item_syntax: Option<ast::UseItem>, | 22 | pub(super) use_item_syntax: Option<ast::UseItem>, |
23 | pub(super) struct_lit_syntax: Option<ast::StructLit>, | 23 | pub(super) struct_lit_syntax: Option<ast::StructLit>, |
24 | pub(super) struct_lit_pat: Option<ast::StructPat>, | ||
24 | pub(super) is_param: bool, | 25 | pub(super) is_param: bool, |
25 | /// If a name-binding or reference to a const in a pattern. | 26 | /// If a name-binding or reference to a const in a pattern. |
26 | /// Irrefutable patterns (like let) are excluded. | 27 | /// Irrefutable patterns (like let) are excluded. |
@@ -60,6 +61,7 @@ impl<'a> CompletionContext<'a> { | |||
60 | function_syntax: None, | 61 | function_syntax: None, |
61 | use_item_syntax: None, | 62 | use_item_syntax: None, |
62 | struct_lit_syntax: None, | 63 | struct_lit_syntax: None, |
64 | struct_lit_pat: None, | ||
63 | is_param: false, | 65 | is_param: false, |
64 | is_pat_binding: false, | 66 | is_pat_binding: false, |
65 | is_trivial_path: false, | 67 | is_trivial_path: false, |
@@ -106,8 +108,7 @@ impl<'a> CompletionContext<'a> { | |||
106 | // Otherwise, see if this is a declaration. We can use heuristics to | 108 | // Otherwise, see if this is a declaration. We can use heuristics to |
107 | // suggest declaration names, see `CompletionKind::Magic`. | 109 | // suggest declaration names, see `CompletionKind::Magic`. |
108 | if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) { | 110 | if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) { |
109 | if is_node::<ast::BindPat>(name.syntax()) { | 111 | if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { |
110 | let bind_pat = name.syntax().ancestors().find_map(ast::BindPat::cast).unwrap(); | ||
111 | let parent = bind_pat.syntax().parent(); | 112 | let parent = bind_pat.syntax().parent(); |
112 | if parent.clone().and_then(ast::MatchArm::cast).is_some() | 113 | if parent.clone().and_then(ast::MatchArm::cast).is_some() |
113 | || parent.and_then(ast::Condition::cast).is_some() | 114 | || parent.and_then(ast::Condition::cast).is_some() |
@@ -119,6 +120,10 @@ impl<'a> CompletionContext<'a> { | |||
119 | self.is_param = true; | 120 | self.is_param = true; |
120 | return; | 121 | return; |
121 | } | 122 | } |
123 | if name.syntax().ancestors().find_map(ast::FieldPatList::cast).is_some() { | ||
124 | self.struct_lit_pat = | ||
125 | find_node_at_offset(original_parse.tree().syntax(), self.offset); | ||
126 | } | ||
122 | } | 127 | } |
123 | } | 128 | } |
124 | 129 | ||
@@ -235,7 +240,7 @@ fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Op | |||
235 | } | 240 | } |
236 | 241 | ||
237 | fn is_node<N: AstNode>(node: &SyntaxNode) -> bool { | 242 | fn is_node<N: AstNode>(node: &SyntaxNode) -> bool { |
238 | match node.ancestors().filter_map(N::cast).next() { | 243 | match node.ancestors().find_map(N::cast) { |
239 | None => false, | 244 | None => false, |
240 | Some(n) => n.syntax().text_range() == node.text_range(), | 245 | Some(n) => n.syntax().text_range() == node.text_range(), |
241 | } | 246 | } |