diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-31 18:05:12 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-31 18:05:12 +0000 |
commit | 1dc5608d0bb6bf2eee5a1b9190fcb2f8cdfa2ef3 (patch) | |
tree | b3c8a97880c625a81a814f4afa4e461ce5a58b82 /crates/ra_analysis/src/completion.rs | |
parent | e60ef6260f49b2b0438f8649ca71034fbafef631 (diff) | |
parent | c09e14a4ff02f774460a70472e1aeb3c598e01dc (diff) |
Merge #176
176: Move completio to ra_analysis r=matklad a=matklad
While we should handle completion for isolated file, it's better
achieved by using empty Analysis, rather than working only with &File:
we need memoization for type inference even inside a single file.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/completion.rs')
-rw-r--r-- | crates/ra_analysis/src/completion.rs | 623 |
1 files changed, 619 insertions, 4 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 0141d132e..340ae3f66 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs | |||
@@ -1,16 +1,32 @@ | |||
1 | use ra_editor::{CompletionItem, find_node_at_offset}; | 1 | use rustc_hash::{FxHashMap, FxHashSet}; |
2 | use ra_editor::{find_node_at_offset}; | ||
2 | use ra_syntax::{ | 3 | use ra_syntax::{ |
3 | AtomEdit, File, TextUnit, AstNode, | 4 | AtomEdit, File, TextUnit, AstNode, SyntaxNodeRef, |
4 | ast::{self, ModuleItemOwner, AstChildren}, | 5 | algo::visit::{visitor, visitor_ctx, Visitor, VisitorCtx}, |
6 | ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner}, | ||
7 | SyntaxKind::*, | ||
5 | }; | 8 | }; |
6 | 9 | ||
7 | use crate::{ | 10 | use crate::{ |
8 | FileId, Cancelable, | 11 | FileId, Cancelable, |
9 | input::FilesDatabase, | 12 | input::FilesDatabase, |
10 | db::{self, SyntaxDatabase}, | 13 | db::{self, SyntaxDatabase}, |
11 | descriptors::module::{ModulesDatabase, ModuleTree, ModuleId, scope::ModuleScope}, | 14 | descriptors::DescriptorDatabase, |
15 | descriptors::function::FnScopes, | ||
16 | descriptors::module::{ModuleTree, ModuleId, ModuleScope}, | ||
12 | }; | 17 | }; |
13 | 18 | ||
19 | |||
20 | #[derive(Debug)] | ||
21 | pub 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 | |||
14 | pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { | 30 | pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { |
15 | let source_root_id = db.file_source_root(file_id); | 31 | let source_root_id = db.file_source_root(file_id); |
16 | let file = db.file_syntax(file_id); | 32 | let file = db.file_syntax(file_id); |
@@ -72,3 +88,602 @@ fn crate_path(name_ref: ast::NameRef) -> Option<Vec<ast::NameRef>> { | |||
72 | res.reverse(); | 88 | res.reverse(); |
73 | Some(res) | 89 | Some(res) |
74 | } | 90 | } |
91 | |||
92 | |||
93 | pub(crate) fn scope_completion( | ||
94 | db: &db::RootDatabase, | ||
95 | file_id: FileId, | ||
96 | offset: TextUnit, | ||
97 | ) -> Option<Vec<CompletionItem>> { | ||
98 | let original_file = db.file_syntax(file_id); | ||
99 | // Insert a fake ident to get a valid parse tree | ||
100 | let file = { | ||
101 | let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); | ||
102 | original_file.reparse(&edit) | ||
103 | }; | ||
104 | let mut has_completions = false; | ||
105 | let mut res = Vec::new(); | ||
106 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), offset) { | ||
107 | has_completions = true; | ||
108 | complete_name_ref(&file, name_ref, &mut res); | ||
109 | // special case, `trait T { fn foo(i_am_a_name_ref) {} }` | ||
110 | if is_node::<ast::Param>(name_ref.syntax()) { | ||
111 | param_completions(name_ref.syntax(), &mut res); | ||
112 | } | ||
113 | let name_range = name_ref.syntax().range(); | ||
114 | let top_node = name_ref | ||
115 | .syntax() | ||
116 | .ancestors() | ||
117 | .take_while(|it| it.range() == name_range) | ||
118 | .last() | ||
119 | .unwrap(); | ||
120 | match top_node.parent().map(|it| it.kind()) { | ||
121 | Some(ROOT) | Some(ITEM_LIST) => complete_mod_item_snippets(&mut res), | ||
122 | _ => (), | ||
123 | } | ||
124 | } | ||
125 | if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) { | ||
126 | if is_node::<ast::Param>(name.syntax()) { | ||
127 | has_completions = true; | ||
128 | param_completions(name.syntax(), &mut res); | ||
129 | } | ||
130 | } | ||
131 | if has_completions { | ||
132 | Some(res) | ||
133 | } else { | ||
134 | None | ||
135 | } | ||
136 | } | ||
137 | |||
138 | fn complete_module_items( | ||
139 | file: &File, | ||
140 | items: AstChildren<ast::ModuleItem>, | ||
141 | this_item: Option<ast::NameRef>, | ||
142 | acc: &mut Vec<CompletionItem>, | ||
143 | ) { | ||
144 | let scope = ModuleScope::from_items(items); | ||
145 | acc.extend( | ||
146 | scope | ||
147 | .entries() | ||
148 | .iter() | ||
149 | .filter(|entry| { | ||
150 | let syntax = entry.ptr().resolve(file); | ||
151 | Some(syntax.borrowed()) != this_item.map(|it| it.syntax()) | ||
152 | }) | ||
153 | .map(|entry| CompletionItem { | ||
154 | label: entry.name().to_string(), | ||
155 | lookup: None, | ||
156 | snippet: None, | ||
157 | }), | ||
158 | ); | ||
159 | } | ||
160 | |||
161 | fn complete_name_ref( | ||
162 | file: &File, | ||
163 | name_ref: ast::NameRef, | ||
164 | acc: &mut Vec<CompletionItem>, | ||
165 | ) { | ||
166 | if !is_node::<ast::Path>(name_ref.syntax()) { | ||
167 | return; | ||
168 | } | ||
169 | let mut visited_fn = false; | ||
170 | for node in name_ref.syntax().ancestors() { | ||
171 | if let Some(items) = visitor() | ||
172 | .visit::<ast::Root, _>(|it| Some(it.items())) | ||
173 | .visit::<ast::Module, _>(|it| Some(it.item_list()?.items())) | ||
174 | .accept(node) | ||
175 | { | ||
176 | if let Some(items) = items { | ||
177 | complete_module_items(file, items, Some(name_ref), acc); | ||
178 | } | ||
179 | break; | ||
180 | } else if !visited_fn { | ||
181 | if let Some(fn_def) = ast::FnDef::cast(node) { | ||
182 | visited_fn = true; | ||
183 | complete_expr_keywords(&file, fn_def, name_ref, acc); | ||
184 | complete_expr_snippets(acc); | ||
185 | let scopes = FnScopes::new(fn_def); | ||
186 | complete_fn(name_ref, &scopes, acc); | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) { | ||
193 | let mut params = FxHashMap::default(); | ||
194 | for node in ctx.ancestors() { | ||
195 | let _ = visitor_ctx(&mut params) | ||
196 | .visit::<ast::Root, _>(process) | ||
197 | .visit::<ast::ItemList, _>(process) | ||
198 | .accept(node); | ||
199 | } | ||
200 | params | ||
201 | .into_iter() | ||
202 | .filter_map(|(label, (count, param))| { | ||
203 | let lookup = param.pat()?.syntax().text().to_string(); | ||
204 | if count < 2 { | ||
205 | None | ||
206 | } else { | ||
207 | Some((label, lookup)) | ||
208 | } | ||
209 | }) | ||
210 | .for_each(|(label, lookup)| { | ||
211 | acc.push(CompletionItem { | ||
212 | label, | ||
213 | lookup: Some(lookup), | ||
214 | snippet: None, | ||
215 | }) | ||
216 | }); | ||
217 | |||
218 | fn process<'a, N: ast::FnDefOwner<'a>>( | ||
219 | node: N, | ||
220 | params: &mut FxHashMap<String, (u32, ast::Param<'a>)>, | ||
221 | ) { | ||
222 | node.functions() | ||
223 | .filter_map(|it| it.param_list()) | ||
224 | .flat_map(|it| it.params()) | ||
225 | .for_each(|param| { | ||
226 | let text = param.syntax().text().to_string(); | ||
227 | params.entry(text).or_insert((0, param)).0 += 1; | ||
228 | }) | ||
229 | } | ||
230 | } | ||
231 | |||
232 | fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool { | ||
233 | match node.ancestors().filter_map(N::cast).next() { | ||
234 | None => false, | ||
235 | Some(n) => n.syntax().range() == node.range(), | ||
236 | } | ||
237 | } | ||
238 | |||
239 | fn complete_expr_keywords( | ||
240 | file: &File, | ||
241 | fn_def: ast::FnDef, | ||
242 | name_ref: ast::NameRef, | ||
243 | acc: &mut Vec<CompletionItem>, | ||
244 | ) { | ||
245 | acc.push(keyword("if", "if $0 {}")); | ||
246 | acc.push(keyword("match", "match $0 {}")); | ||
247 | acc.push(keyword("while", "while $0 {}")); | ||
248 | acc.push(keyword("loop", "loop {$0}")); | ||
249 | |||
250 | if let Some(off) = name_ref.syntax().range().start().checked_sub(2.into()) { | ||
251 | if let Some(if_expr) = find_node_at_offset::<ast::IfExpr>(file.syntax(), off) { | ||
252 | if if_expr.syntax().range().end() < name_ref.syntax().range().start() { | ||
253 | acc.push(keyword("else", "else {$0}")); | ||
254 | acc.push(keyword("else if", "else if $0 {}")); | ||
255 | } | ||
256 | } | ||
257 | } | ||
258 | if is_in_loop_body(name_ref) { | ||
259 | acc.push(keyword("continue", "continue")); | ||
260 | acc.push(keyword("break", "break")); | ||
261 | } | ||
262 | acc.extend(complete_return(fn_def, name_ref)); | ||
263 | } | ||
264 | |||
265 | fn is_in_loop_body(name_ref: ast::NameRef) -> bool { | ||
266 | for node in name_ref.syntax().ancestors() { | ||
267 | if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { | ||
268 | break; | ||
269 | } | ||
270 | let loop_body = visitor() | ||
271 | .visit::<ast::ForExpr, _>(LoopBodyOwner::loop_body) | ||
272 | .visit::<ast::WhileExpr, _>(LoopBodyOwner::loop_body) | ||
273 | .visit::<ast::LoopExpr, _>(LoopBodyOwner::loop_body) | ||
274 | .accept(node); | ||
275 | if let Some(Some(body)) = loop_body { | ||
276 | if name_ref.syntax().range().is_subrange(&body.syntax().range()) { | ||
277 | return true; | ||
278 | } | ||
279 | } | ||
280 | } | ||
281 | false | ||
282 | } | ||
283 | |||
284 | fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option<CompletionItem> { | ||
285 | // let is_last_in_block = name_ref.syntax().ancestors().filter_map(ast::Expr::cast) | ||
286 | // .next() | ||
287 | // .and_then(|it| it.syntax().parent()) | ||
288 | // .and_then(ast::Block::cast) | ||
289 | // .is_some(); | ||
290 | |||
291 | // if is_last_in_block { | ||
292 | // return None; | ||
293 | // } | ||
294 | |||
295 | let is_stmt = match name_ref | ||
296 | .syntax() | ||
297 | .ancestors() | ||
298 | .filter_map(ast::ExprStmt::cast) | ||
299 | .next() | ||
300 | { | ||
301 | None => false, | ||
302 | Some(expr_stmt) => expr_stmt.syntax().range() == name_ref.syntax().range(), | ||
303 | }; | ||
304 | let snip = match (is_stmt, fn_def.ret_type().is_some()) { | ||
305 | (true, true) => "return $0;", | ||
306 | (true, false) => "return;", | ||
307 | (false, true) => "return $0", | ||
308 | (false, false) => "return", | ||
309 | }; | ||
310 | Some(keyword("return", snip)) | ||
311 | } | ||
312 | |||
313 | fn keyword(kw: &str, snip: &str) -> CompletionItem { | ||
314 | CompletionItem { | ||
315 | label: kw.to_string(), | ||
316 | lookup: None, | ||
317 | snippet: Some(snip.to_string()), | ||
318 | } | ||
319 | } | ||
320 | |||
321 | fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) { | ||
322 | acc.push(CompletionItem { | ||
323 | label: "pd".to_string(), | ||
324 | lookup: None, | ||
325 | snippet: Some("eprintln!(\"$0 = {:?}\", $0);".to_string()), | ||
326 | }); | ||
327 | acc.push(CompletionItem { | ||
328 | label: "ppd".to_string(), | ||
329 | lookup: None, | ||
330 | snippet: Some("eprintln!(\"$0 = {:#?}\", $0);".to_string()), | ||
331 | }); | ||
332 | } | ||
333 | |||
334 | fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) { | ||
335 | acc.push(CompletionItem { | ||
336 | label: "tfn".to_string(), | ||
337 | lookup: None, | ||
338 | snippet: Some("#[test]\nfn $1() {\n $0\n}".to_string()), | ||
339 | }); | ||
340 | acc.push(CompletionItem { | ||
341 | label: "pub(crate)".to_string(), | ||
342 | lookup: None, | ||
343 | snippet: Some("pub(crate) $0".to_string()), | ||
344 | }) | ||
345 | } | ||
346 | |||
347 | fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { | ||
348 | let mut shadowed = FxHashSet::default(); | ||
349 | acc.extend( | ||
350 | scopes | ||
351 | .scope_chain(name_ref.syntax()) | ||
352 | .flat_map(|scope| scopes.entries(scope).iter()) | ||
353 | .filter(|entry| shadowed.insert(entry.name())) | ||
354 | .map(|entry| CompletionItem { | ||
355 | label: entry.name().to_string(), | ||
356 | lookup: None, | ||
357 | snippet: None, | ||
358 | }), | ||
359 | ); | ||
360 | if scopes.self_param.is_some() { | ||
361 | acc.push(CompletionItem { | ||
362 | label: "self".to_string(), | ||
363 | lookup: None, | ||
364 | snippet: None, | ||
365 | }) | ||
366 | } | ||
367 | } | ||
368 | |||
369 | #[cfg(test)] | ||
370 | mod tests { | ||
371 | use test_utils::{assert_eq_dbg, extract_offset}; | ||
372 | |||
373 | use crate::FileId; | ||
374 | use crate::mock_analysis::MockAnalysis; | ||
375 | |||
376 | use super::*; | ||
377 | |||
378 | fn check_scope_completion(code: &str, expected_completions: &str) { | ||
379 | let (off, code) = extract_offset(&code); | ||
380 | let analysis = MockAnalysis::with_files(&[("/main.rs", &code)]).analysis(); | ||
381 | let file_id = FileId(1); | ||
382 | let completions = scope_completion(&analysis.imp.db, file_id, off) | ||
383 | .unwrap() | ||
384 | .into_iter() | ||
385 | .filter(|c| c.snippet.is_none()) | ||
386 | .collect::<Vec<_>>(); | ||
387 | assert_eq_dbg(expected_completions, &completions); | ||
388 | } | ||
389 | |||
390 | fn check_snippet_completion(code: &str, expected_completions: &str) { | ||
391 | let (off, code) = extract_offset(&code); | ||
392 | let analysis = MockAnalysis::with_files(&[("/main.rs", &code)]).analysis(); | ||
393 | let file_id = FileId(1); | ||
394 | let completions = scope_completion(&analysis.imp.db, file_id, off) | ||
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 | } | ||