From cbe67339df2bbcb17e12ad74e8b8cd53baffb9f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 20:55:00 +0300 Subject: more completion components --- .../ra_analysis/src/completion/complete_snippet.rs | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 crates/ra_analysis/src/completion/complete_snippet.rs (limited to 'crates/ra_analysis/src/completion/complete_snippet.rs') diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs new file mode 100644 index 000000000..5d6cc5dc9 --- /dev/null +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -0,0 +1,78 @@ +use crate::{ + completion::{CompletionItem, Completions, CompletionKind::*, SyntaxContext}, +}; + +pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &SyntaxContext) { + if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { + return; + } + CompletionItem::new("pd") + .snippet("eprintln!(\"$0 = {:?}\", $0);") + .kind(Snippet) + .add_to(acc); + CompletionItem::new("ppd") + .snippet("eprintln!(\"$0 = {:#?}\", $0);") + .kind(Snippet) + .add_to(acc); +} + +pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &SyntaxContext) { + if !ctx.is_new_item { + return; + } + CompletionItem::new("Test function") + .lookup_by("tfn") + .snippet( + "\ +#[test] +fn ${1:feature}() { + $0 +}", + ) + .kind(Snippet) + .add_to(acc); + CompletionItem::new("pub(crate)") + .snippet("pub(crate) $0") + .kind(Snippet) + .add_to(acc); +} + +#[cfg(test)] +mod tests { + use crate::completion::{CompletionKind, check_completion}; + fn check_snippet_completion(code: &str, expected_completions: &str) { + check_completion(code, expected_completions, CompletionKind::Snippet); + } + + #[test] + fn completes_snippets_in_expressions() { + check_snippet_completion( + r"fn foo(x: i32) { <|> }", + r##" + pd "eprintln!(\"$0 = {:?}\", $0);" + ppd "eprintln!(\"$0 = {:#?}\", $0);" + "##, + ); + } + + #[test] + fn completes_snippets_in_items() { + // check_snippet_completion(r" + // <|> + // ", + // r##"[CompletionItem { label: "Test function", lookup: None, snippet: Some("#[test]\nfn test_${1:feature}() {\n$0\n}"##, + // ); + check_snippet_completion( + r" + #[cfg(test)] + mod tests { + <|> + } + ", + r##" + tfn "Test function" "#[test]\nfn ${1:feature}() {\n $0\n}" + pub(crate) "pub(crate) $0" + "##, + ); + } +} -- cgit v1.2.3