From 5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sun, 21 Jul 2019 14:11:45 +0300 Subject: provide completion in struct patterns --- crates/ra_ide_api/src/completion/completion_context.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api/src/completion/completion_context.rs') 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> { pub(super) function_syntax: Option, pub(super) use_item_syntax: Option, pub(super) struct_lit_syntax: Option, + pub(super) struct_lit_pat: Option, pub(super) is_param: bool, /// If a name-binding or reference to a const in a pattern. /// Irrefutable patterns (like let) are excluded. @@ -60,6 +61,7 @@ impl<'a> CompletionContext<'a> { function_syntax: None, use_item_syntax: None, struct_lit_syntax: None, + struct_lit_pat: None, is_param: false, is_pat_binding: false, is_trivial_path: false, @@ -106,8 +108,7 @@ impl<'a> CompletionContext<'a> { // Otherwise, see if this is a declaration. We can use heuristics to // suggest declaration names, see `CompletionKind::Magic`. if let Some(name) = find_node_at_offset::(file.syntax(), offset) { - if is_node::(name.syntax()) { - let bind_pat = name.syntax().ancestors().find_map(ast::BindPat::cast).unwrap(); + if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { let parent = bind_pat.syntax().parent(); if parent.clone().and_then(ast::MatchArm::cast).is_some() || parent.and_then(ast::Condition::cast).is_some() @@ -119,6 +120,10 @@ impl<'a> CompletionContext<'a> { self.is_param = true; return; } + if name.syntax().ancestors().find_map(ast::FieldPatList::cast).is_some() { + self.struct_lit_pat = + find_node_at_offset(original_parse.tree().syntax(), self.offset); + } } } @@ -235,7 +240,7 @@ fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Op } fn is_node(node: &SyntaxNode) -> bool { - match node.ancestors().filter_map(N::cast).next() { + match node.ancestors().find_map(N::cast) { None => false, Some(n) => n.syntax().text_range() == node.text_range(), } -- cgit v1.2.3