aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-11-07 17:34:16 +0000
committerAleksey Kladov <[email protected]>2018-11-07 17:34:16 +0000
commit9b88ec488b3f83ab718c8cb4d7dff95aff0113ed (patch)
treefcdb2d0922b1492df59e779b5cbcb6086c19c402 /crates/ra_analysis/src/completion.rs
parentaf17fc969742a36cee5199860789ed0b14123240 (diff)
split completion mod
Diffstat (limited to 'crates/ra_analysis/src/completion.rs')
-rw-r--r--crates/ra_analysis/src/completion.rs689
1 files changed, 0 insertions, 689 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs
deleted file mode 100644
index 27566a8a1..000000000
--- a/crates/ra_analysis/src/completion.rs
+++ /dev/null
@@ -1,689 +0,0 @@
1use ra_editor::find_node_at_offset;
2use ra_syntax::{
3 algo::visit::{visitor, visitor_ctx, Visitor, VisitorCtx},
4 ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner},
5 AstNode, AtomEdit, SourceFileNode,
6 SyntaxKind::*,
7 SyntaxNodeRef,
8};
9use rustc_hash::{FxHashMap, FxHashSet};
10
11use crate::{
12 db::{self, SyntaxDatabase},
13 descriptors::function::FnScopes,
14 descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
15 descriptors::DescriptorDatabase,
16 input::FilesDatabase,
17 Cancelable, FilePosition, FileId,
18};
19
20#[derive(Debug)]
21pub struct CompletionItem {
22 /// What user sees in pop-up
23 pub label: String,
24 /// What string is used for filtering, defaults to label
25 pub lookup: Option<String>,
26 /// What is inserted, defaults to label
27 pub snippet: Option<String>,
28}
29
30pub(crate) fn completions(
31 db: &db::RootDatabase,
32 position: FilePosition,
33) -> Cancelable<Option<Vec<CompletionItem>>> {
34 let original_file = db.file_syntax(position.file_id);
35 // Insert a fake ident to get a valid parse tree
36 let file = {
37 let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
38 original_file.reparse(&edit)
39 };
40
41 let mut res = Vec::new();
42 let mut has_completions = false;
43 // First, let's try to complete a reference to some declaration.
44 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
45 has_completions = true;
46 // completion from lexical scope
47 complete_name_ref(&file, name_ref, &mut res);
48 // special case, `trait T { fn foo(i_am_a_name_ref) {} }`
49 if is_node::<ast::Param>(name_ref.syntax()) {
50 param_completions(name_ref.syntax(), &mut res);
51 }
52 // snippet completions
53 {
54 let name_range = name_ref.syntax().range();
55 let top_node = name_ref
56 .syntax()
57 .ancestors()
58 .take_while(|it| it.range() == name_range)
59 .last()
60 .unwrap();
61 match top_node.parent().map(|it| it.kind()) {
62 Some(SOURCE_FILE) | Some(ITEM_LIST) => complete_mod_item_snippets(&mut res),
63 _ => (),
64 }
65 }
66 complete_path(db, position.file_id, name_ref, &mut res)?;
67 }
68
69 // Otherwise, if this is a declaration, use heuristics to suggest a name.
70 if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
71 if is_node::<ast::Param>(name.syntax()) {
72 has_completions = true;
73 param_completions(name.syntax(), &mut res);
74 }
75 }
76 let res = if has_completions { Some(res) } else { None };
77 Ok(res)
78}
79
80fn complete_path(
81 db: &db::RootDatabase,
82 file_id: FileId,
83 name_ref: ast::NameRef,
84 acc: &mut Vec<CompletionItem>,
85) -> Cancelable<()> {
86 let source_root_id = db.file_source_root(file_id);
87 let module_tree = db.module_tree(source_root_id)?;
88 let module_id = match module_tree.any_module_for_source(ModuleSource::SourceFile(file_id)) {
89 None => return Ok(()),
90 Some(it) => it,
91 };
92 let target_module_id = match find_target_module(&module_tree, module_id, name_ref) {
93 None => return Ok(()),
94 Some(it) => it,
95 };
96 let module_scope = db.module_scope(source_root_id, target_module_id)?;
97 let completions = module_scope.entries().iter().map(|entry| CompletionItem {
98 label: entry.name().to_string(),
99 lookup: None,
100 snippet: None,
101 });
102 acc.extend(completions);
103 Ok(())
104}
105
106fn find_target_module(
107 module_tree: &ModuleTree,
108 module_id: ModuleId,
109 name_ref: ast::NameRef,
110) -> Option<ModuleId> {
111 let mut crate_path = crate_path(name_ref)?;
112
113 crate_path.pop();
114 let mut target_module = module_id.root(&module_tree);
115 for name in crate_path {
116 target_module = target_module.child(module_tree, name.text().as_str())?;
117 }
118 Some(target_module)
119}
120
121fn crate_path(name_ref: ast::NameRef) -> Option<Vec<ast::NameRef>> {
122 let mut path = name_ref
123 .syntax()
124 .parent()
125 .and_then(ast::PathSegment::cast)?
126 .parent_path();
127 let mut res = Vec::new();
128 loop {
129 let segment = path.segment()?;
130 match segment.kind()? {
131 ast::PathSegmentKind::Name(name) => res.push(name),
132 ast::PathSegmentKind::CrateKw => break,
133 ast::PathSegmentKind::SelfKw | ast::PathSegmentKind::SuperKw => return None,
134 }
135 path = path.qualifier()?;
136 }
137 res.reverse();
138 Some(res)
139}
140
141fn complete_module_items(
142 file: &SourceFileNode,
143 items: AstChildren<ast::ModuleItem>,
144 this_item: Option<ast::NameRef>,
145 acc: &mut Vec<CompletionItem>,
146) {
147 let scope = ModuleScope::new(items); // FIXME
148 acc.extend(
149 scope
150 .entries()
151 .iter()
152 .filter(|entry| {
153 let syntax = entry.ptr().resolve(file);
154 Some(syntax.borrowed()) != this_item.map(|it| it.syntax())
155 })
156 .map(|entry| CompletionItem {
157 label: entry.name().to_string(),
158 lookup: None,
159 snippet: None,
160 }),
161 );
162}
163
164fn complete_name_ref(file: &SourceFileNode, name_ref: ast::NameRef, acc: &mut Vec<CompletionItem>) {
165 if !is_node::<ast::Path>(name_ref.syntax()) {
166 return;
167 }
168 let mut visited_fn = false;
169 for node in name_ref.syntax().ancestors() {
170 if let Some(items) = visitor()
171 .visit::<ast::SourceFile, _>(|it| Some(it.items()))
172 .visit::<ast::Module, _>(|it| Some(it.item_list()?.items()))
173 .accept(node)
174 {
175 if let Some(items) = items {
176 complete_module_items(file, items, Some(name_ref), acc);
177 }
178 break;
179 } else if !visited_fn {
180 if let Some(fn_def) = ast::FnDef::cast(node) {
181 visited_fn = true;
182 complete_expr_keywords(&file, fn_def, name_ref, acc);
183 complete_expr_snippets(acc);
184 let scopes = FnScopes::new(fn_def);
185 complete_fn(name_ref, &scopes, acc);
186 }
187 }
188 }
189}
190
191fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
192 let mut params = FxHashMap::default();
193 for node in ctx.ancestors() {
194 let _ = visitor_ctx(&mut params)
195 .visit::<ast::SourceFile, _>(process)
196 .visit::<ast::ItemList, _>(process)
197 .accept(node);
198 }
199 params
200 .into_iter()
201 .filter_map(|(label, (count, param))| {
202 let lookup = param.pat()?.syntax().text().to_string();
203 if count < 2 {
204 None
205 } else {
206 Some((label, lookup))
207 }
208 })
209 .for_each(|(label, lookup)| {
210 acc.push(CompletionItem {
211 label,
212 lookup: Some(lookup),
213 snippet: None,
214 })
215 });
216
217 fn process<'a, N: ast::FnDefOwner<'a>>(
218 node: N,
219 params: &mut FxHashMap<String, (u32, ast::Param<'a>)>,
220 ) {
221 node.functions()
222 .filter_map(|it| it.param_list())
223 .flat_map(|it| it.params())
224 .for_each(|param| {
225 let text = param.syntax().text().to_string();
226 params.entry(text).or_insert((0, param)).0 += 1;
227 })
228 }
229}
230
231fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool {
232 match node.ancestors().filter_map(N::cast).next() {
233 None => false,
234 Some(n) => n.syntax().range() == node.range(),
235 }
236}
237
238fn complete_expr_keywords(
239 file: &SourceFileNode,
240 fn_def: ast::FnDef,
241 name_ref: ast::NameRef,
242 acc: &mut Vec<CompletionItem>,
243) {
244 acc.push(keyword("if", "if $0 {}"));
245 acc.push(keyword("match", "match $0 {}"));
246 acc.push(keyword("while", "while $0 {}"));
247 acc.push(keyword("loop", "loop {$0}"));
248
249 if let Some(off) = name_ref.syntax().range().start().checked_sub(2.into()) {
250 if let Some(if_expr) = find_node_at_offset::<ast::IfExpr>(file.syntax(), off) {
251 if if_expr.syntax().range().end() < name_ref.syntax().range().start() {
252 acc.push(keyword("else", "else {$0}"));
253 acc.push(keyword("else if", "else if $0 {}"));
254 }
255 }
256 }
257 if is_in_loop_body(name_ref) {
258 acc.push(keyword("continue", "continue"));
259 acc.push(keyword("break", "break"));
260 }
261 acc.extend(complete_return(fn_def, name_ref));
262}
263
264fn is_in_loop_body(name_ref: ast::NameRef) -> bool {
265 for node in name_ref.syntax().ancestors() {
266 if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR {
267 break;
268 }
269 let loop_body = visitor()
270 .visit::<ast::ForExpr, _>(LoopBodyOwner::loop_body)
271 .visit::<ast::WhileExpr, _>(LoopBodyOwner::loop_body)
272 .visit::<ast::LoopExpr, _>(LoopBodyOwner::loop_body)
273 .accept(node);
274 if let Some(Some(body)) = loop_body {
275 if name_ref
276 .syntax()
277 .range()
278 .is_subrange(&body.syntax().range())
279 {
280 return true;
281 }
282 }
283 }
284 false
285}
286
287fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option<CompletionItem> {
288 // let is_last_in_block = name_ref.syntax().ancestors().filter_map(ast::Expr::cast)
289 // .next()
290 // .and_then(|it| it.syntax().parent())
291 // .and_then(ast::Block::cast)
292 // .is_some();
293
294 // if is_last_in_block {
295 // return None;
296 // }
297
298 let is_stmt = match name_ref
299 .syntax()
300 .ancestors()
301 .filter_map(ast::ExprStmt::cast)
302 .next()
303 {
304 None => false,
305 Some(expr_stmt) => expr_stmt.syntax().range() == name_ref.syntax().range(),
306 };
307 let snip = match (is_stmt, fn_def.ret_type().is_some()) {
308 (true, true) => "return $0;",
309 (true, false) => "return;",
310 (false, true) => "return $0",
311 (false, false) => "return",
312 };
313 Some(keyword("return", snip))
314}
315
316fn keyword(kw: &str, snip: &str) -> CompletionItem {
317 CompletionItem {
318 label: kw.to_string(),
319 lookup: None,
320 snippet: Some(snip.to_string()),
321 }
322}
323
324fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) {
325 acc.push(CompletionItem {
326 label: "pd".to_string(),
327 lookup: None,
328 snippet: Some("eprintln!(\"$0 = {:?}\", $0);".to_string()),
329 });
330 acc.push(CompletionItem {
331 label: "ppd".to_string(),
332 lookup: None,
333 snippet: Some("eprintln!(\"$0 = {:#?}\", $0);".to_string()),
334 });
335}
336
337fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) {
338 acc.push(CompletionItem {
339 label: "tfn".to_string(),
340 lookup: None,
341 snippet: Some("#[test]\nfn $1() {\n $0\n}".to_string()),
342 });
343 acc.push(CompletionItem {
344 label: "pub(crate)".to_string(),
345 lookup: None,
346 snippet: Some("pub(crate) $0".to_string()),
347 })
348}
349
350fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) {
351 let mut shadowed = FxHashSet::default();
352 acc.extend(
353 scopes
354 .scope_chain(name_ref.syntax())
355 .flat_map(|scope| scopes.entries(scope).iter())
356 .filter(|entry| shadowed.insert(entry.name()))
357 .map(|entry| CompletionItem {
358 label: entry.name().to_string(),
359 lookup: None,
360 snippet: None,
361 }),
362 );
363 if scopes.self_param.is_some() {
364 acc.push(CompletionItem {
365 label: "self".to_string(),
366 lookup: None,
367 snippet: None,
368 })
369 }
370}
371
372#[cfg(test)]
373mod tests {
374 use test_utils::assert_eq_dbg;
375
376 use crate::mock_analysis::single_file_with_position;
377
378 use super::*;
379
380 fn check_scope_completion(code: &str, expected_completions: &str) {
381 let (analysis, position) = single_file_with_position(code);
382 let completions = completions(&analysis.imp.db, position)
383 .unwrap()
384 .unwrap()
385 .into_iter()
386 .filter(|c| c.snippet.is_none())
387 .collect::<Vec<_>>();
388 assert_eq_dbg(expected_completions, &completions);
389 }
390
391 fn check_snippet_completion(code: &str, expected_completions: &str) {
392 let (analysis, position) = single_file_with_position(code);
393 let completions = completions(&analysis.imp.db, position)
394 .unwrap()
395 .unwrap()
396 .into_iter()
397 .filter(|c| c.snippet.is_some())
398 .collect::<Vec<_>>();
399 assert_eq_dbg(expected_completions, &completions);
400 }
401
402 #[test]
403 fn test_completion_let_scope() {
404 check_scope_completion(
405 r"
406 fn quux(x: i32) {
407 let y = 92;
408 1 + <|>;
409 let z = ();
410 }
411 ",
412 r#"[CompletionItem { label: "y", lookup: None, snippet: None },
413 CompletionItem { label: "x", lookup: None, snippet: None },
414 CompletionItem { label: "quux", lookup: None, snippet: None }]"#,
415 );
416 }
417
418 #[test]
419 fn test_completion_if_let_scope() {
420 check_scope_completion(
421 r"
422 fn quux() {
423 if let Some(x) = foo() {
424 let y = 92;
425 };
426 if let Some(a) = bar() {
427 let b = 62;
428 1 + <|>
429 }
430 }
431 ",
432 r#"[CompletionItem { label: "b", lookup: None, snippet: None },
433 CompletionItem { label: "a", lookup: None, snippet: None },
434 CompletionItem { label: "quux", lookup: None, snippet: None }]"#,
435 );
436 }
437
438 #[test]
439 fn test_completion_for_scope() {
440 check_scope_completion(
441 r"
442 fn quux() {
443 for x in &[1, 2, 3] {
444 <|>
445 }
446 }
447 ",
448 r#"[CompletionItem { label: "x", lookup: None, snippet: None },
449 CompletionItem { label: "quux", lookup: None, snippet: None }]"#,
450 );
451 }
452
453 #[test]
454 fn test_completion_mod_scope() {
455 check_scope_completion(
456 r"
457 struct Foo;
458 enum Baz {}
459 fn quux() {
460 <|>
461 }
462 ",
463 r#"[CompletionItem { label: "Foo", lookup: None, snippet: None },
464 CompletionItem { label: "Baz", lookup: None, snippet: None },
465 CompletionItem { label: "quux", lookup: None, snippet: None }]"#,
466 );
467 }
468
469 #[test]
470 fn test_completion_mod_scope_no_self_use() {
471 check_scope_completion(
472 r"
473 use foo<|>;
474 ",
475 r#"[]"#,
476 );
477 }
478
479 #[test]
480 fn test_completion_mod_scope_nested() {
481 check_scope_completion(
482 r"
483 struct Foo;
484 mod m {
485 struct Bar;
486 fn quux() { <|> }
487 }
488 ",
489 r#"[CompletionItem { label: "Bar", lookup: None, snippet: None },
490 CompletionItem { label: "quux", lookup: None, snippet: None }]"#,
491 );
492 }
493
494 #[test]
495 fn test_complete_type() {
496 check_scope_completion(
497 r"
498 struct Foo;
499 fn x() -> <|>
500 ",
501 r#"[CompletionItem { label: "Foo", lookup: None, snippet: None },
502 CompletionItem { label: "x", lookup: None, snippet: None }]"#,
503 )
504 }
505
506 #[test]
507 fn test_complete_shadowing() {
508 check_scope_completion(
509 r"
510 fn foo() -> {
511 let bar = 92;
512 {
513 let bar = 62;
514 <|>
515 }
516 }
517 ",
518 r#"[CompletionItem { label: "bar", lookup: None, snippet: None },
519 CompletionItem { label: "foo", lookup: None, snippet: None }]"#,
520 )
521 }
522
523 #[test]
524 fn test_complete_self() {
525 check_scope_completion(
526 r"
527 impl S { fn foo(&self) { <|> } }
528 ",
529 r#"[CompletionItem { label: "self", lookup: None, snippet: None }]"#,
530 )
531 }
532
533 #[test]
534 fn test_completion_kewords() {
535 check_snippet_completion(r"
536 fn quux() {
537 <|>
538 }
539 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
540 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
541 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
542 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
543 CompletionItem { label: "return", lookup: None, snippet: Some("return") },
544 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
545 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
546 }
547
548 #[test]
549 fn test_completion_else() {
550 check_snippet_completion(r"
551 fn quux() {
552 if true {
553 ()
554 } <|>
555 }
556 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
557 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
558 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
559 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
560 CompletionItem { label: "else", lookup: None, snippet: Some("else {$0}") },
561 CompletionItem { label: "else if", lookup: None, snippet: Some("else if $0 {}") },
562 CompletionItem { label: "return", lookup: None, snippet: Some("return") },
563 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
564 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
565 }
566
567 #[test]
568 fn test_completion_return_value() {
569 check_snippet_completion(r"
570 fn quux() -> i32 {
571 <|>
572 92
573 }
574 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
575 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
576 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
577 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
578 CompletionItem { label: "return", lookup: None, snippet: Some("return $0;") },
579 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
580 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
581 check_snippet_completion(r"
582 fn quux() {
583 <|>
584 92
585 }
586 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
587 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
588 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
589 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
590 CompletionItem { label: "return", lookup: None, snippet: Some("return;") },
591 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
592 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
593 }
594
595 #[test]
596 fn test_completion_return_no_stmt() {
597 check_snippet_completion(r"
598 fn quux() -> i32 {
599 match () {
600 () => <|>
601 }
602 }
603 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
604 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
605 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
606 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
607 CompletionItem { label: "return", lookup: None, snippet: Some("return $0") },
608 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
609 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
610 }
611
612 #[test]
613 fn test_continue_break_completion() {
614 check_snippet_completion(r"
615 fn quux() -> i32 {
616 loop { <|> }
617 }
618 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
619 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
620 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
621 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
622 CompletionItem { label: "continue", lookup: None, snippet: Some("continue") },
623 CompletionItem { label: "break", lookup: None, snippet: Some("break") },
624 CompletionItem { label: "return", lookup: None, snippet: Some("return $0") },
625 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
626 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
627 check_snippet_completion(r"
628 fn quux() -> i32 {
629 loop { || { <|> } }
630 }
631 ", r#"[CompletionItem { label: "if", lookup: None, snippet: Some("if $0 {}") },
632 CompletionItem { label: "match", lookup: None, snippet: Some("match $0 {}") },
633 CompletionItem { label: "while", lookup: None, snippet: Some("while $0 {}") },
634 CompletionItem { label: "loop", lookup: None, snippet: Some("loop {$0}") },
635 CompletionItem { label: "return", lookup: None, snippet: Some("return $0") },
636 CompletionItem { label: "pd", lookup: None, snippet: Some("eprintln!(\"$0 = {:?}\", $0);") },
637 CompletionItem { label: "ppd", lookup: None, snippet: Some("eprintln!(\"$0 = {:#?}\", $0);") }]"#);
638 }
639
640 #[test]
641 fn test_param_completion_last_param() {
642 check_scope_completion(r"
643 fn foo(file_id: FileId) {}
644 fn bar(file_id: FileId) {}
645 fn baz(file<|>) {}
646 ", r#"[CompletionItem { label: "file_id: FileId", lookup: Some("file_id"), snippet: None }]"#);
647 }
648
649 #[test]
650 fn test_param_completion_nth_param() {
651 check_scope_completion(r"
652 fn foo(file_id: FileId) {}
653 fn bar(file_id: FileId) {}
654 fn baz(file<|>, x: i32) {}
655 ", r#"[CompletionItem { label: "file_id: FileId", lookup: Some("file_id"), snippet: None }]"#);
656 }
657
658 #[test]
659 fn test_param_completion_trait_param() {
660 check_scope_completion(r"
661 pub(crate) trait SourceRoot {
662 pub fn contains(&self, file_id: FileId) -> bool;
663 pub fn module_map(&self) -> &ModuleMap;
664 pub fn lines(&self, file_id: FileId) -> &LineIndex;
665 pub fn syntax(&self, file<|>)
666 }
667 ", r#"[CompletionItem { label: "self", lookup: None, snippet: None },
668 CompletionItem { label: "SourceRoot", lookup: None, snippet: None },
669 CompletionItem { label: "file_id: FileId", lookup: Some("file_id"), snippet: None }]"#);
670 }
671
672 #[test]
673 fn test_item_snippets() {
674 // check_snippet_completion(r"
675 // <|>
676 // ",
677 // r##"[CompletionItem { label: "tfn", lookup: None, snippet: Some("#[test]\nfn $1() {\n $0\n}") }]"##,
678 // );
679 check_snippet_completion(r"
680 #[cfg(test)]
681 mod tests {
682 <|>
683 }
684 ",
685 r##"[CompletionItem { label: "tfn", lookup: None, snippet: Some("#[test]\nfn $1() {\n $0\n}") },
686 CompletionItem { label: "pub(crate)", lookup: None, snippet: Some("pub(crate) $0") }]"##,
687 );
688 }
689}