]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/patterns.rs
cf5ef07b726fa8e987a27884e5028844fd96ec2f
[rust.git] / crates / ide_completion / src / patterns.rs
1 //! Patterns telling us certain facts about current syntax element, they are used in completion context
2
3 use syntax::{
4     algo::non_trivia_sibling,
5     ast::{self, LoopBodyOwner},
6     match_ast, AstNode, Direction, NodeOrToken, SyntaxElement,
7     SyntaxKind::*,
8     SyntaxNode, SyntaxToken, T,
9 };
10
11 #[cfg(test)]
12 use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable};
13
14 pub(crate) fn has_trait_parent(element: SyntaxElement) -> bool {
15     not_same_range_ancestor(element)
16         .filter(|it| it.kind() == ASSOC_ITEM_LIST)
17         .and_then(|it| it.parent())
18         .filter(|it| it.kind() == TRAIT)
19         .is_some()
20 }
21 #[test]
22 fn test_has_trait_parent() {
23     check_pattern_is_applicable(r"trait A { f$0 }", has_trait_parent);
24 }
25
26 pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool {
27     not_same_range_ancestor(element)
28         .filter(|it| it.kind() == ASSOC_ITEM_LIST)
29         .and_then(|it| it.parent())
30         .filter(|it| it.kind() == IMPL)
31         .is_some()
32 }
33 #[test]
34 fn test_has_impl_parent() {
35     check_pattern_is_applicable(r"impl A { f$0 }", has_impl_parent);
36 }
37
38 pub(crate) fn inside_impl_trait_block(element: SyntaxElement) -> bool {
39     // Here we search `impl` keyword up through the all ancestors, unlike in `has_impl_parent`,
40     // where we only check the first parent with different text range.
41     element
42         .ancestors()
43         .find(|it| it.kind() == IMPL)
44         .map(|it| ast::Impl::cast(it).unwrap())
45         .map(|it| it.trait_().is_some())
46         .unwrap_or(false)
47 }
48 #[test]
49 fn test_inside_impl_trait_block() {
50     check_pattern_is_applicable(r"impl Foo for Bar { f$0 }", inside_impl_trait_block);
51     check_pattern_is_applicable(r"impl Foo for Bar { fn f$0 }", inside_impl_trait_block);
52     check_pattern_is_not_applicable(r"impl A { f$0 }", inside_impl_trait_block);
53     check_pattern_is_not_applicable(r"impl A { fn f$0 }", inside_impl_trait_block);
54 }
55
56 pub(crate) fn has_field_list_parent(element: SyntaxElement) -> bool {
57     not_same_range_ancestor(element).filter(|it| it.kind() == RECORD_FIELD_LIST).is_some()
58 }
59 #[test]
60 fn test_has_field_list_parent() {
61     check_pattern_is_applicable(r"struct Foo { f$0 }", has_field_list_parent);
62     check_pattern_is_applicable(r"struct Foo { f$0 pub f: i32}", has_field_list_parent);
63 }
64
65 pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
66     not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some()
67 }
68 #[test]
69 fn test_has_block_expr_parent() {
70     check_pattern_is_applicable(r"fn my_fn() { let a = 2; f$0 }", has_block_expr_parent);
71 }
72
73 pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool {
74     element.ancestors().find(|it| it.kind() == IDENT_PAT).is_some()
75 }
76 #[test]
77 fn 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() { let m$0 }", has_bind_pat_parent);
80 }
81
82 pub(crate) fn has_ref_parent(element: SyntaxElement) -> bool {
83     not_same_range_ancestor(element)
84         .filter(|it| it.kind() == REF_PAT || it.kind() == REF_EXPR)
85         .is_some()
86 }
87 #[test]
88 fn test_has_ref_parent() {
89     check_pattern_is_applicable(r"fn my_fn(&m$0) {}", has_ref_parent);
90     check_pattern_is_applicable(r"fn my() { let &m$0 }", has_ref_parent);
91 }
92
93 pub(crate) fn has_item_list_or_source_file_parent(element: SyntaxElement) -> bool {
94     let ancestor = not_same_range_ancestor(element);
95     if !ancestor.is_some() {
96         return true;
97     }
98     ancestor.filter(|it| it.kind() == SOURCE_FILE || it.kind() == ITEM_LIST).is_some()
99 }
100 #[test]
101 fn test_has_item_list_or_source_file_parent() {
102     check_pattern_is_applicable(r"i$0", has_item_list_or_source_file_parent);
103     check_pattern_is_applicable(r"mod foo { f$0 }", has_item_list_or_source_file_parent);
104 }
105
106 pub(crate) fn is_match_arm(element: SyntaxElement) -> bool {
107     not_same_range_ancestor(element.clone()).filter(|it| it.kind() == MATCH_ARM).is_some()
108         && previous_sibling_or_ancestor_sibling(element)
109             .and_then(|it| it.into_token())
110             .filter(|it| it.kind() == FAT_ARROW)
111             .is_some()
112 }
113 #[test]
114 fn test_is_match_arm() {
115     check_pattern_is_applicable(r"fn my_fn() { match () { () => m$0 } }", is_match_arm);
116 }
117
118 pub(crate) fn unsafe_is_prev(element: SyntaxElement) -> bool {
119     element
120         .into_token()
121         .and_then(|it| previous_non_trivia_token(it))
122         .filter(|it| it.kind() == T![unsafe])
123         .is_some()
124 }
125 #[test]
126 fn test_unsafe_is_prev() {
127     check_pattern_is_applicable(r"unsafe i$0", unsafe_is_prev);
128 }
129
130 pub(crate) fn if_is_prev(element: SyntaxElement) -> bool {
131     element
132         .into_token()
133         .and_then(|it| previous_non_trivia_token(it))
134         .filter(|it| it.kind() == T![if])
135         .is_some()
136 }
137
138 pub(crate) fn fn_is_prev(element: SyntaxElement) -> bool {
139     element
140         .into_token()
141         .and_then(|it| previous_non_trivia_token(it))
142         .filter(|it| it.kind() == T![fn])
143         .is_some()
144 }
145 #[test]
146 fn test_fn_is_prev() {
147     check_pattern_is_applicable(r"fn l$0", fn_is_prev);
148 }
149
150 /// Check if the token previous to the previous one is `for`.
151 /// For example, `for _ i$0` => true.
152 pub(crate) fn for_is_prev2(element: SyntaxElement) -> bool {
153     element
154         .into_token()
155         .and_then(|it| previous_non_trivia_token(it))
156         .and_then(|it| previous_non_trivia_token(it))
157         .filter(|it| it.kind() == T![for])
158         .is_some()
159 }
160 #[test]
161 fn test_for_is_prev2() {
162     check_pattern_is_applicable(r"for i i$0", for_is_prev2);
163 }
164
165 #[test]
166 fn test_if_is_prev() {
167     check_pattern_is_applicable(r"if l$0", if_is_prev);
168 }
169
170 pub(crate) fn has_trait_as_prev_sibling(element: SyntaxElement) -> bool {
171     previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == TRAIT).is_some()
172 }
173 #[test]
174 fn test_has_trait_as_prev_sibling() {
175     check_pattern_is_applicable(r"trait A w$0 {}", has_trait_as_prev_sibling);
176 }
177
178 pub(crate) fn has_impl_as_prev_sibling(element: SyntaxElement) -> bool {
179     previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == IMPL).is_some()
180 }
181 #[test]
182 fn test_has_impl_as_prev_sibling() {
183     check_pattern_is_applicable(r"impl A w$0 {}", has_impl_as_prev_sibling);
184 }
185
186 pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
187     for node in element.ancestors() {
188         if node.kind() == FN || node.kind() == CLOSURE_EXPR {
189             break;
190         }
191         let loop_body = match_ast! {
192             match node {
193                 ast::ForExpr(it) => it.loop_body(),
194                 ast::WhileExpr(it) => it.loop_body(),
195                 ast::LoopExpr(it) => it.loop_body(),
196                 _ => None,
197             }
198         };
199         if let Some(body) = loop_body {
200             if body.syntax().text_range().contains_range(element.text_range()) {
201                 return true;
202             }
203         }
204     }
205     false
206 }
207
208 fn not_same_range_ancestor(element: SyntaxElement) -> Option<SyntaxNode> {
209     element
210         .ancestors()
211         .take_while(|it| it.text_range() == element.text_range())
212         .last()
213         .and_then(|it| it.parent())
214 }
215
216 fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
217     let mut token = token.prev_token();
218     while let Some(inner) = token.clone() {
219         if !inner.kind().is_trivia() {
220             return Some(inner);
221         } else {
222             token = inner.prev_token();
223         }
224     }
225     None
226 }
227
228 fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<SyntaxElement> {
229     let token_sibling = non_trivia_sibling(element.clone(), Direction::Prev);
230     if let Some(sibling) = token_sibling {
231         Some(sibling)
232     } else {
233         // if not trying to find first ancestor which has such a sibling
234         let range = element.text_range();
235         let top_node = element.ancestors().take_while(|it| it.text_range() == range).last()?;
236         let prev_sibling_node = top_node.ancestors().find(|it| {
237             non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some()
238         })?;
239         non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev)
240     }
241 }