aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/patterns.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/patterns.rs')
-rw-r--r--crates/ide_completion/src/patterns.rs41
1 files changed, 18 insertions, 23 deletions
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index 3d8a83ed8..8f0d76661 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -91,11 +91,10 @@ fn test_has_ref_parent() {
91} 91}
92 92
93pub(crate) fn has_item_list_or_source_file_parent(element: SyntaxElement) -> bool { 93pub(crate) fn has_item_list_or_source_file_parent(element: SyntaxElement) -> bool {
94 let ancestor = not_same_range_ancestor(element); 94 match not_same_range_ancestor(element) {
95 if !ancestor.is_some() { 95 Some(it) => it.kind() == SOURCE_FILE || it.kind() == ITEM_LIST,
96 return true; 96 None => true,
97 } 97 }
98 ancestor.filter(|it| it.kind() == SOURCE_FILE || it.kind() == ITEM_LIST).is_some()
99} 98}
100#[test] 99#[test]
101fn test_has_item_list_or_source_file_parent() { 100fn test_has_item_list_or_source_file_parent() {
@@ -151,25 +150,21 @@ fn test_has_impl_as_prev_sibling() {
151} 150}
152 151
153pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool { 152pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
154 for node in element.ancestors() { 153 element
155 if node.kind() == FN || node.kind() == CLOSURE_EXPR { 154 .ancestors()
156 break; 155 .take_while(|it| it.kind() != FN && it.kind() != CLOSURE_EXPR)
157 } 156 .find_map(|it| {
158 let loop_body = match_ast! { 157 let loop_body = match_ast! {
159 match node { 158 match it {
160 ast::ForExpr(it) => it.loop_body(), 159 ast::ForExpr(it) => it.loop_body(),
161 ast::WhileExpr(it) => it.loop_body(), 160 ast::WhileExpr(it) => it.loop_body(),
162 ast::LoopExpr(it) => it.loop_body(), 161 ast::LoopExpr(it) => it.loop_body(),
163 _ => None, 162 _ => None,
164 } 163 }
165 }; 164 };
166 if let Some(body) = loop_body { 165 loop_body.filter(|it| it.syntax().text_range().contains_range(element.text_range()))
167 if body.syntax().text_range().contains_range(element.text_range()) { 166 })
168 return true; 167 .is_some()
169 }
170 }
171 }
172 false
173} 168}
174 169
175fn not_same_range_ancestor(element: SyntaxElement) -> Option<SyntaxNode> { 170fn not_same_range_ancestor(element: SyntaxElement) -> Option<SyntaxNode> {