aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/completion/complete_keyword.rs318
-rw-r--r--crates/ra_ide/src/completion/test_utils.rs10
2 files changed, 146 insertions, 182 deletions
diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs
index 0ab330b73..b2f621a11 100644
--- a/crates/ra_ide/src/completion/complete_keyword.rs
+++ b/crates/ra_ide/src/completion/complete_keyword.rs
@@ -170,131 +170,119 @@ fn complete_return(
170 170
171#[cfg(test)] 171#[cfg(test)]
172mod tests { 172mod tests {
173 use crate::completion::{test_utils::get_completions, CompletionKind}; 173 use crate::completion::{test_utils::completion_list, CompletionKind};
174 use insta::assert_debug_snapshot; 174 use insta::assert_snapshot;
175 175
176 fn get_keyword_completions(code: &str) -> Vec<String> { 176 fn get_keyword_completions(code: &str) -> String {
177 get_completions(code, CompletionKind::Keyword) 177 completion_list(code, CompletionKind::Keyword)
178 } 178 }
179 179
180 #[test] 180 #[test]
181 fn test_keywords_in_use_stmt() { 181 fn test_keywords_in_use_stmt() {
182 assert_debug_snapshot!( 182 assert_snapshot!(
183 get_keyword_completions(r"use <|>"), 183 get_keyword_completions(r"use <|>"),
184 @r###" 184 @r###"
185 [ 185 kw crate
186 "kw crate", 186 kw self
187 "kw self", 187 kw super
188 "kw super",
189 ]
190 "### 188 "###
191 ); 189 );
192 190
193 assert_debug_snapshot!( 191 assert_snapshot!(
194 get_keyword_completions(r"use a::<|>"), 192 get_keyword_completions(r"use a::<|>"),
195 @r###" 193 @r###"
196 [ 194 kw self
197 "kw self", 195 kw super
198 "kw super",
199 ]
200 "### 196 "###
201 ); 197 );
202 198
203 assert_debug_snapshot!( 199 assert_snapshot!(
204 get_keyword_completions(r"use a::{b, <|>}"), 200 get_keyword_completions(r"use a::{b, <|>}"),
205 @r###" 201 @r###"
206 [ 202 kw self
207 "kw self", 203 kw super
208 "kw super",
209 ]
210 "### 204 "###
211 ); 205 );
212 } 206 }
213 207
214 #[test] 208 #[test]
215 fn test_keywords_at_source_file_level() { 209 fn test_keywords_at_source_file_level() {
216 assert_debug_snapshot!( 210 assert_snapshot!(
217 get_keyword_completions(r"m<|>"), 211 get_keyword_completions(r"m<|>"),
218 @r###" 212 @r###"
219 [ 213 kw const
220 "kw const", 214 kw enum
221 "kw enum", 215 kw extern
222 "kw extern", 216 kw fn
223 "kw fn", 217 kw impl
224 "kw impl", 218 kw mod
225 "kw mod", 219 kw pub
226 "kw pub", 220 kw static
227 "kw static", 221 kw struct
228 "kw struct", 222 kw trait
229 "kw trait", 223 kw type
230 "kw type", 224 kw union
231 "kw union", 225 kw unsafe
232 "kw unsafe", 226 kw use
233 "kw use",
234 ]
235 "### 227 "###
236 ); 228 );
237 } 229 }
238 230
239 #[test] 231 #[test]
240 fn test_keywords_in_function() { 232 fn test_keywords_in_function() {
241 assert_debug_snapshot!( 233 assert_snapshot!(
242 get_keyword_completions(r"fn quux() { <|> }"), 234 get_keyword_completions(r"fn quux() { <|> }"),
243 @r###" 235 @r###"
244 [ 236 kw const
245 "kw const", 237 kw extern
246 "kw extern", 238 kw fn
247 "kw fn", 239 kw if
248 "kw if", 240 kw if let
249 "kw if let", 241 kw impl
250 "kw impl", 242 kw let
251 "kw let", 243 kw loop
252 "kw loop", 244 kw match
253 "kw match", 245 kw mod
254 "kw mod", 246 kw return
255 "kw return", 247 kw static
256 "kw static", 248 kw trait
257 "kw trait", 249 kw type
258 "kw type", 250 kw unsafe
259 "kw unsafe", 251 kw use
260 "kw use", 252 kw while
261 "kw while",
262 ]
263 "### 253 "###
264 ); 254 );
265 } 255 }
266 256
267 #[test] 257 #[test]
268 fn test_keywords_inside_block() { 258 fn test_keywords_inside_block() {
269 assert_debug_snapshot!( 259 assert_snapshot!(
270 get_keyword_completions(r"fn quux() { if true { <|> } }"), 260 get_keyword_completions(r"fn quux() { if true { <|> } }"),
271 @r###" 261 @r###"
272 [ 262 kw const
273 "kw const", 263 kw extern
274 "kw extern", 264 kw fn
275 "kw fn", 265 kw if
276 "kw if", 266 kw if let
277 "kw if let", 267 kw impl
278 "kw impl", 268 kw let
279 "kw let", 269 kw loop
280 "kw loop", 270 kw match
281 "kw match", 271 kw mod
282 "kw mod", 272 kw return
283 "kw return", 273 kw static
284 "kw static", 274 kw trait
285 "kw trait", 275 kw type
286 "kw type", 276 kw unsafe
287 "kw unsafe", 277 kw use
288 "kw use", 278 kw while
289 "kw while",
290 ]
291 "### 279 "###
292 ); 280 );
293 } 281 }
294 282
295 #[test] 283 #[test]
296 fn test_keywords_after_if() { 284 fn test_keywords_after_if() {
297 assert_debug_snapshot!( 285 assert_snapshot!(
298 get_keyword_completions( 286 get_keyword_completions(
299 r" 287 r"
300 fn quux() { 288 fn quux() {
@@ -305,34 +293,32 @@ mod tests {
305 ", 293 ",
306 ), 294 ),
307 @r###" 295 @r###"
308 [ 296 kw const
309 "kw const", 297 kw else
310 "kw else", 298 kw else if
311 "kw else if", 299 kw extern
312 "kw extern", 300 kw fn
313 "kw fn", 301 kw if
314 "kw if", 302 kw if let
315 "kw if let", 303 kw impl
316 "kw impl", 304 kw let
317 "kw let", 305 kw loop
318 "kw loop", 306 kw match
319 "kw match", 307 kw mod
320 "kw mod", 308 kw return
321 "kw return", 309 kw static
322 "kw static", 310 kw trait
323 "kw trait", 311 kw type
324 "kw type", 312 kw unsafe
325 "kw unsafe", 313 kw use
326 "kw use", 314 kw while
327 "kw while",
328 ]
329 "### 315 "###
330 ); 316 );
331 } 317 }
332 318
333 #[test] 319 #[test]
334 fn test_keywords_in_match_arm() { 320 fn test_keywords_in_match_arm() {
335 assert_debug_snapshot!( 321 assert_snapshot!(
336 get_keyword_completions( 322 get_keyword_completions(
337 r" 323 r"
338 fn quux() -> i32 { 324 fn quux() -> i32 {
@@ -343,151 +329,129 @@ mod tests {
343 ", 329 ",
344 ), 330 ),
345 @r###" 331 @r###"
346 [ 332 kw if
347 "kw if", 333 kw if let
348 "kw if let", 334 kw loop
349 "kw loop", 335 kw match
350 "kw match", 336 kw return
351 "kw return", 337 kw unsafe
352 "kw unsafe",
353 ]
354 "### 338 "###
355 ); 339 );
356 } 340 }
357 341
358 #[test] 342 #[test]
359 fn test_keywords_in_trait_def() { 343 fn test_keywords_in_trait_def() {
360 assert_debug_snapshot!( 344 assert_snapshot!(
361 get_keyword_completions(r"trait My { <|> }"), 345 get_keyword_completions(r"trait My { <|> }"),
362 @r###" 346 @r###"
363 [ 347 kw const
364 "kw const", 348 kw fn
365 "kw fn", 349 kw type
366 "kw type", 350 kw unsafe
367 "kw unsafe",
368 ]
369 "### 351 "###
370 ); 352 );
371 } 353 }
372 354
373 #[test] 355 #[test]
374 fn test_keywords_in_impl_def() { 356 fn test_keywords_in_impl_def() {
375 assert_debug_snapshot!( 357 assert_snapshot!(
376 get_keyword_completions(r"impl My { <|> }"), 358 get_keyword_completions(r"impl My { <|> }"),
377 @r###" 359 @r###"
378 [ 360 kw const
379 "kw const", 361 kw fn
380 "kw fn", 362 kw pub
381 "kw pub", 363 kw type
382 "kw type", 364 kw unsafe
383 "kw unsafe",
384 ]
385 "### 365 "###
386 ); 366 );
387 } 367 }
388 368
389 #[test] 369 #[test]
390 fn test_keywords_in_loop() { 370 fn test_keywords_in_loop() {
391 assert_debug_snapshot!( 371 assert_snapshot!(
392 get_keyword_completions(r"fn my() { loop { <|> } }"), 372 get_keyword_completions(r"fn my() { loop { <|> } }"),
393 @r###" 373 @r###"
394 [ 374 kw break
395 "kw break", 375 kw const
396 "kw const", 376 kw continue
397 "kw continue", 377 kw extern
398 "kw extern", 378 kw fn
399 "kw fn", 379 kw if
400 "kw if", 380 kw if let
401 "kw if let", 381 kw impl
402 "kw impl", 382 kw let
403 "kw let", 383 kw loop
404 "kw loop", 384 kw match
405 "kw match", 385 kw mod
406 "kw mod", 386 kw return
407 "kw return", 387 kw static
408 "kw static", 388 kw trait
409 "kw trait", 389 kw type
410 "kw type", 390 kw unsafe
411 "kw unsafe", 391 kw use
412 "kw use", 392 kw while
413 "kw while",
414 ]
415 "### 393 "###
416 ); 394 );
417 } 395 }
418 396
419 #[test] 397 #[test]
420 fn test_keywords_after_unsafe_in_item_list() { 398 fn test_keywords_after_unsafe_in_item_list() {
421 assert_debug_snapshot!( 399 assert_snapshot!(
422 get_keyword_completions(r"unsafe <|>"), 400 get_keyword_completions(r"unsafe <|>"),
423 @r###" 401 @r###"
424 [ 402 kw fn
425 "kw fn", 403 kw impl
426 "kw impl", 404 kw trait
427 "kw trait",
428 ]
429 "### 405 "###
430 ); 406 );
431 } 407 }
432 408
433 #[test] 409 #[test]
434 fn test_keywords_after_unsafe_in_block_expr() { 410 fn test_keywords_after_unsafe_in_block_expr() {
435 assert_debug_snapshot!( 411 assert_snapshot!(
436 get_keyword_completions(r"fn my_fn() { unsafe <|> }"), 412 get_keyword_completions(r"fn my_fn() { unsafe <|> }"),
437 @r###" 413 @r###"
438 [ 414 kw fn
439 "kw fn", 415 kw impl
440 "kw impl", 416 kw trait
441 "kw trait",
442 ]
443 "### 417 "###
444 ); 418 );
445 } 419 }
446 420
447 #[test] 421 #[test]
448 fn test_mut_in_ref_and_in_fn_parameters_list() { 422 fn test_mut_in_ref_and_in_fn_parameters_list() {
449 assert_debug_snapshot!( 423 assert_snapshot!(
450 get_keyword_completions(r"fn my_fn(&<|>) {}"), 424 get_keyword_completions(r"fn my_fn(&<|>) {}"),
451 @r###" 425 @r###"
452 [ 426 kw mut
453 "kw mut",
454 ]
455 "### 427 "###
456 ); 428 );
457 assert_debug_snapshot!( 429 assert_snapshot!(
458 get_keyword_completions(r"fn my_fn(<|>) {}"), 430 get_keyword_completions(r"fn my_fn(<|>) {}"),
459 @r###" 431 @r###"
460 [ 432 kw mut
461 "kw mut",
462 ]
463 "### 433 "###
464 ); 434 );
465 assert_debug_snapshot!( 435 assert_snapshot!(
466 get_keyword_completions(r"fn my_fn() { let &<|> }"), 436 get_keyword_completions(r"fn my_fn() { let &<|> }"),
467 @r###" 437 @r###"
468 [ 438 kw mut
469 "kw mut",
470 ]
471 "### 439 "###
472 ); 440 );
473 } 441 }
474 442
475 #[test] 443 #[test]
476 fn test_where_keyword() { 444 fn test_where_keyword() {
477 assert_debug_snapshot!( 445 assert_snapshot!(
478 get_keyword_completions(r"trait A <|>"), 446 get_keyword_completions(r"trait A <|>"),
479 @r###" 447 @r###"
480 [ 448 kw where
481 "kw where",
482 ]
483 "### 449 "###
484 ); 450 );
485 assert_debug_snapshot!( 451 assert_snapshot!(
486 get_keyword_completions(r"impl A <|>"), 452 get_keyword_completions(r"impl A <|>"),
487 @r###" 453 @r###"
488 [ 454 kw where
489 "kw where",
490 ]
491 "### 455 "###
492 ); 456 );
493 } 457 }
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs
index ba724116d..1e16a43ca 100644
--- a/crates/ra_ide/src/completion/test_utils.rs
+++ b/crates/ra_ide/src/completion/test_utils.rs
@@ -12,8 +12,8 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionI
12 do_completion_with_options(code, kind, &CompletionConfig::default()) 12 do_completion_with_options(code, kind, &CompletionConfig::default())
13} 13}
14 14
15pub(crate) fn get_completions(code: &str, kind: CompletionKind) -> Vec<String> { 15pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
16 get_completions_with_options(code, kind, &CompletionConfig::default()) 16 completion_list_with_options(code, kind, &CompletionConfig::default())
17} 17}
18 18
19pub(crate) fn do_completion_with_options( 19pub(crate) fn do_completion_with_options(
@@ -38,11 +38,11 @@ fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<Compl
38 analysis.completions(options, position).unwrap().unwrap().into() 38 analysis.completions(options, position).unwrap().unwrap().into()
39} 39}
40 40
41pub(crate) fn get_completions_with_options( 41pub(crate) fn completion_list_with_options(
42 code: &str, 42 code: &str,
43 kind: CompletionKind, 43 kind: CompletionKind,
44 options: &CompletionConfig, 44 options: &CompletionConfig,
45) -> Vec<String> { 45) -> String {
46 let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options) 46 let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options)
47 .into_iter() 47 .into_iter()
48 .filter(|c| c.completion_kind == kind) 48 .filter(|c| c.completion_kind == kind)
@@ -50,7 +50,7 @@ pub(crate) fn get_completions_with_options(
50 kind_completions.sort_by_key(|c| c.label().to_owned()); 50 kind_completions.sort_by_key(|c| c.label().to_owned());
51 kind_completions 51 kind_completions
52 .into_iter() 52 .into_iter()
53 .map(|it| format!("{} {}", it.kind().unwrap().tag(), it.label())) 53 .map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label()))
54 .collect() 54 .collect()
55} 55}
56 56