aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-05-27 02:01:46 +0100
committerLukas Wirth <[email protected]>2021-05-27 02:15:48 +0100
commit6ec4ea8d9eb9ad6ad8b91968bde09121b5b791a0 (patch)
treee8750a920f92bbbd4fe3cb23be585eed7ad92b24 /crates/ide_completion
parent7de925b8abcf0d26869dc96f346510af61663edf (diff)
simplify
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/context.rs11
-rw-r--r--crates/ide_completion/src/patterns.rs17
2 files changed, 10 insertions, 18 deletions
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index dfac8f29f..5d15fde2f 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -18,9 +18,8 @@ use text_edit::Indel;
18use crate::{ 18use crate::{
19 patterns::{ 19 patterns::{
20 for_is_prev2, has_bind_pat_parent, has_block_expr_parent, has_field_list_parent, 20 for_is_prev2, has_bind_pat_parent, has_block_expr_parent, has_field_list_parent,
21 has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent, 21 has_impl_parent, has_item_list_or_source_file_parent, has_prev_sibling, has_ref_parent,
22 has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, inside_impl_trait_block, 22 has_trait_parent, inside_impl_trait_block, is_in_loop_body, is_match_arm, previous_token,
23 is_in_loop_body, is_match_arm, previous_token,
24 }, 23 },
25 CompletionConfig, 24 CompletionConfig,
26}; 25};
@@ -44,7 +43,7 @@ pub(crate) enum ImmediateLocation {
44} 43}
45 44
46#[derive(Copy, Clone, Debug, PartialEq, Eq)] 45#[derive(Copy, Clone, Debug, PartialEq, Eq)]
47pub enum PrevSibling { 46pub(crate) enum PrevSibling {
48 Trait, 47 Trait,
49 Impl, 48 Impl,
50} 49}
@@ -323,9 +322,9 @@ impl<'a> CompletionContext<'a> {
323 self.previous_token = previous_token(syntax_element.clone()); 322 self.previous_token = previous_token(syntax_element.clone());
324 self.in_loop_body = is_in_loop_body(syntax_element.clone()); 323 self.in_loop_body = is_in_loop_body(syntax_element.clone());
325 self.is_match_arm = is_match_arm(syntax_element.clone()); 324 self.is_match_arm = is_match_arm(syntax_element.clone());
326 if has_impl_as_prev_sibling(syntax_element.clone()) { 325 if has_prev_sibling(syntax_element.clone(), IMPL) {
327 self.prev_sibling = Some(PrevSibling::Impl) 326 self.prev_sibling = Some(PrevSibling::Impl)
328 } else if has_trait_as_prev_sibling(syntax_element.clone()) { 327 } else if has_prev_sibling(syntax_element.clone(), TRAIT) {
329 self.prev_sibling = Some(PrevSibling::Trait) 328 self.prev_sibling = Some(PrevSibling::Trait)
330 } 329 }
331 330
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index 8f0d76661..04f2c532b 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -4,7 +4,7 @@ use syntax::{
4 algo::non_trivia_sibling, 4 algo::non_trivia_sibling,
5 ast::{self, LoopBodyOwner}, 5 ast::{self, LoopBodyOwner},
6 match_ast, AstNode, Direction, NodeOrToken, SyntaxElement, 6 match_ast, AstNode, Direction, NodeOrToken, SyntaxElement,
7 SyntaxKind::*, 7 SyntaxKind::{self, *},
8 SyntaxNode, SyntaxToken, T, 8 SyntaxNode, SyntaxToken, T,
9}; 9};
10 10
@@ -73,6 +73,7 @@ fn test_has_block_expr_parent() {
73pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool { 73pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool {
74 element.ancestors().any(|it| it.kind() == IDENT_PAT) 74 element.ancestors().any(|it| it.kind() == IDENT_PAT)
75} 75}
76
76#[test] 77#[test]
77fn test_has_bind_pat_parent() { 78fn test_has_bind_pat_parent() {
78 check_pattern_is_applicable(r"fn my_fn(m$0) {}", has_bind_pat_parent); 79 check_pattern_is_applicable(r"fn my_fn(m$0) {}", has_bind_pat_parent);
@@ -133,20 +134,12 @@ fn test_for_is_prev2() {
133 check_pattern_is_applicable(r"for i i$0", for_is_prev2); 134 check_pattern_is_applicable(r"for i i$0", for_is_prev2);
134} 135}
135 136
136pub(crate) fn has_trait_as_prev_sibling(element: SyntaxElement) -> bool { 137pub(crate) fn has_prev_sibling(element: SyntaxElement, kind: SyntaxKind) -> bool {
137 previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == TRAIT).is_some() 138 previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == kind).is_some()
138}
139#[test]
140fn test_has_trait_as_prev_sibling() {
141 check_pattern_is_applicable(r"trait A w$0 {}", has_trait_as_prev_sibling);
142}
143
144pub(crate) fn has_impl_as_prev_sibling(element: SyntaxElement) -> bool {
145 previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == IMPL).is_some()
146} 139}
147#[test] 140#[test]
148fn test_has_impl_as_prev_sibling() { 141fn test_has_impl_as_prev_sibling() {
149 check_pattern_is_applicable(r"impl A w$0 {}", has_impl_as_prev_sibling); 142 check_pattern_is_applicable(r"impl A w$0 {}", |it| has_prev_sibling(it, IMPL));
150} 143}
151 144
152pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool { 145pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {