From 3c12d38a32f5c87e87d9dbced774453216086bab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Oct 2018 14:08:31 +0300 Subject: tfn snippet --- crates/ra_editor/src/completion.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/completion.rs b/crates/ra_editor/src/completion.rs index 62a63fb04..570d72d66 100644 --- a/crates/ra_editor/src/completion.rs +++ b/crates/ra_editor/src/completion.rs @@ -39,6 +39,12 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option(name_ref.syntax()) { param_completions(name_ref.syntax(), &mut res); } + let name_range = name_ref.syntax().range(); + let top_node = name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap(); + match top_node.parent().map(|it| it.kind()) { + Some(ROOT) | Some(ITEM_LIST) => complete_mod_item_snippets(&mut res), + _ => (), + } } if let Some(name) = find_node_at_offset::(file.syntax(), offset) { if is_node::(name.syntax()) { @@ -216,6 +222,15 @@ fn complete_expr_snippets(acc: &mut Vec) { ); } +fn complete_mod_item_snippets(acc: &mut Vec) { + acc.push(CompletionItem { + label: "tfn".to_string(), + lookup: None, + snippet: Some("#[test]\nfn $1() {\n $0\n}".to_string()), + } + ); +} + fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec) { let mut shadowed = HashSet::new(); acc.extend( @@ -506,4 +521,21 @@ mod tests { CompletionItem { label: "SourceRoot", lookup: None, snippet: None }, CompletionItem { label: "file_id: FileId", lookup: Some("file_id"), snippet: None }]"#); } + + #[test] + fn test_tfn_snippet() { + // check_snippet_completion(r" + // <|> + // ", + // r##"[CompletionItem { label: "tfn", lookup: None, snippet: Some("#[test]\nfn $1() {\n $0\n}") }]"##, + // ); + check_snippet_completion(r" + #[cfg(test)] + mod tests { + <|> + } + ", + r##"[CompletionItem { label: "tfn", lookup: None, snippet: Some("#[test]\nfn $1() {\n $0\n}") }]"##, + ); + } } -- cgit v1.2.3