1 //! This file provides snippet completions, like `pd` => `eprintln!(...)`.
3 use ide_db::helpers::SnippetCap;
6 item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
10 fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
11 let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
12 item.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet);
16 pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
17 if !(ctx.is_trivial_path && ctx.function_def.is_some()) {
20 let cap = match ctx.config.snippet_cap {
26 snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
27 snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
31 pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
32 if !ctx.expects_item() {
35 let cap = match ctx.config.snippet_cap {
40 let mut item = snippet(
55 item.lookup_by("tmod");
58 let mut item = snippet(
61 "tfn (Test function)",
68 item.lookup_by("tfn");
71 let item = snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}");
77 use expect_test::{expect, Expect};
79 use crate::{test_utils::completion_list, CompletionKind};
81 fn check(ra_fixture: &str, expect: Expect) {
82 let actual = completion_list(ra_fixture, CompletionKind::Snippet);
83 expect.assert_eq(&actual)
87 fn completes_snippets_in_expressions() {
89 r#"fn foo(x: i32) { $0 }"#,
98 fn should_not_complete_snippets_in_path() {
99 check(r#"fn foo(x: i32) { ::foo$0 }"#, expect![[""]]);
100 check(r#"fn foo(x: i32) { ::$0 }"#, expect![[""]]);
104 fn completes_snippets_in_items() {
113 sn tmod (Test module)
114 sn tfn (Test function)