diff options
author | Mikhail Rakhmanov <[email protected]> | 2020-06-11 23:17:30 +0100 |
---|---|---|
committer | Mikhail Rakhmanov <[email protected]> | 2020-06-11 23:17:30 +0100 |
commit | a2b4385f161134955fd729087f142d54c3a5e035 (patch) | |
tree | 57340237a1bc00f3b5383881bc289d224e4c6ee3 /crates/ra_ide/src/completion/patterns.rs | |
parent | f46bc12199dc88b88bd826f49d5c5af6e9caf727 (diff) |
Add few smoke tests for patterns and refactoring
Diffstat (limited to 'crates/ra_ide/src/completion/patterns.rs')
-rw-r--r-- | crates/ra_ide/src/completion/patterns.rs | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/patterns.rs b/crates/ra_ide/src/completion/patterns.rs index 31d32ccd9..df2594152 100644 --- a/crates/ra_ide/src/completion/patterns.rs +++ b/crates/ra_ide/src/completion/patterns.rs | |||
@@ -22,7 +22,7 @@ pub(crate) fn has_ref_pat_parent(element: SyntaxElement) -> bool { | |||
22 | element.ancestors().find(|it| it.kind() == REF_PAT).is_some() | 22 | element.ancestors().find(|it| it.kind() == REF_PAT).is_some() |
23 | } | 23 | } |
24 | 24 | ||
25 | pub(crate) fn goes_after_unsafe(element: SyntaxElement) -> bool { | 25 | pub(crate) fn unsafe_is_prev(element: SyntaxElement) -> bool { |
26 | element | 26 | element |
27 | .into_token() | 27 | .into_token() |
28 | .and_then(|it| previous_non_trivia_token(it)) | 28 | .and_then(|it| previous_non_trivia_token(it)) |
@@ -30,6 +30,14 @@ pub(crate) fn goes_after_unsafe(element: SyntaxElement) -> bool { | |||
30 | .is_some() | 30 | .is_some() |
31 | } | 31 | } |
32 | 32 | ||
33 | pub(crate) fn if_is_prev(element: SyntaxElement) -> bool { | ||
34 | element | ||
35 | .into_token() | ||
36 | .and_then(|it| previous_non_trivia_token(it)) | ||
37 | .filter(|it| it.kind() == IF_KW) | ||
38 | .is_some() | ||
39 | } | ||
40 | |||
33 | pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool { | 41 | pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool { |
34 | not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some() | 42 | not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some() |
35 | } | 43 | } |
@@ -110,3 +118,79 @@ fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<Syntax | |||
110 | non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev) | 118 | non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev) |
111 | } | 119 | } |
112 | } | 120 | } |
121 | |||
122 | #[cfg(test)] | ||
123 | mod tests { | ||
124 | use super::{ | ||
125 | has_block_expr_parent, has_impl_as_prev_sibling, has_trait_as_prev_sibling, if_is_prev, | ||
126 | inside_trait, unsafe_is_prev, | ||
127 | }; | ||
128 | use crate::completion::test_utils::check_pattern_is_applicable; | ||
129 | |||
130 | #[test] | ||
131 | fn test_unsafe_is_prev() { | ||
132 | check_pattern_is_applicable( | ||
133 | r" | ||
134 | unsafe i<|> | ||
135 | ", | ||
136 | unsafe_is_prev, | ||
137 | ); | ||
138 | } | ||
139 | |||
140 | #[test] | ||
141 | fn test_if_is_prev() { | ||
142 | check_pattern_is_applicable( | ||
143 | r" | ||
144 | if l<|> | ||
145 | ", | ||
146 | if_is_prev, | ||
147 | ); | ||
148 | } | ||
149 | |||
150 | #[test] | ||
151 | fn test_inside_trait() { | ||
152 | check_pattern_is_applicable( | ||
153 | r" | ||
154 | trait A { | ||
155 | fn<|> | ||
156 | } | ||
157 | ", | ||
158 | inside_trait, | ||
159 | ); | ||
160 | } | ||
161 | |||
162 | #[test] | ||
163 | fn test_has_trait_as_prev_sibling() { | ||
164 | check_pattern_is_applicable( | ||
165 | r" | ||
166 | trait A w<|> { | ||
167 | } | ||
168 | ", | ||
169 | has_trait_as_prev_sibling, | ||
170 | ); | ||
171 | } | ||
172 | |||
173 | #[test] | ||
174 | fn test_has_impl_as_prev_sibling() { | ||
175 | check_pattern_is_applicable( | ||
176 | r" | ||
177 | impl A w<|> { | ||
178 | } | ||
179 | ", | ||
180 | has_impl_as_prev_sibling, | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | #[test] | ||
185 | fn test_parent_block_expr() { | ||
186 | check_pattern_is_applicable( | ||
187 | r" | ||
188 | fn my_fn() { | ||
189 | let a = 2; | ||
190 | f<|> | ||
191 | } | ||
192 | ", | ||
193 | has_block_expr_parent, | ||
194 | ); | ||
195 | } | ||
196 | } | ||