2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-07-04 23:05:17 +03:00
|
|
|
use crate::completion::{
|
2020-04-24 02:26:38 +02:00
|
|
|
completion_config::SnippetCap, completion_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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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,
|
2019-01-08 22:33:36 +03:00
|
|
|
"Test function",
|
|
|
|
"\
|
|
|
|
#[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);
|
|
|
|
snippet(ctx, cap, "pub(crate)", "pub(crate) $0").add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-03-11 10:46:43 +01:00
|
|
|
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
2019-08-29 16:49:10 +03:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2019-07-28 11:45:03 +01:00
|
|
|
fn do_snippet_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Snippet)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_snippets_in_expressions() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:08:06 +01:00
|
|
|
do_snippet_completion(r"fn foo(x: i32) { <|> }"),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "pd",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 17..17,
|
|
|
|
delete: 17..17,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "eprintln!(\"$0 = {:?}\", $0);",
|
|
|
|
kind: Snippet,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ppd",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 17..17,
|
|
|
|
delete: 17..17,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "eprintln!(\"$0 = {:#?}\", $0);",
|
|
|
|
kind: Snippet,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:08:06 +01: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() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:08:06 +01:00
|
|
|
do_snippet_completion(r"fn foo(x: i32) { ::foo<|> }"),
|
2019-11-15 12:56:24 +03:00
|
|
|
@"[]"
|
2019-07-28 12:08:06 +01:00
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:08:06 +01:00
|
|
|
do_snippet_completion(r"fn foo(x: i32) { ::<|> }"),
|
2019-11-15 12:56:24 +03:00
|
|
|
@"[]"
|
2019-07-28 12:08:06 +01:00
|
|
|
);
|
2019-01-23 13:21:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
|
|
|
fn completes_snippets_in_items() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_snippet_completion(
|
|
|
|
r"
|
2019-07-28 11:45:03 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Test function",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 78..78,
|
|
|
|
delete: 78..78,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "#[test]\nfn ${1:feature}() {\n $0\n}",
|
|
|
|
kind: Snippet,
|
|
|
|
lookup: "tfn",
|
|
|
|
},
|
2020-01-12 17:49:56 -05:00
|
|
|
CompletionItem {
|
|
|
|
label: "macro_rules",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 78..78,
|
|
|
|
delete: 78..78,
|
2020-01-12 17:49:56 -05:00
|
|
|
insert: "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}",
|
|
|
|
kind: Snippet,
|
|
|
|
},
|
2019-11-15 12:56:24 +03:00
|
|
|
CompletionItem {
|
|
|
|
label: "pub(crate)",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 78..78,
|
|
|
|
delete: 78..78,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "pub(crate) $0",
|
|
|
|
kind: Snippet,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:33:21 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|