aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_keyword.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/complete_keyword.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_keyword.rs183
1 files changed, 29 insertions, 154 deletions
diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs
index 8ea51c7aa..432793de2 100644
--- a/crates/ra_ide/src/completion/complete_keyword.rs
+++ b/crates/ra_ide/src/completion/complete_keyword.rs
@@ -61,7 +61,6 @@ fn add_keyword(
61 61
62pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { 62pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
63 add_keyword(ctx, acc, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent); 63 add_keyword(ctx, acc, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
64 add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
65 add_keyword(ctx, acc, "use", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent); 64 add_keyword(ctx, acc, "use", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
66 add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item); 65 add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item);
67 add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item); 66 add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item);
@@ -111,12 +110,9 @@ fn complete_return(
111 110
112#[cfg(test)] 111#[cfg(test)]
113mod tests { 112mod tests {
114 use crate::{ 113 use crate::completion::{
115 completion::{ 114 test_utils::{do_completion, get_completions},
116 test_utils::{do_completion, do_completion_with_position}, 115 CompletionItem, CompletionKind,
117 CompletionItem, CompletionKind,
118 },
119 CompletionItemKind,
120 }; 116 };
121 use insta::assert_debug_snapshot; 117 use insta::assert_debug_snapshot;
122 118
@@ -124,132 +120,39 @@ mod tests {
124 do_completion(code, CompletionKind::Keyword) 120 do_completion(code, CompletionKind::Keyword)
125 } 121 }
126 122
127 fn get_completion_text_and_assert_positions(code: &str) -> Vec<(String, String)> { 123 fn get_keyword_completions(code: &str) -> Vec<String> {
128 let (position, completion_items) = 124 get_completions(code, CompletionKind::Keyword)
129 do_completion_with_position(code, CompletionKind::Keyword);
130 let mut returned_keywords = Vec::<(String, String)>::new();
131
132 for item in completion_items {
133 assert!(item.text_edit().len() == 1);
134 assert!(item.kind() == Some(CompletionItemKind::Keyword));
135 let atom = item.text_edit().iter().next().unwrap().clone();
136 assert!(atom.delete.start() == position.offset);
137 assert!(atom.delete.end() == position.offset);
138 let pair = (item.label().to_string(), atom.insert);
139 returned_keywords.push(pair);
140 }
141 returned_keywords.sort();
142 returned_keywords
143 }
144
145 #[test]
146 fn completes_keywords_in_use_stmt_new_approach() {
147 assert_debug_snapshot!(
148 get_completion_text_and_assert_positions(r"
149 use <|>
150 "),
151 @r###"
152 [
153 (
154 "crate",
155 "crate::",
156 ),
157 (
158 "self",
159 "self",
160 ),
161 (
162 "super",
163 "super::",
164 ),
165 ]
166 "###
167 );
168 } 125 }
169 126
170 #[test] 127 #[test]
171 fn completes_keywords_in_use_stmt() { 128 fn completes_keywords_in_use_stmt() {
172 assert_debug_snapshot!( 129 assert_debug_snapshot!(
173 do_keyword_completion( 130 get_keyword_completions(r"use <|>"),
174 r"
175 use <|>
176 ",
177 ),
178 @r###" 131 @r###"
179 [ 132 [
180 CompletionItem { 133 "kw crate",
181 label: "crate", 134 "kw self",
182 source_range: 21..21, 135 "kw super",
183 delete: 21..21,
184 insert: "crate::",
185 kind: Keyword,
186 },
187 CompletionItem {
188 label: "self",
189 source_range: 21..21,
190 delete: 21..21,
191 insert: "self",
192 kind: Keyword,
193 },
194 CompletionItem {
195 label: "super",
196 source_range: 21..21,
197 delete: 21..21,
198 insert: "super::",
199 kind: Keyword,
200 },
201 ] 136 ]
202 "### 137 "###
203 ); 138 );
204 139
205 assert_debug_snapshot!( 140 assert_debug_snapshot!(
206 do_keyword_completion( 141 get_keyword_completions(r"use a::<|>"),
207 r"
208 use a::<|>
209 ",
210 ),
211 @r###" 142 @r###"
212 [ 143 [
213 CompletionItem { 144 "kw self",
214 label: "self", 145 "kw super",
215 source_range: 24..24,
216 delete: 24..24,
217 insert: "self",
218 kind: Keyword,
219 },
220 CompletionItem {
221 label: "super",
222 source_range: 24..24,
223 delete: 24..24,
224 insert: "super::",
225 kind: Keyword,
226 },
227 ] 146 ]
228 "### 147 "###
229 ); 148 );
230 149
231 assert_debug_snapshot!( 150 assert_debug_snapshot!(
232 do_keyword_completion( 151 get_keyword_completions(r"use a::{b, <|>}"),
233 r"
234 use a::{b, <|>}
235 ",
236 ),
237 @r###" 152 @r###"
238 [ 153 [
239 CompletionItem { 154 "kw self",
240 label: "self", 155 "kw super",
241 source_range: 28..28,
242 delete: 28..28,
243 insert: "self",
244 kind: Keyword,
245 },
246 CompletionItem {
247 label: "super",
248 source_range: 28..28,
249 delete: 28..28,
250 insert: "super::",
251 kind: Keyword,
252 },
253 ] 156 ]
254 "### 157 "###
255 ); 158 );
@@ -258,50 +161,22 @@ mod tests {
258 #[test] 161 #[test]
259 fn completes_various_keywords_in_function() { 162 fn completes_various_keywords_in_function() {
260 assert_debug_snapshot!( 163 assert_debug_snapshot!(
261 do_keyword_completion( 164 get_keyword_completions(r"fn quux() { <|> }"),
262 r"
263 fn quux() {
264 <|>
265 }
266 ",
267 ),
268 @r###" 165 @r###"
269 [ 166 [
270 CompletionItem { 167 "kw const",
271 label: "if", 168 "kw extern",
272 source_range: 49..49, 169 "kw fn",
273 delete: 49..49, 170 "kw let",
274 insert: "if $0 {}", 171 "kw loop",
275 kind: Keyword, 172 "kw match",
276 }, 173 "kw mod",
277 CompletionItem { 174 "kw return",
278 label: "loop", 175 "kw static",
279 source_range: 49..49, 176 "kw type",
280 delete: 49..49, 177 "kw unsafe",
281 insert: "loop {$0}", 178 "kw use",
282 kind: Keyword, 179 "kw while",
283 },
284 CompletionItem {
285 label: "match",
286 source_range: 49..49,
287 delete: 49..49,
288 insert: "match $0 {}",
289 kind: Keyword,
290 },
291 CompletionItem {
292 label: "return",
293 source_range: 49..49,
294 delete: 49..49,
295 insert: "return;",
296 kind: Keyword,
297 },
298 CompletionItem {
299 label: "while",
300 source_range: 49..49,
301 delete: 49..49,
302 insert: "while $0 {}",
303 kind: Keyword,
304 },
305 ] 180 ]
306 "### 181 "###
307 ); 182 );