2020-10-18 13:09:00 +03:00
|
|
|
//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2020-10-18 13:09:00 +03:00
|
|
|
use crate::{
|
2020-10-25 10:59:15 +03:00
|
|
|
config::SnippetCap, item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
|
|
|
|
CompletionKind, Completions,
|
2019-07-04 23:05:17 +03:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2020-04-24 02:26:38 +02:00
|
|
|
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label)
|
2020-04-24 02:26:38 +02:00
|
|
|
.insert_snippet(cap, snippet)
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Snippet)
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-01-08 22:33:36 +03:00
|
|
|
if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 02:26:38 +02:00
|
|
|
let cap = match ctx.config.snippet_cap {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2019-01-23 13:21:29 +08:00
|
|
|
|
2020-04-24 02:26:38 +02:00
|
|
|
snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
|
|
|
|
snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-01-08 22:33:36 +03:00
|
|
|
if !ctx.is_new_item {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 02:26:38 +02:00
|
|
|
let cap = match ctx.config.snippet_cap {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2020-05-11 18:11:23 +02:00
|
|
|
snippet(
|
|
|
|
ctx,
|
|
|
|
cap,
|
2020-08-11 22:33:17 +03:00
|
|
|
"tmod (Test module)",
|
2020-05-11 18:11:23 +02:00
|
|
|
"\
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ${1:test_name}() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}",
|
|
|
|
)
|
|
|
|
.lookup_by("tmod")
|
|
|
|
.add_to(acc);
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
snippet(
|
2019-01-19 22:02:50 +08:00
|
|
|
ctx,
|
2020-04-24 02:26:38 +02:00
|
|
|
cap,
|
2020-08-11 22:33:17 +03:00
|
|
|
"tfn (Test function)",
|
2019-01-08 22:33:36 +03:00
|
|
|
"\
|
|
|
|
#[test]
|
|
|
|
fn ${1:feature}() {
|
|
|
|
$0
|
|
|
|
}",
|
|
|
|
)
|
|
|
|
.lookup_by("tfn")
|
|
|
|
.add_to(acc);
|
|
|
|
|
2020-04-24 02:26:38 +02:00
|
|
|
snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 13:19:31 +02:00
|
|
|
use expect_test::{expect, Expect};
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2020-10-18 13:09:00 +03:00
|
|
|
use crate::{test_utils::completion_list, CompletionKind};
|
2020-07-04 18:03:34 +02:00
|
|
|
|
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
|
|
|
let actual = completion_list(ra_fixture, CompletionKind::Snippet);
|
|
|
|
expect.assert_eq(&actual)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_snippets_in_expressions() {
|
2020-07-04 18:03:34 +02:00
|
|
|
check(
|
|
|
|
r#"fn foo(x: i32) { <|> }"#,
|
|
|
|
expect![[r#"
|
2020-07-04 19:03:58 +02:00
|
|
|
sn pd
|
|
|
|
sn ppd
|
|
|
|
"#]],
|
2020-07-04 18:03:34 +02:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 13:21:29 +08:00
|
|
|
#[test]
|
|
|
|
fn should_not_complete_snippets_in_path() {
|
2020-07-04 18:03:34 +02:00
|
|
|
check(r#"fn foo(x: i32) { ::foo<|> }"#, expect![[""]]);
|
|
|
|
check(r#"fn foo(x: i32) { ::<|> }"#, expect![[""]]);
|
2019-01-23 13:21:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
|
|
|
fn completes_snippets_in_items() {
|
2020-07-04 18:03:34 +02:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
sn macro_rules
|
2020-08-11 22:33:17 +03:00
|
|
|
sn tfn (Test function)
|
|
|
|
sn tmod (Test module)
|
2020-07-04 18:03:34 +02:00
|
|
|
"#]],
|
|
|
|
)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|