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.rs12
1 files changed, 9 insertions, 3 deletions
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index c8a88367d..f04471b57 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -24,12 +24,12 @@ pub(crate) enum ImmediateLocation {
24 ItemList, 24 ItemList,
25} 25}
26 26
27pub(crate) fn determine_location(tok: SyntaxToken) -> Option<ImmediateLocation> { 27pub(crate) fn determine_location(name_like: &ast::NameLike) -> Option<ImmediateLocation> {
28 // First walk the element we are completing up to its highest node that has the same text range 28 // First walk the element we are completing up to its highest node that has the same text range
29 // as the element so that we can check in what context it immediately lies. We only do this for 29 // as the element so that we can check in what context it immediately lies. We only do this for
30 // NameRef -> Path as that's the only thing that makes sense to being "expanded" semantically. 30 // NameRef -> Path as that's the only thing that makes sense to being "expanded" semantically.
31 // We only wanna do this if the NameRef is the last segment of the path. 31 // We only wanna do this if the NameRef is the last segment of the path.
32 let node = match tok.parent().and_then(ast::NameLike::cast)? { 32 let node = match name_like {
33 ast::NameLike::NameRef(name_ref) => { 33 ast::NameLike::NameRef(name_ref) => {
34 if let Some(segment) = name_ref.syntax().parent().and_then(ast::PathSegment::cast) { 34 if let Some(segment) = name_ref.syntax().parent().and_then(ast::PathSegment::cast) {
35 let p = segment.parent_path(); 35 let p = segment.parent_path();
@@ -93,7 +93,8 @@ pub(crate) fn determine_location(tok: SyntaxToken) -> Option<ImmediateLocation>
93#[cfg(test)] 93#[cfg(test)]
94fn check_location(code: &str, loc: ImmediateLocation) { 94fn check_location(code: &str, loc: ImmediateLocation) {
95 check_pattern_is_applicable(code, |e| { 95 check_pattern_is_applicable(code, |e| {
96 assert_eq!(determine_location(e.into_token().expect("Expected a token")), Some(loc)); 96 let name = &e.parent().and_then(ast::NameLike::cast).expect("Expected a namelike");
97 assert_eq!(determine_location(name), Some(loc));
97 true 98 true
98 }); 99 });
99} 100}
@@ -199,6 +200,11 @@ fn test_has_impl_as_prev_sibling() {
199 check_pattern_is_applicable(r"impl A w$0 {}", |it| has_prev_sibling(it, IMPL)); 200 check_pattern_is_applicable(r"impl A w$0 {}", |it| has_prev_sibling(it, IMPL));
200} 201}
201 202
203#[test]
204fn test_has_trait_as_prev_sibling() {
205 check_pattern_is_applicable(r"trait A w$0 {}", |it| has_prev_sibling(it, TRAIT));
206}
207
202pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool { 208pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
203 element 209 element
204 .ancestors() 210 .ancestors()